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