    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3   */
    4   package test.net.sourceforge.pmd.rules.strings;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class InsufficientStringBufferDeclarationTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("strings", "InsufficientStringBufferDeclaration");
    17      }
    18
    19      public void testAll() {
    20
    21          // first run the legal tests
    22          runTests(new TestDescriptor[] {
    23                  new TestDescriptor(TEST32, "32, Constructor from math", 0, rule),
    24          });
    25
    26          runTests(new TestDescriptor[] {
    27                  new TestDescriptor(TEST1, "1, StringBuffer allocated with enough space", 0, rule),
    28                  new TestDescriptor(TEST3, "3, StringBuffer allocated with space", 0, rule),
    29                  new TestDescriptor(TEST4, "4, StringBuffer allocated from variable", 0, rule),
    30                  new TestDescriptor(TEST5, "5, creating a new StringBuffer", 0, rule),
    31                  new TestDescriptor(TEST6, "6, Initialize with a specific String", 1, rule),
    32                  new TestDescriptor(TEST7, "7, appends inside if statements", 0, rule),
    33                  new TestDescriptor(TEST8, "8, Field level variable", 0, rule),
    34                  new TestDescriptor(TEST10, "10, Appending non-literals", 0, rule),
    35                  new TestDescriptor(TEST11, "11, Initialized to null", 0, rule),
    36                  new TestDescriptor(TEST12, "12, Passed in as parameter", 0, rule),
    37                  new TestDescriptor(TEST14, "14, Compound append, presized just fine", 0, rule),
    38                  new TestDescriptor(TEST16, "16, Append int, properly presized", 0, rule),
    39                  new TestDescriptor(TEST18, "18, Append char, properly presized", 0, rule),
    40                  new TestDescriptor(TEST22, "22, appends inside if/else if/else statements", 0, rule),
    41                  new TestDescriptor(TEST23, "23, appends inside if/else if/else statements", 0, rule),
    42                  new TestDescriptor(TEST24, "24, appends inside if/else if/else statements", 1, rule),
    43                  new TestDescriptor(TEST25, "25, Compound ifs", 0, rule),
    44                  new TestDescriptor(TEST27, "27, Switch statement doesn't exceed 16 characters", 0, rule),
    45                  new TestDescriptor(TEST29, "29, Appending from a cast", 0, rule),
    46                  new TestDescriptor(TEST30, "30, Appending chars", 0, rule),
    47                  new TestDescriptor(TEST31, "31, Appending from a cast in ifs", 0, rule),
    48                  new TestDescriptor(TEST32, "32, Constructor from math", 0, rule),
    49         });
    50
    51         // Then run the failure tests
    52         runTests(new TestDescriptor[] {
    53                 new TestDescriptor(TEST2_FAIL, "2, StringBuffer not allocated with enough space", 1, rule),
    54                 new TestDescriptor(TEST9_FAIL, "9, Field level variable", 1, rule),
    55                 new TestDescriptor(TEST13_FAIL, "13, compound append", 1, rule),
    56                 new TestDescriptor(TEST15_FAIL, "15, Append int, incorrect presize", 1, rule),
    57                 new TestDescriptor(TEST17_FAIL, "17, Append char, incorrect presize", 1, rule),
    58                 new TestDescriptor(TEST19_FAIL, "19, String concatenation, incorrect presize", 1, rule),
    59                 new TestDescriptor(TEST20_FAIL, "20, String concatenation with non-literal, incorrect presize", 1, rule),
    60                 new TestDescriptor(TEST21_FAIL, "21, Incorrectly presized twice", 2, rule),
    61                 new TestDescriptor(TEST26_FAIL, "26, Compound if, pushed over the edge", 1, rule),
    62                 new TestDescriptor(TEST28_FAIL, "28, Compound if, pushed over the edge", 1, rule),
    63         });
    64      }
    65
    66      private static final String TEST1 =
    67          "public class Foo {" + PMD.EOL +
    68          "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    69          "   public void bar() {" + PMD.EOL +
    70          "       StringBuffer sb = new StringBuffer(16);" + PMD.EOL +
    71          "       sb.append(\"foo\");" + PMD.EOL +
    72          " }" + PMD.EOL +
    73          "}";
    74
    75      private static final String TEST2_FAIL =
    76          "public class Foo {" + PMD.EOL +
    77          " public void bar() {" + PMD.EOL +
    78          "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    79          "  sb.append(\"Hello\");" + PMD.EOL +
    80          "  sb.append(\"World\");" + PMD.EOL +
    81          "  sb.append(\"How are you today world\");" + PMD.EOL +
    82          " }" + PMD.EOL +
    83          "}";
    84
    85
    86      private static final String TEST3 =
    87          "public class Foo {" + PMD.EOL +
    88          " public void bar(List l) {" + PMD.EOL +
    89          "  StringBuffer sb = new StringBuffer(l.size());" + PMD.EOL +
    90          "  sb.append(\"Hello\");" + PMD.EOL +
    91          "  sb.append(\"World\");" + PMD.EOL +
    92          "  sb.append(\"How are you today world\");" + PMD.EOL +
    93          " }" + PMD.EOL +
    94          "}";
    95
    96      private static final String TEST4 =
    97          "public class Foo {" + PMD.EOL +
    98          " public void bar(List l) {" + PMD.EOL +
    99          "  int x = 3;" + PMD.EOL +
    100         "  StringBuffer sb = new StringBuffer(x);" + PMD.EOL +
    101         "  sb.append(\"Hello\");" + PMD.EOL +
    102         "  sb.append(\"World\");" + PMD.EOL +
    103         "  sb.append(\"How are you today world\");" + PMD.EOL +
    104         " }" + PMD.EOL +
    105         "}";
    106
    107     private static final String TEST5 =
    108         "public class Foo {" + PMD.EOL +
    109         " public void bar(List l) {" + PMD.EOL +
    110         "  int x = 3;" + PMD.EOL +
    111         "  StringBuffer sb = new StringBuffer(5);" + PMD.EOL +
    112         "  sb.append(\"Hello\");" + PMD.EOL +
    113         "  sb = new StringBuffer(23);" + PMD.EOL +
    114         "  sb.append(\"How are you today world\");" + PMD.EOL +
    115         " }" + PMD.EOL +
    116         "}";
    117
    118     private static final String TEST6 =
    119         "public class Foo {" + PMD.EOL +
    120         " public void bar(List l) {" + PMD.EOL +
    121         "  int x = 3;" + PMD.EOL +
    122         "  StringBuffer sb = new StringBuffer(\"Initialize With A String\");" + PMD.EOL +
    123         "  sb.append(\"Hello\");" + PMD.EOL +
    124         "  sb.append(\"How are you today world\");" + PMD.EOL +
    125         " }" + PMD.EOL +
    126         "}";
    127
    128     private static final String TEST7 =
    129         "public class Foo {" + PMD.EOL +
    130         "    public void bar(List l) {" + PMD.EOL +
    131         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    132         "        if(true){" + PMD.EOL +
    133         "            sb.append(\"1234567890\");" + PMD.EOL +
    134         "        } else {" + PMD.EOL +
    135         "            sb.append(\"123456789\");" + PMD.EOL +
    136         "        }" + PMD.EOL +
    137         "    }" + PMD.EOL +
    138         "}";
    139
    140     private static final String TEST8 =
    141         "public class Foo {" + PMD.EOL +
    142         "  StringBuffer sb = new StringBuffer(200);" + PMD.EOL +
    143         " public void bar(List l) {" + PMD.EOL +
    144         "  sb.append(\"Hello\");" + PMD.EOL +
    145         "  sb.append(\"How are you today world\");" + PMD.EOL +
    146         " }" + PMD.EOL +
    147         "}";
    148
    149     private static final String TEST9_FAIL =
    150         "public class Foo {" + PMD.EOL +
    151         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    152         " public void bar(List l) {" + PMD.EOL +
    153         "  sb.append(\"Hello\");" + PMD.EOL +
    154         "  sb.append(\"How are you today world\");" + PMD.EOL +
    155         " }" + PMD.EOL +
    156         "}";
    157
    158     private static final String TEST10 =
    159         "public class Foo {" + PMD.EOL +
    160         " public void bar(List l) {" + PMD.EOL +
    161         "  StringBuffer sb = new StringBuffer(0);" + PMD.EOL +
    162         "  sb.append(l.get(2));" + PMD.EOL +
    163         "  sb.append(l.toString());" + PMD.EOL +
    164         " }" + PMD.EOL +
    165         "}";
    166
    167     private static final String TEST11 =
    168         "public class Foo {" + PMD.EOL +
    169         " public void bar(List l) {" + PMD.EOL +
    170         "  StringBuffer sb = null;" + PMD.EOL +
    171         "  sb = new StringBuffer(20);" + PMD.EOL +
    172         "  sb.append(l.toString());" + PMD.EOL +
    173         " }" + PMD.EOL +
    174         "}";
    175
    176
    177     private static final String TEST12 =
    178         "public class Foo {" + PMD.EOL +
    179         " public void bar(StringBuffer param) {" + PMD.EOL +
    180         "  param.append(\"Append something\");" + PMD.EOL +
    181         " }" + PMD.EOL +
    182         "}";
    183
    184
    185     private static final String TEST13_FAIL =
    186         "public class Foo {" + PMD.EOL +
    187         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    188         "   public void bar() {" + PMD.EOL +
    189         "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    190         "       sb.append(\"foo\").append(\"this will make it long\");" + PMD.EOL +
    191         " }" + PMD.EOL +
    192         "}";
    193
    194
    195     private static final String TEST14 =
    196         "public class Foo {" + PMD.EOL +
    197         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    198         "   public void bar() {" + PMD.EOL +
    199         "       StringBuffer sb = new StringBuffer(30);" + PMD.EOL +
    200         "       sb.append(\"foo\").append(\"this is presized just right\");" + PMD.EOL +
    201         " }" + PMD.EOL +
    202         "}";
    203
    204     private static final String TEST15_FAIL =
    205         "public class Foo {" + PMD.EOL +
    206         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    207         "   public void bar() {" + PMD.EOL +
    208         "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    209         "       sb.append(12345678901234567890);" + PMD.EOL +
    210         " }" + PMD.EOL +
    211         "}";
    212
    213
    214     private static final String TEST16 =
    215         "public class Foo {" + PMD.EOL +
    216         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    217         "   public void bar() {" + PMD.EOL +
    218         "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    219         "       sb.append(12345);" + PMD.EOL +
    220         " }" + PMD.EOL +
    221         "}";
    222
    223     private static final String TEST17_FAIL =
    224         "public class Foo {" + PMD.EOL +
    225         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    226         "   public void bar() {" + PMD.EOL +
    227         "       StringBuffer sb = new StringBuffer(2);" + PMD.EOL +
    228         "       sb.append('a');" + PMD.EOL +
    229         "       sb.append('a');" + PMD.EOL +
    230         "       sb.append('a');" + PMD.EOL +
    231         " }" + PMD.EOL +
    232         "}";
    233
    234
    235     private static final String TEST18 =
    236         "public class Foo {" + PMD.EOL +
    237         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    238         "   public void bar() {" + PMD.EOL +
    239         "       StringBuffer sb = new StringBuffer(3);" + PMD.EOL +
    240         "       sb.append('a');" + PMD.EOL +
    241         "       sb.append('a');" + PMD.EOL +
    242         "       sb.append('a');" + PMD.EOL +
    243         " }" + PMD.EOL +
    244         "}";
    245
    246
    247     private static final String TEST19_FAIL =
    248         "public class Foo {" + PMD.EOL +
    249         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    250         "   public void bar() {" + PMD.EOL +
    251         "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    252         "       sb.append(\"This string\" + \" \" + \"isn't nice, but valid\");" + PMD.EOL +
    253         " }" + PMD.EOL +
    254         "}";
    255
    256     private static final String TEST20_FAIL =
    257         "public class Foo {" + PMD.EOL +
    258         "   private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Foo.class);" + PMD.EOL +
    259         "   public void bar(String x) {" + PMD.EOL +
    260         "       StringBuffer sb = new StringBuffer();" + PMD.EOL +
    261         "       sb.append(\"This string\" + x + \"isn't nice, but valid\");" + PMD.EOL +
    262         " }" + PMD.EOL +
    263         "}";
    264
    265     private static final String TEST21_FAIL =
    266         "public class Foo {" + PMD.EOL +
    267         " public void bar(List l) {" + PMD.EOL +
    268         "  int x = 3;" + PMD.EOL +
    269         "  StringBuffer sb = new StringBuffer(2);" + PMD.EOL +
    270         "  sb.append(\"Hello\");" + PMD.EOL +
    271         "  sb = new StringBuffer(5);" + PMD.EOL +
    272         "  sb.append(\"How are you today world\");" + PMD.EOL +
    273         " }" + PMD.EOL +
    274         "}";
    275
    276     private static final String TEST22 =
    277         "public class Foo {" + PMD.EOL +
    278         "    public void bar(List l) {" + PMD.EOL +
    279         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    280         "        if(true){" + PMD.EOL +
    281         "            sb.append(\"1234567890\");" + PMD.EOL +
    282         "        } else if( l.size() == 5){" + PMD.EOL +
    283         "            sb.append(\"1234567890\");" + PMD.EOL +
    284         "        } else {" + PMD.EOL +
    285         "            sb.append(\"1234567890\");" + PMD.EOL +
    286         "        }" + PMD.EOL +
    287         "    }" + PMD.EOL +
    288         "}";
    289
    290     private static final String TEST23 =
    291         "public class Foo {" + PMD.EOL +
    292         "    public void bar(List l) {" + PMD.EOL +
    293         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    294         "        if(true){" + PMD.EOL +
    295         "            sb.append(\"12345\");" + PMD.EOL +
    296         "        } else if( l.size() == 5){" + PMD.EOL +
    297         "            sb.append(\"12345\");" + PMD.EOL +
    298         "        } else {" + PMD.EOL +
    299         "            sb.append(\"12345\");" + PMD.EOL +
    300         "        }" + PMD.EOL +
    301         "        if(true){" + PMD.EOL +
    302         "            sb.append(\"12345\");" + PMD.EOL +
    303         "        } else if( l.size() == 5){" + PMD.EOL +
    304         "            sb.append(\"12345\");" + PMD.EOL +
    305         "        } else {" + PMD.EOL +
    306         "            sb.append(\"12345\");" + PMD.EOL +
    307         "        }" + PMD.EOL +
    308         "    }" + PMD.EOL +
    309         "}";
    310
    311
    312     private static final String TEST24 =
    313         "public class Foo {" + PMD.EOL +
    314         "    public void bar(List l) {" + PMD.EOL +
    315         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    316         "        if(true){" + PMD.EOL +
    317         "            sb.append(\"This should use\");" + PMD.EOL +
    318         "        } else if( l.size() == 5){" + PMD.EOL +
    319         "            sb.append(\"The longest if\");" + PMD.EOL +
    320         "        } else {" + PMD.EOL +
    321         "            sb.append(\"statement for its violation, which is this one\");" + PMD.EOL +
    322         "        }" + PMD.EOL +
    323         "    }" + PMD.EOL +
    324         "}";
    325
    326     private static final String TEST25 =
    327         "public class Foo {" + PMD.EOL +
    328         "    public void bar(List l) {" + PMD.EOL +
    329         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    330         "        if(true){" + PMD.EOL +
    331         "           if(true){" + PMD.EOL +
    332         "                sb.append(\"More\");" + PMD.EOL +
    333         "           } else if( l.size() == 5){" + PMD.EOL +
    334         "                sb.append(\"Compound\");" + PMD.EOL +
    335         "           } else {" + PMD.EOL +
    336         "               sb.append(\"If\");" + PMD.EOL +
    337         "           }" + PMD.EOL +
    338         "       } else {" + PMD.EOL +
    339         "           sb.append(\"A compound if\");" + PMD.EOL +
    340         "       } " + PMD.EOL +
    341         "    }" + PMD.EOL +
    342         "}";
    343
    344     private static final String TEST26_FAIL =
    345         "public class Foo {" + PMD.EOL +
    346         "    public void bar(List l) {" + PMD.EOL +
    347         "        StringBuffer sb = new StringBuffer();" + PMD.EOL +
    348         "        if(true){" + PMD.EOL +
    349         "           if(true){" + PMD.EOL +
    350         "                sb.append(\"More\");" + PMD.EOL +
    351         "           } else if( l.size() == 5){" + PMD.EOL +
    352         "                sb.append(\"Compound\");" + PMD.EOL +
    353         "           } else {" + PMD.EOL +
    354         "               sb.append(\"If\");" + PMD.EOL +
    355         "           }" + PMD.EOL +
    356         "       } else {" + PMD.EOL +
    357         "           sb.append(\"A compound if\");" + PMD.EOL +
    358         "       } " + PMD.EOL +
    359         "       sb.append(\"Push\");" + PMD.EOL +
    360         "    }" + PMD.EOL +
    361         "}";
    362
    363     private static final String TEST27 =
    364         "public class Foo {" + PMD.EOL +
    365         " public void bar(String str) {" + PMD.EOL +
    366         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    367         "    switch(str.charAt(0)){" + PMD.EOL +
    368         "        case 'a':" + PMD.EOL +
    369         "            sb.append(\"Switch block\");" + PMD.EOL +
    370         "        break;" + PMD.EOL +
    371         "        case 'b':" + PMD.EOL +
    372         "            sb.append(\"Doesn't exceed\");" + PMD.EOL +
    373         "        break;" + PMD.EOL +
    374         "        default:" + PMD.EOL +
    375         "            sb.append(\"16 chars\");" + PMD.EOL +
    376         "    }" + PMD.EOL +
    377         " }" + PMD.EOL +
    378         "}";
    379
    380     private static final String TEST28_FAIL =
    381         "public class Foo {" + PMD.EOL +
    382         " public void bar(String str) {" + PMD.EOL +
    383         "  StringBuffer sb = new StringBuffer();" + PMD.EOL +
    384         "    switch(str.charAt(0)){" + PMD.EOL +
    385         "        case 'a':" + PMD.EOL +
    386         "            sb.append(\"Switch block\");" + PMD.EOL +
    387         "        break;" + PMD.EOL +
    388         "        default:" + PMD.EOL +
    389         "            sb.append(\"The default block exceeds 16 characters and will fail\");" + PMD.EOL +
    390         "    }" + PMD.EOL +
    391         " }" + PMD.EOL +
    392         "}";
    393
    394     private static final String TEST29 =
    395             "public class Foo {" + PMD.EOL +
    396             " public void bar() {" + PMD.EOL +
    397             "  StringBuffer sb = new StringBuffer(1);" + PMD.EOL +
    398             "   sb.append((char) 0x0041);" + PMD.EOL +
    399             " }" + PMD.EOL +
    400             "}";
    401
    402     private static final String TEST30 =
    403             "public class Foo {" + PMD.EOL +
    404             " public void bar(char longnamedchar) {" + PMD.EOL +
    405             "  StringBuffer sb = new StringBuffer(1);" + PMD.EOL +
    406             "   sb.append(longnamedchar);" + PMD.EOL +
    407             " }" + PMD.EOL +
    408             "}";
    409
    410     private static final String TEST31 =
    411             "public class Foo {" + PMD.EOL +
    412             " public void bar(int i) {" + PMD.EOL +
    413             "  StringBuffer sb = new StringBuffer(1);" + PMD.EOL +
    414             "   if(i == 1){" + PMD.EOL +
    415             "       sb.append((char) 0x0041);" + PMD.EOL +
    416             "   } else if(i == 2){" + PMD.EOL +
    417             "       sb.append((char) 0x0041);" + PMD.EOL +
    418             "   } else if(i == 19){" + PMD.EOL +
    419             "       sb.append((char) 0x0041);" + PMD.EOL +
    420             "   } else {" + PMD.EOL +
    421             "       sb.append((char) 0x0041);" + PMD.EOL +
    422             "   } " + PMD.EOL +
    423             " }" + PMD.EOL +
    424             "}";
    425
    426
    427     private static final String TEST32 =
    428             "public class Foo {" + PMD.EOL +
    429             " public void bar(char longnamedchar) {" + PMD.EOL +
    430             "  StringBuffer sb = new StringBuffer(132+42);" + PMD.EOL +
    431             "   sb.append(\"Some string. At this point I'm not doing anything to count characters, assuming developer knows what they're doing\");" + PMD.EOL +
    432             "  StringBuffer sb1 = new StringBuffer(132*42);" + PMD.EOL +
    433             "   sb1.append(\"Some string. At this point I'm not doing anything to count characters, assuming developer knows what they're doing\");" + PMD.EOL +
    434             " }" + PMD.EOL +
    435             "}";
    436
    437
    438 }