/testability-explorer/src/test/java/com/google/test/metric/WorkInConstructorTest.java

http://testability-explorer.googlecode.com/ · Java · 80 lines · 50 code · 13 blank · 17 comment · 0 complexity · 19394733c3e7c09ed9910fe48d06bf4b MD5 · raw file

  1. /*
  2. * Copyright 2009 Google Inc. All Rights Reserved.
  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;
  17. import com.google.test.metric.testing.MetricComputerBuilder;
  18. import com.google.test.metric.testing.MetricComputerJavaDecorator;
  19. public class WorkInConstructorTest extends AutoFieldClearTestCase {
  20. private ClassRepository repo = new JavaClassRepository();
  21. private MetricComputerJavaDecorator computer = new MetricComputerJavaDecorator(
  22. new MetricComputerBuilder().withClassRepository(repo).build(), repo);
  23. static class ConstructorDoesWork {
  24. ConstructorDoesWork() {
  25. CostUtil.staticCost3();
  26. }
  27. void method() {
  28. }
  29. }
  30. public void testWorkInConstructorGetsPenalized() throws Exception {
  31. ClassCost classCost = computer.compute(ConstructorDoesWork.class);
  32. MethodCost constCost = classCost.getMethodCost("ConstructorDoesWork()");
  33. assertNotNull(constCost);
  34. assertEquals(3, constCost.getTotalCost().getCyclomaticComplexityCost());
  35. assertEquals(0, constCost.getDirectCost().getCyclomaticComplexityCost());
  36. assertEquals(3, constCost.getDependentCost().getCyclomaticComplexityCost());
  37. assertEquals(0, constCost.getConstructorDependentCost().getCyclomaticComplexityCost());
  38. MethodCost methodCost = classCost.getMethodCost("void method()");
  39. assertNotNull(methodCost);
  40. assertEquals(3, methodCost.getTotalCost().getCyclomaticComplexityCost());
  41. assertEquals(0, methodCost.getDirectCost().getCyclomaticComplexityCost());
  42. assertEquals(0, methodCost.getDependentCost().getCyclomaticComplexityCost());
  43. assertEquals(3, methodCost.getConstructorDependentCost().getCyclomaticComplexityCost());
  44. //fail("Add cost model assertions here");
  45. }
  46. static class DependantConstructorDoesWork {
  47. void method() {
  48. new ConstructorDoesWork().method();
  49. }
  50. }
  51. public void testWorkInDependantConstructorDoesNotGetPenalized() throws Exception {
  52. ClassCost classCost = computer.compute(DependantConstructorDoesWork.class);
  53. MethodCost constCost = classCost.getMethodCost("DependantConstructorDoesWork()");
  54. assertNotNull(constCost);
  55. assertEquals(0, constCost.getTotalCost().getCyclomaticComplexityCost());
  56. assertEquals(0, constCost.getDirectCost().getCyclomaticComplexityCost());
  57. assertEquals(0, constCost.getDependentCost().getCyclomaticComplexityCost());
  58. assertEquals(0, constCost.getConstructorDependentCost().getCyclomaticComplexityCost());
  59. MethodCost methodCost = classCost.getMethodCost("void method()");
  60. assertNotNull(methodCost);
  61. assertEquals(3, methodCost.getTotalCost().getCyclomaticComplexityCost());
  62. assertEquals(0, methodCost.getDirectCost().getCyclomaticComplexityCost());
  63. assertEquals(0, methodCost.getDependentCost().getCyclomaticComplexityCost());
  64. assertEquals(3, methodCost.getConstructorDependentCost().getCyclomaticComplexityCost());
  65. //fail("Add cost model assertions here");
  66. }
  67. }