001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors.testcases.violations;
024:
025: /**
026: * CyclomaticComplexityRule
027: * @author Pavel Vlasov
028: * @version $Revision: 1.1 $
029: */
030: public class CyclomaticComplexityRuleViolationTestCase {
031:
032: private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
033: .getRootLogger();
034:
035: private static final int INT_ZERO = 0;
036: private static final int INT_1 = 1;
037: private static final int NEG_INT_1 = -INT_1;
038: private static final int INT_2 = 2;
039: private static final int NEG_INT_2 = -INT_2;
040: private static final int INT_3 = 3;
041: private static final int NEG_INT_3 = -INT_3;
042:
043: private static final String CONST_SPACE = " ";
044: private static final char CONST_A = 'A';
045: private static final char CHAR_B = 'B';
046:
047: // --- VIOLATION ---
048: /** Java doc automaticaly generated by Hammurapi */
049: public int complexMethod(final String strToProc,
050: final boolean firstType) {
051: if (firstType) {
052: if (strToProc == null) {
053: return INT_1;
054: } else if (strToProc.length() == INT_ZERO) {
055: return INT_2;
056: } else if (strToProc.length() == INT_1
057: && strToProc.equalsIgnoreCase(CONST_SPACE)) {
058: return INT_3;
059: } else {
060: int retVal = INT_ZERO;
061: for (int i = INT_ZERO; i < strToProc.length(); i++) {
062: if (strToProc.charAt(i) == CONST_A
063: || i % INT_3 == INT_2) {
064: retVal++;
065: } else {
066: retVal += INT_2;
067: }
068: }
069: return retVal;
070: }
071: } else {
072: if (strToProc == null) {
073: return NEG_INT_1;
074: } else if (strToProc.length() == INT_ZERO) {
075: return NEG_INT_2;
076: } else if (strToProc.length() == INT_1
077: && strToProc.equalsIgnoreCase(CONST_SPACE)) {
078: return NEG_INT_3;
079: } else {
080: int retVal = INT_ZERO;
081: for (int i = INT_ZERO; i < strToProc.length(); i++) {
082: if (strToProc.charAt(i) == CONST_A
083: || i % INT_3 == INT_2) {
084: retVal--;
085: } else if (strToProc.charAt(i) == CHAR_B
086: || i % INT_3 == INT_2) {
087: retVal -= INT_2;
088: } else {
089: retVal -= INT_3;
090: }
091: }
092: return retVal;
093: }
094: }
095: }
096:
097: //--- END VIOLATION ---
098:
099: /** Java doc automaticaly generated by Hammurapi */
100: public int tooComplexClassMethod(final int valToProc) {
101: int retVal = INT_ZERO;
102:
103: if (valToProc < INT_2 || valToProc > INT_3) {
104: retVal = INT_1;
105: }
106:
107: return retVal;
108: }
109: }
|