KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > net > sourceforge > pmd > rules > strings > InefficientStringBufferingTest


1
2  /**
3   * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
4  */

5  package test.net.sourceforge.pmd.rules.strings;
6  
7  import net.sourceforge.pmd.PMD;
8  import net.sourceforge.pmd.Rule;
9  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
11  
12  public class InefficientStringBufferingTest extends SimpleAggregatorTst {
13  
14      private Rule rule;
15  
16      public void setUp() {
17          rule = findRule("strings", "InefficientStringBuffering");
18      }
19  
20      public void testAll() {
21         runTests(new TestDescriptor[] {
22                 new TestDescriptor(TEST1, "concatenating a literal to a method return value", 1, rule),
23                 new TestDescriptor(TEST2, "same as TEST1, but in SB constructor", 1, rule),
24                 new TestDescriptor(TEST3, "chained appends", 0, rule),
25                 new TestDescriptor(TEST4, "concatenating two literals in SB constructor", 0, rule),
26                 new TestDescriptor(TEST5, "concatenating two literals post-construction", 0, rule),
27                 new TestDescriptor(TEST6, "case where concatenation is not a child of a BlockStatement, but instead is a child of an ExplicitConstructorInvocation", 0, rule),
28                 new TestDescriptor(TEST7, "don't error out on array instantiation", 0, rule),
29                 new TestDescriptor(TEST8, "usage of the StringBuffer constructor that takes an int", 0, rule),
30                 new TestDescriptor(TEST9, "nested", 0, rule),
31                 new TestDescriptor(TEST10, "looking up too high", 0, rule),
32                 new TestDescriptor(TEST11, "looking too deep", 0, rule),
33                 new TestDescriptor(TEST12, "concatenating two non-literals", 1, rule),
34                 new TestDescriptor(TEST13, "concatenating method + int", 0, rule),
35                 new TestDescriptor(TEST14, "JTextArea.append", 0, rule),
36                 new TestDescriptor(TEST15, "don't get thrown off by a buried literal", 1, rule),
37                 new TestDescriptor(TEST16, "sb.delete shouldn't trigger it", 0, rule),
38                 new TestDescriptor(TEST17, "skip additions involving static finals, compiler will do constant folding for these", 0, rule),
39                 new TestDescriptor(TEST18, "for statement without braces", 1, rule),
40                 new TestDescriptor(TEST19, "if statement without braces", 1, rule),
41                 new TestDescriptor(TEST20, "3 args version of StringBuffer.append", 0, rule),
42                 new TestDescriptor(TEST21, "compile-time concats are ok", 0, rule),
43                 new TestDescriptor(TEST22, "compile-time concats are ok, v2", 0, rule),
44         });
45      }
46  
47     private static final String JavaDoc TEST1 =
48      "public class Foo {" + PMD.EOL +
49      " private void baz() {" + PMD.EOL +
50      " StringBuffer sb = new StringBuffer();" + PMD.EOL +
51      " sb.append(\"hello\"+ world()); "+
52      " }" + PMD.EOL +
53      "}";
54  
55     private static final String JavaDoc TEST2 =
56         "public class Foo {" + PMD.EOL +
57         " private void baz() {" + PMD.EOL +
58         " StringBuffer sb = new StringBuffer(\"hello\"+ world());" + PMD.EOL +
59         " }" + PMD.EOL +
60         "}";
61  
62     private static final String JavaDoc TEST3 =
63         "public class Foo {" + PMD.EOL +
64         " private void baz() {" + PMD.EOL +
65         " StringBuffer sb = new StringBuffer();" + PMD.EOL +
66         " sb.append(\"hello\").append(world()); "+
67         " }" + PMD.EOL +
68         "}";
69  
70     private static final String JavaDoc TEST4 =
71         "public class Foo {" + PMD.EOL +
72         " private void baz() {" + PMD.EOL +
73         " StringBuffer sb = new StringBuffer(\"hello\"+ \"world\");" + PMD.EOL +
74         " }" + PMD.EOL +
75         "}";
76     
77     private static final String JavaDoc TEST5 =
78         "public class Foo {" + PMD.EOL +
79         " private void baz() {" + PMD.EOL +
80         " StringBuffer sb = new StringBuffer();" + PMD.EOL +
81         " sb.append(\"hello\"+\"world\"); "+
82         " }" + PMD.EOL +
83         "}";
84  
85     private static final String JavaDoc TEST6 =
86         "public class Foo {" + PMD.EOL +
87         " public Foo() {" + PMD.EOL +
88         " super(\"CauseMsg:\" + ex.getMessage(), ex); " + PMD.EOL +
89         " }" + PMD.EOL +
90         "}";
91  
92      private static final String JavaDoc TEST7 =
93          "public class Foo {" + PMD.EOL +
94          " public void bar() {" + PMD.EOL +
95          " int t[] = new int[x+y+1];" + PMD.EOL +
96          " }" + PMD.EOL +
97          "}";
98  
99  
100     private static final String JavaDoc TEST8 =
101         "public class Foo {" + PMD.EOL +
102         " public int foor() {return 2;}" + PMD.EOL +
103         " public void bar(int x) {" + PMD.EOL +
104         " StringBuffer buf = new StringBuffer(1 + foo());" + PMD.EOL +
105         " }" + PMD.EOL +
106         "}";
107  
108     private static final String JavaDoc TEST9 =
109         "public class Foo {" + PMD.EOL +
110         " public void bar(int x) {" + PMD.EOL +
111         " StringBuffer buf = new StringBuffer(x);" + PMD.EOL +
112         " }" + PMD.EOL +
113         "}";
114  
115     private static final String JavaDoc TEST10 =
116         "public class Foo {" + PMD.EOL +
117         " public void bar() {" + PMD.EOL +
118         " if (foo) {" + PMD.EOL +
119         " StringBuffer buf = new StringBuffer();" + PMD.EOL +
120         " buf.append(\"hello\");" + PMD.EOL +
121         " Object x = a(\"world\" + x, buf.toString());" + PMD.EOL +
122         " }" + PMD.EOL +
123         " }" + PMD.EOL +
124         "}";
125  
126     private static final String JavaDoc TEST11 =
127         "public class Foo {" + PMD.EOL +
128         " public void bar(int i) {" + PMD.EOL +
129         " StringBuffer buf = new StringBuffer();" + PMD.EOL +
130         " buf.append(getFoo(getBar(i + \"hi\")));" + PMD.EOL +
131         " }" + PMD.EOL +
132         "}";
133  
134     private static final String JavaDoc TEST12 =
135         "public class Foo {" + PMD.EOL +
136         " public void bar(String a, String b) {" + PMD.EOL +
137         " StringBuffer buf = new StringBuffer();" + PMD.EOL +
138         " buf.append(a + b);" + PMD.EOL +
139         " }" + PMD.EOL +
140         "}";
141  
142     private static final String JavaDoc TEST13 =
143         "public class Foo {" + PMD.EOL +
144         " public void bar(Date a) {" + PMD.EOL +
145         " StringBuffer buf = new StringBuffer();" + PMD.EOL +
146         " buf.append(a.getYear() + 1900);" + PMD.EOL +
147         " }" + PMD.EOL +
148         "}";
149  
150     private static final String JavaDoc TEST14 =
151         "public class Foo {" + PMD.EOL +
152         " public void bar(JTextArea jta) {" + PMD.EOL +
153         " jta.append(f + \"hi\");" + PMD.EOL +
154         " }" + PMD.EOL +
155         "}";
156  
157      private static final String JavaDoc TEST15 =
158          "public class Foo {" + PMD.EOL +
159          " private void baz() {" + PMD.EOL +
160          " StringBuffer sb = new StringBuffer(\"hello\"+ System.getProperty(\"blah\"));" + PMD.EOL +
161          " }" + PMD.EOL +
162          "}";
163  
164      private static final String JavaDoc TEST16 =
165          "public class Foo {" + PMD.EOL +
166          " public void bar(StringBuffer sb) {" + PMD.EOL +
167          " sb.delete(x, y+z);" + PMD.EOL +
168          " }" + PMD.EOL +
169          "}";
170  
171      private static final String JavaDoc TEST17 =
172          "public class Foo {" + PMD.EOL +
173          " public static final String FOO = \"bar\";" + PMD.EOL +
174          " public void bar(StringBuffer sb) {" + PMD.EOL +
175          " sb.append(\"foo\" + FOO);" + PMD.EOL +
176          " }" + PMD.EOL +
177          "}";
178     
179      private static final String JavaDoc TEST18 =
180          "public class Foo {" + PMD.EOL +
181          " private void baz() {" + PMD.EOL +
182          " StringBuffer sb = new StringBuffer();" + PMD.EOL +
183          " for(int ix = 0; ix < 100; ix++) "+
184          " sb.append(\"hello\"+ world()); "+
185          " }" + PMD.EOL +
186          "}";
187  
188      private static final String JavaDoc TEST19 =
189          "public class Foo {" + PMD.EOL +
190          " private void baz() {" + PMD.EOL +
191          " StringBuffer sb = new StringBuffer();" + PMD.EOL +
192          " if(true) "+
193          " sb.append(\"hello\"+ world()); "+
194          " }" + PMD.EOL +
195          "}";
196  
197      private static final String JavaDoc TEST20 =
198          "public class Foo {" + PMD.EOL +
199          " private void baz(StringBuffer s, char[] chars, int start, int end) {" + PMD.EOL +
200          " s.append(chars, start, start - end);" + PMD.EOL +
201          " }" + PMD.EOL +
202          "}";
203  
204  
205      private static final String JavaDoc TEST21 =
206              "public class Foo {" + PMD.EOL +
207              " private void baz() {" + PMD.EOL +
208              "StringBuffer buffer = new StringBuffer(" + PMD.EOL +
209              "\"a\" + \"b\" + \"c\");" + PMD.EOL +
210              "} }";
211  
212      private static final String JavaDoc TEST22 =
213              "public class Foo {" + PMD.EOL +
214              "static final String BAR = \"foo\";" + PMD.EOL +
215              " private void baz() {" + PMD.EOL +
216              "StringBuffer buffer = new StringBuffer(" + PMD.EOL +
217              "\"a\" + BAR + \"b\" + BAR);" + PMD.EOL +
218              "} }";
219  }
220
Popular Tags