PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/report/about/AboutTestabilityReport.java

http://testability-explorer.googlecode.com/
Java | 105 lines | 71 code | 14 blank | 20 comment | 3 complexity | 48d064c186aa7dbd47659c96d5387ae8 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2009 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.report.about;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import com.google.common.base.Supplier;
  21. import com.google.test.metric.ClassCost;
  22. import com.google.test.metric.collection.LazyHashMap;
  23. import com.google.test.metric.report.ReportModel;
  24. import com.google.test.metric.report.Source;
  25. import com.google.test.metric.report.SourceLoader;
  26. import com.google.test.metric.report.issues.ClassIssues;
  27. import com.google.test.metric.report.issues.IssuesReporter;
  28. /**
  29. * A report which shows classes issues, their name, and their source code.
  30. *
  31. * @author alexeagle@google.com (Alex Eagle)
  32. */
  33. public class AboutTestabilityReport extends ReportModel {
  34. private final IssuesReporter issuesReporter;
  35. private final SourceLoader sourceLoader;
  36. private final String PACKAGE_PREFIX = "com.google.test.metric.example.";
  37. private final Map<String, List<ClassModel>> classesByPackage =
  38. LazyHashMap.newLazyHashMap(new Supplier<List<ClassModel>>() {
  39. public List<ClassModel> get() {
  40. return new LinkedList<ClassModel>();
  41. }
  42. });
  43. public AboutTestabilityReport(IssuesReporter issuesReporter, SourceLoader sourceLoader) {
  44. this.issuesReporter = issuesReporter;
  45. this.sourceLoader = sourceLoader;
  46. }
  47. public void addClassCost(ClassCost classCost) {
  48. String className = classCost.getClassName();
  49. if (isInnerClass(className)) {
  50. return;
  51. }
  52. Source source = sourceLoader.load(className);
  53. if (source.getLines().isEmpty()) {
  54. throw new IllegalStateException("Failed to load source for class " + className);
  55. }
  56. ClassIssues issues = issuesReporter.determineIssues(classCost);
  57. String displayName = className;
  58. if (displayName.startsWith(PACKAGE_PREFIX)) {
  59. displayName = displayName.substring(PACKAGE_PREFIX.length());
  60. }
  61. int indexOfLastPackageSeparator = displayName.lastIndexOf(".");
  62. String packageName = displayName.substring(0, indexOfLastPackageSeparator);
  63. String displayClassName = displayName.substring(indexOfLastPackageSeparator + 1);
  64. classesByPackage.get(packageName).add(new ClassModel(issues, source, displayClassName));
  65. }
  66. private boolean isInnerClass(String className) {
  67. return className.contains("$");
  68. }
  69. public Map<String, List<ClassModel>> getClassesByPackage() {
  70. return classesByPackage;
  71. }
  72. public class ClassModel {
  73. private final ClassIssues classIssues;
  74. private final Source source;
  75. private final String className;
  76. public ClassModel(ClassIssues classCost, Source source, String className) {
  77. this.classIssues = classCost;
  78. this.source = source;
  79. this.className = className;
  80. }
  81. public ClassIssues getIssues() {
  82. return classIssues;
  83. }
  84. public Source getSource() {
  85. return source;
  86. }
  87. public String getName() {
  88. return className;
  89. }
  90. }
  91. }