Skip to content

Commit 90c333f

Browse files
committed
Better exception handling in reporters
1 parent 6be8b17 commit 90c333f

File tree

8 files changed

+106
-8
lines changed

8 files changed

+106
-8
lines changed

java/org/approvaltests/reporters/GenericDiffReporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class GenericDiffReporter implements EnvironmentAwareReporter
1010
{
11+
public static boolean REPORT_MISSING_FILES = false;
1112
protected String diffProgram;
1213
protected String arguments;
1314
protected String diffProgramNotFoundMessage;
@@ -52,7 +53,16 @@ public String getCommandLine(String received, String approved)
5253
@Override
5354
public boolean isWorkingInThisEnvironment(String forFile)
5455
{
55-
return new File(diffProgram).exists() && isFileExtensionHandled(forFile);
56+
return checkFileExists() && isFileExtensionHandled(forFile);
57+
}
58+
public boolean checkFileExists()
59+
{
60+
boolean exists = new File(diffProgram).exists();
61+
if (REPORT_MISSING_FILES && !exists)
62+
{
63+
System.out.println(String.format("%s can't find '%s'", this.getClass().getSimpleName(), diffProgram));
64+
}
65+
return exists;
5666
}
5767
public boolean isFileExtensionHandled(String forFile)
5868
{

java/org/approvaltests/reporters/MultiReporter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.approvaltests.reporters;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
45
import java.util.Collection;
56

@@ -19,10 +20,19 @@ public MultiReporter(ApprovalFailureReporter... reporters)
1920
@Override
2021
public void report(String received, String approved) throws Exception
2122
{
23+
ArrayList<Throwable> exceptions = new ArrayList<Throwable>();
2224
for (ApprovalFailureReporter reporter : reporters)
2325
{
24-
reporter.report(received, approved);
26+
try
27+
{
28+
reporter.report(received, approved);
29+
}
30+
catch (Throwable t)
31+
{
32+
exceptions.add(t);
33+
}
2534
}
35+
MultipleExceptions.rethrowExceptions(exceptions);
2636
}
2737
public ApprovalFailureReporter[] getReporters()
2838
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.approvaltests.reporters;
2+
3+
import java.util.ArrayList;
4+
5+
public class MultipleExceptions extends RuntimeException
6+
{
7+
private ArrayList<Throwable> exceptions;
8+
public MultipleExceptions(ArrayList<Throwable> exceptions)
9+
{
10+
super(getText(exceptions), exceptions.get(0));
11+
this.exceptions = exceptions;
12+
}
13+
public Throwable[] getExceptions()
14+
{
15+
return exceptions.toArray(new Throwable[0]);
16+
}
17+
public static void rethrowExceptions(ArrayList<Throwable> exceptions) throws Exception
18+
{
19+
if (exceptions.size() == 0)
20+
{
21+
return;
22+
}
23+
else if (exceptions.size() == 0)
24+
{
25+
Throwable t = exceptions.get(0);
26+
if (t instanceof Exception) { throw ((Exception) t); }
27+
throw (Error) t;
28+
}
29+
else
30+
{
31+
throw new MultipleExceptions(exceptions);
32+
}
33+
}
34+
private static String getText(ArrayList<Throwable> exceptions)
35+
{
36+
StringBuffer b = new StringBuffer("Multiple Exceptions Thrown:");
37+
for (int i = 0; i < exceptions.size(); i++)
38+
{
39+
b.append(String.format("\n #%s): %s", i + 1, exceptions.get(i).getMessage()));
40+
}
41+
return b.toString();
42+
}
43+
}

java/org/approvaltests/reporters/tests/ReporterChainingTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
import junit.framework.TestCase;
44

5+
import org.approvaltests.Approvals;
56
import org.approvaltests.reporters.EnvironmentAwareReporter;
67
import org.approvaltests.reporters.FirstWorkingReporter;
78
import org.approvaltests.reporters.MultiReporter;
89

910
public class ReporterChainingTest extends TestCase
1011
{
12+
public static class ExceptionThrowingReporter implements EnvironmentAwareReporter
13+
{
14+
public boolean run = false;
15+
@Override
16+
public void report(String received, String approved) throws Exception
17+
{
18+
run = true;
19+
throw new Error("Error");
20+
}
21+
@Override
22+
public boolean isWorkingInThisEnvironment(String forFile)
23+
{
24+
return true;
25+
}
26+
}
1127
public static class NonWorkingReporter implements EnvironmentAwareReporter
1228
{
1329
@Override
@@ -54,4 +70,20 @@ public void testMultiReporter() throws Exception
5470
assertEquals("Hello", workingReporter.received);
5571
assertEquals("Hello", workingReporter2.received);
5672
}
73+
public void testMultiReporterWithExceptions() throws Exception
74+
{
75+
ExceptionThrowingReporter exception1 = new ExceptionThrowingReporter();
76+
ExceptionThrowingReporter exception2 = new ExceptionThrowingReporter();
77+
MultiReporter reporter = new MultiReporter(exception1, exception2);
78+
try
79+
{
80+
reporter.report("Hello", "world");
81+
}
82+
catch (Throwable t)
83+
{
84+
assertEquals(true, exception1.run);
85+
assertEquals(true, exception2.run);
86+
Approvals.verify(t.getMessage());
87+
}
88+
}
5789
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Multiple Exceptions Thrown:
2+
#1): Error
3+
#2): Error

java/org/approvaltests/webpages/WebPageChangeDetector.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
public class WebPageChangeDetector implements ActionListener
2121
{
22-
private boolean keyPressed = false;
23-
private URI url;
24-
private boolean validUrl;
25-
private Boolean filesMatched;
26-
private WebPageChangeDetectorGui gui;
22+
private boolean keyPressed = false;
23+
private URI url;
24+
private boolean validUrl;
25+
private Boolean filesMatched;
26+
public WebPageChangeDetectorGui gui;
2727
public WebPageChangeDetector()
2828
{
2929
gui = new WebPageChangeDetectorGui(this);

java/org/approvaltests/webpages/tests/WebPageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public void ptestWikipedia() throws Exception
2020
}
2121
public void testChangeDetectorUI() throws Exception
2222
{
23-
Approvals.verify(new WebPageChangeDetector());
23+
Approvals.verify(new WebPageChangeDetector().gui);
2424
}
2525
}
Binary file not shown.

0 commit comments

Comments
 (0)