1 23 package org.hammurapi.inspectors.testcases.violations; 24 25 30 public class CyclomaticComplexityRuleViolationTestCase { 31 32 private static org.apache.log4j.Logger logger = 33 org.apache.log4j.Logger.getRootLogger(); 34 35 private static final int INT_ZERO = 0; 36 private static final int INT_1 = 1; 37 private static final int NEG_INT_1 = -INT_1; 38 private static final int INT_2 = 2; 39 private static final int NEG_INT_2 = -INT_2; 40 private static final int INT_3 = 3; 41 private static final int NEG_INT_3 = -INT_3; 42 43 private static final String CONST_SPACE = " "; 44 private static final char CONST_A = 'A'; 45 private static final char CHAR_B = 'B'; 46 47 49 public int complexMethod(final String strToProc, final boolean firstType) { 50 if (firstType) { 51 if (strToProc == null) { 52 return INT_1; 53 } else if (strToProc.length() == INT_ZERO) { 54 return INT_2; 55 } else if (strToProc.length() == INT_1 && 56 strToProc.equalsIgnoreCase(CONST_SPACE)) { 57 return INT_3; 58 } else { 59 int retVal = INT_ZERO; 60 for (int i = INT_ZERO; i < strToProc.length(); i++) { 61 if (strToProc.charAt(i) == CONST_A || 62 i % INT_3 == INT_2) { 63 retVal++; 64 } else { 65 retVal += INT_2; 66 } 67 } 68 return retVal; 69 } 70 } else { 71 if (strToProc == null) { 72 return NEG_INT_1; 73 } else if (strToProc.length() == INT_ZERO) { 74 return NEG_INT_2; 75 } else if (strToProc.length() == INT_1 && 76 strToProc.equalsIgnoreCase(CONST_SPACE)) { 77 return NEG_INT_3; 78 } else { 79 int retVal = INT_ZERO; 80 for (int i = INT_ZERO; i < strToProc.length(); i++) { 81 if (strToProc.charAt(i) == CONST_A || 82 i % INT_3 == INT_2) { 83 retVal--; 84 } else if (strToProc.charAt(i) == CHAR_B || 85 i % INT_3 == INT_2) { 86 retVal -= INT_2; 87 } else { 88 retVal -= INT_3; 89 } 90 } 91 return retVal; 92 } 93 } 94 } 95 97 98 public int tooComplexClassMethod(final int valToProc) { 99 int retVal = INT_ZERO; 100 101 if (valToProc < INT_2 || valToProc > INT_3) { 102 retVal = INT_1; 103 } 104 105 return retVal; 106 } 107 } 108 109 | Popular Tags |