1   package test.net.sourceforge.pmd.rules.optimization;
2   
3   import net.sourceforge.pmd.PMD;
4   import net.sourceforge.pmd.Rule;
5   import net.sourceforge.pmd.RuleSetNotFoundException;
6   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
7   import test.net.sourceforge.pmd.testframework.TestDescriptor;
8   
9   public class UseArraysAsListTest extends SimpleAggregatorTst {
10  
11      private Rule rule;
12  
13      public void setUp() throws RuleSetNotFoundException {
14          rule = findRule("optimizations", "UseArraysAsList");
15      }
16  
17      // FIXME should be able to catch case where Integer[] is passed
18      // as an argument... but may need to rewrite in Java for that.
19      public void testAll() {
20          runTests(new TestDescriptor[]{
21              new TestDescriptor(TEST1, "failure case", 1, rule),
22              new TestDescriptor(TEST2, "adding first element repeatedly", 0, rule),
23              new TestDescriptor(TEST3, "inside conditional", 0, rule),
24              new TestDescriptor(TEST4, "adding new object", 0, rule),
25              new TestDescriptor(TEST5, "calling method", 0, rule),
26          });
27      }
28  
29      private static final String TEST1 =
30              "public class Bar {" + PMD.EOL +
31              " void foo() {" + PMD.EOL +
32              "  Integer[] ints = new Integer(10); " + PMD.EOL +
33              "  List l= new ArrayList(10); " + PMD.EOL +
34              "  for (int i=0; i< 100; i++) { " + PMD.EOL +
35              "   l.add(ints[i]); " + PMD.EOL +
36              "  } " + PMD.EOL +
37              " }" + PMD.EOL +
38              "}";
39  
40      private static final String TEST2 =
41              "public class Bar {" + PMD.EOL +
42              " void foo() {" + PMD.EOL +
43              "  Integer[] ints = new Integer(10); " + PMD.EOL +
44              "  List l= new ArrayList(10); " + PMD.EOL +
45              "  for (int i=0; i< 100; i++) { " + PMD.EOL +
46              "   l.add(ints[1]); " + PMD.EOL +
47              "  } " + PMD.EOL +
48              " }" + PMD.EOL +
49              "}";
50  
51      private static final String TEST3 =
52              "public class Bar {" + PMD.EOL +
53              " void foo() {" + PMD.EOL +
54              "  Integer[] ints = new Integer(10); " + PMD.EOL +
55              "  List l= new ArrayList(10); " + PMD.EOL +
56              "  for (int i=0; i< 100; i++) { " + PMD.EOL +
57              "   if (y > 10) { l.add(ints[1]);} " + PMD.EOL +
58              "  } " + PMD.EOL +
59              " }" + PMD.EOL +
60              "}";
61  
62      private static final String TEST4 =
63              "public class Bar {" + PMD.EOL +
64              " void foo() {" + PMD.EOL +
65              "  Integer[] ints = new Integer(10); " + PMD.EOL +
66              "  List l= new ArrayList(10); " + PMD.EOL +
67              "  for (int i=0; i< 100; i++) { " + PMD.EOL +
68              "   l.add(new Integer(i+1)); " + PMD.EOL +
69              "  } " + PMD.EOL +
70              " }" + PMD.EOL +
71              "}";
72  
73      private static final String TEST5 =
74              "public class Bar {" + PMD.EOL +
75              " void foo() {" + PMD.EOL +
76              "  Integer[] ints = new Integer(10); " + PMD.EOL +
77              "  List l= new ArrayList(10); " + PMD.EOL +
78              "  for (int i=0; i< 100; i++) { " + PMD.EOL +
79              "   l.add(String.valueOf(i)); " + PMD.EOL +
80              "  } " + PMD.EOL +
81              " }" + PMD.EOL +
82              "}";
83  }