PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/method/MethodCostTest.java

http://testability-explorer.googlecode.com/
Java | 66 lines | 44 code | 7 blank | 15 comment | 1 complexity | e43ec0953de413a94a2a95e65e0c1c90 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.method;
  17. import com.google.test.metric.ClassCost;
  18. import com.google.test.metric.Cost;
  19. import com.google.test.metric.CostModel;
  20. import com.google.test.metric.CyclomaticCost;
  21. import com.google.test.metric.GlobalCost;
  22. import com.google.test.metric.JavaClassRepository;
  23. import com.google.test.metric.MethodCost;
  24. import com.google.test.metric.MethodInvocationCost;
  25. import com.google.test.metric.MetricComputer;
  26. import static com.google.test.metric.Reason.IMPLICIT_STATIC_INIT;
  27. import com.google.test.metric.RegExpWhiteList;
  28. import com.google.test.metric.SourceLocation;
  29. import junit.framework.TestCase;
  30. public class MethodCostTest extends TestCase {
  31. public void testComputeOverallCost() throws Exception {
  32. MethodCost cost = new MethodCost("", "a", 0, false, false, false);
  33. cost.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  34. cost.addCostSource(new GlobalCost(new SourceLocation(null, 0), null, Cost.global(1)));
  35. MethodCost cost3 = new MethodCost("", "b", 0, false, false, false);
  36. cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  37. cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  38. cost3.addCostSource(new CyclomaticCost(new SourceLocation(null, 0), Cost.cyclomatic(1)));
  39. cost.addCostSource(new MethodInvocationCost(new SourceLocation(null, 0), cost3,
  40. IMPLICIT_STATIC_INIT, Cost.cyclomatic(3)));
  41. CostModel costModel = new CostModel(2, 10, 1);
  42. cost.link();
  43. assertEquals((long) 2 * (3 + 1) + 10 * 1, costModel.computeOverall(cost.getTotalCost()));
  44. assertEquals(2, cost.getExplicitViolationCosts().size());
  45. assertEquals(1, cost.getImplicitViolationCosts().size());
  46. }
  47. private static class Setters {
  48. boolean foo;
  49. public void setFoo(String foo) {
  50. this.foo = (foo == null);
  51. }
  52. }
  53. public void testImplicitSetterCostShouldNotBeDoubleCounted() throws Exception {
  54. MetricComputer computer = new MetricComputer(new JavaClassRepository(), null, new RegExpWhiteList(), 1);
  55. ClassCost cost = computer.compute(Setters.class.getCanonicalName());
  56. MethodCost cost1 = cost.getMethodCost("void setFoo(java.lang.String)");
  57. assertEquals(1, cost1.getTotalCost().getCyclomaticComplexityCost());
  58. }
  59. }