Skip to content

Commit e68600d

Browse files
Ville Heikkinenpuny
authored andcommitted
Initial refactoring, ant testrunning reworked
1 parent 5f34f1b commit e68600d

File tree

5 files changed

+80
-67
lines changed

5 files changed

+80
-67
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/runners/AntExerciseRunner.java

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import fi.helsinki.cs.tmc.data.Exercise;
44
import fi.helsinki.cs.tmc.data.TestRunResult;
5+
<<<<<<< HEAD
6+
=======
7+
import fi.helsinki.cs.tmc.langs.TestResult;
8+
import fi.helsinki.cs.tmc.langs.util.TaskExecutor;
9+
import fi.helsinki.cs.tmc.langs.util.TaskExecutorImpl;
10+
>>>>>>> 7e632d9... Initial refactoring, ant testrunning reworked
511
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
612
import fi.helsinki.cs.tmc.model.UserVisibleException;
713
import fi.helsinki.cs.tmc.testscanner.TestMethod;
@@ -17,58 +23,79 @@
1723
import java.util.concurrent.CancellationException;
1824
import java.util.concurrent.ExecutionException;
1925
import java.util.concurrent.Future;
26+
import java.util.concurrent.FutureTask;
2027
import java.util.logging.Level;
28+
import static java.util.logging.Level.INFO;
2129
import java.util.logging.Logger;
2230
import org.apache.tools.ant.module.api.support.ActionUtils;
2331
import org.netbeans.api.java.classpath.ClassPath;
2432
import org.netbeans.api.project.Project;
2533
import org.openide.execution.ExecutorTask;
2634
import org.openide.filesystems.FileObject;
2735
import org.openide.filesystems.FileUtil;
36+
import org.openide.util.Exceptions;
2837
import org.openide.windows.InputOutput;
2938

3039
public class AntExerciseRunner extends AbstractJavaExerciseRunner {
31-
private static final Logger log = Logger.getLogger(AntExerciseRunner.class.getName());
3240

33-
@Override
34-
public Callable<Integer> getCompilingTask(TmcProjectInfo projectInfo) {
35-
Project project = projectInfo.getProject();
36-
FileObject buildScript = project.getProjectDirectory().getFileObject("build.xml");
37-
if (buildScript == null) {
38-
throw new RuntimeException("Project has no build.xml");
39-
}
40-
ExecutorTask task;
41-
try {
42-
task = ActionUtils.runTarget(buildScript, new String[]{"compile-test"}, null);
43-
return executorTaskToCallable(task);
44-
} catch (IOException ex) {
45-
throw ExceptionUtils.toRuntimeException(ex);
46-
}
47-
}
41+
private static final Logger log = Logger.getLogger(AntExerciseRunner.class.getName());
4842

4943
@Override
5044
public Callable<TestRunResult> getTestRunningTask(final TmcProjectInfo projectInfo) {
5145
return new Callable<TestRunResult>() {
46+
5247
@Override
53-
public TestRunResult call() throws UserVisibleException, IOException, InterruptedException, ExecutionException {
54-
FileObject testDir = findTestDir(projectInfo);
55-
if (testDir == null) {
56-
throw new UserVisibleException("No test directory in project");
57-
}
48+
public TestRunResult call() throws Exception {
5849

59-
List<TestMethod> tests = findProjectTests(projectInfo, testDir);
50+
log.log(INFO,
51+
"Starting compile");
52+
53+
Project project = projectInfo.getProject();
54+
FileObject buildScript = project.getProjectDirectory().getFileObject("build.xml");
55+
if (buildScript == null) {
56+
throw new RuntimeException("Project has no build.xml");
57+
}
58+
ExecutorTask task;
6059

61-
File tempFile;
62-
tempFile = File.createTempFile("tmc_test_results", ".txt");
6360
try {
64-
return runTests(projectInfo, testDir, tests, tempFile);
65-
} finally {
66-
tempFile.delete();
61+
task = ActionUtils.runTarget(buildScript, new String[]{"compile-test"}, null);
62+
} catch (IOException ex) {
63+
Exceptions.printStackTrace(ex);
64+
throw ExceptionUtils.toRuntimeException(ex);
65+
} catch (IllegalArgumentException ex) {
66+
Exceptions.printStackTrace(ex);
67+
throw ExceptionUtils.toRuntimeException(ex);
68+
}
69+
70+
int compileResult = task.result();
71+
if (compileResult == 0) {
72+
log.log(INFO, "Compile resulted in 0");
73+
return runTests(projectInfo);
74+
} else {
75+
return null;
6776
}
6877
}
6978
};
7079
}
7180

81+
protected TestRunResult runTests(final TmcProjectInfo projectInfo) throws UserVisibleException, IOException, InterruptedException, ExecutionException {
82+
83+
FileObject testDir = findTestDir(projectInfo);
84+
if (testDir == null) {
85+
throw new UserVisibleException("No test directory in project");
86+
}
87+
88+
List<TestMethod> tests = findProjectTests(projectInfo, testDir);
89+
90+
File tempFile;
91+
tempFile = File.createTempFile("tmc_test_results", ".txt");
92+
try {
93+
return runTests(projectInfo, testDir, tests, tempFile);
94+
} finally {
95+
tempFile.delete();
96+
}
97+
}
98+
7299
private TestRunResult runTests(final TmcProjectInfo projectInfo, FileObject testDir, List<TestMethod> testMethods, File tempFile) throws UserVisibleException {
73100
try {
74101
ArrayList<String> args = new ArrayList<String>();

tmc-plugin/src/fi/helsinki/cs/tmc/runners/ExerciseRunner.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@
55
import java.util.concurrent.Callable;
66

77
public interface ExerciseRunner {
8-
public abstract Callable<Integer> getCompilingTask(TmcProjectInfo projectInfo);
9-
108
public abstract Callable<TestRunResult> getTestRunningTask(TmcProjectInfo projectInfo);
119
}

tmc-plugin/src/fi/helsinki/cs/tmc/runners/MakefileExerciseRunner.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package fi.helsinki.cs.tmc.runners;
1+
/**
2+
* package fi.helsinki.cs.tmc.runners;
23
34
import fi.helsinki.cs.tmc.data.Exercise;
45
import fi.helsinki.cs.tmc.data.TestRunResult;
@@ -119,3 +120,5 @@ private TestRunResult runTests(final TmcProjectInfo projectInfo, final boolean w
119120
}
120121
}
121122
}
123+
124+
*/

tmc-plugin/src/fi/helsinki/cs/tmc/runners/MavenExerciseRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package fi.helsinki.cs.tmc.runners;
1+
/**
2+
* package fi.helsinki.cs.tmc.runners;
23
34
import fi.helsinki.cs.tmc.data.Exercise;
45
import fi.helsinki.cs.tmc.data.TestRunResult;
@@ -95,3 +96,4 @@ public TestRunResult call() throws Exception {
9596
};
9697
}
9798
}
99+
*/

tmc-plugin/src/fi/helsinki/cs/tmc/runners/TestRunHandler.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,29 @@ public void performAction(final ResultCollector resultCollector, Project... proj
5555
final TmcProjectInfo projectInfo = projectMediator.wrapProject(project);
5656
eventBus.post(new InvokedEvent(projectInfo));
5757
final ExerciseRunner runner = getRunner(projectInfo);
58-
BgTask.start("Compiling project", runner.getCompilingTask(projectInfo), new BgTaskListener<Integer>() {
58+
BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<TestRunResult>() {
5959
@Override
60-
public void bgTaskReady(Integer result) {
61-
if (result == 0) {
62-
startRunningTests(runner, projectInfo, resultCollector);
63-
} else {
60+
public void bgTaskReady(TestRunResult result) {
61+
if (result == null) {
6462
dialogDisplayer.displayError("The code did not compile.");
63+
return;
64+
6565
}
66+
Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
67+
boolean canSubmit = ex.isReturnable();
68+
resultDisplayer.showLocalRunResult(result.getTestCaseResults(), canSubmit, new Runnable() {
69+
@Override
70+
public void run() {
71+
exerciseSubmitter.performAction(projectInfo.getProject());
72+
}
73+
}, resultCollector);
6674
}
6775

6876
@Override
6977
public void bgTaskFailed(Throwable ex) {
70-
log.log(INFO, "CompilingProject failed {0}", ex.getMessage());
71-
dialogDisplayer.displayError("Failed to compile the code:" + ex);
78+
log.log(INFO, "performAction failed message: {0}, trace: {1}",
79+
new Object[]{ex.getMessage(), Arrays.deepToString(ex.getStackTrace())});
80+
dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage());
7281
}
7382

7483
@Override
@@ -78,41 +87,15 @@ public void bgTaskCancelled() {
7887
}
7988
}
8089

81-
private void startRunningTests(ExerciseRunner runner, final TmcProjectInfo projectInfo, final ResultCollector resultCollector) {
82-
BgTask.start("Running tests", runner.getTestRunningTask(projectInfo), new BgTaskListener<TestRunResult>() {
83-
@Override
84-
public void bgTaskReady(TestRunResult result) {
85-
Exercise ex = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
86-
boolean canSubmit = ex.isReturnable();
87-
resultDisplayer.showLocalRunResult(result.getTestCaseResults(), canSubmit, new Runnable() {
88-
@Override
89-
public void run() {
90-
exerciseSubmitter.performAction(projectInfo.getProject());
91-
}
92-
}, resultCollector);
93-
}
94-
95-
@Override
96-
public void bgTaskFailed(Throwable ex) {
97-
log.log(INFO, "StartRunningTests failed message: {0}, trace: {1}",
98-
new Object[] {ex.getMessage(), Arrays.deepToString(ex.getStackTrace())});
99-
dialogDisplayer.displayError("Failed to run the tests: " + ex.getMessage());
100-
}
101-
102-
@Override
103-
public void bgTaskCancelled() {
104-
}
105-
});
106-
}
107-
10890
private AbstractExerciseRunner getRunner(TmcProjectInfo projectInfo) {
10991
switch (projectInfo.getProjectType()) {
11092
case JAVA_MAVEN:
111-
return new MavenExerciseRunner();
93+
return null;
11294
case JAVA_SIMPLE:
95+
log.log(INFO, "Ant task selected");
11396
return new AntExerciseRunner();
11497
case MAKEFILE:
115-
return new MakefileExerciseRunner();
98+
return null;
11699
default:
117100
throw new IllegalArgumentException("Unknown project type: " + projectInfo.getProjectType());
118101
}

0 commit comments

Comments
 (0)