IProject project = ProjectUtils.getProject(projectName);
project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
IJavaProject javaProject = JavaUtils.getJavaProject(project);
JUnitTask junit = createJUnitTask(javaProject, debug, halt);
String[] vmargs =
getPreferences().getArrayValue(project, "org.eclim.java.junit.jvmargs");
for(String vmarg : vmargs){
if (!vmarg.startsWith("-")){
continue;
}
Argument a = junit.createJvmarg();
a.setValue(vmarg);
}
String[] props =
getPreferences().getArrayValue(project, "org.eclim.java.junit.sysprops");
for(String prop : props){
String[] sysprop = StringUtils.split(prop, "=", 2);
if (sysprop.length != 2){
continue;
}
if (sysprop[0].startsWith("-D")){
sysprop[0] = sysprop[0].substring(2);
}
Variable var = new Variable();
var.setKey(sysprop[0]);
var.setValue(sysprop[1]);
junit.addConfiguredSysproperty(var);
}
String[] envs =
getPreferences().getArrayValue(project, "org.eclim.java.junit.envvars");
for(String env : envs){
String[] envvar = StringUtils.split(env, "=", 2);
if (envvar.length != 2){
continue;
}
Variable var = new Variable();
var.setKey(envvar[0]);
var.setValue(envvar[1]);
junit.addEnv(var);
}
if (file != null){
ICompilationUnit src = JavaUtils.getCompilationUnit(javaProject, file);
IMethod method = null;
if (offset != -1){
IJavaElement element = src.getElementAt(offset);
if(element != null && element.getElementType() == IJavaElement.METHOD){
method = (IMethod)element;
}
}
JUnit4TestFinder finder = new JUnit4TestFinder();
IType type = src.getTypes()[0];
if (!finder.isTest(type)){
src = JUnitUtils.findTest(javaProject, type);
if (src == null){
println(Services.getMessage("junit.testing.test.not.found"));
return null;
}
if (method != null){
method = JUnitUtils.findTestMethod(src, method);
if (method == null){
println(Services.getMessage("junit.testing.test.method.not.found"));
return null;
}
}
}
JUnitTest test = new JUnitTest();
test.setName(JavaUtils.getFullyQualifiedName(src));
if (method != null){
IAnnotation testAnnotation = method.getAnnotation("Test");
if (testAnnotation == null || !testAnnotation.exists()){
println(Services.getMessage(
"junit.testing.test.method.not.annotated",
method.getElementName()));
return null;
}
test.setMethods(method.getElementName());
}
junit.addTest(test);
}else if (testName != null){
if (testName.indexOf('*') != -1){
createBatchTest(javaProject, junit, testName);
}else{
JUnitTest test = new JUnitTest();
test.setName(testName);
junit.addTest(test);
}
}else{
ArrayList<String> names = new ArrayList<String>();
IType[] types = JUnitCore.findTestTypes(javaProject, null);
for (IType type : types) {
names.add(type.getFullyQualifiedName());
}
Collections.sort(names);
for (String name : names){
JUnitTest test = new JUnitTest();
test.setName(name);
junit.addTest(test);
}
}
try{
junit.init();
junit.execute();
}catch(BuildException be){
if(debug){
be.printStackTrace(getContext().err);
}
}