PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 136 lines | 77 code | 16 blank | 43 comment | 0 complexity | 0a13f9cbe0b8230f564b7dd4869e948e 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.MethodInfo;
 24import com.google.test.metric.MetricComputer;
 25import com.google.test.metric.example.MutableGlobalState.FinalGlobalExample;
 26import com.google.test.metric.example.MutableGlobalState.FinalGlobalExample.FinalGlobal;
 27import com.google.test.metric.example.MutableGlobalState.FinalGlobalExample.Gadget;
 28import com.google.test.metric.testing.MetricComputerBuilder;
 29import com.google.test.metric.testing.MetricComputerJavaDecorator;
 30
 31
 32/**
 33 * @see MutableGlobalExampleTest MutableGlobalExampleTest for contrasting examples that access
 34 * mutable global state.
 35 *
 36 * @author Misko Hevery
 37 * @author Jonathan Wolter
 38 */
 39public class FinalGlobalExampleTest extends AutoFieldClearTestCase {
 40
 41  private final ClassRepository repo = new JavaClassRepository();
 42  private MetricComputerJavaDecorator decoratedComputer;
 43
 44  @Override
 45  protected void setUp() throws Exception {
 46    MetricComputer toDecorate = new MetricComputerBuilder().withClassRepository(repo).build();
 47    decoratedComputer = new MetricComputerJavaDecorator(toDecorate, repo);
 48  }
 49
 50  public void testAccessingAFinalStaticIsOK() throws Exception {
 51    MethodCost methodCost = decoratedComputer.compute(FinalGlobalExample.class,
 52        "com.google.test.metric.example.MutableGlobalState.FinalGlobalExample.Gadget getInstance()");
 53    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 54    assertEquals(0, methodCost.getCost().getGlobalCost());
 55    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 56    assertEquals(0, methodCost.getTotalCost().getGlobalCost());
 57  }
 58
 59  public void testAccessingAFinalFieldDoesNotCountAgainstYou() throws Exception {
 60    // This method goes into final global state (cost +0) and reads a final value (cost +0)
 61    MethodCost methodCost = decoratedComputer.compute(FinalGlobalExample.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    assertEquals(0, methodCost.getTotalCost().getGlobalCost());
 67  }
 68
 69  public void testAccessingANonFinalFieldCountsAgainstYou() throws Exception {
 70    // This method goes into final global state (cost +0) and reads a mutable value (cost +1)
 71    MethodCost methodCost = decoratedComputer.compute(FinalGlobalExample.class, "int getGlobalCount()");
 72    assertEquals(1, methodCost.getTotalCost().getGlobalCost());
 73    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 74    assertEquals(0, methodCost.getCost().getGlobalCost());
 75    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 76  }
 77
 78  public void testWritingANonFinalFieldCountsAgainstYou() throws Exception {
 79    // This method goes into final global state (cost +0) and writes a mutable value (cost +1)
 80    MethodCost methodCost = decoratedComputer.compute(FinalGlobalExample.class, "int globalIncrement()");
 81    assertEquals(0, methodCost.getCost().getCyclomaticComplexityCost());
 82    assertEquals(0, methodCost.getCost().getGlobalCost());
 83    assertEquals(0, methodCost.getTotalCost().getCyclomaticComplexityCost());
 84    assertEquals(1, methodCost.getTotalCost().getGlobalCost());
 85  }
 86
 87  public void testGadgetGetCountHasOneReturnOperation() throws Exception {
 88    MethodInfo getCount = repo.getClass(Gadget.class.getCanonicalName()).getMethod("int getCount()");
 89    assertEquals(1, getCount.getOperations().size());
 90  }
 91
 92  public void testGadgetTotalClassCosts() {
 93    // This class has no cost, because there are no global references or cyclomatic complexity.
 94    ClassCost classCost = decoratedComputer.compute(Gadget.class);
 95    assertEquals(0, classCost.getHighestMethodComplexityCost());
 96    assertEquals(0, classCost.getHighestMethodGlobalCost());
 97    assertEquals(0, classCost.getTotalComplexityCost());
 98    assertEquals(0, classCost.getTotalGlobalCost());
 99  }
100
101  public void testFinalGlobalTotalClassCosts() {
102    // This class has static (global) state, but has no cost.
103    ClassCost classCost = decoratedComputer.compute(FinalGlobal.class);
104    assertEquals(0, classCost.getHighestMethodComplexityCost());
105    assertEquals(0, classCost.getHighestMethodGlobalCost());
106    assertEquals(0, classCost.getTotalComplexityCost());
107    assertEquals(0, classCost.getTotalGlobalCost());
108    // Note to the reader: This is interesting. This class does the harm,
109    // exposing the mutable global state. He himself is easy to test, though.
110    // (Low cyclomatic complexity, and no external global state is accessed).
111    // It is only when others start to use him does he become a problem.
112
113    // To repeat, the cost of his global state will be seen in his users, not
114    // in him.
115  }
116
117  public void testFinalGlobalExampleTotalClassCosts() {
118    // This class has methods which access both mutable and non-mutable global state.
119    ClassCost classCost = decoratedComputer.compute(FinalGlobalExample.class);
120    assertEquals(0, classCost.getHighestMethodComplexityCost());
121    assertEquals(1, classCost.getHighestMethodGlobalCost());
122    assertEquals(0, classCost.getTotalComplexityCost());
123
124    /* There are two instance methods which access expensive (mutable) global state:
125     * 1) Gadget#getGlobalCount()
126     * 2) Gadget#globalIncrement()
127     * Each has a global state cost of 1, so the class' total global cost is 2.
128     *
129     * Note that the other methods Gadget#getInstance() and Gadget#getGlobalId() do not have a
130     * global state cost. Why? Because while they access global state, it is <em>final</em>
131     * global state. This is non-mutable, and it does not count against you.
132     */
133    assertEquals(2, classCost.getTotalGlobalCost());
134  }
135
136}