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

/testability-explorer/src/test/java/com/google/test/metric/example/MutableGlobalExampleTest.java

http://testability-explorer.googlecode.com/
Java | 126 lines | 72 code | 14 blank | 40 comment | 0 complexity | 93a461541dd6b81b94aea9d98395c101 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.example;
 17
 18import com.google.test.metric.AutoFieldClearTestCase;
 19import com.google.test.metric.ClassCost;
 20import com.google.test.metric.ClassRepository;
 21import com.google.test.metric.JavaClassRepository;
 22import com.google.test.metric.MethodCost;
 23import com.google.test.metric.MetricComputer;
 24import com.google.test.metric.example.MutableGlobalState.MutableGlobalExample;
 25import com.google.test.metric.example.MutableGlobalState.FinalGlobalExample.Gadget;
 26import com.google.test.metric.example.MutableGlobalState.MutableGlobalExample.MutableGlobal;
 27import com.google.test.metric.testing.MetricComputerBuilder;
 28import com.google.test.metric.testing.MetricComputerJavaDecorator;
 29
 30/**
 31 * @see FinalGlobalExampleTest FinalGlobalExampleTest for contrasting examples that access
 32 * non-mutable global state.
 33 *
 34 * @author Jonathan Wolter
 35 */
 36public class MutableGlobalExampleTest extends AutoFieldClearTestCase {
 37
 38  private final ClassRepository repo = new JavaClassRepository();
 39  private MetricComputerJavaDecorator decoratedComputer;
 40
 41  @Override
 42  protected void setUp() throws Exception {
 43    MetricComputer toDecorate = new MetricComputerBuilder().withClassRepository(repo).build();
 44    decoratedComputer = new MetricComputerJavaDecorator(toDecorate, repo);
 45  }
 46
 47  public void testAccessingMutableStaticItselfDirectlyDoesntCountAgainstYou() throws Exception {
 48    MethodCost methodCost = decoratedComputer.compute(MutableGlobalExample.class,
 49        "com.google.test.metric.example.MutableGlobalState.MutableGlobalExample.Gadget getInstance()");
 50    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 51
 52    // Noteworthy: code which exposes global state to others does not have the cost itself.
 53    // TODO(jwolter): is this correct?
 54    assertEquals(0, methodCost.getCost().getGlobalCost());
 55    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 56    assertEquals(0, methodCost.getTotalCost().getGlobalCost());
 57  }
 58
 59  public void testAccessingAFinalFieldDoesNotCountAgainstYouButInitialGlobalDoes() throws Exception {
 60    // This method goes into mutable global state (cost +1) and reads a final value (cost +0)
 61    MethodCost methodCost = decoratedComputer.compute(MutableGlobalExample.class,
 62        "java.lang.String getGlobalId()");
 63    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 64    assertEquals(0, methodCost.getCost().getGlobalCost());
 65    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 66    // Total Global Cost of 1, because of the {@code mutableInstance}.
 67    assertEquals(1, methodCost.getTotalCost().getGlobalCost());
 68  }
 69
 70  public void testAccessingANonFinalFieldCountsAgainstYou() throws Exception {
 71    // This method goes into mutable global state (cost +1) and reads a mutable value (cost +1)
 72    MethodCost methodCost = decoratedComputer.compute(MutableGlobalExample.class, "int getGlobalCount()");
 73    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 74    assertEquals(0, methodCost.getCost().getGlobalCost());
 75    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 76    assertEquals(2, methodCost.getTotalCost().getGlobalCost());
 77  }
 78
 79  public void testWritingANonFinalFieldCountsAgainstYou() throws Exception {
 80    // This method goes into mutable global state (cost +1) and changes a mutable value (cost +1)
 81    MethodCost methodCost = decoratedComputer.compute(MutableGlobalExample.class, "int globalIncrement()");
 82    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 83    assertEquals(0, methodCost.getCost().getGlobalCost());
 84    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 85    assertEquals(2, methodCost.getTotalCost().getGlobalCost());
 86  }
 87
 88  public void testGadgetTotalClassCosts() {
 89    // This class has no cost, because there are no global references or cyclomatic complexity.
 90    ClassCost classCost = decoratedComputer.compute(Gadget.class);
 91    assertEquals(0, classCost.getHighestMethodComplexityCost());
 92    assertEquals(0, classCost.getHighestMethodGlobalCost());
 93    assertEquals(0, classCost.getTotalComplexityCost());
 94    assertEquals(0, classCost.getTotalGlobalCost());
 95  }
 96
 97  public void testMutableGlobalTotalClassCosts() {
 98    // This class has a static mutable instance that exposes global state.
 99    // Contrast this with {@code FinalGlobalExampleTest#testFinalGlobalTotalClassCosts()}
100    ClassCost classCost = decoratedComputer.compute(MutableGlobal.class);
101    assertEquals(0, classCost.getHighestMethodComplexityCost());
102    assertEquals(1, classCost.getHighestMethodGlobalCost());
103    assertEquals(0, classCost.getTotalComplexityCost());
104    assertEquals(2, classCost.getTotalGlobalCost());
105  }
106
107  public void testMutableGlobalExampleTotalClassCosts() {
108    // This class uses a class which has a static mutable instance.
109    ClassCost classCost = decoratedComputer.compute(MutableGlobalExample.class);
110    assertEquals(0, classCost.getHighestMethodComplexityCost());
111    assertEquals(2, classCost.getHighestMethodGlobalCost());
112    assertEquals(0, classCost.getTotalComplexityCost());
113
114    /* There are three instance methods which access expensive (mutable) global state:
115     * 1) Gadget#getGlobalId() - cost of 1
116     * 2) Gadget#getGlobalCount() - cost of 2
117     * 3) Gadget#globalIncrement() - cost of 2
118     * The class' total global cost is 5.
119     *
120     * Note that the only other method is the constructor, which has zero global state cost, and
121     * zero complexity cost.
122     */
123    assertEquals(5, classCost.getTotalGlobalCost());
124  }
125
126}