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