PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/eclipse-plugin/plugins/com.google.test.metric.eclipse.core/src/main/java/com/google/test/metric/eclipse/internal/core/TestabilityLauncher.java

http://testability-explorer.googlecode.com/
Java | 240 lines | 191 code | 27 blank | 22 comment | 12 complexity | 27ede7eded7299ee6766e6a5c3582ca8 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.eclipse.internal.core;
  17. import com.google.classpath.ClassPath;
  18. import com.google.classpath.ClassPathFactory;
  19. import com.google.test.metric.AnalysisModel;
  20. import com.google.test.metric.CostModel;
  21. import com.google.test.metric.JavaClassRepository;
  22. import com.google.test.metric.JavaTestabilityModule;
  23. import com.google.test.metric.JavaTestabilityRunner;
  24. import com.google.test.metric.MetricComputer;
  25. import com.google.test.metric.RegExpWhiteList;
  26. import com.google.test.metric.ReportGeneratorProvider;
  27. import com.google.test.metric.ReportGeneratorProvider.ReportFormat;
  28. import com.google.test.metric.eclipse.core.TestabilityLaunchListener;
  29. import com.google.test.metric.eclipse.core.plugin.Activator;
  30. import com.google.test.metric.eclipse.internal.util.JavaProjectHelper;
  31. import com.google.test.metric.eclipse.internal.util.Logger;
  32. import com.google.test.metric.eclipse.internal.util.TestabilityConstants;
  33. import com.google.test.metric.report.ReportGenerator;
  34. import com.google.test.metric.report.ReportModel;
  35. import com.google.test.metric.report.ReportOptions;
  36. import com.google.test.metric.report.SourceLoader;
  37. import com.google.test.metric.report.html.HtmlReportModel;
  38. import com.google.test.metric.report.issues.ClassIssues;
  39. import com.google.test.metric.report.issues.HypotheticalCostModel;
  40. import com.google.test.metric.report.issues.IssuesReporter;
  41. import com.google.test.metric.report.issues.TriageIssuesQueue;
  42. import org.eclipse.core.runtime.CoreException;
  43. import org.eclipse.core.runtime.IConfigurationElement;
  44. import org.eclipse.core.runtime.IPath;
  45. import org.eclipse.core.runtime.IProgressMonitor;
  46. import org.eclipse.core.runtime.Platform;
  47. import org.eclipse.debug.core.ILaunch;
  48. import org.eclipse.debug.core.ILaunchConfiguration;
  49. import org.eclipse.debug.core.Launch;
  50. import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
  51. import org.eclipse.jdt.core.IClasspathEntry;
  52. import org.eclipse.jdt.core.IJavaProject;
  53. import org.eclipse.jdt.core.JavaModelException;
  54. import java.io.File;
  55. import java.io.FileOutputStream;
  56. import java.io.PrintStream;
  57. import java.util.List;
  58. /**
  59. * Launcher for testability configurations.
  60. *
  61. * @author klundberg@google.com (Karin Lundberg)
  62. *
  63. */
  64. public class TestabilityLauncher implements ILaunchConfigurationDelegate2 {
  65. private JavaProjectHelper javaProjectHelper = new JavaProjectHelper();
  66. private final Logger logger = new Logger();
  67. public boolean buildForLaunch(ILaunchConfiguration configuration, String mode,
  68. IProgressMonitor monitor) {
  69. // make sure everything is built before the launch
  70. return TestabilityConstants.TESTABILITY.equals(mode);
  71. }
  72. public boolean finalLaunchCheck(ILaunchConfiguration configuration, String mode,
  73. IProgressMonitor monitor) {
  74. return TestabilityConstants.TESTABILITY.equals(mode);
  75. }
  76. public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) {
  77. if (TestabilityConstants.TESTABILITY.equals(mode)) {
  78. return new Launch(configuration, mode, null);
  79. } else {
  80. throw new IllegalStateException(
  81. "Cannot launch testability configuration when not in testability mode.");
  82. }
  83. }
  84. public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode,
  85. IProgressMonitor monitor) {
  86. return TestabilityConstants.TESTABILITY.equals(mode);
  87. }
  88. @SuppressWarnings("unchecked")
  89. public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch,
  90. IProgressMonitor monitor) throws CoreException {
  91. if (!TestabilityConstants.TESTABILITY.equals(mode)) {
  92. throw new IllegalStateException(
  93. "Cannot launch testability configuration when not in testability mode.");
  94. }
  95. String projectName =
  96. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_PROJECT_NAME, "");
  97. IJavaProject javaProject = javaProjectHelper.getJavaProject(projectName);
  98. String projectLocation = javaProjectHelper.getProjectLocation(javaProject);
  99. String[] classPaths = getClassPaths(javaProject, projectLocation);
  100. List<String> allJavaPackages = javaProjectHelper.getAllJavaPackages(javaProject);
  101. ClassPathFactory classPathFactory = new ClassPathFactory();
  102. ClassPath classPath = classPathFactory.createFromPaths(classPaths);
  103. IPath pluginStateLocation = Activator.getDefault().getStateLocation();
  104. String baseReportDirectoryString =
  105. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_REPORT_FOLDER_NAME, "");
  106. if ("".equals(baseReportDirectoryString)) {
  107. baseReportDirectoryString = pluginStateLocation.toOSString();
  108. }
  109. File reportDirectory =
  110. new File(baseReportDirectoryString, javaProject.getProject().getName()
  111. + "-TestabilityReport");
  112. if (!reportDirectory.exists()) {
  113. reportDirectory.mkdirs();
  114. }
  115. int maxExcellentCost =
  116. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_MAX_EXCELLENT_COST,
  117. TestabilityConstants.MAX_EXCELLENT_COST);
  118. int maxAcceptableCost =
  119. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_MAX_ACCEPTABLE_COST,
  120. TestabilityConstants.MAX_ACCEPTABLE_COST);
  121. int maxClassesInReport =
  122. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_MAX_CLASSES_IN_REPORT,
  123. TestabilityConstants.MAX_CLASSES_TO_SHOW_IN_ISSUES_REPORTER);
  124. double globalCost =
  125. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_GLOBAL_STATE_COST,
  126. TestabilityConstants.GLOBAL_STATE_COST);
  127. double cyclomaticCost =
  128. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_CYCLOMATIC_COST,
  129. TestabilityConstants.CYCLOMATIC_COST);
  130. double constructorMultiplier =
  131. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_CONSTRUCTOR_MULT,
  132. TestabilityConstants.CONSTRUCTOR_MULT);
  133. int printDepth = TestabilityConstants.RECORDING_DEPTH;
  134. List<String> whitelistPackages =
  135. configuration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_WHITELIST,
  136. TestabilityConstants.WHITELIST);
  137. try {
  138. PrintStream reportStream = new PrintStream(new FileOutputStream(
  139. new File(reportDirectory, TestabilityConstants.HTML_REPORT_FILENAME)));
  140. PrintStream errorStream = new PrintStream(new FileOutputStream(
  141. new File(reportDirectory, TestabilityConstants.ERROR_LOG_FILENAME)));
  142. RegExpWhiteList whitelist = new RegExpWhiteList("java.");
  143. for (String packageName : whitelistPackages) {
  144. whitelist.addPackage(packageName);
  145. }
  146. CostModel costModel = new CostModel(cyclomaticCost, globalCost, constructorMultiplier);
  147. JavaClassRepository classRepository = new JavaClassRepository(classPath);
  148. MetricComputer computer = new MetricComputer(classRepository, errorStream, whitelist, printDepth);
  149. HypotheticalCostModel hypotheticalCostModel = new HypotheticalCostModel(costModel);
  150. IssuesReporter issuesReporter = new IssuesReporter(
  151. new TriageIssuesQueue<ClassIssues>(maxAcceptableCost,
  152. maxClassesInReport, new ClassIssues.TotalCostComparator()), hypotheticalCostModel);
  153. ReportOptions options = new ReportOptions(cyclomaticCost, globalCost, constructorMultiplier,
  154. maxExcellentCost, maxAcceptableCost, maxClassesInReport, -1, -1, printDepth, -1, "", "");
  155. SourceLoader sourceLoader = new SourceLoader(classPath);
  156. AnalysisModel analysisModel = new AnalysisModel(issuesReporter);
  157. ReportModel reportModel = new HtmlReportModel(costModel, analysisModel, options);
  158. ReportGenerator report = new ReportGeneratorProvider(classPath, options,
  159. reportStream, hypotheticalCostModel, ReportFormat.html).build(costModel, reportModel, sourceLoader);
  160. new JavaTestabilityRunner(report, classPath, classRepository, computer, allJavaPackages, whitelist, errorStream).run();
  161. boolean runningInCompilationMode = configuration.getAttribute(
  162. TestabilityConstants.CONFIGURATION_ATTR_RUNNING_IN_COMPILATION_MODE, false);
  163. notifyAllListeners(options, analysisModel.getWorstOffenders(), javaProject, reportDirectory,
  164. runningInCompilationMode);
  165. reportStream.flush();
  166. reportStream.close();
  167. } catch (Exception e) {
  168. logger.logException(e);
  169. }
  170. }
  171. private void notifyAllListeners(ReportOptions reportOptions,
  172. List<ClassIssues> classIssues, IJavaProject javaProject, File reportDirectory,
  173. boolean runningInCompilationMode) {
  174. IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
  175. "com.google.test.metric.eclipse.core.testabilityLaunchListener");
  176. for (IConfigurationElement element : elements) {
  177. try {
  178. TestabilityLaunchListener launchListener =
  179. (TestabilityLaunchListener) element.createExecutableExtension("class");
  180. launchListener.onLaunchCompleted(reportOptions, javaProject, classIssues, reportDirectory,
  181. runningInCompilationMode);
  182. } catch (CoreException e) {
  183. logger.logException("Error creating Testability Launch Listener", e);
  184. }
  185. }
  186. }
  187. public String[] getClassPaths(IJavaProject javaProject, String projectLocation)
  188. throws JavaModelException {
  189. IClasspathEntry[] classPathEntries = javaProject.getRawClasspath();
  190. String[] classPaths = new String[classPathEntries.length + 1];
  191. for (int i = 0; i < classPathEntries.length; i++) {
  192. IClasspathEntry classPathEntry = classPathEntries[i];
  193. String classPathString = null;
  194. IPath outputPath = classPathEntry.getOutputLocation();
  195. if (outputPath != null) {
  196. classPathString = projectLocation + outputPath.toOSString();
  197. } else {
  198. IPath classPath = classPathEntry.getPath();
  199. classPathString = classPath.toOSString();
  200. if (!classPathString.startsWith(System.getProperty("file.separator"))) {
  201. classPathString = System.getProperty("file.separator") + classPathString;
  202. }
  203. classPathString = projectLocation + classPathString;
  204. }
  205. classPathString = classPathString.replace("\\", "/");
  206. classPaths[i] = classPathString;
  207. }
  208. String defaultOutputPath = javaProject.getOutputLocation().toOSString();
  209. defaultOutputPath = defaultOutputPath.replace("\\", "/");
  210. classPaths[classPathEntries.length] = projectLocation + defaultOutputPath;
  211. return classPaths;
  212. }
  213. }