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.commons.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.swap.*;
26
27
28
29
30
31 public class OneWayStringSwap_Tester<T> {
32
33 public static <T> Builder<T> create(int index, String label, T object, StringSwap<T> swap, String expected, BeanSession beanSession) {
34 return new Builder<>(index, label, ()->object, swap, expected, beanSession);
35 }
36
37 public static class Builder<T> {
38 private int index;
39 private String label;
40 private Supplier<T> objectSupplier;
41 private StringSwap<T> swap;
42 private String expected;
43 private BeanSession beanSession;
44 private String exceptionMsg;
45
46 public Builder(int index, String label, Supplier<T> objectSupplier, StringSwap<T> swap, String expected, BeanSession beanSession) {
47 this.index = index;
48 this.label = label;
49 this.objectSupplier = objectSupplier;
50 this.swap = swap;
51 this.expected = expected;
52 this.beanSession = beanSession;
53 }
54
55 public Builder<T> exceptionMsg(String v) {
56 exceptionMsg = v;
57 return this;
58 }
59
60 public OneWayStringSwap_Tester<T> build() {
61 return new OneWayStringSwap_Tester<>(this);
62 }
63 }
64
65 private final String label;
66 private final Supplier<T> objectSupplier;
67 private final StringSwap<T> swap;
68 private final String expected;
69 private final BeanSession beanSession;
70 private final String exceptionMsg;
71
72 private OneWayStringSwap_Tester(Builder<T> b) {
73 label = "[" + b.index + "] " + b.label;
74 objectSupplier = b.objectSupplier;
75 swap = b.swap;
76 expected = b.expected;
77 beanSession = b.beanSession;
78 exceptionMsg = b.exceptionMsg;
79 }
80
81 public void testSwap() throws Exception {
82 try {
83 var o = objectSupplier.get();
84 var s = swap.swap(beanSession, o);
85 if (neq(expected, s)) {
86 if (expected.isEmpty()) {
87 if (! label.startsWith("[]"))
88 System.err.println(label.substring(0, label.indexOf(']')+1) + " "+s);
89 fail("Test [" + label + " swap] failed - expected was empty but got: " + s);
90 } else {
91 fail("Test [" + label + " swap] failed. Expected=[" + expected + "], Actual=[" + s + "]");
92 }
93 }
94 } catch (AssertionError e) {
95 if (exceptionMsg == null)
96 throw e;
97 assertTrue(e.getMessage().contains(exceptionMsg), fs("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
98 } catch (Exception e) {
99 if (exceptionMsg == null)
100 throw new AssertionError("Test [" + label + " swap] failed with exception: " + e.getLocalizedMessage(), e);
101 assertTrue(e.getMessage().contains(exceptionMsg), fs("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
102 }
103 }
104
105 @Override
106 public String toString() {
107 return "OneWayStringSwapTester: " + label;
108 }
109 }