    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.sunsecure;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class ArrayIsStoredDirectlyTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("sunsecure", "ArrayIsStoredDirectly");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "Clear violation", 1, rule),
    23              new TestDescriptor(TEST2, "Clear violation with this.", 1, rule),
    24              new TestDescriptor(TEST3, "assignment to an internal array", 1, rule),
    25              new TestDescriptor(TEST4, "assignment of param to local", 0, rule),
    26              new TestDescriptor(TEST5, "skip interfaces", 0, rule),
    27              new TestDescriptor(TEST6, "skip abstract, native", 0, rule),
    28              new TestDescriptor(TEST7, "equality expression, not assignment", 0, rule),
    29              new TestDescriptor(TEST8, "assignment of array element", 0, rule),
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo {" + PMD.EOL +
    35              " String [] arr;" + PMD.EOL +
    36              " void foo (String[] x) {arr = x;} ;" + PMD.EOL +
    37              "}";
    38
    39      private static final String TEST2 =
    40              "public class Foo {" + PMD.EOL +
    41              " String [] arr;" + PMD.EOL +
    42              " void foo (String[] arr) {this.arr = arr;} ;" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST3 =
    46              "public class Foo {" + PMD.EOL +
    47              " String [] arr;" + PMD.EOL +
    48              " void foo (String[] x) {this.arr = x;} ;" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST4 =
    52              "public class Foo {" + PMD.EOL +
    53              " String [] arr;" + PMD.EOL +
    54              " void getArr(String[] arr) {String[] foo; foo = arr;} ;" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST5 =
    58              "public interface Foo {" + PMD.EOL +
    59              " void getArr(String[] arr);" + PMD.EOL +
    60              "}";
    61
    62      private static final String TEST6 =
    63              "public class Foo {" + PMD.EOL +
    64              " abstract void getArr(String[] arr);" + PMD.EOL +
    65              " native void getArr2(String[] arr);" + PMD.EOL +
    66              "}";
    67
    68      private static final String TEST7 =
    69              "public class Foo {" + PMD.EOL +
    70              " void bar(String[] buf) {" + PMD.EOL +
    71              "   x = (buf[0] == 1);" + PMD.EOL +
    72              " }" + PMD.EOL +
    73              "}";
    74
    75      private static final String TEST8 =
    76              "public class Foo {" + PMD.EOL +
    77              " void bar(String[] buf) {" + PMD.EOL +
    78              "   x = buf[0];" + PMD.EOL +
    79              " }" + PMD.EOL +
    80              "}";
    81
    82
    83  }