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