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