1package com.github.javaparser.ast.validator;
2
3import com.github.javaparser.JavaParser;
4import com.github.javaparser.ParseResult;
5import com.github.javaparser.ParserConfiguration;
6import com.github.javaparser.Problem;
7import com.github.javaparser.ast.CompilationUnit;
8import com.github.javaparser.ast.expr.ArrayCreationExpr;
9import com.github.javaparser.ast.expr.Expression;
10import com.github.javaparser.ast.stmt.Statement;
11import com.github.javaparser.ast.type.PrimitiveType;
12import org.junit.Test;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import static com.github.javaparser.ParseStart.*;
18import static com.github.javaparser.ParserConfiguration.LanguageLevel.*;
19import static com.github.javaparser.Providers.provider;
20import static com.github.javaparser.utils.TestUtils.assertNoProblems;
21import static com.github.javaparser.utils.TestUtils.assertProblems;
22import static org.junit.Assert.assertEquals;
23
24public class Java1_0ValidatorTest {
25    public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_0));
26
27    @Test
28    public void tryWithoutResources() {
29        ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("try(X x=new Y()){}"));
30        assertProblems(result,
31                "(line 1,col 1) Catch with resource is not supported.",
32                "(line 1,col 1) Try has no finally and no catch.");
33    }
34
35    @Test
36    public void classExtendingMoreThanOne() {
37        ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X extends Y, Z {}"));
38        assertProblems(result, "(line 1,col 20) A class cannot extend more than one other class.");
39    }
40
41    @Test
42    public void interfaceUsingImplements() {
43        ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X implements Y {}"));
44        assertProblems(result, "(line 1,col 24) An interface cannot implement other interfaces.");
45    }
46
47    @Test
48    public void interfaceWithInitializer() {
49        ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X {{}}"));
50        assertProblems(result, "(line 1,col 14) An interface cannot have initializers.");
51    }
52
53    @Test
54    public void defaultInClass() {
55        ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X {default void a(){};}"));
56        assertProblems(result, "(line 1,col 10) 'default' is not allowed here.");
57    }
58
59    @Test
60    public void leftHandAssignmentCannotBeAConditional() {
61        ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(1==2)=3"));
62        assertProblems(result, "(line 1,col 1) Illegal left hand side of an assignment.");
63    }
64
65    @Test
66    public void leftHandAssignmentCannotBeEmptyBraces() {
67        ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("()=3"));
68        assertProblems(result,
69                "(line 1,col 1) Illegal left hand side of an assignment.",
70                "(line 1,col 1) Lambdas are not supported.");
71    }
72
73    @Test
74    public void leftHandAssignmentCanBeInBraces() {
75        ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("(i) += (i) += 1"));
76        assertNoProblems(result);
77    }
78
79    @Test
80    public void noInnerClasses() {
81        ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{class Y{}}"));
82        assertProblems(result, "(line 1,col 9) inner classes or interfaces are not supported.");
83    }
84
85    @Test
86    public void noReflection() {
87        ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("Abc.class"));
88        assertProblems(result, "(line 1,col 1) Reflection is not supported.");
89    }
90
91    @Test
92    public void nonEmptyList() {
93        ArrayCreationExpr expr = new ArrayCreationExpr(PrimitiveType.booleanType());
94        List<Problem> problems= new ArrayList<>();
95        new Java1_0Validator().accept(expr, new ProblemReporter(problems::add));
96        assertEquals("ArrayCreationExpr.levels can not be empty.", problems.get(0).getMessage());
97    }
98}
99