1 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 28 public class AssertionVMArg { 29 30 private static final String LONG_VM_ARG_TEXT = "-enableassertions"; private static final String SHORT_VM_ARG_TEXT = "-ea"; 33 public static final int ASSERT_ARG_NOT_FOUND = -1; 34 35 41 public static void setArgDefault(ILaunchConfigurationWorkingCopy config) { 42 String argText= getEnableAssertionsPreference() ? SHORT_VM_ARG_TEXT : ""; config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, argText); 44 } 45 46 public static String enableAssertInArgString(String currentArgs) { 47 String [] argArray= DebugPlugin.parseArguments(currentArgs); 48 boolean assertAlreadyEnabled= (findAssertEnabledArg(argArray) != ASSERT_ARG_NOT_FOUND); 49 String result= currentArgs; 50 if (!assertAlreadyEnabled && getEnableAssertionsPreference()) { 53 result= setAssertInArgString(currentArgs); 54 } 55 56 return result; 57 } 58 59 public static int findAssertEnabledArg(String [] argArray) { 60 int assertArgIndex= ASSERT_ARG_NOT_FOUND; 61 62 for (int i= 0; i < argArray.length; ++i) { 63 String 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 setAssertInArgString(String currentArgs) { 74 return (currentArgs.length() == 0) 78 ? SHORT_VM_ARG_TEXT 79 : currentArgs + " " + SHORT_VM_ARG_TEXT; } 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 132 } 133 | Popular Tags |