PageRenderTime 55ms CodeModel.GetById 24ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/testability-explorer/src/test/java/com/google/test/metric/report/issues/HypotheticalCostModelTest.java

http://testability-explorer.googlecode.com/
Java | 94 lines | 75 code | 13 blank | 6 comment | 0 complexity | 5ce2c958aebb161e0e1703279b323f83 MD5 | raw file
 1// Copyright 2009 Google Inc. All Rights Reserved.
 2
 3package com.google.test.metric.report.issues;
 4
 5import com.google.test.metric.ClassCost;
 6import com.google.test.metric.ClassInfo;
 7import com.google.test.metric.ClassRepository;
 8import com.google.test.metric.Cost;
 9import com.google.test.metric.CostModel;
10import com.google.test.metric.CostUtil;
11import com.google.test.metric.CyclomaticCost;
12import com.google.test.metric.JavaClassRepository;
13import com.google.test.metric.MethodCost;
14import com.google.test.metric.MethodInvocationCost;
15import com.google.test.metric.MetricComputer;
16import static com.google.test.metric.Reason.NON_OVERRIDABLE_METHOD_CALL;
17import com.google.test.metric.SourceLocation;
18import com.google.test.metric.testing.MetricComputerBuilder;
19
20import junit.framework.TestCase;
21
22import java.util.Arrays;
23
24/**
25 * @author alexeagle@google.com (Alex Eagle)
26 */
27public class HypotheticalCostModelTest extends TestCase {
28  private CostModel costModel = new CostModel();
29  private ClassRepository repo = new JavaClassRepository();
30  private MetricComputer computer = new MetricComputerBuilder().withClassRepository(repo).build();
31  private HypotheticalCostModel hypotheticalCostModel = new HypotheticalCostModel(costModel);
32  public MethodCost doThingMethod;
33  private MethodCost methodWithIndirectCosts;
34
35  protected void setUp() throws Exception {
36    super.setUp();
37    doThingMethod = new MethodCost("Foo", "doThing()", 1, false, false, false);
38    doThingMethod.addCostSource(new CyclomaticCost(new SourceLocation(null, 3), Cost.cyclomatic(100)));
39
40    methodWithIndirectCosts = new MethodCost("Foo", "hasIndirect()", 2, false, false, false);
41    methodWithIndirectCosts.addCostSource(new CyclomaticCost(new SourceLocation(null, 4), Cost.cyclomatic(50)));
42    methodWithIndirectCosts.addCostSource(new MethodInvocationCost(new SourceLocation(null, 1),
43        doThingMethod, NON_OVERRIDABLE_METHOD_CALL, Cost.cyclomatic(33)));
44    costModel = new CostModel();
45  }
46
47  public void testDirectCostOfAMethodCanBeSubtractedFromClassCost() {
48    ClassCost classCost = new ClassCost("com.google.Foo", Arrays.asList(doThingMethod));
49    MethodCost methodCost = doThingMethod;
50    assertEquals(1.0f, hypotheticalCostModel.computeContributionFromMethod(classCost, methodCost));
51  }
52
53  public void testContributionFromOneMethodIsCorrect() {
54    ClassCost classCost = new ClassCost("com.google.Foo", Arrays.asList(doThingMethod, methodWithIndirectCosts));
55    MethodCost methodCost = doThingMethod;
56    float costWithoutDoThing = (0 + (50 + 33)) / 2;
57    float costWithDoThing = (100 + (50 + 33)) / 2;
58    assertEquals(1 - costWithoutDoThing / costWithDoThing,
59        hypotheticalCostModel.computeContributionFromMethod(classCost, methodCost));
60  }
61
62  private static class Example {
63    public Example() {
64      new CostUtil().instanceCost4();
65    }
66
67    public int doThing() {
68      new CostUtil().instanceCost3();
69      return 1;
70    }
71  }
72
73  public void testHypotheticalModelGivesTheSameNumberWithNoOverrides() throws Exception {
74    ClassInfo aClass = repo.getClass(Example.class.getCanonicalName());
75    ClassCost cost = computer.compute(aClass);
76    int originalCost = costModel.computeClass(cost);
77    int newCost = hypotheticalCostModel.computeClass(cost);
78    assertEquals(originalCost, newCost);
79  }
80
81  public void testMethodCostGoesDownWhenADependentCostIsRemoved() throws Exception {
82    ClassInfo aClass = repo.getClass(Example.class.getCanonicalName());
83    ClassCost classCost = computer.compute(aClass);
84    float costWithWorkInConstructor = (4 + 7) / 2;
85    float costWithoutWorkInConstructor = (0 + 3) / 2;
86    MethodCost constructorCost = classCost.getMethodCost("Example()");
87    MethodInvocationCost instanceCost4Invocation =
88        (MethodInvocationCost) constructorCost.getViolationCosts().get(0);
89    float actual = hypotheticalCostModel.computeContributionFromIssue(classCost, constructorCost,
90        instanceCost4Invocation);
91    // TODO
92    //assertEquals(1 - costWithoutWorkInConstructor / costWithWorkInConstructor, actual);
93  }
94}