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