Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/com/seleniumboot/migrator/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
Expand All @@ -25,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.seleniumboot.migrator.Finding.Status.AUTO;
Expand All @@ -37,6 +39,9 @@ public final class Analyzer {
"BeforeMethod", "AfterMethod", "BeforeClass", "AfterClass", "BeforeTest", "AfterTest",
"BeforeSuite", "AfterSuite", "Before", "After", "BeforeEach", "AfterEach", "BeforeAll", "AfterAll");

private static final String MIG_019_ADVICE =
"Properties-based config: move these values to selenium-boot.yml (see docs.seleniumboot.com/docs/configuration).";

private final JavaParser parser = new JavaParser(
new ParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_25));

Expand Down Expand Up @@ -146,6 +151,29 @@ private void scan(CompilationUnit cu, String file, List<Finding> out, LocatorSta
"Remove; mixing implicit and explicit waits causes flakiness."));
}
});
// MIG-019: Properties-based config (flag only; Selenium Boot uses selenium-boot.yml)
Set<String> propertiesVars = cu.findAll(VariableDeclarator.class).stream()
.filter(v -> v.getType().isClassOrInterfaceType()
&& v.getType().asClassOrInterfaceType().getNameAsString().equals("Properties"))
.map(VariableDeclarator::getNameAsString)
.collect(Collectors.toSet());
cu.findAll(MethodCallExpr.class).forEach(m -> {
if (m.getNameAsString().equals("load") && m.getScope()
.filter(sc -> sc.isNameExpr() && propertiesVars.contains(sc.asNameExpr().getNameAsString()))
.isPresent()) {
out.add(new Finding("MIG-019", MANUAL, file, line(m), "Properties.load(...)", MIG_019_ADVICE));
}
if (m.getNameAsString().equals("getResourceAsStream")
&& m.getArguments().stream().anyMatch(this::isPropertiesFileLiteral)) {
out.add(new Finding("MIG-019", MANUAL, file, line(m),
"getResourceAsStream(\"....properties\")", MIG_019_ADVICE));
}
});
cu.findAll(ObjectCreationExpr.class).stream()
.filter(e -> e.getType().getNameAsString().equals("FileInputStream")
&& e.getArguments().stream().anyMatch(this::isPropertiesFileLiteral))
.forEach(e -> out.add(new Finding("MIG-019", MANUAL, file, line(e),
"new FileInputStream(\"....properties\")", MIG_019_ADVICE)));
// MIG-015: custom DriverManager (flag only)
cu.findAll(ClassOrInterfaceDeclaration.class).stream()
.filter(c -> c.getNameAsString().endsWith("DriverManager") || c.getNameAsString().endsWith("DriverFactory"))
Expand All @@ -165,6 +193,10 @@ private void scan(CompilationUnit cu, String file, List<Finding> out, LocatorSta
});
}

private boolean isPropertiesFileLiteral(Expression e) {
return e.isStringLiteralExpr() && e.asStringLiteralExpr().asString().endsWith(".properties");
}

private boolean isDriverLifecycleMethod(MethodDeclaration method) {
if (lifecycleAnnotation(method) == null || method.getBody().isEmpty()) return false;
var body = method.getBody().get();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/seleniumboot/migrator/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private static String ruleLabel(String ruleId) {
case "MIG-010" -> "MIG-010 (Page objects)";
case "MIG-011" -> "MIG-011 (@FindBy fields)";
case "MIG-012" -> "MIG-012 (PageFactory.initElements calls)";
case "MIG-019" -> "MIG-019 (Properties-based config)";
default -> ruleId;
};
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/seleniumboot/migrator/AnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,27 @@ void locate() {
assertEquals(100, report.estimatedConfidence());
}

@Test
void detectsPropertiesBasedConfig() {
var r = rules("""
import java.util.Properties;
class ConfigTest {
void setup() throws Exception {
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
var url = ConfigTest.class.getResourceAsStream("/env.properties");
}
}""");
assertEquals(3, r.stream().filter(id -> id.equals("MIG-019")).count());
}

@Test
void plainTestClassGivesNoPropertiesFinding() {
var r = rules("""
class PlainTest {
void testLogin() { driver.findElement(By.id("login")).click(); }
}""");
assertFalse(r.contains("MIG-019"));
}

}