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;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  
21  import java.lang.reflect.*;
22  import java.util.*;
23  import java.util.function.*;
24  
25  import org.apache.juneau.common.utils.*;
26  import org.apache.juneau.utils.*;
27  
28  /**
29   * Represents the input to a ComboTest.
30   * @param <T>
31   */
32  public class ComboInput<T> {
33  
34  	final String label;
35  	final Supplier<T> in;
36  	String exceptionMsg;
37  	private Predicate<String> skipTest;
38  	private Function<T,T> convert;
39  	private List<Function<T,String>> verify = list();
40  	List<Class<?>> swaps = list();
41  	final Type type;
42  	String json, jsonT, jsonR, xml, xmlT, xmlR, xmlNs, html, htmlT, htmlR, uon, uonT, uonR, urlEncoding,
43  		urlEncodingT, urlEncodingR, msgPack, msgPackT, rdfXml, rdfXmlT, rdfXmlR;
44  	List<Tuple2<Class<?>,Consumer<?>>> applies = list();
45  
46  	public ComboInput<T> beanContext(Consumer<BeanContext.Builder> c) {
47  		apply(BeanContext.Builder.class, c);
48  		return this;
49  	}
50  
51  	public <T2> ComboInput<T> apply(Class<T2> t, Consumer<T2> c) {
52  		applies.add(Tuple2.of(t, c));
53  		return this;
54  	}
55  
56  	public ComboInput<T> exceptionMsg(String exceptionMsg) {
57  		this.exceptionMsg = exceptionMsg;
58  		return this;
59  	}
60  
61  	public ComboInput<T> skipTest(Predicate<String> skipTest) {
62  		this.skipTest = skipTest;
63  		return this;
64  	}
65  
66  	public ComboInput<T> convert(Function<T,T> convert) {
67  		this.convert = convert;
68  		return this;
69  	}
70  
71  	public ComboInput<T> verify(Function<T,String> verify) {
72  		this.verify.add(verify);
73  		return this;
74  	}
75  
76  	public ComboInput<T> verify(Predicate<T> verify, String msg, Object...args) {
77  		this.verify.add(x -> verify.test(x) ? null : StringUtils.format(msg, args));
78  		return this;
79  	}
80  
81  	public ComboInput<T> swaps(Class<?>...c) {
82  		this.swaps.addAll(Arrays.asList(c));
83  		return this;
84  	}
85  
86  	public ComboInput(String label, Type type, T in) {
87  		this.label = label;
88  		this.type = type;
89  		this.in = () -> in;
90  	}
91  
92  	public ComboInput(String label, Type type, Supplier<T> in) {
93  		this.label = label;
94  		this.type = type;
95  		this.in = in;
96  	}
97  
98  	public ComboInput<T> json(String value) {
99  		this.json = value;
100 		return this;
101 	}
102 	public ComboInput<T> jsonT(String value) {
103 		this.jsonT = value;
104 		return this;
105 	}
106 	public ComboInput<T> jsonR(String value) {
107 		this.jsonR = value;
108 		return this;
109 	}
110 	public ComboInput<T> xml(String value) {
111 		this.xml = value;
112 		return this;
113 	}
114 	public ComboInput<T> xmlT(String value) {
115 		this.xmlT = value;
116 		return this;
117 	}
118 	public ComboInput<T> xmlR(String value) {
119 		this.xmlR = value;
120 		return this;
121 	}
122 	public ComboInput<T> xmlNs(String value) {
123 		this.xmlNs = value;
124 		return this;
125 	}
126 	public ComboInput<T> html(String value) {
127 		this.html = value;
128 		return this;
129 	}
130 	public ComboInput<T> htmlT(String value) {
131 		this.htmlT = value;
132 		return this;
133 	}
134 	public ComboInput<T> htmlR(String value) {
135 		this.htmlR = value;
136 		return this;
137 	}
138 	public ComboInput<T> uon(String value) {
139 		this.uon = value;
140 		return this;
141 	}
142 	public ComboInput<T> uonT(String value) {
143 		this.uonT = value;
144 		return this;
145 	}
146 	public ComboInput<T> uonR(String value) {
147 		this.uonR = value;
148 		return this;
149 	}
150 	public ComboInput<T> urlEnc(String value) {
151 		this.urlEncoding = value;
152 		return this;
153 	}
154 	public ComboInput<T> urlEncT(String value) {
155 		this.urlEncodingT = value;
156 		return this;
157 	}
158 	public ComboInput<T> urlEncR(String value) {
159 		this.urlEncodingR = value;
160 		return this;
161 	}
162 	public ComboInput<T> msgPack(String value) {
163 		this.msgPack = value;
164 		return this;
165 	}
166 	public ComboInput<T> msgPackT(String value) {
167 		this.msgPackT = value;
168 		return this;
169 	}
170 	public ComboInput<T> rdfXml(String value) {
171 		this.rdfXml = value;
172 		return this;
173 	}
174 	public ComboInput<T> rdfXmlT(String value) {
175 		this.rdfXmlT = value;
176 		return this;
177 	}
178 	public ComboInput<T> rdfXmlR(String value) {
179 		this.rdfXmlR = value;
180 		return this;
181 	}
182 
183 	public ComboInput(
184 			String label,
185 			Type type,
186 			T in,
187 			String json,
188 			String jsonT,
189 			String jsonR,
190 			String xml,
191 			String xmlT,
192 			String xmlR,
193 			String xmlNs,
194 			String html,
195 			String htmlT,
196 			String htmlR,
197 			String uon,
198 			String uonT,
199 			String uonR,
200 			String urlEncoding,
201 			String urlEncodingT,
202 			String urlEncodingR,
203 			String msgPack,
204 			String msgPackT,
205 			String rdfXml,
206 			String rdfXmlT,
207 			String rdfXmlR
208 		) {
209 		this.label = label;
210 		this.type = type;
211 		this.in = () -> in;
212 		this.json = json;
213 		this.jsonT = jsonT;
214 		this.jsonR = jsonR;
215 		this.xml = xml;
216 		this.xmlT = xmlT;
217 		this.xmlR = xmlR;
218 		this.xmlNs = xmlNs;
219 		this.html = html;
220 		this.htmlT = htmlT;
221 		this.htmlR = htmlR;
222 		this.uon = uon;
223 		this.uonT = uonT;
224 		this.uonR = uonR;
225 		this.urlEncoding = urlEncoding;
226 		this.urlEncodingT = urlEncodingT;
227 		this.urlEncodingR = urlEncodingR;
228 		this.msgPack = msgPack;
229 		this.msgPackT = msgPackT;
230 		this.rdfXml = rdfXml;
231 		this.rdfXmlT = rdfXmlT;
232 		this.rdfXmlR = rdfXmlR;
233 	}
234 
235 	/**
236 	 * Checks to see if the test should be skipped.
237 	 */
238 	public boolean isTestSkipped(String testName) {
239 		return skipTest != null && skipTest.test(testName);
240 	}
241 
242 	public T convert(T t) {
243 		return convert == null ? t : convert.apply(t);
244 	}
245 
246 	/**
247 	 * Override this method if you want to do a post-parse verification on the object.
248 	 * <p>
249 	 * Note that a Function would be preferred here, but it's not available in Java 6.
250 	 *
251 	 * @param o The object returned by the parser.
252 	 */
253 	public void verify(T o, String testName) {
254 		for (Function<T,String> f : verify) {
255 			var s = f.apply(o);
256 			if (! Utils.isEmpty(s)) {
257 				throw new BasicAssertionError("Verification failed on test {0}/{1}: {2}", label, testName, s);
258 			}
259 		}
260 	}
261 }