Skip to content

Commit 1d3e044

Browse files
isidoreLarsEckartragunathjawahar
committed
F!! Junit 3-5 reporters
Co-Authored-By: Lars Eckart <[email protected]> Co-Authored-By: Ragunath Jawahar <[email protected]>
1 parent e54b1d0 commit 1d3e044

File tree

11 files changed

+132
-27
lines changed

11 files changed

+132
-27
lines changed
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 com.spun.util.ObjectUtils;
4+
import com.spun.util.io.FileUtils;
5+
6+
import java.io.File;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
10+
public abstract class AbstractJUnitReporter implements EnvironmentAwareReporter {
11+
private String className;
12+
13+
public AbstractJUnitReporter(String className) {
14+
this.className = className;
15+
}
16+
17+
public void assertEquals(String expected, String actual) {
18+
try {
19+
Class<?> clazz = ObjectUtils.loadClass(className);
20+
Method assertEquals = clazz.getMethod("assertEquals", Object.class, Object.class);
21+
assertEquals.invoke(null, expected, actual);
22+
} catch(InvocationTargetException exception) {
23+
throw ObjectUtils.throwAsError(exception.getTargetException());
24+
}catch (Throwable throwable) {
25+
throw ObjectUtils.throwAsError(throwable);
26+
}
27+
}
28+
29+
@Override
30+
public void report(String received, String approved) {
31+
String aText = new File(approved).exists() ? FileUtils.readFile(approved) : "";
32+
String rText = FileUtils.readFile(received);
33+
String approveCommand = "To approve run : " + ClipboardReporter.getAcceptApprovalText(received, approved);
34+
System.out.println(approveCommand);
35+
assertEquals(aText, rText);
36+
}
37+
38+
@Override
39+
public boolean isWorkingInThisEnvironment(String forFile) {
40+
boolean present = ObjectUtils.isClassPresent(className);
41+
return present && GenericDiffReporter.isFileExtensionValid(forFile, GenericDiffReporter.TEXT_FILE_EXTENSIONS);
42+
}
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.approvaltests.reporters;
2+
3+
public class Junit3Reporter extends AbstractJUnitReporter {
4+
5+
public Junit3Reporter() {
6+
super("junit.framework.Assert");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.approvaltests.reporters;
2+
3+
public class Junit4Reporter extends AbstractJUnitReporter {
4+
5+
public Junit4Reporter() {
6+
super("org.junit.Assert");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.approvaltests.reporters;
2+
3+
public class Junit5Reporter extends AbstractJUnitReporter {
4+
5+
public Junit5Reporter() {
6+
super("org.junit.jupiter.api.Assertions");
7+
}
8+
}
Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
11
package org.approvaltests.reporters;
22

3-
import java.io.File;
43

5-
import com.spun.util.ObjectUtils;
6-
import com.spun.util.io.FileUtils;
7-
8-
import junit.framework.TestCase;
9-
10-
public class JunitReporter implements EnvironmentAwareReporter
4+
public class JunitReporter extends FirstWorkingReporter
115
{
126
public static final JunitReporter INSTANCE = new JunitReporter();
13-
@Override
14-
public void report(String received, String approved)
7+
public JunitReporter()
158
{
16-
String aText = new File(approved).exists() ? FileUtils.readFile(approved) : "";
17-
String rText = FileUtils.readFile(received);
18-
String approveCommand = "To approve run : " + ClipboardReporter.getAcceptApprovalText(received, approved);
19-
System.out.println(approveCommand);
20-
TestCase.assertEquals(aText, rText);
21-
}
22-
@Override
23-
public boolean isWorkingInThisEnvironment(String forFile)
24-
{
25-
try
26-
{
27-
ObjectUtils.loadClass("junit.framework.TestCase");
28-
}
29-
catch (Throwable t)
30-
{
31-
return false;
32-
}
33-
return GenericDiffReporter.isFileExtensionValid(forFile, GenericDiffReporter.TEXT_FILE_EXTENSIONS);
9+
super(new Junit5Reporter(), new Junit4Reporter(), new Junit3Reporter());
3410
}
11+
3512
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.approvaltests.reporters;
2+
3+
import org.approvaltests.Approvals;
4+
import org.approvaltests.core.ApprovalFailureReporter;
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.spun.util.ClassUtils;
8+
9+
public class JUnitReporterTest
10+
{
11+
@Test
12+
void testJUnit3()
13+
{
14+
verifyReporter(new Junit3Reporter());
15+
}
16+
@Test
17+
void testJUnit4()
18+
{
19+
verifyReporter(new Junit4Reporter());
20+
}
21+
@Test
22+
void testJUnit5()
23+
{
24+
verifyReporter(new Junit5Reporter());
25+
}
26+
private void verifyReporter(ApprovalFailureReporter reporter)
27+
{
28+
String a = ClassUtils.getAdjacentFile(this.getClass(), "a.txt").getAbsolutePath();
29+
String b = ClassUtils.getAdjacentFile(this.getClass(), "b.txt").getAbsolutePath();
30+
Approvals.verifyException(() -> {
31+
reporter.report(b, a);
32+
});
33+
}
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
junit.framework.AssertionFailedError: expected:<1
2+
a
3+
I
4+
> but was:<1
5+
b
6+
I
7+
>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
org.junit.ComparisonFailure: expected:<1
2+
[a]
3+
I
4+
> but was:<1
5+
[b]
6+
I
7+
>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
org.opentest4j.AssertionFailedError: expected: <1
2+
a
3+
I
4+
> but was: <1
5+
b
6+
I
7+
>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
a
3+
I

0 commit comments

Comments
 (0)