Home | History | Annotate | Download | only in validator
      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.ParserConfiguration;
      6 import com.github.javaparser.ast.CompilationUnit;
      7 import com.github.javaparser.ast.expr.Expression;
      8 import com.github.javaparser.ast.stmt.Statement;
      9 import org.junit.Test;
     10 
     11 import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
     12 import static com.github.javaparser.ParseStart.EXPRESSION;
     13 import static com.github.javaparser.ParseStart.STATEMENT;
     14 import static com.github.javaparser.ParserConfiguration.LanguageLevel.*;
     15 import static com.github.javaparser.Providers.provider;
     16 import static com.github.javaparser.utils.TestUtils.assertNoProblems;
     17 import static com.github.javaparser.utils.TestUtils.assertProblems;
     18 
     19 public class Java1_1ValidatorTest {
     20     public static final JavaParser javaParser = new JavaParser(new ParserConfiguration().setLanguageLevel(JAVA_1_1));
     21 
     22     public static final String allModifiers = "public protected private abstract static final transient volatile synchronized native strictfp transitive default ";
     23 
     24     @Test
     25     public void topClass() {
     26         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "class X{}"));
     27         assertProblems(result,
     28                 "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.",
     29                 "(line 1,col 1) Can have only one of 'final', 'abstract'.",
     30                 "(line 1,col 1) 'transient' is not allowed here.",
     31                 "(line 1,col 1) 'default' is not allowed here.",
     32                 "(line 1,col 1) 'volatile' is not allowed here.",
     33                 "(line 1,col 1) 'strictfp' is not allowed here.",
     34                 "(line 1,col 1) 'private' is not allowed here.",
     35                 "(line 1,col 1) 'protected' is not allowed here.",
     36                 "(line 1,col 1) 'synchronized' is not allowed here.",
     37                 "(line 1,col 1) 'native' is not allowed here.",
     38                 "(line 1,col 1) 'transitive' is not allowed here.",
     39                 "(line 1,col 1) 'static' is not allowed here."
     40         );
     41     }
     42 
     43     @Test
     44     public void nestedClass() {
     45         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "class I{}}"));
     46         assertProblems(result,
     47                 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.",
     48                 "(line 1,col 9) Can have only one of 'final', 'abstract'.",
     49                 "(line 1,col 9) 'transient' is not allowed here.",
     50                 "(line 1,col 9) 'default' is not allowed here.",
     51                 "(line 1,col 9) 'strictfp' is not allowed here.",
     52                 "(line 1,col 9) 'volatile' is not allowed here.",
     53                 "(line 1,col 9) 'synchronized' is not allowed here.",
     54                 "(line 1,col 9) 'native' is not allowed here.",
     55                 "(line 1,col 9) 'transitive' is not allowed here."
     56         );
     57     }
     58 
     59     @Test
     60     public void localClass() {
     61         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{ void x() {" + allModifiers + "class I{}}}"));
     62         assertProblems(result,
     63                 "(line 1,col 20) Can have only one of 'public', 'protected', 'private'.",
     64                 "(line 1,col 20) Can have only one of 'final', 'abstract'.",
     65                 "(line 1,col 20) 'transient' is not allowed here.",
     66                 "(line 1,col 20) 'volatile' is not allowed here.",
     67                 "(line 1,col 20) 'default' is not allowed here.",
     68                 "(line 1,col 20) 'synchronized' is not allowed here.",
     69                 "(line 1,col 20) 'native' is not allowed here.",
     70                 "(line 1,col 20) 'transitive' is not allowed here.",
     71                 "(line 1,col 20) 'strictfp' is not allowed here.",
     72                 "(line 1,col 20) 'static' is not allowed here.",
     73                 "(line 1,col 20) 'public' is not allowed here.",
     74                 "(line 1,col 20) 'private' is not allowed here.",
     75                 "(line 1,col 20) 'protected' is not allowed here."
     76         );
     77     }
     78 
     79     @Test
     80     public void topInterface() {
     81         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(allModifiers + "interface X{}"));
     82         assertProblems(result,
     83                 "(line 1,col 1) Can have only one of 'public', 'protected', 'private'.",
     84                 "(line 1,col 1) Can have only one of 'final', 'abstract'.",
     85                 "(line 1,col 1) 'transient' is not allowed here.",
     86                 "(line 1,col 1) 'volatile' is not allowed here.",
     87                 "(line 1,col 1) 'default' is not allowed here.",
     88                 "(line 1,col 1) 'strictfp' is not allowed here.",
     89                 "(line 1,col 1) 'synchronized' is not allowed here.",
     90                 "(line 1,col 1) 'native' is not allowed here.",
     91                 "(line 1,col 1) 'transitive' is not allowed here.",
     92                 "(line 1,col 1) 'static' is not allowed here.",
     93                 "(line 1,col 1) 'final' is not allowed here.",
     94                 "(line 1,col 1) 'private' is not allowed here.",
     95                 "(line 1,col 1) 'protected' is not allowed here."
     96         );
     97     }
     98 
     99     @Test
    100     public void nestedInterface() {
    101         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "interface I{}}"));
    102         assertProblems(result,
    103                 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.",
    104                 "(line 1,col 9) Can have only one of 'final', 'abstract'.",
    105                 "(line 1,col 9) 'transient' is not allowed here.",
    106                 "(line 1,col 9) 'volatile' is not allowed here.",
    107                 "(line 1,col 9) 'default' is not allowed here.",
    108                 "(line 1,col 9) 'final' is not allowed here.",
    109                 "(line 1,col 9) 'strictfp' is not allowed here.",
    110                 "(line 1,col 9) 'synchronized' is not allowed here.",
    111                 "(line 1,col 9) 'native' is not allowed here.",
    112                 "(line 1,col 9) 'transitive' is not allowed here."
    113         );
    114     }
    115 
    116     @Test
    117     public void constructor() {
    118         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "X(){};}"));
    119         assertProblems(result,
    120                 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.",
    121                 "(line 1,col 9) Can have only one of 'final', 'abstract'.",
    122                 "(line 1,col 9) 'transient' is not allowed here.",
    123                 "(line 1,col 9) 'volatile' is not allowed here.",
    124                 "(line 1,col 9) 'final' is not allowed here.",
    125                 "(line 1,col 9) 'strictfp' is not allowed here.",
    126                 "(line 1,col 9) 'synchronized' is not allowed here.",
    127                 "(line 1,col 9) 'default' is not allowed here.",
    128                 "(line 1,col 9) 'native' is not allowed here.",
    129                 "(line 1,col 9) 'strictfp' is not allowed here.",
    130                 "(line 1,col 9) 'abstract' is not allowed here.",
    131                 "(line 1,col 9) 'static' is not allowed here.",
    132                 "(line 1,col 9) 'transitive' is not allowed here."
    133         );
    134     }
    135 
    136     @Test
    137     public void constructorParameter() {
    138         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{X(" + allModifiers + " int i){};}"));
    139         assertProblems(result,
    140                 "(line 1,col 11) Can have only one of 'public', 'protected', 'private'.",
    141                 "(line 1,col 11) Can have only one of 'final', 'abstract'.",
    142                 "(line 1,col 11) 'transient' is not allowed here.",
    143                 "(line 1,col 11) 'volatile' is not allowed here.",
    144                 "(line 1,col 11) 'synchronized' is not allowed here.",
    145                 "(line 1,col 11) 'native' is not allowed here.",
    146                 "(line 1,col 11) 'strictfp' is not allowed here.",
    147                 "(line 1,col 11) 'default' is not allowed here.",
    148                 "(line 1,col 11) 'abstract' is not allowed here.",
    149                 "(line 1,col 11) 'static' is not allowed here.",
    150                 "(line 1,col 11) 'transitive' is not allowed here.",
    151                 "(line 1,col 11) 'private' is not allowed here.",
    152                 "(line 1,col 11) 'public' is not allowed here.",
    153                 "(line 1,col 11) 'protected' is not allowed here."
    154         );
    155     }
    156 
    157     @Test
    158     public void classMethod() {
    159         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int x(){};}"));
    160         assertProblems(result,
    161                 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.",
    162                 "(line 1,col 9) Can have only one of 'final', 'abstract'.",
    163                 "(line 1,col 9) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.",
    164                 "(line 1,col 9) 'transient' is not allowed here.",
    165                 "(line 1,col 9) 'default' is not allowed here.",
    166                 "(line 1,col 9) 'strictfp' is not allowed here.",
    167                 "(line 1,col 9) 'volatile' is not allowed here.",
    168                 "(line 1,col 9) 'transitive' is not allowed here."
    169         );
    170     }
    171 
    172     @Test
    173     public void interfaceMethod() {
    174         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("interface X{" + allModifiers + "int x(){};}"));
    175         assertProblems(result,
    176                 "(line 1,col 13) Can have only one of 'public', 'protected', 'private'.",
    177                 "(line 1,col 13) Can have only one of 'final', 'abstract'.",
    178                 "(line 1,col 13) Cannot be 'abstract' and also 'private', 'static', 'final', 'native', 'strictfp', 'synchronized'.",
    179                 "(line 1,col 13) 'transient' is not allowed here.",
    180                 "(line 1,col 13) 'strictfp' is not allowed here.",
    181                 "(line 1,col 13) 'volatile' is not allowed here.",
    182                 "(line 1,col 13) 'default' is not allowed here.",
    183                 "(line 1,col 13) 'transitive' is not allowed here.",
    184                 "(line 1,col 13) 'private' is not allowed here.",
    185                 "(line 1,col 13) 'static' is not allowed here."
    186         );
    187     }
    188 
    189     @Test
    190     public void methodParameter() {
    191         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(" + allModifiers + " int i){};}"));
    192         assertProblems(result,
    193                 "(line 1,col 15) Can have only one of 'public', 'protected', 'private'.",
    194                 "(line 1,col 15) Can have only one of 'final', 'abstract'.",
    195                 "(line 1,col 15) 'transient' is not allowed here.",
    196                 "(line 1,col 15) 'volatile' is not allowed here.",
    197                 "(line 1,col 15) 'synchronized' is not allowed here.",
    198                 "(line 1,col 15) 'native' is not allowed here.",
    199                 "(line 1,col 15) 'strictfp' is not allowed here.",
    200                 "(line 1,col 15) 'abstract' is not allowed here.",
    201                 "(line 1,col 15) 'default' is not allowed here.",
    202                 "(line 1,col 15) 'static' is not allowed here.",
    203                 "(line 1,col 15) 'transitive' is not allowed here.",
    204                 "(line 1,col 15) 'private' is not allowed here.",
    205                 "(line 1,col 15) 'public' is not allowed here.",
    206                 "(line 1,col 15) 'protected' is not allowed here."
    207         );
    208     }
    209 
    210     @Test
    211     public void field() {
    212         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{" + allModifiers + "int i;}"));
    213         assertProblems(result,
    214                 "(line 1,col 9) Can have only one of 'public', 'protected', 'private'.",
    215                 "(line 1,col 9) Can have only one of 'final', 'abstract'.",
    216                 "(line 1,col 9) 'synchronized' is not allowed here.",
    217                 "(line 1,col 9) 'native' is not allowed here.",
    218                 "(line 1,col 9) 'strictfp' is not allowed here.",
    219                 "(line 1,col 9) 'default' is not allowed here.",
    220                 "(line 1,col 9) 'abstract' is not allowed here.",
    221                 "(line 1,col 9) 'transitive' is not allowed here."
    222         );
    223     }
    224 
    225     @Test
    226     public void localVariable() {
    227         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){" + allModifiers + "int i;}}"));
    228         assertProblems(result,
    229                 "(line 1,col 17) Can have only one of 'public', 'protected', 'private'.",
    230                 "(line 1,col 17) Can have only one of 'final', 'abstract'.",
    231                 "(line 1,col 17) 'transient' is not allowed here.",
    232                 "(line 1,col 17) 'volatile' is not allowed here.",
    233                 "(line 1,col 17) 'synchronized' is not allowed here.",
    234                 "(line 1,col 17) 'native' is not allowed here.",
    235                 "(line 1,col 17) 'default' is not allowed here.",
    236                 "(line 1,col 17) 'strictfp' is not allowed here.",
    237                 "(line 1,col 17) 'abstract' is not allowed here.",
    238                 "(line 1,col 17) 'static' is not allowed here.",
    239                 "(line 1,col 17) 'transitive' is not allowed here.",
    240                 "(line 1,col 17) 'private' is not allowed here.",
    241                 "(line 1,col 17) 'public' is not allowed here.",
    242                 "(line 1,col 17) 'protected' is not allowed here."
    243         );
    244     }
    245 
    246 
    247     @Test
    248     public void catchParameter() {
    249         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{int x(){ try{}catch(" + allModifiers + " Integer x){}}}"));
    250         assertProblems(result,
    251                 "(line 1,col 144) Can have only one of 'public', 'protected', 'private'.",
    252                 "(line 1,col 144) Can have only one of 'final', 'abstract'.",
    253                 "(line 1,col 144) 'transient' is not allowed here.",
    254                 "(line 1,col 144) 'volatile' is not allowed here.",
    255                 "(line 1,col 144) 'synchronized' is not allowed here.",
    256                 "(line 1,col 144) 'native' is not allowed here.",
    257                 "(line 1,col 144) 'default' is not allowed here.",
    258                 "(line 1,col 144) 'strictfp' is not allowed here.",
    259                 "(line 1,col 144) 'abstract' is not allowed here.",
    260                 "(line 1,col 144) 'static' is not allowed here.",
    261                 "(line 1,col 144) 'transitive' is not allowed here.",
    262                 "(line 1,col 144) 'private' is not allowed here.",
    263                 "(line 1,col 144) 'public' is not allowed here.",
    264                 "(line 1,col 144) 'protected' is not allowed here."
    265         );
    266     }
    267 
    268     @Test
    269     public void innerClasses() {
    270         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{class Y{}}"));
    271         assertNoProblems(result);
    272     }
    273 
    274     @Test
    275     public void localInterface() {
    276         ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{ void x() {" + allModifiers + "interface I{}}}"));
    277         assertProblems(result, "(line 1,col 20) There is no such thing as a local interface."
    278         );
    279     }
    280 
    281     @Test
    282     public void reflection() {
    283         ParseResult<Expression> result = javaParser.parse(EXPRESSION, provider("Abc.class"));
    284         assertNoProblems(result);
    285     }
    286 
    287     @Test
    288     public void strictfpAllowedAsIdentifier() {
    289         ParseResult<Statement> result = javaParser.parse(STATEMENT, provider("int strictfp;"));
    290         assertNoProblems(result);
    291     }
    292 }
    293