/eclipse-plugin/plugins/com.google.test.metric.eclipse.ui/src/main/java/com/google/test/metric/eclipse/ui/internal/TestabilityReportLaunchListener.java

http://testability-explorer.googlecode.com/ · Java · 162 lines · 131 code · 10 blank · 21 comment · 19 complexity · ea7f2996b1d57c68105c1ec9ebfb2bfe 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. */
  16. package com.google.test.metric.eclipse.ui.internal;
  17. import com.google.test.metric.eclipse.core.TestabilityLaunchListener;
  18. import com.google.test.metric.eclipse.internal.util.Logger;
  19. import com.google.test.metric.eclipse.internal.util.TestabilityConstants;
  20. import com.google.test.metric.eclipse.internal.util.TestabilityExplorerMessageRetriever;
  21. import com.google.test.metric.eclipse.ui.TestabilityReportView;
  22. import com.google.test.metric.report.ReportOptions;
  23. import com.google.test.metric.report.issues.ClassIssues;
  24. import com.google.test.metric.report.issues.Issue;
  25. import com.google.test.metric.report.issues.IssueType;
  26. import org.eclipse.core.resources.IMarker;
  27. import org.eclipse.core.resources.IProject;
  28. import org.eclipse.core.resources.IResource;
  29. import org.eclipse.core.runtime.CoreException;
  30. import org.eclipse.core.runtime.IPath;
  31. import org.eclipse.jdt.core.IJavaProject;
  32. import org.eclipse.jdt.core.IPackageFragmentRoot;
  33. import org.eclipse.swt.widgets.Display;
  34. import org.eclipse.ui.IViewPart;
  35. import org.eclipse.ui.IWorkbenchPage;
  36. import org.eclipse.ui.PartInitException;
  37. import org.eclipse.ui.PlatformUI;
  38. import org.eclipse.ui.texteditor.MarkerUtilities;
  39. import java.io.File;
  40. import java.util.ArrayList;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. /**
  45. * Listener which knows how to handle tasks after a testability launch is successfully completed,
  46. * including starting a view with the html report and adding annotations to open editors.
  47. *
  48. * @author shyamseshadri@google.com (Shyam Seshadri)
  49. */
  50. public class TestabilityReportLaunchListener implements TestabilityLaunchListener {
  51. private final Logger logger = new Logger();
  52. private final TestabilityExplorerMessageRetriever retriever =
  53. new TestabilityExplorerMessageRetriever();
  54. public void onLaunchCompleted(final ReportOptions reportOptions, final IJavaProject javaProject,
  55. final List<ClassIssues> classIssues, File reportDirectory, boolean runningInCompilationMode) {
  56. if (!runningInCompilationMode) {
  57. showHtmlReportView(reportDirectory);
  58. }
  59. try {
  60. createMarkersFromClassIssues(classIssues, javaProject);
  61. if (!runningInCompilationMode) {
  62. showTestabilityView();
  63. }
  64. } catch (CoreException e) {
  65. logger.logException(e);
  66. }
  67. }
  68. private void createMarkersFromClassIssues(List<ClassIssues> classIssues,
  69. IJavaProject javaProject) throws CoreException {
  70. javaProject.getProject().deleteMarkers(TestabilityConstants.TESTABILITY_COLLABORATOR_MARKER_TYPE,
  71. true, IResource.DEPTH_INFINITE);
  72. javaProject.getProject().deleteMarkers(TestabilityConstants.TESTABILITY_CONSTRUCTOR_MARKER_TYPE,
  73. true, IResource.DEPTH_INFINITE);
  74. javaProject.getProject().deleteMarkers(TestabilityConstants.TESTABILITY_DIRECT_COST_MARKER_TYPE,
  75. true, IResource.DEPTH_INFINITE);
  76. IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
  77. List<IPath> sourceFolderPaths = new ArrayList<IPath>();
  78. for (IPackageFragmentRoot root : roots) {
  79. if (!root.isArchive()) {
  80. IResource rootResource = root.getCorrespondingResource();
  81. sourceFolderPaths.add(rootResource.getFullPath().removeFirstSegments(1));
  82. }
  83. }
  84. for (ClassIssues classIssue : classIssues) {
  85. IResource resource = getAbsolutePathFromJavaFile(classIssue.getClassName(), sourceFolderPaths,
  86. javaProject.getProject());
  87. if (resource != null) {
  88. for (Issue issue : classIssue.getMostImportantIssues()) {
  89. Map<String, Object> attributes = new HashMap<String, Object>();
  90. attributes.put(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
  91. attributes.put(IMarker.LINE_NUMBER, issue.getLocation().getLineNumber());
  92. attributes.put(IMarker.MESSAGE,
  93. retriever.getSuggestion(issue.getType(), issue.getSubType()));
  94. IssueType issueType = issue.getType();
  95. attributes.put(TestabilityConstants.ISSUE_TYPE, issue.getType().toString());
  96. String markerType = null;
  97. if (IssueType.COLLABORATOR.equals(issueType)) {
  98. markerType = TestabilityConstants.TESTABILITY_COLLABORATOR_MARKER_TYPE;
  99. } else if (IssueType.CONSTRUCTION.equals(issueType)) {
  100. markerType = TestabilityConstants.TESTABILITY_CONSTRUCTOR_MARKER_TYPE;
  101. } else if (IssueType.DIRECT_COST.equals(issueType)) {
  102. markerType = TestabilityConstants.TESTABILITY_DIRECT_COST_MARKER_TYPE;
  103. }
  104. if (markerType != null) {
  105. MarkerUtilities.createMarker(resource, attributes, markerType);
  106. }
  107. }
  108. } else {
  109. logger.logException("No Resource found for Class : " + classIssue.getClassName(), null);
  110. }
  111. }
  112. }
  113. private IResource getAbsolutePathFromJavaFile(String completeClassName,
  114. List<IPath> sourceFolderPaths, IProject project) {
  115. String path = completeClassName.replaceAll("\\.", "/");
  116. for (IPath sourceFolderPath : sourceFolderPaths) {
  117. IPath totalPath = sourceFolderPath.append(path + ".java");
  118. if (project.exists(totalPath)) {
  119. return project.findMember(totalPath);
  120. }
  121. }
  122. return null;
  123. }
  124. private void showTestabilityView() {
  125. Display.getDefault().asyncExec(new Runnable() {
  126. public void run() {
  127. IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  128. try {
  129. IViewPart viewPart = page.showView("com.google.test.metric.eclipse.ui.testabilityView");
  130. } catch (PartInitException e) {
  131. logger.logException("Error initializing Testability View", e);
  132. }
  133. }
  134. });
  135. }
  136. private void showHtmlReportView(final File reportDirectory) {
  137. Display.getDefault().asyncExec(new Runnable() {
  138. public void run() {
  139. IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  140. try {
  141. IViewPart viewPart = page.showView("com.google.test.metric.eclipse.ui.browserview");
  142. if (viewPart instanceof TestabilityReportView) {
  143. ((TestabilityReportView) viewPart).setUrl(reportDirectory.getAbsolutePath()
  144. + "/" + TestabilityConstants.HTML_REPORT_FILENAME);
  145. }
  146. } catch (PartInitException e) {
  147. logger.logException("Error initializing Testability Report View", e);
  148. }
  149. }
  150. });
  151. }
  152. }