1 package test.net.sourceforge.pmd.rules.design; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.Rule; 5 import net.sourceforge.pmd.RuleSetNotFoundException; 6 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 7 import test.net.sourceforge.pmd.testframework.TestDescriptor; 8 9 public class ConfusingTernaryRuleTest extends SimpleAggregatorTst { 10 11 private Rule rule; 12 13 public void setUp() throws RuleSetNotFoundException { 14 rule = findRule("design", "ConfusingTernary"); 15 } 16 17 public void testAll() { 18 runTests(new TestDescriptor[]{ 19 new TestDescriptor(TEST1, "!=, bad", 1, rule), 20 new TestDescriptor(TEST2, "==, good", 0, rule), 21 }); 22 } 23 24 private static final String TEST1 = 25 "public class Foo {" + PMD.EOL + 26 " void bar() {" + PMD.EOL + 27 " x = a != b ? c : d;" + PMD.EOL + 28 " }" + PMD.EOL + 29 "}"; 30 31 private static final String TEST2 = 32 "public class Foo {" + PMD.EOL + 33 " void bar() {" + PMD.EOL + 34 " x = a == b ? c : d;" + PMD.EOL + 35 " }" + PMD.EOL + 36 "}"; 37 38 /* 39 public class BadTernaries { 40 public static void main(String[] args) { 41 int i = 0; 42 int j = 1; 43 int k = 2; 44 boolean x = true; 45 boolean y = false; 46 boolean z = true; 47 48 // flag all of these, lines 11 - 42: 49 if (i != 11) {a();} else {b();} 50 if (i != 12 && j != 0) {a();} else {b();} 51 if (i != 13 || j != 0) {a();} else {b();} 52 if (i != 14 && j != 0 && k != 0) {a();} else {b();} 53 if (i != 15 || j != 0 || k != 0) {a();} else {b();} 54 if (i != 16) {a();} else if (i != j) {b();} else{c();} 55 if (i != 17) {a();} else if (i == j) {b();} else{c();} 56 if (i == 18) {a();} else if (i != j) {b();} else{c();} 57 x = (!y ? x : y); 58 x = (!(x && y) ? y : z); 59 x = (!(x || y) ? y : z); 60 x = ((!x && !y) ? y : z); 61 x = ((!x || !y) ? y : z); 62 if (i != 24 && !x) {a();} else {b();} 63 if (i != 25 || !x) {a();} else {b();} 64 if (i != 26 && j != 0 && !y) {a();} else {b();} 65 if (i != 27 || j != 0 || !y) {a();} else {b();} 66 if (i != 28) {a();} else if (!x) {b();} else{c();} 67 if (i != 29) {a();} else if (x) {b();} else{c();} 68 if (i == 30) {a();} else if (!x) {b();} else{c();} 69 x = !(c() == y) ? y : !z; 70 if (!c()) {a();} else {b();} 71 if (c() != x) {a();} else {b();} 72 if (!c() != x) {a();} else {b();} 73 if (!c() != !x) {a();} else {b();} 74 if ((i != 36) || !(j == 0)) {a();} else {b();} 75 if ((i != 37) || !(x ? y : z)) {a();} else {b();} 76 if ((i != 38)) {a();} else {b();} 77 if (i != 39 || (j != 0 || k != 0)) {a();} else {b();} 78 if (i != 40 && (j != 0 && k != 0)) {a();} else {b();} 79 if (!x && (j != 41 && k != 0)) {a();} else {b();} 80 if (((x != y)) || !(x)) { a(); } else { b(); } 81 82 // don't flag these: 83 if (i != 0) {a();} 84 if (!x) {a();} 85 if (i == 0) {a();} else {b();} 86 if (i == 0 && j != 0) {a();} else {b();} 87 if (i == 0 || j != 0) {a();} else {b();} 88 if (i == 0 && !x) {a();} else {b();} 89 if (x) {a();} else {b();} 90 if (x ? y : !z) {a();} else {b();} 91 if (c() == !x) {a();} else {b();} 92 if (c() ? !x : !c()) {a();} else {b();} 93 if (!x && d() instanceof String) {a();} else {b();} 94 if (!x && (d() instanceof String)) {a();} else {b();} 95 } 96 97 private static void a() { } 98 private static void b() { } 99 private static boolean c() { return true; } 100 private static Object d() { return null; } 101 } 102 103 */ 104 105 }