    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 MethodReturnsInternalArrayTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("sunsecure", "MethodReturnsInternalArray");
    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, "ok", 0, rule),
    25              new TestDescriptor(TEST4, "tricky field hiding", 0, rule),
    26              new TestDescriptor(TEST5, "really sick code", 1, rule),
    27              new TestDescriptor(TEST6, "returning a local array is ok", 0, rule),
    28              new TestDescriptor(TEST7, "returning a local array is ok part deux", 0, rule),
    29              new TestDescriptor(TEST8, "returning a cloned field", 0, rule),
    30              new TestDescriptor(TEST9, "returning a new array", 0, rule),
    31          });
    32      }
    33
    34      private static final String TEST1 =
    35              "public class Foo {" + PMD.EOL +
    36              " String [] arr;" + PMD.EOL +
    37              " String [] getArr() {return arr;} ;" + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST2 =
    41              "public class Foo {" + PMD.EOL +
    42              " String [] arr;" + PMD.EOL +
    43              " String [] getArr() {return this.arr;} ;" + PMD.EOL +
    44              "}";
    45
    46      private static final String TEST3 =
    47              "public class Foo {" + PMD.EOL +
    48              " String [] arr;" + PMD.EOL +
    49              " String [] getArr() {String[] foo; return foo;} ;" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST4 =
    53              "public class Foo {" + PMD.EOL +
    54              " String [] arr;" + PMD.EOL +
    55              " String [] getArr() {String[] arr; return arr;} ;" + PMD.EOL +
    56              "}";
    57
    58      private static final String TEST5 =
    59              "public class Foo {" + PMD.EOL +
    60              " String [] arr;" + PMD.EOL +
    61              " String [] getArr() {String[] arr; return this.arr;} ;" + PMD.EOL +
    62              "}";
    63
    64      private static final String TEST6 =
    65              "public class Foo {" + PMD.EOL +
    66              " int[] getArr() {" + PMD.EOL +
    67              "  int[] x = new int[] {1,2,3};" + PMD.EOL +
    68              "  return x;" + PMD.EOL +
    69              " } ;" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST7 =
    73              "public class Foo {" + PMD.EOL +
    74              " Object[] getArr() {" + PMD.EOL +
    75              "  return new Object[] {foo,bar};" + PMD.EOL +
    76              " } ;" + PMD.EOL +
    77              "}";
    78
    79      private static final String TEST8 =
    80              "public class Foo {" + PMD.EOL +
    81              " private Object[] x;" + PMD.EOL +
    82              " Object[] getArr() {" + PMD.EOL +
    83              "  return this.x.clone();" + PMD.EOL +
    84              " } ;" + PMD.EOL +
    85              "}";
    86
    87      private static final String TEST9 =
    88              "public class Foo {" + PMD.EOL +
    89              " private Object x;" + PMD.EOL +
    90              " Object[] getArr() {" + PMD.EOL +
    91              "  return new Object[] {x};" + PMD.EOL +
    92              " } ;" + PMD.EOL +
    93              "}";
    94
    95  }