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