/testability-explorer/src/test/java/com/google/test/metric/method/MethodCostTest.java
Java | 66 lines | 44 code | 7 blank | 15 comment | 1 complexity | e43ec0953de413a94a2a95e65e0c1c90 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.method; 17 18import com.google.test.metric.ClassCost; 19import com.google.test.metric.Cost; 20import com.google.test.metric.CostModel; 21import com.google.test.metric.CyclomaticCost; 22import com.google.test.metric.GlobalCost; 23import com.google.test.metric.JavaClassRepository; 24import com.google.test.metric.MethodCost; 25import com.google.test.metric.MethodInvocationCost; 26import com.google.test.metric.MetricComputer; 27import static com.google.test.metric.Reason.IMPLICIT_STATIC_INIT; 28import com.google.test.metric.RegExpWhiteList; 29import com.google.test.metric.SourceLocation; 30 31import junit.framework.TestCase; 32 33public class MethodCostTest extends TestCase { 34 35 public void testComputeOverallCost() throws Exception { 36 MethodCost cost = new MethodCost("", "a", 0, false, false, false); 37 cost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1))); 38 cost.addCostSource(new GlobalCost(new SourceLocation(null, 0), null, Cost.global(1))); 39 MethodCost cost3 = new MethodCost("", "b", 0, false, false, false); 40 cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1))); 41 cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1))); 42 cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1))); 43 cost.addCostSource(new MethodInvocationCost(new SourceLocation(null, 0), cost3, 44 IMPLICIT_STATIC_INIT, Cost.cyclomatic(3))); 45 CostModel costModel = new CostModel(2, 10, 1); 46 cost.link(); 47 48 assertEquals((long) 2 * (3 + 1) + 10 * 1, costModel.computeOverall(cost.getTotalCost())); 49 assertEquals(2, cost.getExplicitViolationCosts().size()); 50 assertEquals(1, cost.getImplicitViolationCosts().size()); 51 } 52 53 private static class Setters { 54 boolean foo; 55 public void setFoo(String foo) { 56 this.foo = (foo == null); 57 } 58 } 59 60 public void testImplicitSetterCostShouldNotBeDoubleCounted() throws Exception { 61 MetricComputer computer = new MetricComputer(new JavaClassRepository(), null, new RegExpWhiteList(), 1); 62 ClassCost cost = computer.compute(Setters.class.getCanonicalName()); 63 MethodCost cost1 = cost.getMethodCost("void setFoo(java.lang.String)"); 64 assertEquals(1, cost1.getTotalCost().getCyclomaticComplexityCost()); 65 } 66}