PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/CyclomaticComplexityTest.java

http://testability-explorer.googlecode.com/
Java | 270 lines | 224 code | 31 blank | 15 comment | 14 complexity | 6942c7639fe99d51070cd8cb856f901e MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric;
 17
 18import static java.util.Arrays.asList;
 19
 20import java.util.List;
 21
 22
 23public class CyclomaticComplexityTest extends AutoFieldClearTestCase {
 24
 25  private final ClassRepository repo = new JavaClassRepository();
 26  private ClassInfo classInfo;
 27
 28  @Override
 29  protected void setUp() throws Exception {
 30    super.setUp();
 31    classInfo = repo.getClass(CyclomaticMethods.class.getCanonicalName());
 32  }
 33
 34  public static class CyclomaticMethods {
 35    public void emptyMethod_1() {
 36    }
 37
 38    public void simpleMethod_1() {
 39      int i = 0;
 40      i += 1;
 41    }
 42
 43    public void ifMethod_2() {
 44      int a = 0;
 45      if (a < 0) {
 46        a++;
 47      } else {
 48        a--;
 49      }
 50    }
 51
 52    public void ifMethodNoElse_2() {
 53      int a = 0;
 54      if (a < 0) {
 55        a++;
 56      }
 57    }
 58
 59    public void tryCatch_2() {
 60      int a = 0;
 61      try {
 62        a++;
 63      } catch (RuntimeException e) {
 64        a++;
 65      }
 66    }
 67
 68    public void tryCatchFinally_2() {
 69      int a = 0;
 70      try {
 71        a++;
 72      } catch (RuntimeException e) {
 73        a++;
 74      } finally {
 75        a++;
 76      }
 77    }
 78
 79    public void emptySwitch_2() {
 80      int a = 0;
 81      switch (a) {
 82        case 0:
 83          a = 0;
 84      }
 85    }
 86
 87    public void emptySwitch_5() {
 88      int a = 0;
 89      switch (a) {
 90        case 0:
 91          a = 0;
 92          break;
 93        case 1:
 94          a = 1;
 95          break;
 96        case 2:
 97          a = 2;
 98          break;
 99        case 4:
100          a = 4;
101          break;
102        default:
103      }
104    }
105
106    public void switchImplementWithLookUp_3() {
107      int a = 0;
108      switch (a) {
109        case 0:
110          a = 0;
111          break;
112        case 9999:
113          a = 9999;
114          break;
115        default:
116          a = -1;
117      }
118    }
119
120    public void switchWithDefault_2() {
121      int a = 0;
122      switch (a) {
123        case 0:
124          a = 0;
125          break;
126        default:
127      }
128    }
129  }
130
131  public void testVerifyAllMethodsCyclomaticComplexity() throws Exception {
132    String errors = "";
133    for (MethodInfo method : classInfo.getMethods()) {
134      String name = method.getName();
135      int _Index = name.lastIndexOf('_');
136      if (_Index > 0) {
137        long expectedCC = Long.parseLong(name.substring(_Index + 1, name.indexOf('(')));
138        long actualCC = method.getLinesOfComplexity().size() + 1;
139        if (expectedCC != actualCC) {
140          errors += "\n" + method.getName()
141              + " should have Cyclomatic Complexity of "
142              + expectedCC + " but was " + actualCC;
143        }
144      }
145    }
146    assertTrue(errors, errors.length() == 0);
147  }
148
149  public static class LineNumbersForConditionals {
150    int a=0;
151    boolean flag;
152
153    public void method() {
154      a = flag ? 1 : 2;
155      a = !flag  ? 3 : 4;
156    }
157
158    public void tryCatch() {
159      try {
160        a = 1;
161      } catch (RuntimeException e) {
162        a = 2;
163      } finally {
164        a = 3;
165      }
166    }
167
168    public void tryCatchCatch() {
169      try {
170        a = 1;
171      } catch (RuntimeException e) {
172        a = 2;
173      } catch (Exception e) {
174        a = 2;
175      } finally {
176        a = 3;
177      }
178    }
179
180    public void nestedTryCatch() {
181      try {
182        try {
183          a = 1;
184        } catch (RuntimeException e) {
185          a = 2;
186        } finally {
187          a = 3;
188        }
189      } catch (RuntimeException e) {
190        a = 2;
191      } finally {
192        a = 3;
193      }
194    }
195
196    public void tableSwitchMethod() {
197      switch (a) {
198      case 1:
199        a = 1;
200        break;
201      default:
202        a = 2;
203        break;
204      }
205    }
206
207    public void lookupSwitchMethod() {
208      switch (a) {
209      case 1:
210        a = 1;
211        break;
212      case 1000:
213        a = 2;
214        break;
215      default:
216        a = 3;
217        break;
218      }
219    }
220
221  }
222
223  public void testLineNumbersForConditionals() throws Exception {
224    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
225    MethodInfo methodInfo = classInfo.getMethod("void method()");
226    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
227    int firstExpectedComplexityLine = methodInfo.getStartingLineNumber();
228    assertEquals(asList(firstExpectedComplexityLine, firstExpectedComplexityLine + 1), linesOfComplexity);
229  }
230
231  public void testLineNumbersForTryCatch() throws Exception {
232    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
233    MethodInfo methodInfo = classInfo.getMethod("void tryCatch()");
234    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
235    int firstExpectedComplexityLine = methodInfo.getStartingLineNumber();
236    assertEquals(asList(firstExpectedComplexityLine + 1), linesOfComplexity);
237  }
238
239  public void testLineNumbersForNestedTryCatch() throws Exception {
240    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
241    MethodInfo methodInfo = classInfo.getMethod("void nestedTryCatch()");
242    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
243    int methodLine = methodInfo.getStartingLineNumber();
244    assertEquals(asList(methodLine + 1, methodLine + 6), linesOfComplexity);
245  }
246
247  public void testLineNumbersForTryCatchCatch() throws Exception {
248    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
249    MethodInfo methodInfo = classInfo.getMethod("void tryCatchCatch()");
250    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
251    int methodLine = methodInfo.getStartingLineNumber();
252    assertEquals(asList(methodLine + 1, methodLine + 3), linesOfComplexity);
253  }
254
255  public void testLineNumbersForTableSwitch() throws Exception {
256    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
257    MethodInfo methodInfo = classInfo.getMethod("void tableSwitchMethod()");
258    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
259    int methodLine = methodInfo.getStartingLineNumber();
260    assertEquals(asList(methodLine + 2), linesOfComplexity);
261  }
262
263  public void testLineNumbersForLookupSwitch() throws Exception {
264    ClassInfo classInfo = repo.getClass(LineNumbersForConditionals.class.getCanonicalName());
265    MethodInfo methodInfo = classInfo.getMethod("void lookupSwitchMethod()");
266    List<Integer> linesOfComplexity = methodInfo.getLinesOfComplexity();
267    int methodLine = methodInfo.getStartingLineNumber();
268    assertEquals(asList(methodLine + 2, methodLine + 5), linesOfComplexity);
269  }
270}