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
31 public class RoundTripObjectSwap_Tester<T,S> {
32
33 public static <T,S> Builder<T,S> create(int index, String label, T object, ObjectSwap<T,S> swap, S expected, BeanSession beanSession) {
34 return new Builder<>(index, label, ()->object, swap, expected, beanSession);
35 }
36
37 public static class Builder<T,S> {
38 private int index;
39 private String label;
40 private Supplier<T> objectSupplier;
41 private ObjectSwap<T,S> swap;
42 private S expected;
43 private BeanSession beanSession;
44 private String exceptionMsg;
45
46 public Builder(int index, String label, Supplier<T> objectSupplier, ObjectSwap<T,S> swap, S 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,S> exceptionMsg(String v) {
56 exceptionMsg = v;
57 return this;
58 }
59
60 public RoundTripObjectSwap_Tester<T,S> build() {
61 return new RoundTripObjectSwap_Tester<>(this);
62 }
63 }
64
65 private final String label;
66 private final Supplier<T> objectSupplier;
67 private final ObjectSwap<T,S> swap;
68 private final S expected;
69 private final BeanSession beanSession;
70 private final String exceptionMsg;
71
72 private RoundTripObjectSwap_Tester(Builder<T,S> 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 (ne(expected, s)) {
86 fail("Test [" + label + " swap] failed. Expected=[" + expected + "], Actual=[" + s + "]");
87 }
88 } catch (AssertionError e) {
89 if (exceptionMsg == null)
90 throw e;
91 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
92 } catch (Exception e) {
93 if (exceptionMsg == null)
94 throw new AssertionError("Test [" + label + " swap] failed with exception: " + e.getLocalizedMessage(), e);
95 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
96 }
97 }
98
99 public void testUnswap() throws Exception {
100 try {
101 var o = objectSupplier.get();
102 var s = swap.swap(beanSession, o);
103 var o2 = swap.unswap(beanSession, s, beanSession.getClassMetaForObject(o));
104 var s2 = swap.swap(beanSession, o2);
105 if (ne(s, s2)) {
106 System.err.println("s=["+s+"], o=["+o+"], o.type=["+o.getClass().getName()+"], o2=["+o2+"], o2.type=["+o2.getClass().getName()+"]");
107 fail("Test [" + label + " unswap] failed. Expected=[" + s + "], Actual=[" + s2 + "]");
108 }
109 } catch (AssertionError e) {
110 if (exceptionMsg == null)
111 throw e;
112 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
113 } catch (Exception e) {
114 if (exceptionMsg == null)
115 throw new AssertionError("Test [" + label + " unswap] failed with exception: " + e.getLocalizedMessage(), e);
116 assertTrue(e.getMessage().contains(exceptionMsg), fms("Expected exception message to contain: {0}, but was {1}.", exceptionMsg, e.getMessage()));
117 }
118 }
119
120 @Override
121 public String toString() {
122 return "RoundTripObjectSwapTester: " + label;
123 }
124 }