    1   package test.net.sourceforge.pmd.ast;
    2
    3   import junit.framework.TestCase;
    4   import net.sourceforge.pmd.PMD;
    5   import net.sourceforge.pmd.TargetJDK1_3;
    6   import net.sourceforge.pmd.TargetJDK1_4;
    7   import net.sourceforge.pmd.TargetJDK1_5;
    8   import net.sourceforge.pmd.TargetJDKVersion;
    9   import net.sourceforge.pmd.ast.JavaParser;
    10  import net.sourceforge.pmd.ast.ParseException;
    11
    12  import java.io.StringReader;
    13
    14  public class JDKVersionTest extends TestCase {
    15
    16      // enum keyword/identifier
    17      public void testEnumAsKeywordShouldFailWith14() throws Throwable {
    18          try {
    19              JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_ENUM));
    20              p.CompilationUnit();
    21              throw new Error("JDK 1.4 parser should have failed to parse enum used as keyword");
    22          } catch (ParseException e) {
    23          }    // cool
    24      }
    25
    26      public void testEnumAsIdentifierShouldPassWith14() throws Throwable {
    27          JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK14_ENUM));
    28          p.CompilationUnit();
    29      }
    30
    31      public void testEnumAsKeywordShouldPassWith15() throws Throwable {
    32          JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_ENUM));
    33          p.CompilationUnit();
    34      }
    35
    36      public void testEnumAsIdentifierShouldFailWith15() throws Throwable {
    37          try {
    38              TargetJDKVersion jdk = new TargetJDK1_5();
    39              JavaParser p = jdk.createParser(new StringReader(JDK14_ENUM));
    40              p.CompilationUnit();
    41              throw new Error("JDK 1.5 parser should have failed to parse enum used as identifier");
    42          } catch (ParseException e) {
    43          }    // cool
    44      }
    45      // enum keyword/identifier
    46
    47      // assert keyword/identifier
    48      public void testAssertAsKeywordVariantsSucceedWith1_4() {
    49          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST1)).CompilationUnit();
    50          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST2)).CompilationUnit();
    51          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST3)).CompilationUnit();
    52          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST4)).CompilationUnit();
    53      }
    54
    55      public void testAssertAsVariableDeclIdentifierFailsWith1_4() {
    56          try {
    57              (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST5)).CompilationUnit();
    58              throw new RuntimeException("Usage of assert as identifier should have failed with 1.4");
    59          } catch (ParseException pe) {
    60              // cool
    61          }
    62      }
    63
    64      public void testAssertAsMethodNameIdentifierFailsWith1_4() {
    65          try {
    66              (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST7)).CompilationUnit();
    67              throw new RuntimeException("Usage of assert as identifier should have failed with 1.4");
    68          } catch (ParseException pe) {
    69              // cool
    70          }
    71      }
    72
    73      public void testAssertAsIdentifierSucceedsWith1_3() {
    74          JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST5));
    75          jp.CompilationUnit();
    76      }
    77
    78      public void testAssertAsKeywordFailsWith1_3() {
    79          try {
    80              JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST6));
    81              jp.CompilationUnit();
    82              throw new RuntimeException("Usage of assert as keyword should have failed with 1.3");
    83          } catch (ParseException pe) {
    84              // cool
    85          }
    86      }
    87      // assert keyword/identifier
    88
    89      public void testVarargsShouldPassWith15() throws Throwable {
    90          JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_VARARGS));
    91          p.CompilationUnit();
    92      }
    93
    94      public void testVarargsShouldFailWith14() throws Throwable {
    95          try {
    96              JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_VARARGS));
    97              p.CompilationUnit();
    98              fail("Should have throw ParseException!");
    99          } catch (ParseException pe) {
    100             // cool
    101         }
    102     }
    103
    104     public void testJDK15ForLoopSyntaxShouldPassWith15() throws Throwable {
    105         JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_FORLOOP));
    106         p.CompilationUnit();
    107     }
    108
    109     public void testJDK15ForLoopSyntaxWithModifiers() throws Throwable {
    110         JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_FORLOOP_WITH_MODIFIER));
    111         p.CompilationUnit();
    112     }
    113
    114     public void testJDK15ForLoopShouldFailWith14() throws Throwable {
    115         try {
    116             JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK15_FORLOOP));
    117             p.CompilationUnit();
    118             fail("Should have throw ParseException!");
    119         } catch (ParseException pe) {
    120             // cool
    121         }
    122     }
    123
    124     public void testJDK15GenericsSyntaxShouldPassWith15() throws Throwable {
    125         JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_GENERICS));
    126         p.CompilationUnit();
    127     }
    128
    129     public void testVariousParserBugs() throws Throwable {
    130         JavaParser p = new TargetJDK1_5().createParser(new StringReader(FIELDS_BUG));
    131         p.CompilationUnit();
    132         p = new TargetJDK1_5().createParser(new StringReader(GT_BUG));
    133         p.CompilationUnit();
    134         p = new TargetJDK1_5().createParser(new StringReader(ANNOTATIONS_BUG));
    135         p.CompilationUnit();
    136         p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_FIELD));
    137         p.CompilationUnit();
    138     }
    139
    140     public void testNestedClassInMethodBug() throws Throwable {
    141         JavaParser p = new TargetJDK1_5().createParser(new StringReader(INNER_BUG));
    142         p.CompilationUnit();
    143         p = new TargetJDK1_5().createParser(new StringReader(INNER_BUG2));
    144         p.CompilationUnit();
    145     }
    146
    147     public void testGenericsInMethodCall() throws Throwable {
    148         JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_METHOD_CALL));
    149         p.CompilationUnit();
    150     }
    151
    152     public void testGenericINAnnotation() throws Throwable {
    153         JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_IN_ANNOTATION));
    154         p.CompilationUnit();
    155     }
    156
    157     public void testGenericReturnType() throws Throwable {
    158         JavaParser p = new TargetJDK1_5().createParser(new StringReader(GENERIC_RETURN_TYPE));
    159         p.CompilationUnit();
    160     }
    161
    162     public void testMultipleGenerics() throws Throwable {
    163         JavaParser p = new TargetJDK1_5().createParser(new StringReader(FUNKY_GENERICS));
    164         p.CompilationUnit();
    165         p = new TargetJDK1_5().createParser(new StringReader(MULTIPLE_GENERICS));
    166         p.CompilationUnit();
    167     }
    168
    169     public void testAnnotatedParams() throws Throwable {
    170         JavaParser p = new TargetJDK1_5().createParser(new StringReader(ANNOTATED_PARAMS));
    171         p.CompilationUnit();
    172     }
    173
    174     public void testAnnotatedLocals() throws Throwable {
    175         JavaParser p = new TargetJDK1_5().createParser(new StringReader(ANNOTATED_LOCALS));
    176         p.CompilationUnit();
    177     }
    178
    179     public void testAssertAsIdentifierSucceedsWith1_3_test2() {
    180         JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST5_a));
    181         jp.CompilationUnit();
    182     }
    183
    184
    185     private static final String ANNOTATED_LOCALS =
    186             "public class Foo {" + PMD.EOL +
    187             " void bar() {" + PMD.EOL +
    188             "  @SuppressWarnings(\"foo\") int y = 5;" + PMD.EOL +
    189             " }" + PMD.EOL +
    190             "}";
    191
    192     private static final String ANNOTATED_PARAMS =
    193             "public class Foo {" + PMD.EOL +
    194             " void bar(@SuppressWarnings(\"foo\") int x) {}" + PMD.EOL +
    195             "}";
    196
    197     private static final String ASSERT_TEST1 =
    198             "public class Foo {" + PMD.EOL +
    199             " void bar() {" + PMD.EOL +
    200             "  assert x == 2;" + PMD.EOL +
    201             " }" + PMD.EOL +
    202             "}";
    203
    204     private static final String ASSERT_TEST2 =
    205             "public class Foo {" + PMD.EOL +
    206             " void bar() {" + PMD.EOL +
    207             "  assert (x == 2);" + PMD.EOL +
    208             " }" + PMD.EOL +
    209             "}";
    210
    211     private static final String ASSERT_TEST3 =
    212             "public class Foo {" + PMD.EOL +
    213             " void bar() {" + PMD.EOL +
    214             "  assert (x==2) : \"hi!\";" + PMD.EOL +
    215             " }" + PMD.EOL +
    216             "}";
    217
    218     private static final String ASSERT_TEST4 =
    219             "public class Foo {" + PMD.EOL +
    220             " void bar() {" + PMD.EOL +
    221             "  assert (x==2) : \"hi!\";" + PMD.EOL +
    222             " }" + PMD.EOL +
    223             "}";
    224
    225     private static final String ASSERT_TEST5 =
    226             "public class Foo {" + PMD.EOL +
    227             "  int assert = 2;" + PMD.EOL +
    228             "}";
    229
    230
    231     private static final String ASSERT_TEST5_a =
    232             "public class Foo {" + PMD.EOL +
    233             "  void bar() { assert(); }" + PMD.EOL +
    234             "}";
    235
    236     private static final String ASSERT_TEST6 =
    237             "public class Foo {" + PMD.EOL +
    238             " void foo() {" + PMD.EOL +
    239             "  assert (x == 2) : \"hi!\";" + PMD.EOL +
    240             " }" + PMD.EOL +
    241             "}";
    242
    243     private static final String ASSERT_TEST7 =
    244             "public class Foo {" + PMD.EOL +
    245             " void assert() {}" + PMD.EOL +
    246             "}";
    247
    248     private static final String JDK15_ENUM =
    249             "public class Test {" + PMD.EOL +
    250             " enum Season { winter, spring, summer, fall };" + PMD.EOL +
    251             "}";
    252
    253     private static final String JDK14_ENUM =
    254             "public class Test {" + PMD.EOL +
    255             " int enum;" + PMD.EOL +
    256             "}";
    257
    258     private static final String JDK15_VARARGS =
    259             "public class Test {" + PMD.EOL +
    260             " void bar(Object ... args) {}" + PMD.EOL +
    261             "}";
    262
    263     private static final String JDK15_FORLOOP =
    264             "public class Test {" + PMD.EOL +
    265             " void foo(List list) {" + PMD.EOL +
    266             "  for (Integer i : list) {}" + PMD.EOL +
    267             " }" + PMD.EOL +
    268             "}";
    269
    270     private static final String JDK15_FORLOOP_WITH_MODIFIER =
    271             "public class Test {" + PMD.EOL +
    272             " void foo(List list) {" + PMD.EOL +
    273             "  for (final Integer i : list) {}" + PMD.EOL +
    274             " }" + PMD.EOL +
    275             "}";
    276
    277     private static final String JDK15_GENERICS =
    278             "public class Test {" + PMD.EOL +
    279             "  ArrayList<Integer> list =  new ArrayList<Integer>();" + PMD.EOL +
    280             "}";
    281
    282     private static final String FIELDS_BUG =
    283             "public class Test {" + PMD.EOL +
    284             "  private Foo bar;" + PMD.EOL +
    285             "}";
    286
    287     private static final String GT_BUG =
    288             "public class Test {" + PMD.EOL +
    289             "  int y = x > 32;" + PMD.EOL +
    290             "}";
    291
    292     private static final String ANNOTATIONS_BUG =
    293             "@Target(ElementType.METHOD)" + PMD.EOL +
    294             "public @interface Foo {" + PMD.EOL +
    295             "}";
    296
    297     private static final String GENERIC_IN_FIELD =
    298             "public class Foo {" + PMD.EOL +
    299             " Class<Double> foo = (Class<Double>)clazz;" + PMD.EOL +
    300             "}";
    301
    302     private static final String GENERIC_IN_ANNOTATION =
    303             "public class Foo {" + PMD.EOL +
    304             " public <A extends Annotation> A foo(Class<A> c) {" + PMD.EOL +
    305             "  return null;" + PMD.EOL +
    306             " }" + PMD.EOL +
    307             "}";
    308
    309     private static final String INNER_BUG =
    310             "public class Test {" + PMD.EOL +
    311             "  void bar() {" + PMD.EOL +
    312             "   final class Inner {};" + PMD.EOL +
    313             "   Inner i = new Inner();" + PMD.EOL +
    314             "  }" + PMD.EOL +
    315             "}";
    316
    317     private static final String INNER_BUG2 =
    318             "public class Test {" + PMD.EOL +
    319             "  void bar() {" + PMD.EOL +
    320             "   class Inner {};" + PMD.EOL +
    321             "   Inner i = new Inner();" + PMD.EOL +
    322             "  }" + PMD.EOL +
    323             "}";
    324
    325     private static final String GENERIC_IN_METHOD_CALL =
    326             "public class Test {" + PMD.EOL +
    327             "  List<String> test() {" + PMD.EOL +
    328             "   return Collections.<String>emptyList();" + PMD.EOL +
    329             "  }" + PMD.EOL +
    330             "}";
    331
    332     private static final String GENERIC_RETURN_TYPE =
    333             "public class Test {" + PMD.EOL +
    334             "  public static <String> String test(String x) {" + PMD.EOL +
    335             "   return x;" + PMD.EOL +
    336             "  }" + PMD.EOL +
    337             "}";
    338
    339     // See java/lang/concurrent/ConcurrentHashMap
    340     private static final String MULTIPLE_GENERICS =
    341             "public class Foo<K,V> {" + PMD.EOL +
    342             "  public <A extends K, B extends V> Foo(Bar<A,B> t) {}" + PMD.EOL +
    343             "}";
    344
    345     // See java/lang/concurrent/CopyOnWriteArraySet
    346     private static final String FUNKY_GENERICS =
    347             "public class Foo {" + PMD.EOL +
    348             "  public <T extends E> Foo() {}" + PMD.EOL +
    349             "}";
    350 }