    1   /*
    2    * Created on Jan 10, 2005
    3    *
    4    * $Id: AvoidInstantiatingObjectsInLoopsTest.java,v 1.7 2006/02/10 14:26:26 tomcopeland Exp $
    5    */
    6   package test.net.sourceforge.pmd.rules.optimization;
    7
    8   import net.sourceforge.pmd.PMD;
    9   import net.sourceforge.pmd.Rule;
    10  import net.sourceforge.pmd.RuleSetNotFoundException;
    11  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    12  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    13
    14  /***
    15   * Tests for the rule AvoidInstantiatingObjectsInLoops
    16   *
    17   * @author mgriffa
    18   */
    19  public class AvoidInstantiatingObjectsInLoopsTest extends SimpleAggregatorTst {
    20
    21      private Rule rule;
    22
    23      public void setUp() throws RuleSetNotFoundException {
    24          rule = findRule("optimizations", "AvoidInstantiatingObjectsInLoops");
    25      }
    26
    27      public void testAll() {
    28          runTests(new TestDescriptor[]{
    29              new TestDescriptor(TEST1, "TEST1", 1, rule),
    30              new TestDescriptor(TEST2, "TEST2", 1, rule),
    31              new TestDescriptor(TEST3, "TEST3", 1, rule),
    32              new TestDescriptor(TEST4, "TEST4", 2, rule),
    33              new TestDescriptor(TEST5, "throw new is OK", 0, rule),
    34              new TestDescriptor(TEST6, "return new in loop is OK", 0, rule),
    35              //new TestDescriptor(BUG_1114051, "BUG [ 1114051 ] Semi-false positive for instantiating new object in loop", 0, rule), //FIXME
    36          });
    37      }
    38
    39      private static final String TEST1 =
    40              "public class Foo {" + PMD.EOL +
    41              " public void test1() {" + PMD.EOL +
    42              "   for(;;) {" + PMD.EOL +
    43              "       String a = new String();" + PMD.EOL +
    44              "   }" + PMD.EOL +
    45              " }" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST2 =
    49              "public class Foo {" + PMD.EOL +
    50              " public void test1() {" + PMD.EOL +
    51              "   while(true) {" + PMD.EOL +
    52              "       String a = new String();" + PMD.EOL +
    53              "   }" + PMD.EOL +
    54              " }" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST3 =
    58              "public class Foo {" + PMD.EOL +
    59              " public void test1() {" + PMD.EOL +
    60              "   do{" + PMD.EOL +
    61              "       String a = new String();" + PMD.EOL +
    62              "   }while(true) ;" + PMD.EOL +
    63              " }" + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST4 =
    67              "public class Foo {" + PMD.EOL +
    68              " public void test1() {" + PMD.EOL +
    69              "   do{" + PMD.EOL +
    70              "       String a = new String();" + PMD.EOL +
    71              "       String b = new String();" + PMD.EOL +
    72              "   }while(true) ;" + PMD.EOL +
    73              " }" + PMD.EOL +
    74              "}";
    75
    76      private static final String TEST5 =
    77              "public class Foo {" + PMD.EOL +
    78              " public void test1() {" + PMD.EOL +
    79              "   for(;;) {" + PMD.EOL +
    80              "       throw new Exception();" + PMD.EOL +
    81              "   }" + PMD.EOL +
    82              " }" + PMD.EOL +
    83              "}";
    84
    85      private static final String TEST6 =
    86              "public class Foo {" + PMD.EOL +
    87              " public String test1() {" + PMD.EOL +
    88              "   for(;;) {" + PMD.EOL +
    89              "       return new String();" + PMD.EOL +
    90              "   }" + PMD.EOL +
    91              " }" + PMD.EOL +
    92              "}";
    93
    94      private static final String BUG_1114051 =
    95              "public class Foo {" + PMD.EOL +
    96              " public void test1() {" + PMD.EOL +
    97              "   while (true) {" +
    98              "        try {" +
    99              "       } catch (Exception e) {" + PMD.EOL +
    100             "           url = new StringBuffer(); " + PMD.EOL +
    101             "           break; " + PMD.EOL +
    102             "       }" + PMD.EOL +
    103             "  }" + PMD.EOL +
    104             " }" + PMD.EOL +
    105             "}";
    106
    107 }