diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2018-03-04 08:21:35 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2018-03-04 08:21:35 +0000 |
commit | b4c1397d5df9370f6358d4f8e9efd27e0f67dec1 (patch) | |
tree | 6789ec288d344cf5fd5d057bcf1efc9545b1af28 /javaparser-testing/src/test/java/com/github/javaparser/ast/validator | |
parent | 92d661a1d239131fb5c1e019a8f2ac7584d2d3f6 (diff) | |
parent | 1afe9e0652b9b53edade5aa276162abe27b32a67 (diff) | |
download | platform_external_javaparser-b4c1397d5df9370f6358d4f8e9efd27e0f67dec1.tar.gz platform_external_javaparser-b4c1397d5df9370f6358d4f8e9efd27e0f67dec1.tar.bz2 platform_external_javaparser-b4c1397d5df9370f6358d4f8e9efd27e0f67dec1.zip |
Snap for 4632767 from 1afe9e0652b9b53edade5aa276162abe27b32a67 to pi-releaseandroid-wear-9.0.0_r9android-wear-9.0.0_r8android-wear-9.0.0_r7android-wear-9.0.0_r6android-wear-9.0.0_r5android-wear-9.0.0_r4android-wear-9.0.0_r3android-wear-9.0.0_r23android-wear-9.0.0_r22android-wear-9.0.0_r21android-wear-9.0.0_r20android-wear-9.0.0_r2android-wear-9.0.0_r19android-wear-9.0.0_r18android-wear-9.0.0_r17android-wear-9.0.0_r16android-wear-9.0.0_r15android-wear-9.0.0_r14android-wear-9.0.0_r13android-wear-9.0.0_r12android-wear-9.0.0_r11android-wear-9.0.0_r10android-wear-9.0.0_r1android-vts-9.0_r9android-vts-9.0_r8android-vts-9.0_r7android-vts-9.0_r6android-vts-9.0_r5android-vts-9.0_r4android-vts-9.0_r14android-vts-9.0_r13android-vts-9.0_r12android-vts-9.0_r11android-vts-9.0_r10android-cts-9.0_r9android-cts-9.0_r8android-cts-9.0_r7android-cts-9.0_r6android-cts-9.0_r5android-cts-9.0_r4android-cts-9.0_r3android-cts-9.0_r2android-cts-9.0_r13android-cts-9.0_r12android-cts-9.0_r11android-cts-9.0_r10android-cts-9.0_r1android-9.0.0_r9android-9.0.0_r8android-9.0.0_r7android-9.0.0_r60android-9.0.0_r6android-9.0.0_r59android-9.0.0_r58android-9.0.0_r57android-9.0.0_r56android-9.0.0_r55android-9.0.0_r54android-9.0.0_r53android-9.0.0_r52android-9.0.0_r51android-9.0.0_r50android-9.0.0_r5android-9.0.0_r49android-9.0.0_r48android-9.0.0_r3android-9.0.0_r2android-9.0.0_r18android-9.0.0_r17android-9.0.0_r10android-9.0.0_r1security-pi-releasepie-vts-releasepie-security-releasepie-s2-releasepie-release-2pie-releasepie-r2-s2-releasepie-r2-s1-releasepie-r2-releasepie-platform-releasepie-gsipie-cuttlefish-testingpie-cts-release
Change-Id: Ibe65883e94ed5a7272dff3f100393987a1cf3da2
Diffstat (limited to 'javaparser-testing/src/test/java/com/github/javaparser/ast/validator')
12 files changed, 1352 insertions, 0 deletions
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java10ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java10ValidatorTest.java new file mode 100644 index 000000000..8c2b93568 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java10ValidatorTest.java @@ -0,0 +1,108 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.body.BodyDeclaration; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Ignore; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.CLASS_BODY; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_10; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java10ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_10)); + + @Test + public void varAllowedInLocalVariableDeclaration() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a = 5;")); + assertNoProblems(result); + } + + @Test + public void varAllowedInForEach() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(var a : as){}")); + assertNoProblems(result); + } + + @Test + public void varAllowedInOldFor() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(var a = 5;a<9;a++){}")); + assertNoProblems(result); + } + + @Test + public void varNotAllowedInCast() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int a = (var)20;")); + assertNoProblems(result); + } + + @Test + public void varNotAllowedInTryWithResources() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(var f = new FileReader(\"\")){ }catch (Exception e){ }")); + assertProblems(result, "(line 1,col 5) \"var\" is not allowed here."); + } + + @Test + public void varNotAllowedInField() { + ParseResult<BodyDeclaration<?>> result = javaParser.parse(CLASS_BODY, provider("var a = 20;")); + assertProblems(result, "(line 1,col 1) \"var\" is not allowed here."); + } + + @Test + public void varNotAllowedInTypeArguments() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("new X<var>();")); + assertProblems(result, "(line 1,col 7) \"var\" is not allowed here."); + } + + @Test + public void varNotAllowedInLambdaParameters() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("x((var x) -> null);")); + assertProblems(result, "(line 1,col 4) \"var\" is not allowed here."); + } + + @Test + public void emptyInitializerNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a;")); + assertProblems(result, "(line 1,col 1) \"var\" needs an initializer."); + } + + @Test + public void multipleVariablesNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a=1, b=2;")); + assertProblems(result, "(line 1,col 1) \"var\" only takes a single variable."); + } + + @Test + public void nullVariablesNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a=null;")); + assertProblems(result, "(line 1,col 1) \"var\" cannot infer type from just null."); + } + + @Test + public void arrayDimensionBracketsNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a=new String[]{};")); + assertProblems(result, "(line 1,col 1) \"var\" cannot infer array types."); + } + + // This is pretty hard to impossible to implement correctly with just the AST. + @Ignore + @Test + public void selfReferenceNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a=a;")); + assertProblems(result, ""); + } + + // Can be implemented once https://github.com/javaparser/javaparser/issues/1434 is implemented. + @Ignore + @Test + public void polyExpressionAsInitializerNotAllowed() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("var a=new ArrayList<>();")); + assertProblems(result, ""); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java11ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java11ValidatorTest.java new file mode 100644 index 000000000..1e1f8c0f8 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java11ValidatorTest.java @@ -0,0 +1,22 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_11_PREVIEW; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; + +public class Java11ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_11_PREVIEW)); + + @Test + public void varAllowedInLocalVariableDeclaration() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("x((var x, var y) -> x+y);")); + assertNoProblems(result); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java new file mode 100644 index 000000000..7160bcfb9 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_0ValidatorTest.java @@ -0,0 +1,98 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.Problem; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.ArrayCreationExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.type.PrimitiveType; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static com.github.javaparser.ParseStart.*; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; +import static org.junit.Assert.assertEquals; + +public class Java1_0ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_0)); + + @Test + public void tryWithoutResources() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(X x=new Y()){}")); + assertProblems(result, + "(line 1,col 1) Catch with resource is not supported.", + "(line 1,col 1) Try has no finally and no catch."); + } + + @Test + public void classExtendingMoreThanOne() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X extends Y, Z {}")); + assertProblems(result, "(line 1,col 20) A class cannot extend more than one other class."); + } + + @Test + public void interfaceUsingImplements() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X implements Y {}")); + assertProblems(result, "(line 1,col 24) An interface cannot implement other interfaces."); + } + + @Test + public void interfaceWithInitializer() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X {{}}")); + assertProblems(result, "(line 1,col 14) An interface cannot have initializers."); + } + + @Test + public void defaultInClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X {default void a(){};}")); + assertProblems(result, "(line 1,col 10) 'default' is not allowed here."); + } + + @Test + public void leftHandAssignmentCannotBeAConditional() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(1==2)=3")); + assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment."); + } + + @Test + public void leftHandAssignmentCannotBeEmptyBraces() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("()=3")); + assertProblems(result, + "(line 1,col 1) Illegal left hand side of an assignment.", + "(line 1,col 1) Lambdas are not supported."); + } + + @Test + public void leftHandAssignmentCanBeInBraces() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(i) += (i) += 1")); + assertNoProblems(result); + } + + @Test + public void noInnerClasses() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{class Y{}}")); + assertProblems(result, "(line 1,col 9) inner classes or interfaces are not supported."); + } + + @Test + public void noReflection() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("Abc.class")); + assertProblems(result, "(line 1,col 1) Reflection is not supported."); + } + + @Test + public void nonEmptyList() { + ArrayCreationExpr expr = new ArrayCreationExpr(PrimitiveType.booleanType()); + List<Problem> problems= new ArrayList<>(); + new Java1_0Validator().accept(expr, new ProblemReporter(problems::add)); + assertEquals("ArrayCreationExpr.levels can not be empty.", problems.get(0).getMessage()); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_1ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_1ValidatorTest.java new file mode 100644 index 000000000..6331d9ab1 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_1ValidatorTest.java @@ -0,0 +1,292 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParseStart.EXPRESSION; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java1_1ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_1)); + + public static final String allModifiers = "public protected private abstract static final transient volatile synchronized native strictfp transitive default "; + + @Test + public void topClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "class X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'strictfp' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here." + ); + } + + @Test + public void nestedClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "class I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void localClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{ void x() {" + allModifiers + "class I{}}}")); + assertProblems(result, + "(line 1,col 20) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 20) Can have only one of 'final', 'abstract'.", + "(line 1,col 20) 'transient' is not allowed here.", + "(line 1,col 20) 'volatile' is not allowed here.", + "(line 1,col 20) 'default' is not allowed here.", + "(line 1,col 20) 'synchronized' is not allowed here.", + "(line 1,col 20) 'native' is not allowed here.", + "(line 1,col 20) 'transitive' is not allowed here.", + "(line 1,col 20) 'strictfp' is not allowed here.", + "(line 1,col 20) 'static' is not allowed here.", + "(line 1,col 20) 'public' is not allowed here.", + "(line 1,col 20) 'private' is not allowed here.", + "(line 1,col 20) 'protected' is not allowed here." + ); + } + + @Test + public void topInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "interface X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'strictfp' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here.", + "(line 1,col 1) 'final' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here." + ); + } + + @Test + public void nestedInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "interface I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void constructor() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "X(){};}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'abstract' is not allowed here.", + "(line 1,col 9) 'static' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void constructorParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{X(" + allModifiers + " int i){};}")); + assertProblems(result, + "(line 1,col 11) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 11) Can have only one of 'final', 'abstract'.", + "(line 1,col 11) 'transient' is not allowed here.", + "(line 1,col 11) 'volatile' is not allowed here.", + "(line 1,col 11) 'synchronized' is not allowed here.", + "(line 1,col 11) 'native' is not allowed here.", + "(line 1,col 11) 'strictfp' is not allowed here.", + "(line 1,col 11) 'default' is not allowed here.", + "(line 1,col 11) 'abstract' is not allowed here.", + "(line 1,col 11) 'static' is not allowed here.", + "(line 1,col 11) 'transitive' is not allowed here.", + "(line 1,col 11) 'private' is not allowed here.", + "(line 1,col 11) 'public' is not allowed here.", + "(line 1,col 11) 'protected' is not allowed here." + ); + } + + @Test + public void classMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void interfaceMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 13) Can have only one of 'final', 'abstract'.", + "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 13) 'transient' is not allowed here.", + "(line 1,col 13) 'strictfp' is not allowed here.", + "(line 1,col 13) 'volatile' is not allowed here.", + "(line 1,col 13) 'default' is not allowed here.", + "(line 1,col 13) 'transitive' is not allowed here.", + "(line 1,col 13) 'private' is not allowed here.", + "(line 1,col 13) 'static' is not allowed here." + ); + } + + @Test + public void methodParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(" + allModifiers + " int i){};}")); + assertProblems(result, + "(line 1,col 15) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 15) Can have only one of 'final', 'abstract'.", + "(line 1,col 15) 'transient' is not allowed here.", + "(line 1,col 15) 'volatile' is not allowed here.", + "(line 1,col 15) 'synchronized' is not allowed here.", + "(line 1,col 15) 'native' is not allowed here.", + "(line 1,col 15) 'strictfp' is not allowed here.", + "(line 1,col 15) 'abstract' is not allowed here.", + "(line 1,col 15) 'default' is not allowed here.", + "(line 1,col 15) 'static' is not allowed here.", + "(line 1,col 15) 'transitive' is not allowed here.", + "(line 1,col 15) 'private' is not allowed here.", + "(line 1,col 15) 'public' is not allowed here.", + "(line 1,col 15) 'protected' is not allowed here." + ); + } + + @Test + public void field() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int i;}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'abstract' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void localVariable() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){" + allModifiers + "int i;}}")); + assertProblems(result, + "(line 1,col 17) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 17) Can have only one of 'final', 'abstract'.", + "(line 1,col 17) 'transient' is not allowed here.", + "(line 1,col 17) 'volatile' is not allowed here.", + "(line 1,col 17) 'synchronized' is not allowed here.", + "(line 1,col 17) 'native' is not allowed here.", + "(line 1,col 17) 'default' is not allowed here.", + "(line 1,col 17) 'strictfp' is not allowed here.", + "(line 1,col 17) 'abstract' is not allowed here.", + "(line 1,col 17) 'static' is not allowed here.", + "(line 1,col 17) 'transitive' is not allowed here.", + "(line 1,col 17) 'private' is not allowed here.", + "(line 1,col 17) 'public' is not allowed here.", + "(line 1,col 17) 'protected' is not allowed here." + ); + } + + + @Test + public void catchParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){ try{}catch(" + allModifiers + " Integer x){}}}")); + assertProblems(result, + "(line 1,col 144) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 144) Can have only one of 'final', 'abstract'.", + "(line 1,col 144) 'transient' is not allowed here.", + "(line 1,col 144) 'volatile' is not allowed here.", + "(line 1,col 144) 'synchronized' is not allowed here.", + "(line 1,col 144) 'native' is not allowed here.", + "(line 1,col 144) 'default' is not allowed here.", + "(line 1,col 144) 'strictfp' is not allowed here.", + "(line 1,col 144) 'abstract' is not allowed here.", + "(line 1,col 144) 'static' is not allowed here.", + "(line 1,col 144) 'transitive' is not allowed here.", + "(line 1,col 144) 'private' is not allowed here.", + "(line 1,col 144) 'public' is not allowed here.", + "(line 1,col 144) 'protected' is not allowed here." + ); + } + + @Test + public void innerClasses() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{class Y{}}")); + assertNoProblems(result); + } + + @Test + public void localInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{ void x() {" + allModifiers + "interface I{}}}")); + assertProblems(result, "(line 1,col 20) There is no such thing as a local interface." + ); + } + + @Test + public void reflection() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("Abc.class")); + assertNoProblems(result); + } + + @Test + public void strictfpAllowedAsIdentifier() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int strictfp;")); + assertNoProblems(result); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_2ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_2ValidatorTest.java new file mode 100644 index 000000000..3ae7018fe --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_2ValidatorTest.java @@ -0,0 +1,275 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java1_2ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_2)); + + private final String allModifiers = "public protected private abstract static final transient volatile synchronized native strictfp transitive default "; + + @Test + public void topClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "class X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) Can have only one of 'native', 'strictfp'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here." + ); + } + + @Test + public void nestedClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "class I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void localClass() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{ void x() {" + allModifiers + "class I{}}}")); + assertProblems(result, + "(line 1,col 20) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 20) Can have only one of 'final', 'abstract'.", + "(line 1,col 20) Can have only one of 'native', 'strictfp'.", + "(line 1,col 20) 'transient' is not allowed here.", + "(line 1,col 20) 'volatile' is not allowed here.", + "(line 1,col 20) 'default' is not allowed here.", + "(line 1,col 20) 'synchronized' is not allowed here.", + "(line 1,col 20) 'native' is not allowed here.", + "(line 1,col 20) 'transitive' is not allowed here.", + "(line 1,col 20) 'static' is not allowed here.", + "(line 1,col 20) 'public' is not allowed here.", + "(line 1,col 20) 'private' is not allowed here.", + "(line 1,col 20) 'protected' is not allowed here." + ); + } + + @Test + public void topInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "interface X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) Can have only one of 'native', 'strictfp'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here.", + "(line 1,col 1) 'final' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here." + ); + } + + @Test + public void nestedInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "interface I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void constructor() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "X(){};}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'abstract' is not allowed here.", + "(line 1,col 9) 'static' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void constructorParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{X(" + allModifiers + " int i){};}")); + assertProblems(result, + "(line 1,col 11) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 11) Can have only one of 'final', 'abstract'.", + "(line 1,col 11) Can have only one of 'native', 'strictfp'.", + "(line 1,col 11) 'transient' is not allowed here.", + "(line 1,col 11) 'volatile' is not allowed here.", + "(line 1,col 11) 'synchronized' is not allowed here.", + "(line 1,col 11) 'native' is not allowed here.", + "(line 1,col 11) 'strictfp' is not allowed here.", + "(line 1,col 11) 'default' is not allowed here.", + "(line 1,col 11) 'abstract' is not allowed here.", + "(line 1,col 11) 'static' is not allowed here.", + "(line 1,col 11) 'transitive' is not allowed here.", + "(line 1,col 11) 'private' is not allowed here.", + "(line 1,col 11) 'public' is not allowed here.", + "(line 1,col 11) 'protected' is not allowed here." + ); + } + + @Test + public void classMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void interfaceMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 13) Can have only one of 'final', 'abstract'.", + "(line 1,col 13) Can have only one of 'native', 'strictfp'.", + "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 13) 'transient' is not allowed here.", + "(line 1,col 13) 'volatile' is not allowed here.", + "(line 1,col 13) 'default' is not allowed here.", + "(line 1,col 13) 'transitive' is not allowed here.", + "(line 1,col 13) 'private' is not allowed here.", + "(line 1,col 13) 'static' is not allowed here." + ); + } + + @Test + public void methodParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(" + allModifiers + " int i){};}")); + assertProblems(result, + "(line 1,col 15) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 15) Can have only one of 'final', 'abstract'.", + "(line 1,col 15) Can have only one of 'native', 'strictfp'.", + "(line 1,col 15) 'transient' is not allowed here.", + "(line 1,col 15) 'volatile' is not allowed here.", + "(line 1,col 15) 'synchronized' is not allowed here.", + "(line 1,col 15) 'native' is not allowed here.", + "(line 1,col 15) 'strictfp' is not allowed here.", + "(line 1,col 15) 'abstract' is not allowed here.", + "(line 1,col 15) 'default' is not allowed here.", + "(line 1,col 15) 'static' is not allowed here.", + "(line 1,col 15) 'transitive' is not allowed here.", + "(line 1,col 15) 'private' is not allowed here.", + "(line 1,col 15) 'public' is not allowed here.", + "(line 1,col 15) 'protected' is not allowed here." + ); + } + + @Test + public void field() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int i;}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'strictfp' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'abstract' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void localVariable() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){" + allModifiers + "int i;}}")); + assertProblems(result, + "(line 1,col 17) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 17) Can have only one of 'final', 'abstract'.", + "(line 1,col 17) Can have only one of 'native', 'strictfp'.", + "(line 1,col 17) 'transient' is not allowed here.", + "(line 1,col 17) 'volatile' is not allowed here.", + "(line 1,col 17) 'synchronized' is not allowed here.", + "(line 1,col 17) 'native' is not allowed here.", + "(line 1,col 17) 'default' is not allowed here.", + "(line 1,col 17) 'strictfp' is not allowed here.", + "(line 1,col 17) 'abstract' is not allowed here.", + "(line 1,col 17) 'static' is not allowed here.", + "(line 1,col 17) 'transitive' is not allowed here.", + "(line 1,col 17) 'private' is not allowed here.", + "(line 1,col 17) 'public' is not allowed here.", + "(line 1,col 17) 'protected' is not allowed here." + ); + } + + @Test + public void catchParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){ try{}catch(" + allModifiers + " Integer x){}}}")); + assertProblems(result, + "(line 1,col 144) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 144) Can have only one of 'final', 'abstract'.", + "(line 1,col 144) Can have only one of 'native', 'strictfp'.", + "(line 1,col 144) 'transient' is not allowed here.", + "(line 1,col 144) 'volatile' is not allowed here.", + "(line 1,col 144) 'synchronized' is not allowed here.", + "(line 1,col 144) 'native' is not allowed here.", + "(line 1,col 144) 'default' is not allowed here.", + "(line 1,col 144) 'strictfp' is not allowed here.", + "(line 1,col 144) 'abstract' is not allowed here.", + "(line 1,col 144) 'static' is not allowed here.", + "(line 1,col 144) 'transitive' is not allowed here.", + "(line 1,col 144) 'private' is not allowed here.", + "(line 1,col 144) 'public' is not allowed here.", + "(line 1,col 144) 'protected' is not allowed here." + ); + } + + @Test + public void strictfpNotAllowedAsIdentifier() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int strictfp;")); + assertProblems(result, "(line 1,col 5) 'strictfp' cannot be used as an identifier as it is a keyword."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_3ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_3ValidatorTest.java new file mode 100644 index 000000000..b10114abf --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_3ValidatorTest.java @@ -0,0 +1,22 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java1_3ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_3)); + + @Test + public void noAssert() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("assert a;")); + assertProblems(result, "(line 1,col 1) 'assert' keyword is not supported."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_4ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_4ValidatorTest.java new file mode 100644 index 000000000..ed0be99fe --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java1_4ValidatorTest.java @@ -0,0 +1,70 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.*; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java1_4ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_4)); + + @Test + public void yesAssert() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("assert a;")); + assertNoProblems(result); + } + + @Test + public void noGenerics() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X<A>{List<String> b;}")); + assertProblems(result, + "(line 1,col 12) Generics are not supported.", + "(line 1,col 1) Generics are not supported." + ); + } + + @Test + public void noAnnotations() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("@Abc @Def() @Ghi(a=3) @interface X{}")); + assertProblems(result, + "(line 1,col 6) Annotations are not supported.", + "(line 1,col 13) Annotations are not supported.", + "(line 1,col 1) Annotations are not supported." + ); + } + + @Test + public void novarargs() { + ParseResult<Parameter> result = javaParser.parse(PARAMETER, provider("String... x")); + assertProblems(result, "(line 1,col 1) Varargs are not supported."); + } + + @Test + public void noforeach() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(X x: xs){}")); + assertProblems(result, "(line 1,col 1) For-each loops are not supported."); + } + + @Test + public void staticImport() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("import static x;import static x.*;import x.X;import x.*;")); + assertProblems(result, + "(line 1,col 17) Static imports are not supported.", + "(line 1,col 1) Static imports are not supported."); + } + + @Test + public void enumAllowedAsIdentifier() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int enum;")); + assertNoProblems(result); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java5ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java5ValidatorTest.java new file mode 100644 index 000000000..315167fda --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java5ValidatorTest.java @@ -0,0 +1,155 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParseStart; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.*; +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.ast.validator.Java1_1ValidatorTest.allModifiers; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java5ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_5)); + + @Test + public void genericsWithoutDiamond() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X<A>{List<String> b = new ArrayList<>();}")); + assertProblems(result, "(line 1,col 33) The diamond operator is not supported."); + } + + @Test + public void topAnnotationDeclaration() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "@interface X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) Can have only one of 'native', 'strictfp'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here.", + "(line 1,col 1) 'final' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here." + ); + } + + @Test + public void nestedAnnotationDeclaration() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "@interface I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void annotationMember() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("@interface X{" + allModifiers + "int x();}")); + assertProblems(result, + "(line 1,col 14) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 14) Can have only one of 'final', 'abstract'.", + "(line 1,col 14) Can have only one of 'native', 'strictfp'.", + "(line 1,col 14) 'transient' is not allowed here.", + "(line 1,col 14) 'volatile' is not allowed here.", + "(line 1,col 14) 'final' is not allowed here.", + "(line 1,col 14) 'synchronized' is not allowed here.", + "(line 1,col 14) 'default' is not allowed here.", + "(line 1,col 14) 'native' is not allowed here.", + "(line 1,col 14) 'protected' is not allowed here.", + "(line 1,col 14) 'private' is not allowed here.", + "(line 1,col 14) 'strictfp' is not allowed here.", + "(line 1,col 14) 'static' is not allowed here.", + "(line 1,col 14) 'transitive' is not allowed here." + ); + } + + @Test + public void topEnum() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "enum X{}")); + assertProblems(result, + "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 1) Can have only one of 'final', 'abstract'.", + "(line 1,col 1) Can have only one of 'native', 'strictfp'.", + "(line 1,col 1) 'transient' is not allowed here.", + "(line 1,col 1) 'volatile' is not allowed here.", + "(line 1,col 1) 'synchronized' is not allowed here.", + "(line 1,col 1) 'default' is not allowed here.", + "(line 1,col 1) 'native' is not allowed here.", + "(line 1,col 1) 'transitive' is not allowed here.", + "(line 1,col 1) 'static' is not allowed here.", + "(line 1,col 1) 'abstract' is not allowed here.", + "(line 1,col 1) 'final' is not allowed here.", + "(line 1,col 1) 'private' is not allowed here.", + "(line 1,col 1) 'protected' is not allowed here." + ); + } + + @Test + public void nestedEnum() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "enum I{}}")); + assertProblems(result, + "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 9) Can have only one of 'final', 'abstract'.", + "(line 1,col 9) Can have only one of 'native', 'strictfp'.", + "(line 1,col 9) 'transient' is not allowed here.", + "(line 1,col 9) 'volatile' is not allowed here.", + "(line 1,col 9) 'default' is not allowed here.", + "(line 1,col 9) 'abstract' is not allowed here.", + "(line 1,col 9) 'final' is not allowed here.", + "(line 1,col 9) 'synchronized' is not allowed here.", + "(line 1,col 9) 'native' is not allowed here.", + "(line 1,col 9) 'transitive' is not allowed here." + ); + } + + @Test + public void varargs() { + ParseResult<Parameter> result = javaParser.parse(PARAMETER, provider("String... x")); + assertNoProblems(result); + } + + @Test + public void foreach() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(X x: xs){}")); + assertNoProblems(result); + } + + @Test + public void staticImport() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("import static x;import static x.*;import x.X;import x.*;")); + assertNoProblems(result); + } + + @Test + public void noPrimitiveTypeArguments() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X extends Y<int> {}")); + assertProblems(result, "(line 1,col 17) Type arguments may not be primitive."); + } + + @Test + public void enumAllowedAsIdentifier() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int enum;")); + assertProblems(result, "(line 1,col 5) 'enum' cannot be used as an identifier as it is a keyword."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java6ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java6ValidatorTest.java new file mode 100644 index 000000000..d066abe07 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java6ValidatorTest.java @@ -0,0 +1,42 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.EXPRESSION; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java6ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_6)); + + @Test + public void noStringsInSwitch() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("switch(x){case \"abc\": ;}")); + assertProblems(result, "(line 1,col 16) Strings in switch statements are not supported."); + } + + @Test + public void nobinaryIntegerLiterals() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("0b01")); + assertProblems(result, "(line 1,col 1) Binary literal values are not supported."); + } + + @Test + public void noUnderscoresInIntegerLiterals() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("1_000_000")); + assertProblems(result, "(line 1,col 1) Underscores in literal values are not supported."); + } + + @Test + public void noMultiCatch() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try{}catch(Abc|Def e){}")); + assertProblems(result, "(line 1,col 12) Multi-catch is not supported."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java new file mode 100644 index 000000000..a2a03f837 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java7ValidatorTest.java @@ -0,0 +1,107 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.Problem; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.type.ClassOrInterfaceType; +import com.github.javaparser.ast.type.UnionType; +import org.junit.Test; + +import java.util.*; + +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParseStart.EXPRESSION; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java7ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_7)); + + @Test + public void generics() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X<A>{List<String> b = new ArrayList<>();}")); + assertNoProblems(result); + } + + @Test + public void defaultMethodWithoutBody() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X {default void a();}")); + assertProblems(result, "(line 1,col 14) 'default' is not allowed here."); + } + + @Test + public void tryWithoutAnything() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try{}")); + assertProblems(result, "(line 1,col 1) Try has no finally, no catch, and no resources."); + } + + @Test + public void tryWithResourceVariableDeclaration() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(Reader r = new Reader()){}")); + assertNoProblems(result); + } + + @Test + public void tryWithResourceReference() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(a.b.c){}")); + assertProblems(result, "(line 1,col 1) Try with resources only supports variable declarations."); + } + + @Test + public void stringsInSwitch() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("switch(x){case \"abc\": ;}")); + assertNoProblems(result); + } + + @Test + public void binaryIntegerLiterals() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("0b01")); + assertNoProblems(result); + } + + @Test + public void underscoresInIntegerLiterals() { + ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("1_000_000")); + assertNoProblems(result); + } + + @Test + public void multiCatch() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try{}catch(Abc|Def e){}")); + assertNoProblems(result); + } + + @Test + public void multiCatchWithoutElements() { + UnionType unionType = new UnionType(); + + List<Problem> problems = new ArrayList<>(); + new Java7Validator().accept(unionType, new ProblemReporter(problems::add)); + + assertProblems(problems, "UnionType.elements can not be empty."); + } + + @Test + public void multiCatchWithOneElement() { + UnionType unionType = new UnionType(); + unionType.getElements().add(new ClassOrInterfaceType()); + + List<Problem> problems = new ArrayList<>(); + new Java7Validator().accept(unionType, new ProblemReporter(problems::add)); + + assertProblems(problems, "Union type (multi catch) must have at least two elements."); + } + + @Test + public void noLambdas() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a(() -> 1);")); + assertProblems(result, "(line 1,col 3) Lambdas are not supported."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java8ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java8ValidatorTest.java new file mode 100644 index 000000000..216fb5b13 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java8ValidatorTest.java @@ -0,0 +1,82 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.ast.validator.Java1_1ValidatorTest.allModifiers; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java8ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_8)); + + @Test + public void localInterface() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X {void a(){interface I{}};}")); + assertProblems(result, "(line 1,col 19) There is no such thing as a local interface."); + } + + @Test + public void lambdaParameter() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){ a((" + allModifiers + " Integer x) -> 10);}}")); + assertProblems(result, + "(line 1,col 21) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 21) Can have only one of 'final', 'abstract'.", + "(line 1,col 21) Can have only one of 'native', 'strictfp'.", + "(line 1,col 21) 'transient' is not allowed here.", + "(line 1,col 21) 'volatile' is not allowed here.", + "(line 1,col 21) 'synchronized' is not allowed here.", + "(line 1,col 21) 'strictfp' is not allowed here.", + "(line 1,col 21) 'default' is not allowed here.", + "(line 1,col 21) 'native' is not allowed here.", + "(line 1,col 21) 'strictfp' is not allowed here.", + "(line 1,col 21) 'abstract' is not allowed here.", + "(line 1,col 21) 'static' is not allowed here.", + "(line 1,col 21) 'transitive' is not allowed here.", + "(line 1,col 21) 'private' is not allowed here.", + "(line 1,col 21) 'public' is not allowed here.", + "(line 1,col 21) 'protected' is not allowed here." + ); + } + + @Test + public void interfaceMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 13) Can have only one of 'final', 'abstract'.", + "(line 1,col 13) Can have only one of 'native', 'strictfp'.", + "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 13) 'transient' is not allowed here.", + "(line 1,col 13) 'volatile' is not allowed here.", + "(line 1,col 13) 'transitive' is not allowed here.", + "(line 1,col 13) 'private' is not allowed here." + ); + } + + @Test + public void defaultMethodWithoutBody() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X {default void a();}")); + assertProblems(result, "(line 1,col 14) 'default' methods must have a body."); + } + + @Test + public void lambdas() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a(() -> 1);")); + assertNoProblems(result); + } + + @Test + public void noModules() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("open module x {}")); + assertProblems(result, "(line 1,col 1) Modules are not supported."); + } +} diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java9ValidatorTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java9ValidatorTest.java new file mode 100644 index 000000000..911f6a984 --- /dev/null +++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/validator/Java9ValidatorTest.java @@ -0,0 +1,79 @@ +package com.github.javaparser.ast.validator; + +import com.github.javaparser.JavaParser; +import com.github.javaparser.ParseResult; +import com.github.javaparser.ParserConfiguration; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.stmt.Statement; +import org.junit.Test; + +import static com.github.javaparser.ParseStart.COMPILATION_UNIT; +import static com.github.javaparser.ParseStart.STATEMENT; +import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; +import static com.github.javaparser.Providers.provider; +import static com.github.javaparser.ast.validator.Java1_1ValidatorTest.allModifiers; +import static com.github.javaparser.utils.TestUtils.assertNoProblems; +import static com.github.javaparser.utils.TestUtils.assertProblems; + +public class Java9ValidatorTest { + public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_9)); + + @Test + public void underscoreIdentifiers() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("a.b._.c.d = act(_, _ -> _);")); + assertProblems(result, + "(line 1,col 5) '_' is a reserved keyword.", + "(line 1,col 17) '_' is a reserved keyword.", + "(line 1,col 20) '_' is a reserved keyword.", + "(line 1,col 25) '_' is a reserved keyword." + ); + } + + @Test + public void moduleRequires() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("module x{requires " + allModifiers + " a;}")); + assertProblems(result, + "(line 1,col 10) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 10) Can have only one of 'final', 'abstract'.", + "(line 1,col 10) Can have only one of 'native', 'strictfp'.", + "(line 1,col 10) 'transient' is not allowed here.", + "(line 1,col 10) 'volatile' is not allowed here.", + "(line 1,col 10) 'final' is not allowed here.", + "(line 1,col 10) 'synchronized' is not allowed here.", + "(line 1,col 10) 'default' is not allowed here.", + "(line 1,col 10) 'native' is not allowed here.", + "(line 1,col 10) 'private' is not allowed here.", + "(line 1,col 10) 'protected' is not allowed here.", + "(line 1,col 10) 'strictfp' is not allowed here.", + "(line 1,col 10) 'abstract' is not allowed here.", + "(line 1,col 10) 'public' is not allowed here." + ); + } + + @Test + public void interfaceMethod() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}")); + assertProblems(result, + "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.", + "(line 1,col 13) Can have only one of 'final', 'abstract'.", + "(line 1,col 13) Can have only one of 'native', 'strictfp'.", + "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.", + "(line 1,col 13) 'transient' is not allowed here.", + "(line 1,col 13) 'volatile' is not allowed here.", + "(line 1,col 13) 'transitive' is not allowed here." + ); + } + + @Test + public void modules() { + ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("open module x {}")); + assertNoProblems(result); + } + + @Test + public void tryWithResourceReference() { + ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(a.b.c){}")); + assertNoProblems(result); + } + +} |