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