View Javadoc
1 package test.net.sourceforge.pmd.rules.design; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.rules.design.PositionalIteratorRule; 5 import test.net.sourceforge.pmd.rules.RuleTst; 6 7 public class PositionalIteratorRuleTest extends RuleTst { 8 9 private static final String TEST1 = 10 "public class PositionalIterator1 {" + PMD.EOL + 11 " public void foo(Iterator i) {" + PMD.EOL + 12 " while(i.hasNext()) {" + PMD.EOL + 13 " Object one = i.next();" + PMD.EOL + 14 " " + PMD.EOL + 15 " // 2 calls to next() inside the loop == bad!" + PMD.EOL + 16 " Object two = i.next(); " + PMD.EOL + 17 " }" + PMD.EOL + 18 " }" + PMD.EOL + 19 "}"; 20 21 private static final String TEST2 = 22 "public class PositionalIterator2 {" + PMD.EOL + 23 " public void foo(Iterator i) {" + PMD.EOL + 24 " while(i.hasNext()) {" + PMD.EOL + 25 " Object one = i.next();" + PMD.EOL + 26 " }" + PMD.EOL + 27 " }" + PMD.EOL + 28 "}"; 29 30 private static final String TEST3 = 31 "public class PositionalIterator3 {" + PMD.EOL + 32 " public void foo() {" + PMD.EOL + 33 " Iterator i = (new List()).iterator();" + PMD.EOL + 34 " while(i.hasNext()) {" + PMD.EOL + 35 " Object one = i.next();" + PMD.EOL + 36 " Iterator j = (new List()).iterator();" + PMD.EOL + 37 " while (j.hasNext()) {" + PMD.EOL + 38 " j.next();" + PMD.EOL + 39 " }" + PMD.EOL + 40 " }" + PMD.EOL + 41 " }" + PMD.EOL + 42 "}"; 43 44 public void test1() throws Throwable { 45 runTestFromString(TEST1, 1, new PositionalIteratorRule()); 46 } 47 public void test2() throws Throwable { 48 runTestFromString(TEST2, 0, new PositionalIteratorRule()); 49 } 50 public void test3() throws Throwable { 51 runTestFromString(TEST3, 0, new PositionalIteratorRule()); 52 } 53 }

This page was automatically generated by Maven