Skip to content

Commit

Permalink
test: only run tests with compliance level >=8 (#5994)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Oct 9, 2024
1 parent a7b4e9b commit 594b2d8
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/java/spoon/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ protected static JSAP defineArgs() {
// java compliance
opt2 = new FlaggedOption("compliance");
opt2.setLongFlag("compliance");
opt2.setHelp("Java source code compliance level (1,2,3,4,5, 6, 7 or 8).");
opt2.setHelp("Java source code compliance level (e.g. 21 for Java 21).");
opt2.setStringParser(JSAP.INTEGER_PARSER);
opt2.setDefault(DEFAULT_CODE_COMPLIANCE_LEVEL + "");
jsap.registerParameter(opt2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import spoon.reflect.visitor.Query;
import spoon.support.Level;
import spoon.support.QueueProcessingManager;
import spoon.support.StandardEnvironment;
import spoon.support.comparator.FixedOrderBasedOnFileNameCompilationUnitComparator;
import spoon.support.compiler.SnippetCompilationError;
import spoon.support.compiler.SpoonProgress;
Expand Down Expand Up @@ -73,7 +74,7 @@ public class JDTBasedSpoonCompiler implements spoon.SpoonModelBuilder {
protected final List<CategorizedProblem> probs = new ArrayList<>();
protected final TreeBuilderRequestor requestor = new TreeBuilderRequestor(this);
protected Factory factory;
protected int javaCompliance = 7;
protected int javaCompliance = StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL;
//list of java files or folders with java files which represents source of the CtModel
protected SpoonFolder sources = new VirtualFolder();
//list of java files or folders with java files which represents templates. Templates are added to CtModel too.
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/compilation/CompilationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.SpoonClassNotFoundException;
import spoon.support.StandardEnvironment;
import spoon.support.compiler.FileSystemFolder;
import spoon.support.compiler.ProgressLogger;
import spoon.support.compiler.jdt.JDTBasedSpoonCompiler;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void compileCommandLineTest() {
"-i", sourceFile,
"-o", "target/spooned",
"--compile",
"--compliance", "7",
"--compliance", String.valueOf(StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL),
"--level", "OFF"
});

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.StandardEnvironment;
import spoon.test.SpoonTestHelpers;
import spoon.test.ctClass.testclasses.AnonymousClass;
import spoon.test.ctClass.testclasses.Foo;
Expand Down Expand Up @@ -398,7 +399,7 @@ public void testRemoveAnnotation() {
assertFalse(secondRemovalSuccessful);
}

@org.junit.jupiter.api.Test
@Test
void testLocalClassExists() {
// contract: local classes and their members are part of the model
String code = SpoonTestHelpers.wrapLocal(
Expand All @@ -407,7 +408,7 @@ void testLocalClassExists() {
" public void doNothing() { }\n" +
" }\n"
);
CtModel model = SpoonTestHelpers.createModelFromString(code, 5);
CtModel model = SpoonTestHelpers.createModelFromString(code, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
CtBlock<?> block = SpoonTestHelpers.getBlock(model);

MatcherAssert.assertThat("The local class does not exist in the model", block.getStatements().size(), CoreMatchers.is(1));
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/spoon/test/ctType/CtTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.test.ctType;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.CtModel;
Expand Down Expand Up @@ -295,6 +296,7 @@ public void testGetAllExecutablesOnTypeImplementingNestedInterface() {
* "enum" was only introduced in Java 5
*/
@Test
@Disabled("Compliance level 4 is not supported anymore")
public void testEnumPackage() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/keywordCompliance/enum/Foo.java");
Expand Down
23 changes: 13 additions & 10 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.test.imports;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -776,7 +777,7 @@ public void testWithInnerEnumDoesNotImportStaticInnerMethods() {
output,
not(containsString("import static spoon.test.imports.testclasses.ItfWithEnum$Bar.Lip;"))
);
canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -799,7 +800,7 @@ public void testStaticImportOfEnumField() {

assertThat("The file should not contain the import of enum", output, not(containsString("import spoon.reflect.path.CtRole;")));
assertThat("The file should contain the static import of enum field", output, not(containsString("import spoon.reflect.path.CtRole.NAME;")));
canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -820,7 +821,7 @@ public void testShouldNotCreateAutoreference() {
String output = prettyPrinter.getResult();

assertThat("The file should not contain a static import for NOFOLLOW_LINKS", output, not(containsString("import static java.nio.file.LinkOption.NOFOLLOW_LINKS;")));
canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -840,7 +841,7 @@ public void testAccessPath() {
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();

canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand Down Expand Up @@ -1014,7 +1015,7 @@ public void testJavaLangIsConsideredAsImported() {
launcher.setSourceOutputDirectory(outputDir);
launcher.run();

canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -1026,7 +1027,7 @@ public void testJavaLangIsConsideredAsImportedButNotForSubPackages() {
launcher.setSourceOutputDirectory(outputDir);
launcher.run();

canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand Down Expand Up @@ -1064,7 +1065,7 @@ public void testMultipleCU() throws IOException {
launcher.setSourceOutputDirectory(outputDir);
launcher.run();

canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);

Path outputDirPath = Path.of(outputDir);
Path pathA = outputDirPath.resolve("spoon/test/imports/testclasses/multiplecu/A.java");
Expand All @@ -1080,7 +1081,7 @@ public void testMultipleCU() throws IOException {
}

@Test
public void testStaticMethodWithDifferentClassSameNameJava7NoCollision() {
public void testStaticMethodWithDifferentClassSameNameJava7PlusNoCollision() {
// contract: when there is a collision between class names when using static method, we should create a static import for the method
final Launcher launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
Expand All @@ -1090,7 +1091,7 @@ public void testStaticMethodWithDifferentClassSameNameJava7NoCollision() {
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
launcher.setSourceOutputDirectory(outputDir);
launcher.getEnvironment().setComplianceLevel(7);
launcher.getEnvironment().setComplianceLevel(StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
launcher.run();
PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();

Expand All @@ -1104,10 +1105,11 @@ public void testStaticMethodWithDifferentClassSameNameJava7NoCollision() {
assertThat("The file should contain a static import ", output, containsString("import static spoon.test.imports.testclasses2.apachetestsuite.enums.EnumTestSuite.suite;"));
assertThat("The call to the last EnumTestSuite should be in FQN", output, containsString("suite.add(suite());"));

canBeBuilt(outputDir, 7);
canBeBuilt(outputDir, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
@Disabled("Compliance level 3 is not supported anymore")
public void testStaticMethodWithDifferentClassSameNameJava3NoCollision() {
// contract: when there is a collision between class names when using static method, we could not create a static import
// as it is not compliant with java < 1.5, so we should use fully qualified name of the class
Expand Down Expand Up @@ -1137,6 +1139,7 @@ public void testStaticMethodWithDifferentClassSameNameJava3NoCollision() {
}

@Test
@Disabled("Compliance level 3 is not supported anymore")
public void testStaticMethodWithDifferentClassSameNameCollision() {
// contract: when using static method, if there is a collision between class name AND between method names,
// we can only use the fully qualified name of the class to call the static method
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/spoon/test/prettyprinter/PrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import spoon.reflect.visitor.PrinterHelper;
import spoon.reflect.visitor.TokenWriter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.StandardEnvironment;
import spoon.test.prettyprinter.testclasses.MissingVariableDeclaration;
import spoon.testing.utils.ModelUtils;

Expand Down Expand Up @@ -213,7 +214,7 @@ public void testRuleCanBeBuild() {
String result = printer.getResult();

assertTrue(!result.contains("Rule.Phoneme.this.phonemeText"), "The result should contain direct this accessor for field: " + result);
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}


Expand Down Expand Up @@ -252,7 +253,7 @@ public void testJDTBatchCompilerCanBeBuild() {
String result = printer.getResult();

assertTrue(!result.contains("Rule.Phoneme.this.phonemeText"), "The result should contain direct this accessor for field: " + result);
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/spoon/test/trycatch/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import spoon.reflect.reference.CtVariableReference;
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.StandardEnvironment;
import spoon.support.compiler.VirtualFile;
import spoon.support.reflect.CtExtendedModifier;
import spoon.test.trycatch.testclasses.Foo;
Expand Down Expand Up @@ -298,7 +299,7 @@ public void testCatchWithExplicitFinalVariable() throws IOException {
String inputResource = "./src/test/java/spoon/test/trycatch/testclasses/Bar.java";
Launcher launcher = new Launcher();
launcher.addInputResource(inputResource);
launcher.getEnvironment().setComplianceLevel(5);
launcher.getEnvironment().setComplianceLevel(StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
launcher.buildModel();

CtTry tryStmt = launcher.getModel().getElements(new TypeFilter<>(CtTry.class)).get(0);
Expand All @@ -317,7 +318,7 @@ public void testCatchWithExplicitFinalVariable() throws IOException {
launcher.addInputResource(inputResource);
launcher.setSourceOutputDirectory("./target/spoon-trycatch");
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setComplianceLevel(5);
launcher.getEnvironment().setComplianceLevel(StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
launcher.run();

File f = new File("target/spoon-trycatch/spoon/test/trycatch/testclasses/Bar.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.PrettyPrinter;
import spoon.support.StandardEnvironment;
import spoon.test.variable.testclasses.Tacos;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void testNoFQNWhenShadowedByField() {

assertTrue(result.contains("import spoon.Launcher;"), "The java file should contain import for Launcher");
assertTrue(result.contains("xx = Launcher.SPOONED_CLASSES"), "The xx variable is attributed with Launcher.SPOONED_CLASSES");
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -81,7 +82,7 @@ public void testNoFQNWhenShadowedByLocalVariable() {
assertTrue(!result.contains("import java.util.Map"), "The java.util.Map is not imported");
assertTrue(result.contains("java.util.Map uneMap"), "The Map type use FQN");
assertTrue(result.contains("ForStaticVariables.Map"), "The other variable use FQN too");
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -93,7 +94,7 @@ public void testNoFQNWhenUsedInInnerClassAndShadowedByLocalVariable() {
//the package name `spoon.test.variable.testclasses` cannot be used in FQN mode because it is shadowed by local variable `spoon`
//so use at least Type name
assertTrue(result.contains(" BurritosStaticMethod.toto();"), "The inner class should contain call using import");
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -104,7 +105,7 @@ public void testNoFQNWhenUsedInTryCatch() {
String result = this.buildResourceAndReturnResult(pathResource, output);
assertTrue(result.contains("import spoon.Launcher;"), "The java file should contain import for Launcher");
assertTrue(result.contains("xx = Launcher.SPOONED_CLASSES"), "The xx variable should be attributed with SPOONED_CLASSES");
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -115,7 +116,7 @@ public void testNoFQNWhenUsedInLoop() {
String result = this.buildResourceAndReturnResult(pathResource, output);
assertTrue(result.contains("import spoon.Launcher;"), "The java file should contain import for Launcher");
assertTrue(result.contains("xx = Launcher.SPOONED_CLASSES"), "The xx variable should be attributed with SPOONED_CLASSES");
canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand Down Expand Up @@ -144,7 +145,7 @@ public void testStaticImportWithAutoImport() {
assertTrue(!result.contains("spoon.test.variable.testclasses.MultiBurritos.spoon = \"truc\";"), "The result should not contain a FQN for spoon access");
assertTrue(!result.contains("spoon.test.variable.testclasses.ForStaticVariables.foo();"), "The result should not contain a FQN for foo");

canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -156,7 +157,7 @@ public void testNoFQNAndStaticImport() {
assertTrue(result.contains("import static spoon.Launcher.SPOONED_CLASSES;"), "The result should contain a static import for spoon.Launcher.SPOONED_CLASSES");
assertTrue(!result.contains("spoon.test.variable.testclasses.ForStaticVariables.foo()"), "The result should not contain a FQN call for foo (i.e. spoon.test.variable.testclasses.ForStaticVariables.foo())");

canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand All @@ -166,6 +167,6 @@ public void testPrivateStaticImportShouldNotBeImportedInSameClass() {
String result = this.buildResourceAndReturnResult(pathResource, output);
assertTrue(!result.contains("import static spoon.test.variable.testclasses.digest.DigestUtil.STREAM_BUFFER_LENGTH;"), "The result should not contain a static import for STREAM_BUFFER_LENGTH");

canBeBuilt(output, 7);
canBeBuilt(output, StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}
}
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/visibility/VisibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.AbstractReferenceFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.StandardEnvironment;
import spoon.test.visibility.testclasses.A;
import spoon.test.visibility.testclasses.A2;

Expand Down Expand Up @@ -85,7 +86,7 @@ public void testVisibilityOfClassesNamedByClassesInJavaLangPackage() {
assertNotNull(aFloat);
assertSame(spoon.test.visibility.testclasses.Float.class, aFloat.getActualClass());

canBeBuilt(new File("./target/spooned/spoon/test/visibility_package/testclasses/"), 7);
canBeBuilt(new File("./target/spooned/spoon/test/visibility_package/testclasses/"), StandardEnvironment.DEFAULT_CODE_COMPLIANCE_LEVEL);
}

@Test
Expand Down

0 comments on commit 594b2d8

Please sign in to comment.