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) {}    // cool
23      }
24  
25      public void testEnumAsIdentifierShouldPassWith14() throws Throwable {
26          JavaParser p = new TargetJDK1_4().createParser(new StringReader(JDK14_ENUM));
27          p.CompilationUnit();
28      }
29  
30      public void testEnumAsKeywordShouldPassWith15() throws Throwable {
31          JavaParser p = new TargetJDK1_5().createParser(new StringReader(JDK15_ENUM));
32          p.CompilationUnit();
33      }
34  
35      public void testEnumAsIdentifierShouldFailWith15() throws Throwable {
36          try {
37              TargetJDKVersion jdk = new TargetJDK1_5();
38              JavaParser p = jdk.createParser(new StringReader(JDK14_ENUM));
39              p.CompilationUnit();
40              throw new Error("JDK 1.5 parser should have failed to parse enum used as identifier");
41          } catch (ParseException e) {}    // cool
42      }
43      // enum keyword/identifier
44  
45      // assert keyword/identifier
46      public void testAssertAsKeywordVariantsSucceedWith1_4() {
47          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST1)).CompilationUnit();
48          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST2)).CompilationUnit();
49          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST3)).CompilationUnit();
50          (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST4)).CompilationUnit();
51      }
52  
53      public void testAssertAsVariableDeclIdentifierFailsWith1_4() {
54          try {
55              (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST5)).CompilationUnit();
56              throw new RuntimeException("Usage of assert as identifier should have failed with 1.4");
57          } catch (ParseException pe) {
58              // cool
59          }
60      }
61  
62      public void testAssertAsMethodNameIdentifierFailsWith1_4() {
63          try {
64              (new TargetJDK1_4()).createParser(new StringReader(ASSERT_TEST7)).CompilationUnit();
65              throw new RuntimeException("Usage of assert as identifier should have failed with 1.4");
66          } catch (ParseException pe) {
67              // cool
68          }
69      }
70  
71      public void testAssertAsIdentifierSucceedsWith1_3() {
72          JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST5));
73          jp.CompilationUnit();
74      }
75  
76      public void testAssertAsKeywordFailsWith1_3() {
77          try {
78              JavaParser jp = (new TargetJDK1_3()).createParser(new StringReader(ASSERT_TEST6));
79              jp.CompilationUnit();
80              throw new RuntimeException("Usage of assert as keyword should have failed with 1.3");
81          } catch (ParseException pe) {
82              // cool
83          }
84      }
85      // assert keyword/identifier
86  
87      private static final String ASSERT_TEST1 =
88      "public class Foo {" + PMD.EOL +
89      " void bar() {" + PMD.EOL +
90      "  assert x>2;" + PMD.EOL +
91      " }" + PMD.EOL +
92      "}";
93  
94      private static final String ASSERT_TEST2 =
95      "public class Foo {" + PMD.EOL +
96      " void bar() {" + PMD.EOL +
97      "  assert (x>2);" + PMD.EOL +
98      " }" + PMD.EOL +
99      "}";
100 
101     private static final String ASSERT_TEST3 =
102     "public class Foo {" + PMD.EOL +
103     " void bar() {" + PMD.EOL +
104     "  assert x>2 : \"hi!\";" + PMD.EOL +
105     " }" + PMD.EOL +
106     "}";
107 
108     private static final String ASSERT_TEST4 =
109     "public class Foo {" + PMD.EOL +
110     " void bar() {" + PMD.EOL +
111     "  assert (x>2) : \"hi!\";" + PMD.EOL +
112     " }" + PMD.EOL +
113     "}";
114 
115     private static final String ASSERT_TEST5 =
116     "public class Foo {" + PMD.EOL +
117     "  int assert = 2;" + PMD.EOL +
118     "}";
119 
120     private static final String ASSERT_TEST6 =
121     "public class Foo {" + PMD.EOL +
122     " void foo() {" + PMD.EOL +
123     "  assert (x>2) : \"hi!\";" + PMD.EOL +
124     " }" + PMD.EOL +
125     "}";
126 
127     private static final String ASSERT_TEST7 =
128     "public class Foo {" + PMD.EOL +
129     " void assert() {}" + PMD.EOL +
130     "}";
131 
132     private static final String JDK15_ENUM =
133     "public class Test {" + PMD.EOL +
134     " enum Season { winter, spring, summer, fall };" + PMD.EOL +
135     "}";
136 
137     private static final String JDK14_ENUM =
138     "public class Test {" + PMD.EOL +
139     " int enum;" + PMD.EOL +
140     "}";
141 }