View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.symboltable;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertTrue;
9   import static org.junit.Assert.fail;
10  
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Map;
14  
15  import net.sourceforge.pmd.PMD;
16  import net.sourceforge.pmd.lang.ast.Node;
17  import net.sourceforge.pmd.lang.java.ast.ASTBlock;
18  import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
19  import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
20  import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
21  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
22  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
23  import net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration;
24  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
25  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
26  import net.sourceforge.pmd.lang.symboltable.Scope;
27  
28  import org.junit.Test;
29  public class AcceptanceTest extends STBBaseTst {
30  
31      @Test
32      public void testClashingSymbols() {
33          parseCode(TEST1);
34      }
35  
36      @Test
37      public void testInitializer() {
38          parseCode(TEST_INITIALIZERS);
39          ASTInitializer a = acu.findDescendantsOfType(ASTInitializer.class).get(0);
40          assertFalse(a.isStatic());
41          a = acu.findDescendantsOfType(ASTInitializer.class).get(1);
42          assertTrue(a.isStatic());
43      }
44  
45      @Test
46      public void testCatchBlocks() {
47          parseCode(TEST_CATCH_BLOCKS);
48          ASTCatchStatement c = acu.findDescendantsOfType(ASTCatchStatement.class).get(0);
49          ASTBlock a = c.findDescendantsOfType(ASTBlock.class).get(0);
50          Scope s = a.getScope();
51          Map<NameDeclaration, List<NameOccurrence>> vars = s.getParent().getDeclarations();
52          assertEquals(1, vars.size());
53          NameDeclaration v = vars.keySet().iterator().next();
54          assertEquals("e", v.getImage());
55          assertEquals(1, (vars.get(v)).size());
56      }
57  
58      @Test
59      public void testEq() {
60          parseCode(TEST_EQ);
61          ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
62          ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
63          Scope s = method.getScope();
64          Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
65          assertEquals(2, m.size());
66          for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
67              NameDeclaration vnd = entry.getKey();
68              List<NameOccurrence> usages = entry.getValue();
69  
70              if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
71                  assertEquals(1, usages.size());
72                  assertEquals(3, usages.get(0).getLocation().getBeginLine());
73              } else {
74                  fail("Unkown variable " + vnd);
75              }
76          }
77      }
78  
79      @Test
80      public void testFieldFinder() {
81          parseCode(TEST_FIELD);
82  //        System.out.println(TEST_FIELD);
83  
84          ASTVariableDeclaratorId declaration = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
85          assertEquals(3, declaration.getBeginLine());
86          assertEquals("bbbbbbbbbb", declaration.getImage());
87          assertEquals(1, declaration.getUsages().size());
88          NameOccurrence no = declaration.getUsages().get(0);
89          Node location = no.getLocation();
90          assertEquals(6, location.getBeginLine());
91  //        System.out.println("variable " + declaration.getImage() + " is used here: " + location.getImage());
92      }
93  
94      @Test
95      public void testDemo() {
96          parseCode(TEST_DEMO);
97  //        System.out.println(TEST_DEMO);
98          ASTMethodDeclaration node = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
99          Scope s = node.getScope();
100         Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
101         for (Iterator<NameDeclaration> i = m.keySet().iterator(); i.hasNext();) {
102             NameDeclaration d = i.next();
103             assertEquals("buz", d.getImage());
104             assertEquals("ArrayList", ((TypedNameDeclaration)d).getTypeImage());
105             List<NameOccurrence> u = m.get(d);
106             assertEquals(1, u.size());
107             NameOccurrence o = u.get(0);
108             int beginLine = o.getLocation().getBeginLine();
109             assertEquals(3, beginLine);
110 
111 //            System.out.println("Variable: " + d.getImage());
112 //            System.out.println("Type: " + d.getTypeImage());
113 //            System.out.println("Usages: " + u.size());
114 //            System.out.println("Used in line " + beginLine);
115         }
116     }
117 
118     @Test
119     public void testEnum() {
120 	parseCode(NameOccurrencesTest.TEST_ENUM);
121 
122 	ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
123 	List<NameOccurrence> usages = vdi.getUsages();
124 	assertEquals(2, usages.size());
125 	assertEquals(5, usages.get(0).getLocation().getBeginLine());
126 	assertEquals(9, usages.get(1).getLocation().getBeginLine());
127     }
128 
129     @Test
130     public void testInnerOuterClass() {
131         parseCode(TEST_INNER_CLASS);
132         ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
133         List<NameOccurrence> usages = vdi.getUsages();
134         assertEquals(2, usages.size());
135         assertEquals(5, usages.get(0).getLocation().getBeginLine());
136         assertEquals(10, usages.get(1).getLocation().getBeginLine());
137     }
138 
139     private static final String TEST_DEMO =
140             "public class Foo  {" + PMD.EOL +
141             " void bar(ArrayList buz) { " + PMD.EOL +
142             "  buz.add(\"foo\");" + PMD.EOL +
143             " } " + PMD.EOL +
144             "}" + PMD.EOL;
145 
146     private static final String TEST_EQ =
147             "public class Foo  {" + PMD.EOL +
148             " boolean foo(String a, String b) { " + PMD.EOL +
149             "  return a == b; " + PMD.EOL +
150             " } " + PMD.EOL +
151             "}" + PMD.EOL;
152 
153     private static final String TEST1 =
154             "import java.io.*;" + PMD.EOL +
155             "public class Foo  {" + PMD.EOL +
156             " void buz( ) {" + PMD.EOL +
157             "  Object o = new Serializable() { int x; };" + PMD.EOL +
158             "  Object o1 = new Serializable() { int x; };" + PMD.EOL +
159             " }" + PMD.EOL +
160             "}" + PMD.EOL;
161 
162     private static final String TEST_INITIALIZERS =
163             "public class Foo  {" + PMD.EOL +
164             " {} " + PMD.EOL +
165             " static {} " + PMD.EOL +
166             "}" + PMD.EOL;
167 
168     private static final String TEST_CATCH_BLOCKS =
169             "public class Foo  {" + PMD.EOL +
170             " void foo() { " + PMD.EOL +
171             "  try { " + PMD.EOL +
172             "  } catch (Exception e) { " + PMD.EOL +
173             "   e.printStackTrace(); " + PMD.EOL +
174             "  } " + PMD.EOL +
175             " } " + PMD.EOL +
176             "}" + PMD.EOL;
177 
178     private static final String TEST_FIELD =
179             "public class MyClass {" + PMD.EOL +
180             " private int aaaaaaaaaa; " + PMD.EOL +
181             " boolean bbbbbbbbbb = MyClass.ASCENDING; " + PMD.EOL +
182             " private int zzzzzzzzzz;" + PMD.EOL +
183             " private void doIt() {" + PMD.EOL +
184             "  if (bbbbbbbbbb) {" + PMD.EOL +
185             "  }" + PMD.EOL +
186             " }" + PMD.EOL +
187             "}" + PMD.EOL;
188 
189     public static final String TEST_INNER_CLASS =
190             "public class Outer {" + PMD.EOL +
191             "  private static class Inner {" + PMD.EOL +
192             "    private int i;" + PMD.EOL +
193             "    private Inner(int i) {" + PMD.EOL +
194             "      this.i = i;" + PMD.EOL +
195             "    }" + PMD.EOL +
196             "  }" + PMD.EOL +
197             "  public int modify(int i) {" + PMD.EOL +
198             "    Inner in = new Inner(i);" + PMD.EOL +
199             "    return in.i;" + PMD.EOL +
200             "  }" + PMD.EOL +
201             "}" + PMD.EOL;
202 
203     public static junit.framework.Test suite() {
204         return new junit.framework.JUnit4TestAdapter(AcceptanceTest.class);
205     }
206 }