1 package com.github.javaparser.ast.validator; 2 3 import com.github.javaparser.JavaParser; 4 import com.github.javaparser.ParseResult; 5 import com.github.javaparser.ParseStart; 6 import com.github.javaparser.ParserConfiguration; 7 import com.github.javaparser.ast.CompilationUnit; 8 import com.github.javaparser.ast.body.Parameter; 9 import com.github.javaparser.ast.stmt.Statement; 10 import org.junit.Test; 11 12 import static com.github.javaparser.ParseStart.*; 13 import static com.github.javaparser.ParseStart.COMPILATION_UNIT; 14 import static com.github.javaparser.ParserConfiguration.LanguageLevel.*; 15 import static com.github.javaparser.Providers.provider; 16 import static com.github.javaparser.ast.validator.Java1_1ValidatorTest.allModifiers; 17 import static com.github.javaparser.utils.TestUtils.assertNoProblems; 18 import static com.github.javaparser.utils.TestUtils.assertProblems; 19 20 public class Java5ValidatorTest { 21 public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_5)); 22 23 @Test 24 public void genericsWithoutDiamond() { 25 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X<A>{List<String> b = new ArrayList<>();}")); 26 assertProblems(result, "(line 1,col 33) The diamond operator is not supported."); 27 } 28 29 @Test 30 public void topAnnotationDeclaration() { 31 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "@interface X{}")); 32 assertProblems(result, 33 "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", 34 "(line 1,col 1) Can have only one of 'final', 'abstract'.", 35 "(line 1,col 1) Can have only one of 'native', 'strictfp'.", 36 "(line 1,col 1) 'transient' is not allowed here.", 37 "(line 1,col 1) 'volatile' is not allowed here.", 38 "(line 1,col 1) 'synchronized' is not allowed here.", 39 "(line 1,col 1) 'default' is not allowed here.", 40 "(line 1,col 1) 'native' is not allowed here.", 41 "(line 1,col 1) 'transitive' is not allowed here.", 42 "(line 1,col 1) 'static' is not allowed here.", 43 "(line 1,col 1) 'final' is not allowed here.", 44 "(line 1,col 1) 'private' is not allowed here.", 45 "(line 1,col 1) 'protected' is not allowed here." 46 ); 47 } 48 49 @Test 50 public void nestedAnnotationDeclaration() { 51 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "@interface I{}}")); 52 assertProblems(result, 53 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", 54 "(line 1,col 9) Can have only one of 'final', 'abstract'.", 55 "(line 1,col 9) Can have only one of 'native', 'strictfp'.", 56 "(line 1,col 9) 'transient' is not allowed here.", 57 "(line 1,col 9) 'volatile' is not allowed here.", 58 "(line 1,col 9) 'default' is not allowed here.", 59 "(line 1,col 9) 'final' is not allowed here.", 60 "(line 1,col 9) 'synchronized' is not allowed here.", 61 "(line 1,col 9) 'native' is not allowed here.", 62 "(line 1,col 9) 'transitive' is not allowed here." 63 ); 64 } 65 66 @Test 67 public void annotationMember() { 68 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("@interface X{" + allModifiers + "int x();}")); 69 assertProblems(result, 70 "(line 1,col 14) Can have only one of 'public', 'protected', 'private'.", 71 "(line 1,col 14) Can have only one of 'final', 'abstract'.", 72 "(line 1,col 14) Can have only one of 'native', 'strictfp'.", 73 "(line 1,col 14) 'transient' is not allowed here.", 74 "(line 1,col 14) 'volatile' is not allowed here.", 75 "(line 1,col 14) 'final' is not allowed here.", 76 "(line 1,col 14) 'synchronized' is not allowed here.", 77 "(line 1,col 14) 'default' is not allowed here.", 78 "(line 1,col 14) 'native' is not allowed here.", 79 "(line 1,col 14) 'protected' is not allowed here.", 80 "(line 1,col 14) 'private' is not allowed here.", 81 "(line 1,col 14) 'strictfp' is not allowed here.", 82 "(line 1,col 14) 'static' is not allowed here.", 83 "(line 1,col 14) 'transitive' is not allowed here." 84 ); 85 } 86 87 @Test 88 public void topEnum() { 89 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "enum X{}")); 90 assertProblems(result, 91 "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.", 92 "(line 1,col 1) Can have only one of 'final', 'abstract'.", 93 "(line 1,col 1) Can have only one of 'native', 'strictfp'.", 94 "(line 1,col 1) 'transient' is not allowed here.", 95 "(line 1,col 1) 'volatile' is not allowed here.", 96 "(line 1,col 1) 'synchronized' is not allowed here.", 97 "(line 1,col 1) 'default' is not allowed here.", 98 "(line 1,col 1) 'native' is not allowed here.", 99 "(line 1,col 1) 'transitive' is not allowed here.", 100 "(line 1,col 1) 'static' is not allowed here.", 101 "(line 1,col 1) 'abstract' is not allowed here.", 102 "(line 1,col 1) 'final' is not allowed here.", 103 "(line 1,col 1) 'private' is not allowed here.", 104 "(line 1,col 1) 'protected' is not allowed here." 105 ); 106 } 107 108 @Test 109 public void nestedEnum() { 110 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "enum I{}}")); 111 assertProblems(result, 112 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.", 113 "(line 1,col 9) Can have only one of 'final', 'abstract'.", 114 "(line 1,col 9) Can have only one of 'native', 'strictfp'.", 115 "(line 1,col 9) 'transient' is not allowed here.", 116 "(line 1,col 9) 'volatile' is not allowed here.", 117 "(line 1,col 9) 'default' is not allowed here.", 118 "(line 1,col 9) 'abstract' is not allowed here.", 119 "(line 1,col 9) 'final' is not allowed here.", 120 "(line 1,col 9) 'synchronized' is not allowed here.", 121 "(line 1,col 9) 'native' is not allowed here.", 122 "(line 1,col 9) 'transitive' is not allowed here." 123 ); 124 } 125 126 @Test 127 public void varargs() { 128 ParseResult<Parameter> result = javaParser.parse(PARAMETER, provider("String... x")); 129 assertNoProblems(result); 130 } 131 132 @Test 133 public void foreach() { 134 ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("for(X x: xs){}")); 135 assertNoProblems(result); 136 } 137 138 @Test 139 public void staticImport() { 140 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("import static x;import static x.*;import x.X;import x.*;")); 141 assertNoProblems(result); 142 } 143 144 @Test 145 public void noPrimitiveTypeArguments() { 146 ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X extends Y<int> {}")); 147 assertProblems(result, "(line 1,col 17) Type arguments may not be primitive."); 148 } 149 150 @Test 151 public void enumAllowedAsIdentifier() { 152 ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int enum;")); 153 assertProblems(result, "(line 1,col 5) 'enum' cannot be used as an identifier as it is a keyword."); 154 } 155 } 156