PageRenderTime 44ms CodeModel.GetById 10ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 132 lines | 96 code | 21 blank | 15 comment | 0 complexity | ecb8c5c513a98ebbf5f3eb349bcbecec 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 junit.framework.TestCase;
 19
 20import com.google.test.metric.testing.MetricComputerBuilder;
 21import com.google.test.metric.testing.MetricComputerJavaDecorator;
 22
 23public class CostUtilTest extends TestCase {
 24
 25  private MetricComputerJavaDecorator decoratedComputer;
 26  private final ClassRepository repo = new JavaClassRepository();
 27
 28  @Override
 29  protected void setUp() throws Exception {
 30    MetricComputer toDecorate = new MetricComputerBuilder().withClassRepository(repo).build();
 31    decoratedComputer = new MetricComputerJavaDecorator(toDecorate, repo);
 32  }
 33
 34  public void testInstanceCost0() {
 35    assertEquals(1, totalGlobalCost("boolean instanceCost0()"));
 36    assertEquals(0, cyclomaticCost("boolean instanceCost0()"));
 37    assertEquals(0, globalCost("boolean instanceCost0()"));
 38    assertEquals(0, totalComplexityCost("boolean instanceCost0()"));
 39  }
 40
 41  public void testStaticCost0() {
 42    assertEquals(0, cyclomaticCost("boolean staticCost0()"));
 43    assertEquals(0, globalCost("boolean staticCost0()"));
 44    assertEquals(0, totalComplexityCost("boolean staticCost0()"));
 45    assertEquals(1, totalGlobalCost("boolean staticCost0()"));
 46  }
 47
 48  public void testInstanceCost1() {
 49    assertEquals(1, totalGlobalCost("boolean instanceCost1()"));
 50    assertEquals(1, cyclomaticCost("boolean instanceCost1()"));
 51    assertEquals(0, globalCost("boolean instanceCost1()"));
 52    assertEquals(1, totalComplexityCost("boolean instanceCost1()"));
 53  }
 54
 55  public void testStaticCost1() {
 56    assertEquals(1, cyclomaticCost("boolean staticCost1()"));
 57    assertEquals(0, globalCost("boolean staticCost1()"));
 58    assertEquals(1, totalComplexityCost("boolean staticCost1()"));
 59    assertEquals(1, totalGlobalCost("boolean staticCost1()"));
 60  }
 61
 62  public void testInstanceCost2() {
 63    assertEquals(2, cyclomaticCost("boolean instanceCost2()"));
 64    assertEquals(0, globalCost("boolean instanceCost2()"));
 65    assertEquals(2, totalComplexityCost("boolean instanceCost2()"));
 66    assertEquals(1, totalGlobalCost("boolean instanceCost2()"));
 67  }
 68
 69  public void testStatcCost2() {
 70    assertEquals(2, cyclomaticCost("boolean staticCost2()"));
 71    assertEquals(0, globalCost("boolean staticCost2()"));
 72    assertEquals(2, totalComplexityCost("boolean staticCost2()"));
 73    assertEquals(1, totalGlobalCost("boolean staticCost2()"));
 74  }
 75
 76  public void testInstanceCost3() {
 77    assertEquals(3, cyclomaticCost("boolean instanceCost3()"));
 78    assertEquals(0, globalCost("boolean instanceCost3()"));
 79    assertEquals(3, totalComplexityCost("boolean instanceCost3()"));
 80    assertEquals(1, totalGlobalCost("boolean instanceCost3()"));
 81  }
 82
 83  public void testStaticCost3() {
 84    assertEquals(3, cyclomaticCost("boolean staticCost3()"));
 85    assertEquals(0, globalCost("boolean staticCost3()"));
 86    assertEquals(3, totalComplexityCost("boolean staticCost3()"));
 87    assertEquals(1, totalGlobalCost("boolean staticCost3()"));
 88  }
 89
 90  public void testInstanceCost4() {
 91    assertEquals(4, cyclomaticCost("boolean instanceCost4()"));
 92    assertEquals(0, globalCost("boolean instanceCost4()"));
 93    assertEquals(4, totalComplexityCost("boolean instanceCost4()"));
 94    assertEquals(1, totalGlobalCost("boolean instanceCost4()"));
 95  }
 96
 97  public void testStaticCost4() {
 98    assertEquals(4, cyclomaticCost("boolean staticCost4()"));
 99    assertEquals(0, globalCost("boolean staticCost4()"));
100    assertEquals(4, totalComplexityCost("boolean staticCost4()"));
101    assertEquals(1, totalGlobalCost("boolean staticCost4()"));
102  }
103
104  public void testCostUtilClassCost() throws Exception {
105    ClassCost classCost = decoratedComputer.compute(CostUtil.class);
106    assertEquals(4, classCost.getHighestMethodComplexityCost());
107    assertEquals(1, classCost.getHighestMethodGlobalCost());
108    assertEquals(20, classCost.getTotalComplexityCost());
109    assertEquals(12, classCost.getTotalGlobalCost());
110  }
111
112  private int totalComplexityCost(String method) {
113    return methodCostFor(method).getTotalCost().getCyclomaticComplexityCost();
114  }
115
116  private int cyclomaticCost(String method) {
117    return methodCostFor(method).getCost().getCyclomaticComplexityCost();
118  }
119
120  private int globalCost(String method) {
121    return methodCostFor(method).getCost().getGlobalCost();
122  }
123
124  private int totalGlobalCost(String method) {
125    return methodCostFor(method).getTotalCost().getGlobalCost();
126  }
127
128  private MethodCost methodCostFor(String method) {
129    MethodCost cost = decoratedComputer.compute(CostUtil.class, method);
130    return cost;
131  }
132}