View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tester class for round-trip string swap testing designed for JUnit 5 parameterized tests.
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);  // NOT DEBUG
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 }