PageRenderTime 46ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 160 lines | 129 code | 17 blank | 14 comment | 0 complexity | 0fe7e95506aa2bfe3b56871bc2148342 MD5 | raw file
  1// Copyright 2008 Google Inc. All Rights Reserved.
  2
  3package com.google.test.metric.report.issues;
  4
  5import com.google.test.metric.ClassRepository;
  6import com.google.test.metric.CostModel;
  7import com.google.test.metric.JavaClassRepository;
  8import com.google.test.metric.MetricComputer;
  9import com.google.test.metric.example.ExpensiveConstructor.Cost2ToConstruct;
 10import com.google.test.metric.example.ExpensiveConstructor.ObjectInstantiationWorkInTheConstructor;
 11import com.google.test.metric.example.ExpensiveConstructor.StaticWorkInTheConstructor;
 12import com.google.test.metric.example.Lessons.Primeness;
 13import com.google.test.metric.example.Lessons.SumOfPrimes1;
 14import com.google.test.metric.example.MutableGlobalState.FinalGlobalExample;
 15import com.google.test.metric.example.MutableGlobalState.MutableGlobalExample;
 16import com.google.test.metric.example.NonMockableCollaborator.FinalMethodCantBeOverridden;
 17import com.google.test.metric.example.NonMockableCollaborator.StaticMethodCalled;
 18import static com.google.test.metric.report.issues.IssueSubType.COMPLEXITY;
 19import static com.google.test.metric.report.issues.IssueSubType.FINAL_METHOD;
 20import static com.google.test.metric.report.issues.IssueSubType.NON_MOCKABLE;
 21import static com.google.test.metric.report.issues.IssueSubType.SINGLETON;
 22import static com.google.test.metric.report.issues.IssueSubType.STATIC_METHOD;
 23import com.google.test.metric.testing.MetricComputerBuilder;
 24import com.google.test.metric.testing.MetricComputerJavaDecorator;
 25
 26import junit.framework.TestCase;
 27
 28import java.util.LinkedList;
 29import java.util.List;
 30
 31/**
 32 * Tests the issues that are reported for the testability example code.
 33 *
 34 * @author alexeagle@google.com (Alex Eagle)
 35 */
 36public class IssuesReportingIntegrationTest extends TestCase {
 37  private IssuesReporter issuesReporter;
 38  private MetricComputerJavaDecorator decoratedComputer;
 39
 40  @Override
 41  protected void setUp() throws Exception {
 42    super.setUp();
 43    ClassRepository repo = new JavaClassRepository();
 44    MetricComputer toDecorate = new MetricComputerBuilder().withClassRepository(repo).build();
 45    decoratedComputer = new MetricComputerJavaDecorator(toDecorate, repo);
 46    HypotheticalCostModel hypotheticalCostModel = new HypotheticalCostModel(new CostModel());
 47    issuesReporter = new IssuesReporter(new LinkedList<ClassIssues>(), hypotheticalCostModel);
 48  }
 49
 50    public void testCost2ToConstructIssues() throws Exception {
 51    ClassIssues classIssues = issuesReporter.determineIssues(
 52        decoratedComputer.compute(Cost2ToConstruct.class));
 53    List<Issue> issues = classIssues.getConstructionIssues().get(COMPLEXITY.toString());
 54    assertEquals(1, issues.size());
 55    Issue issue = issues.get(0);
 56    assertEquals(22, issue.getLocation().getLineNumber());
 57    assertFalse(issue.isLineNumberApproximate());
 58    assertEquals("Cost2ToConstruct()", issue.getElement());
 59    assertEquals(1.0f, issue.getContributionToClassCost());
 60  }
 61
 62  public void testStaticWorkInConstructorIssues() throws Exception {
 63    ClassIssues classIssues = issuesReporter.determineIssues(
 64        decoratedComputer.compute(StaticWorkInTheConstructor.class));
 65    List<Issue> issues = classIssues.getConstructionIssues().get(STATIC_METHOD.toString());
 66    assertEquals(1, issues.size());
 67    Issue issue = issues.get(0);
 68    assertEquals(31, issue.getLocation().getLineNumber());
 69    assertFalse(issue.isLineNumberApproximate());
 70    assertEquals("boolean staticCost2()", issue.getElement());
 71    assertEquals(1.0f, issue.getContributionToClassCost());
 72  }
 73
 74  public void testObjectInstantiationWorkInTheConstructorIssues() throws Exception {
 75    ClassIssues classIssues = issuesReporter.determineIssues(
 76        decoratedComputer.compute(ObjectInstantiationWorkInTheConstructor.class));
 77    List<Issue> issues = classIssues.getConstructionIssues().get(NON_MOCKABLE.toString());
 78    assertEquals(1, issues.size());
 79    Issue issue = issues.get(0);
 80    assertEquals(25, issue.getLocation().getLineNumber());
 81    assertFalse(issue.isLineNumberApproximate());
 82    assertEquals("Cost2ToConstruct()", issue.getElement());
 83    assertEquals(1f, issue.getContributionToClassCost());
 84
 85  }
 86
 87    public void testFinalMethodCantBeOverriddenIssues() throws Exception {
 88    ClassIssues classIssues = issuesReporter.determineIssues(
 89        decoratedComputer.compute(FinalMethodCantBeOverridden.class));
 90    assertTrue(classIssues.getConstructionIssues().isEmpty());
 91    assertTrue(classIssues.getDirectCostIssues().isEmpty());
 92    List<Issue> issues = classIssues.getCollaboratorIssues().get(FINAL_METHOD.toString());
 93    //TODO
 94    //assertEquals(1, issues.size());
 95  }
 96
 97  public void testPrimenessIssues() throws Exception {
 98    ClassIssues classIssues = issuesReporter.determineIssues(decoratedComputer.compute(Primeness.class));
 99    assertEquals(1, classIssues.getSize());
100    Issue issue = classIssues.getDirectCostIssues().get(COMPLEXITY.toString()).get(0);
101    // FIXME(alexeagle): the method really starts on line 20, but it's not available in the bytecode.
102    // run this:
103    // javap -classpath target/core-1.3.1-SNAPSHOT.jar -c -l com.google.test.metric.example.Lessons.Primeness
104    // Only answer is to look at the source... :(
105    assertEquals(21, issue.getLocation().getLineNumber());
106    assertTrue(issue.isLineNumberApproximate());
107    assertEquals(1.0f, issue.getContributionToClassCost());
108    assertEquals("boolean isPrime(int)", issue.getElement());
109  }
110
111  public void testSumOfPrimes1Issues() throws Exception {
112    ClassIssues classIssues = issuesReporter.determineIssues(
113        decoratedComputer.compute(SumOfPrimes1.class));
114    List<Issue> issues = classIssues.getCollaboratorIssues().get(NON_MOCKABLE.toString());
115    assertEquals(1, issues.size());
116    Issue issue = issues.get(0);
117    assertEquals(25, issue.getLocation().getLineNumber());
118    assertFalse(issue.isLineNumberApproximate());
119    // TODO: we'd rather see "Primeness primeness" on line 20 as the root issue here
120    assertEquals("boolean isPrime(int)", issue.getElement());
121    assertEquals(0.5f, issue.getContributionToClassCost());
122  }
123
124  public void testStaticMethodCalledIssues() throws Exception {
125    ClassIssues classIssues = issuesReporter.determineIssues(
126        decoratedComputer.compute(StaticMethodCalled.class));
127    List<Issue> issues = classIssues.getCollaboratorIssues().get(STATIC_METHOD.toString());
128
129    assertEquals(1, issues.size());
130    Issue issue = issues.get(0);
131    assertEquals(46, issue.getLocation().getLineNumber());
132    assertFalse(issue.isLineNumberApproximate());
133    assertEquals("boolean isGreat()", issue.getElement());
134    assertEquals(1.0f, issue.getContributionToClassCost());
135    assertTrue(classIssues.getConstructionIssues().isEmpty());
136  }
137
138  public void testFinalGlobalExampleIssues() throws Exception {
139    ClassIssues classIssues = issuesReporter.determineIssues(
140        decoratedComputer.compute(FinalGlobalExample.class));
141    assertEquals(2, classIssues.getSize());
142    assertTrue(classIssues.toString(), classIssues.getCollaboratorIssues().containsKey(SINGLETON.toString()));
143    List<Issue> issues = classIssues.getCollaboratorIssues().get(SINGLETON.toString());
144    Issue issue1 = issues.get(0);
145    Issue issue2 = issues.get(1);
146    //TODO: we'd rather see "FinalGlobalExample$Gadget finalInstance" on line 68 as the root issue
147    assertEquals("int increment()", issue1.getElement());
148    assertEquals(88, issue1.getLocation().getLineNumber());
149    assertEquals(0.5f, issue1.getContributionToClassCost());
150    assertEquals("int getCount()", issue2.getElement());
151    assertEquals(84, issue2.getLocation().getLineNumber());
152    assertEquals(0.5f, issue2.getContributionToClassCost());
153  }
154
155  public void testMutableGlobalExampleIssues() throws Exception {
156    ClassIssues classIssues = issuesReporter.determineIssues(
157        decoratedComputer.compute(MutableGlobalExample.class));
158    assertEquals(classIssues.toString(), 3, classIssues.getSize());
159  }
160}