KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > AssertionVMArg


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * John Kaplan (johnkaplantech@gmail.com) - initial API and implementation
10  * (report 45408: Enable assertions during unit tests [JUnit])
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.launcher;
13
14 import org.eclipse.jface.preference.IPreferenceStore;
15
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18
19 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
20
21 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
22 import org.eclipse.jdt.internal.junit.ui.JUnitPreferencesConstants;
23
24 /**
25  * Utilities to manipulate virtual machine arguments to enable assertions
26  * according to user preferences.
27  */

28 public class AssertionVMArg {
29     
30     private static final String JavaDoc LONG_VM_ARG_TEXT = "-enableassertions"; //$NON-NLS-1$
31
private static final String JavaDoc SHORT_VM_ARG_TEXT = "-ea"; //$NON-NLS-1$
32

33     public static final int ASSERT_ARG_NOT_FOUND = -1;
34     
35     /**
36      * Sets default VM args in launch configuration to enable assertions if
37      * user preference indicates such, or blank if not.
38      *
39      * @param config the launch configuration to default
40      */

41     public static void setArgDefault(ILaunchConfigurationWorkingCopy config) {
42         String JavaDoc argText= getEnableAssertionsPreference() ? SHORT_VM_ARG_TEXT : ""; //$NON-NLS-1$
43
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, argText);
44     }
45     
46     public static String JavaDoc enableAssertInArgString(String JavaDoc currentArgs) {
47         String JavaDoc[] argArray= DebugPlugin.parseArguments(currentArgs);
48         boolean assertAlreadyEnabled= (findAssertEnabledArg(argArray) != ASSERT_ARG_NOT_FOUND);
49         String JavaDoc result= currentArgs;
50         // logic here is: if assertion is already enabled, take no action
51
// if assertion is not already enabled, enable based on user preference
52
if (!assertAlreadyEnabled && getEnableAssertionsPreference()) {
53             result= setAssertInArgString(currentArgs);
54         }
55
56         return result;
57     }
58     
59     public static int findAssertEnabledArg(String JavaDoc[] argArray) {
60         int assertArgIndex= ASSERT_ARG_NOT_FOUND;
61
62         for (int i= 0; i < argArray.length; ++i) {
63             String JavaDoc arg= argArray[i].toLowerCase();
64             if (arg.startsWith(SHORT_VM_ARG_TEXT) || arg.startsWith(LONG_VM_ARG_TEXT)) {
65                 assertArgIndex= i;
66                 break;
67             }
68         }
69
70         return assertArgIndex;
71     }
72     
73     public static String JavaDoc setAssertInArgString(String JavaDoc currentArgs) {
74         // add argument to end (with separating space)
75
// ..if there are VM arguments already,
76
// ..otherwise, default args to single enabling arg
77
return (currentArgs.length() == 0)
78             ? SHORT_VM_ARG_TEXT
79             : currentArgs + " " + SHORT_VM_ARG_TEXT; //$NON-NLS-1$
80
}
81     
82     public static boolean getEnableAssertionsPreference() {
83         IPreferenceStore store= JUnitPlugin.getDefault().getPreferenceStore();
84         return store.getBoolean(JUnitPreferencesConstants.ENABLE_ASSERTIONS);
85     }
86
87     public static void setEnableAssertionsPreference(boolean preference) {
88         IPreferenceStore store= JUnitPlugin.getDefault().getPreferenceStore();
89         store.setValue(JUnitPreferencesConstants.ENABLE_ASSERTIONS, preference);
90     }
91     
92     /* not needed unless you're manipulating already entered configurations
93     public static String removeAssertFromArgString(String currentArgs, String assertArg)
94     {
95         currentArgs.indexOf(assertArg);
96     }
97     
98     public static void syncAssertionVMArgInConfigs()
99     {
100         ILaunchManager lm= DebugPlugin.getDefault().getLaunchManager();
101         ILaunchConfigurationType configType= lm.getLaunchConfigurationType(JUnitLaunchConfiguration.ID_JUNIT_APPLICATION);
102         try {
103             ILaunchConfiguration[] configs= DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configType);
104             for (int i=0; i<configs.length; ++i) {
105                 ILaunchConfiguration config = configs[i];
106                 String vmArgs = config.getAttribute(
107                     IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, ""); //$NON-NLS-1$
108                 String[] argArray = DebugPlugin.parseArguments(vmArgs);
109                 int assertArgIndex = findAssertEnabledArg(argArray);
110                 boolean assertAlreadyEnabled = (assertArgIndex != ASSERT_ARG_NOT_FOUND);
111                 if (assertAlreadyEnabled != getEnableAssertionsPreference()) {
112                     if (assertAlreadyEnabled && !getEnableAssertionsPreference()) {
113                         // remove the assertion argument
114                         vmArgs = removeAssertFromArgString(vmArgs, argArray[assertArgIndex]);
115                     }
116                     else if (!assertAlreadyEnabled && getEnableAssertionsPreference()) {
117                         // add the assertion argument
118                         vmArgs = setAssertInArgString(vmArgs);
119                     }
120                     // save whatever change was made
121                     ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
122                     wc.setAttribute(
123                         IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs);
124                     wc.doSave();
125                 }
126             }
127         } catch (CoreException e) {
128             JUnitPlugin.log(e);
129         }
130
131     }*/

132 }
133
Popular Tags