1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.transforms;
18
19 import static org.apache.juneau.common.utils.Utils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.util.function.*;
23
24 import org.apache.juneau.*;
25 import org.apache.juneau.common.utils.*;
26 import org.apache.juneau.swap.*;
27
28
29
30
31
32 public class OneWayStringSwap_Tester<T> {
33
34 public static <T> Builder<T> create(int index, String label, T object, StringSwap<T> swap, String expected, BeanSession beanSession) {
35 return new Builder<>(index, label, ()->object, swap, expected, beanSession);
36 }
37
38 public static class Builder<T> {
39 private int index;
40 private String label;
41 private Supplier<T> objectSupplier;
42 private StringSwap<T> swap;
43 private String expected;
44 private BeanSession beanSession;
45 private String exceptionMsg;
46
47 public Builder(int index, String label, Supplier<T> objectSupplier, StringSwap<T> swap, String expected, BeanSession beanSession) {
48 this.index = index;
49 this.label = label;
50 this.objectSupplier = objectSupplier;
51 this.swap = swap;
52 this.expected = expected;
53 this.beanSession = beanSession;
54 }
55
56 public Builder<T> exceptionMsg(String v) {
57 exceptionMsg = v;
58 return this;
59 }
60
61 public OneWayStringSwap_Tester<T> build() {
62 return new OneWayStringSwap_Tester<>(this);
63 }
64 }
65
66 private final String label;
67 private final Supplier<T> objectSupplier;
68 private final StringSwap<T> swap;
69 private final String expected;
70 private final BeanSession beanSession;
71 private final String exceptionMsg;
72
73 private OneWayStringSwap_Tester(Builder<T> b) {
74 label = "[" + b.index + "] " + b.label;
75 objectSupplier = b.objectSupplier;
76 swap = b.swap;
77 expected = b.expected;
78 beanSession = b.beanSession;
79 exceptionMsg = b.exceptionMsg;
80 }
81
82 public void testSwap() throws Exception {
83 try {
84 var o = objectSupplier.get();
85 var s = swap.swap(beanSession, o);
86 if (Utils.ne(expected, s)) {
87 if (expected.isEmpty()) {
88 if (! label.startsWith("[]"))
89 System.err.println(label.substring(0, label.indexOf(']')+1) + " "+s);
90 fail("Test [" + label + " swap] failed - expected was empty but got: " + s);
91 } else {
92 fail("Test [" + label + " swap] failed. Expected=[" + expected + "], Actual=[" + s + "]");
93 }
94 }
95 } catch (AssertionError e) {
96 if (exceptionMsg == null)
97 throw e;
98 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
99 } catch (Exception e) {
100 if (exceptionMsg == null)
101 throw new AssertionError("Test [" + label + " swap] failed with exception: " + e.getLocalizedMessage(), e);
102 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
103 }
104 }
105
106 @Override
107 public String toString() {
108 return "OneWayStringSwapTester: " + label;
109 }
110 }