PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 187 lines | 134 code | 23 blank | 30 comment | 0 complexity | 19de23e53cd5558a822283bfc93ead8c MD5 | raw file
  1/*
  2 * Copyright 2009 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.report.issues;
 17
 18import com.google.test.metric.ClassCost;
 19import com.google.test.metric.ClassInfo;
 20import com.google.test.metric.ClassRepository;
 21import com.google.test.metric.CostModel;
 22import com.google.test.metric.CostUtil;
 23import com.google.test.metric.JavaClassRepository;
 24import com.google.test.metric.MetricComputer;
 25import static com.google.test.metric.report.issues.IssueSubType.COMPLEXITY;
 26import static com.google.test.metric.report.issues.IssueSubType.NON_MOCKABLE;
 27import static com.google.test.metric.report.issues.IssueSubType.SETTER;
 28import static com.google.test.metric.report.issues.IssueSubType.STATIC_INIT;
 29import static com.google.test.metric.report.issues.IssueSubType.STATIC_METHOD;
 30import static com.google.test.metric.report.issues.IssueType.CONSTRUCTION;
 31import com.google.test.metric.testing.MetricComputerBuilder;
 32import com.google.test.metric.testing.MetricComputerJavaDecorator;
 33
 34import junit.framework.TestCase;
 35
 36import java.util.LinkedList;
 37import java.util.List;
 38import java.util.Map;
 39
 40/**
 41 * Tests for {@link com.google.test.metric.report.issues.IssuesReporterTest}
 42 * These are integration tests which start from an actual class, and assert
 43 * what issues are reported on that class.
 44 *
 45 * @author alexeagle@google.com (Alex Eagle)
 46 */
 47public class IssuesReporterTest extends TestCase {
 48  private IssuesReporter issuesReporter;
 49  private MetricComputerJavaDecorator decoratedComputer;
 50  private HypotheticalCostModel costModel;
 51
 52  @Override
 53  protected void setUp() throws Exception {
 54    super.setUp();
 55    ClassRepository repo = new JavaClassRepository();
 56    MetricComputer toDecorate = new MetricComputerBuilder().withClassRepository(repo).build();
 57    decoratedComputer = new MetricComputerJavaDecorator(toDecorate, repo);
 58    costModel = new HypotheticalCostModel(new CostModel());
 59    issuesReporter = new IssuesReporter(new LinkedList<ClassIssues>(), this.costModel);
 60  }
 61
 62  private static class SeveralConstructionIssues {
 63    public SeveralConstructionIssues() {
 64      // Contributes 3
 65      CostUtil.staticCost3();
 66      // Contributes 2
 67      int a = 0;
 68      @SuppressWarnings("unused")
 69      int b = a > 5 ? 3 : 5;
 70      b = a < 4 ? 4 : 3;
 71      // Contributes 4
 72      new CostUtil().instanceCost4();
 73    }
 74  }
 75
 76  public void testSeveralConstructionIssues() throws Exception {
 77    ClassIssues classIssues = issuesReporter.determineIssues(
 78        decoratedComputer.compute(SeveralConstructionIssues.class));
 79    assertEquals(classIssues.toString(), 3, classIssues.getSize());
 80    Map<String,List<Issue>> constructionIssues = classIssues.getConstructionIssues();
 81    assertTrue(classIssues.toString(), constructionIssues.containsKey(COMPLEXITY.toString()));
 82    Issue complexity = constructionIssues.get(COMPLEXITY.toString()).get(0);
 83    assertEquals(2/9f, complexity.getContributionToClassCost(), 0.001f);
 84    assertTrue(classIssues.toString(), constructionIssues.containsKey(STATIC_METHOD.toString()));
 85    Issue staticCall = constructionIssues.get(STATIC_METHOD.toString()).get(0);
 86    assertEquals(3/9f, staticCall.getContributionToClassCost(), 0.001f);
 87    assertTrue(classIssues.toString(), constructionIssues.containsKey(NON_MOCKABLE.toString()));
 88    Issue collaborator = constructionIssues.get(NON_MOCKABLE.toString()).get(0);
 89    assertEquals(4/9f, collaborator.getContributionToClassCost(), 0.001f);
 90  }
 91
 92  private static class SeveralNonMockableMethodIssues {
 93    public void foo() {
 94      CostUtil.staticCost2();
 95      CostUtil.staticCost4();
 96    }
 97  }
 98
 99  public void testSeveralNonMockableMethodIssues() throws Exception {
100    ClassCost cost = decoratedComputer.compute(SeveralNonMockableMethodIssues.class);
101    ClassIssues classIssues = issuesReporter.determineIssues(
102        cost);
103    assertEquals(2, classIssues.getSize());
104    List<Issue> issues = classIssues.getCollaboratorIssues().get(STATIC_METHOD.toString());
105    Issue issue0 = issues.get(0);
106    Issue issue1 = issues.get(1);
107    assertEquals(6, cost.getTotalComplexityCost() + 10 * cost.getTotalGlobalCost());
108    assertEquals(4/6f, issue0.getContributionToClassCost(), 0.001f);
109    assertEquals(2/6f, issue1.getContributionToClassCost(), 0.001f);
110
111  }
112
113  public void testMultipleMethodInvocationSourcesDoesntBlowUp() throws Exception {
114    // Threw an exception at one time
115    ClassIssues classIssues = issuesReporter.determineIssues(
116        decoratedComputer.compute(ClassInfo.class));
117  }
118
119  private static class StaticInit {
120    // 10
121    private static int a = 1;
122    // 10
123    private static String b = "b";
124
125    // 4
126    public void otherCost() {
127      new CostUtil().instanceCost4();
128    }
129  }
130
131  public void testStaticInitializationInClass() throws Exception {
132    ClassIssues classIssues = issuesReporter.determineIssues(decoratedComputer.compute(StaticInit.class));
133    List<Issue> issues = classIssues.getIssues(CONSTRUCTION, STATIC_INIT);
134    assertEquals(classIssues.toString(), 2, issues.size());
135    assertEquals("int a", issues.get(1).getElement());
136    //TODO 1/7 doesn't seem right - class is 21 but reduced to 18 without this issue??
137    assertEquals(1/7f, issues.get(1).getContributionToClassCost(), 0.001f);
138    assertEquals("String b", issues.get(0).getElement());
139    assertEquals(1/7f, issues.get(0).getContributionToClassCost(), 0.001f);
140  }
141
142  private static class Setters {
143    private String foo;
144
145    public void setFoo(String foo) {
146      int a = 0;
147      @SuppressWarnings("unused")
148      int b = a > 5 ? 3 : 5;
149      this.foo = foo;
150    }
151
152    public String doFoo() {
153      return foo;
154    }
155  }
156
157  public void testSetterCountsAsConstructionIssue() throws Exception {
158    ClassIssues classIssues = issuesReporter.determineIssues(decoratedComputer.compute(Setters.class));
159    assertEquals(classIssues.toString(), 2, classIssues.getSize());
160    assertEquals(classIssues.toString(), 1, classIssues.getIssues(CONSTRUCTION, SETTER).size());
161  }
162
163  static class SubclassOfSetterCost extends HasSetterCost {
164    public void doThing() {
165
166    }
167  }
168
169  public void testImplicitCostFromSuperClassHasSourceLocationInOtherFile() throws Exception {
170    ClassIssues classIssues =
171        issuesReporter.determineIssues(decoratedComputer.compute(SubclassOfSetterCost.class));
172    List<Issue> issues = classIssues.getIssues(CONSTRUCTION, IssueSubType.SETTER);
173    assertEquals(1, issues.size());
174    assertEquals("com/google/test/metric/report/issues/HasSetterCost.java",
175        issues.get(0).getLocation().getFile());
176  }
177
178  public void testClassesWithNoIssuesAreStillAddedToQueue() throws Exception {
179    TriageIssuesQueue<ClassIssues> queue =
180        new TriageIssuesQueue<ClassIssues>(1, 10, new ClassIssues.TotalCostComparator());
181    IssuesReporter issuesReporterWithRealQueue = new IssuesReporter(queue, costModel);
182    ClassCost cost = decoratedComputer.compute(SeveralConstructionIssues.class);
183    issuesReporterWithRealQueue.inspectClass(cost);
184    assertEquals(1, queue.size());
185  }
186
187}