PageRenderTime 33ms CodeModel.GetById 17ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 94 lines | 69 code | 5 blank | 20 comment | 10 complexity | 2ecc9dbe237760018bc13c4944deccc8 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.internal.util;
17
18import org.eclipse.core.runtime.CoreException;
19import org.eclipse.debug.core.DebugPlugin;
20import org.eclipse.debug.core.ILaunchConfiguration;
21import org.eclipse.debug.core.ILaunchConfigurationType;
22import org.eclipse.debug.core.ILaunchManager;
23
24/**
25 * Helper methods relevant to {@link ILaunchConfiguration} for testability.
26 * 
27 * @author shyamseshadri@google.com (Shyam Seshadri)
28 */
29public class TestabilityLaunchConfigurationHelper {
30  private Logger logger = new Logger();
31  public ILaunchConfiguration getLaunchConfiguration(String projectName) {
32    try {
33      ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
34      ILaunchConfigurationType type = launchManager
35          .getLaunchConfigurationType(TestabilityConstants.TESTABILITY_LAUNCH_CONFIGURATION_TYPE);
36      ILaunchConfiguration[] launchConfigurations = launchManager.getLaunchConfigurations(type);
37      for (ILaunchConfiguration launchConfiguration : launchConfigurations) {
38        String configProjectName =
39            launchConfiguration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_PROJECT_NAME,
40                "");
41        if (configProjectName.equals(projectName)
42            && isValidToRun(projectName, launchConfiguration)) {
43          return launchConfiguration;
44        }
45      }
46    } catch (CoreException e) {
47      logger.logException(e);
48    }
49    return null;
50  }
51  
52  public boolean isExistingLaunchConfigWithRunOnBuildOtherThanCurrent(
53      String projectName, String launchConfigName) {
54    try {
55      ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
56      ILaunchConfigurationType type = launchManager
57          .getLaunchConfigurationType(TestabilityConstants.TESTABILITY_LAUNCH_CONFIGURATION_TYPE);
58      ILaunchConfiguration[] launchConfigurations = launchManager.getLaunchConfigurations(type);
59      for (ILaunchConfiguration launchConfiguration : launchConfigurations) {
60        String configProjectName =
61            launchConfiguration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_PROJECT_NAME,
62                "");
63        boolean runOnEveryBuild =
64            launchConfiguration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_RUN_ON_BUILD,
65                false);
66        String configName = launchConfiguration.getName();
67        boolean isProjectNameEqual = configProjectName.equals(projectName);
68        boolean isLaunchConfigNameEqual = launchConfigName.equals(configName); 
69        if (isProjectNameEqual && runOnEveryBuild && !isLaunchConfigNameEqual) {
70          return true;
71        }
72      }
73    } catch (CoreException e) {
74      logger.logException(e);
75    }
76    return false;
77  }
78  
79  public boolean isExistingLaunchConfigurationWithRunOnBuild(String projectName) {
80    return getLaunchConfiguration(projectName) != null;
81  }
82  
83  private boolean isValidToRun(String projectName, ILaunchConfiguration launchConfiguration) throws CoreException {
84    boolean runOnEveryBuild =
85        launchConfiguration.getAttribute(TestabilityConstants.CONFIGURATION_ATTR_RUN_ON_BUILD,
86            false);
87    if (runOnEveryBuild
88        && !isExistingLaunchConfigWithRunOnBuildOtherThanCurrent(projectName,
89            launchConfiguration.getName())) {
90      return true;
91    }
92    return false;
93  }
94}