PageRenderTime 17ms CodeModel.GetById 5ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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