|
15 | 15 | import org.teachingextensions.approvals.lite.writers.ComponentApprovalWriter; |
16 | 16 | import org.teachingextensions.approvals.lite.writers.ImageApprovalWriter; |
17 | 17 |
|
18 | | - |
19 | | -public class Approvals { |
20 | | - public static void verify(String response) throws Exception { |
21 | | - verify(new ApprovalTextWriter(response, "txt"), FileTypes.Text); |
22 | | - } |
23 | | - |
24 | | - public static <T> void verifyAll(String header, T[] values) { |
25 | | - Approvals.verifyAll(header, Arrays.asList(values)); |
26 | | - } |
27 | | - |
28 | | - public static <T> void verifyAll(String header, Iterable<T> values) { |
29 | | - Approvals.verifyAll(header, values, new Function1<T, String>() { |
30 | | - @Override |
31 | | - public String call(T i) { |
32 | | - return i + ""; |
33 | | - } |
34 | | - }); |
35 | | - } |
36 | | - |
37 | | - public static <T> void verifyAll(String header, T[] values, Function1<T, String> f1) { |
38 | | - verifyAll(header, Arrays.asList(values), f1); |
39 | | - } |
40 | | - |
41 | | - public static <T> void verifyAll(String header, Iterable<T> array, Function1<T, String> f1) { |
42 | | - String text = formatHeader(header) + ArrayUtils.toString(array, f1); |
43 | | - verify(new ApprovalTextWriter(text, "txt"), FileTypes.Text); |
44 | | - } |
45 | | - |
46 | | - private static String formatHeader(String header) { |
47 | | - return StringUtils.isEmpty(header) ? "" : header + "\r\n\r\n\r\n"; |
48 | | - } |
49 | | - |
50 | | - public static void verifyHtml(String response) throws Exception { |
51 | | - verify(new ApprovalTextWriter(response, "html"), FileTypes.Html); |
52 | | - } |
53 | | - |
54 | | - public static void verify(Component component) { |
55 | | - Approvals.verify(new ComponentApprovalWriter(component), FileTypes.Image); |
56 | | - } |
57 | | - |
58 | | - public static void verify(BufferedImage bufferedImage) { |
59 | | - verify(new ImageApprovalWriter(bufferedImage), FileTypes.Image); |
60 | | - } |
61 | | - |
62 | | - public static void verify(ApprovalWriter writer, ApprovalNamer namer, ApprovalFailureReporter reporter) { |
63 | | - verify(new FileApprover(writer, namer), reporter); |
64 | | - } |
65 | | - |
66 | | - public static void verify(ApprovalWriter writer, String fileType) { |
67 | | - verify(writer, createApprovalNamer(), ReporterFactory.get(fileType)); |
68 | | - } |
69 | | - |
70 | | - public static void verify(FileApprover approver, ApprovalFailureReporter reporter) { |
71 | | - try { |
72 | | - if (!approver.approve()) { |
73 | | - boolean passed = false; |
74 | | - if (reporter instanceof ApprovalFailureOverrider) { |
75 | | - passed = approver.askToChangeReceivedToApproved((ApprovalFailureOverrider)reporter); |
76 | | - } |
77 | | - if (!passed) { |
78 | | - approver.reportFailure(reporter); |
79 | | - approver.fail(); |
80 | | - } else { |
81 | | - approver.cleanUpAfterSuccess(reporter); |
82 | | - } |
83 | | - } else { |
84 | | - approver.cleanUpAfterSuccess(reporter); |
85 | | - } |
86 | | - } catch (Exception e) { |
87 | | - throw ObjectUtils.throwAsError(e); |
| 18 | +/** |
| 19 | + * Approvals allows you to unit test complex objects <br><br> |
| 20 | + * |
| 21 | + * -- Create a Golden Master file (approved) <br> |
| 22 | + * -- Verify against your result file (received) <br> |
| 23 | + * -- Download BeyondCompare to view the two files <br> |
| 24 | + */ |
| 25 | +public class Approvals |
| 26 | +{ |
| 27 | + public static void verify(String response) throws Exception |
| 28 | + { |
| 29 | + verify(new ApprovalTextWriter(response, "txt"), FileTypes.Text); |
| 30 | + } |
| 31 | + public static <T> void verifyAll(String header, T[] values) |
| 32 | + { |
| 33 | + Approvals.verifyAll(header, Arrays.asList(values)); |
| 34 | + } |
| 35 | + public static <T> void verifyAll(String header, Iterable<T> values) |
| 36 | + { |
| 37 | + Approvals.verifyAll(header, values, new Function1<T, String>() |
| 38 | + { |
| 39 | + @Override |
| 40 | + public String call(T i) |
| 41 | + { |
| 42 | + return i + ""; |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + public static <T> void verifyAll(String header, T[] values, Function1<T, String> f1) |
| 47 | + { |
| 48 | + verifyAll(header, Arrays.asList(values), f1); |
| 49 | + } |
| 50 | + public static <T> void verifyAll(String header, Iterable<T> array, Function1<T, String> f1) |
| 51 | + { |
| 52 | + String text = formatHeader(header) + ArrayUtils.toString(array, f1); |
| 53 | + verify(new ApprovalTextWriter(text, "txt"), FileTypes.Text); |
| 54 | + } |
| 55 | + private static String formatHeader(String header) |
| 56 | + { |
| 57 | + return StringUtils.isEmpty(header) ? "" : header + "\r\n\r\n\r\n"; |
| 58 | + } |
| 59 | + public static void verifyHtml(String response) throws Exception |
| 60 | + { |
| 61 | + verify(new ApprovalTextWriter(response, "html"), FileTypes.Html); |
| 62 | + } |
| 63 | + public static void verify(Component component) |
| 64 | + { |
| 65 | + Approvals.verify(new ComponentApprovalWriter(component), FileTypes.Image); |
| 66 | + } |
| 67 | + public static void verify(BufferedImage bufferedImage) |
| 68 | + { |
| 69 | + verify(new ImageApprovalWriter(bufferedImage), FileTypes.Image); |
| 70 | + } |
| 71 | + public static void verify(ApprovalWriter writer, ApprovalNamer namer, ApprovalFailureReporter reporter) |
| 72 | + { |
| 73 | + verify(new FileApprover(writer, namer), reporter); |
| 74 | + } |
| 75 | + public static void verify(ApprovalWriter writer, String fileType) |
| 76 | + { |
| 77 | + verify(writer, createApprovalNamer(), ReporterFactory.get(fileType)); |
| 78 | + } |
| 79 | + public static void verify(FileApprover approver, ApprovalFailureReporter reporter) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + if (!approver.approve()) |
| 84 | + { |
| 85 | + boolean passed = false; |
| 86 | + if (reporter instanceof ApprovalFailureOverrider) |
| 87 | + { |
| 88 | + passed = approver.askToChangeReceivedToApproved((ApprovalFailureOverrider) reporter); |
88 | 89 | } |
| 90 | + if (!passed) |
| 91 | + { |
| 92 | + approver.reportFailure(reporter); |
| 93 | + approver.fail(); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + approver.cleanUpAfterSuccess(reporter); |
| 98 | + } |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + approver.cleanUpAfterSuccess(reporter); |
| 103 | + } |
89 | 104 | } |
90 | | - |
91 | | - public static ApprovalNamer createApprovalNamer() { |
92 | | - return new JUnitStackTraceNamer(); |
93 | | - } |
94 | | - |
95 | | - public static void verify(Object o) throws Exception { |
96 | | - Approvals.verify(o + ""); |
| 105 | + catch (Exception e) |
| 106 | + { |
| 107 | + throw ObjectUtils.throwAsError(e); |
97 | 108 | } |
| 109 | + } |
| 110 | + public static ApprovalNamer createApprovalNamer() |
| 111 | + { |
| 112 | + return new JUnitStackTraceNamer(); |
| 113 | + } |
| 114 | + public static void verify(Object o) throws Exception |
| 115 | + { |
| 116 | + Approvals.verify(o + ""); |
| 117 | + } |
98 | 118 | } |
0 commit comments