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.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   * Tester class for one-way string swap testing designed for JUnit 5 parameterized tests.
29   * Only performs swap operation (no unswap).
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);  // NOT DEBUG
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 }