Custom java rule to match a test class name to a class name in main scope

custom_rules

(Braedan) #1

I am looking to write a custom java rule to enforce that a test class name follows a regex pattern, but also contains a valid class name from the main scope. For example, the class in main scope:
package com.test;

public class ABC {

	...
	
}

And the rule file:

public class RandomTest { // Noncompliant

}

public class ABCTest { // Compliant
	
}

(All test class files ending with Test.java must match CLASSINMAINSCOPETest.java. Files that don’t end in Test.java wouldn’t be matched and don’t need this restriction)

I’ve been playing around and I am able to run rules on test scope, but I am unable to check or access semantic information from the main scope. Any help or insight will be greatly appreciated. Thanks!


(Nicolas Peru) #2

In short : what you are trying to achieve is not easily doable as per design of analyzer on a “by file” basis as of today.

One workaround you could imagine would be to list files in your main directory and infer class name from that (or from the compiled directory as well) : but this is more of a dirty hack than a clever solution.