001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.testng;
018
019 import java.io.File;
020 import java.util.Collection;
021 import java.util.List;
022 import java.util.Locale;
023
024 import org.apache.camel.CamelContext;
025 import org.apache.camel.Channel;
026 import org.apache.camel.Endpoint;
027 import org.apache.camel.Exchange;
028 import org.apache.camel.Expression;
029 import org.apache.camel.InvalidPayloadException;
030 import org.apache.camel.Message;
031 import org.apache.camel.Predicate;
032 import org.apache.camel.Processor;
033 import org.apache.camel.Route;
034 import org.apache.camel.builder.Builder;
035 import org.apache.camel.builder.RouteBuilder;
036 import org.apache.camel.builder.ValueBuilder;
037 import org.apache.camel.impl.DefaultCamelContext;
038 import org.apache.camel.impl.DefaultExchange;
039 import org.apache.camel.processor.DelegateProcessor;
040 import org.apache.camel.util.ExchangeHelper;
041 import org.apache.camel.util.PredicateAssertHelper;
042 import org.slf4j.Logger;
043 import org.slf4j.LoggerFactory;
044 import org.testng.Assert;
045
046 /**
047 * A bunch of useful testing methods
048 *
049 * @version $Revision$
050 */
051 public abstract class TestSupport extends Assert {
052 private static final Logger LOG = LoggerFactory.getLogger(TestSupport.class);
053 protected transient Logger log = LoggerFactory.getLogger(getClass());
054
055 // Builder methods for expressions used when testing
056 // -------------------------------------------------------------------------
057
058 /**
059 * Returns a value builder for the given header
060 */
061 public static ValueBuilder header(String name) {
062 return Builder.header(name);
063 }
064
065 /**
066 * Returns a value builder for the given property
067 */
068 public static ValueBuilder property(String name) {
069 return Builder.property(name);
070 }
071
072 /**
073 * Returns a predicate and value builder for the inbound body on an exchange
074 */
075 public static ValueBuilder body() {
076 return Builder.body();
077 }
078
079 /**
080 * Returns a predicate and value builder for the inbound message body as a
081 * specific type
082 */
083 public static <T> ValueBuilder bodyAs(Class<T> type) {
084 return Builder.bodyAs(type);
085 }
086
087 /**
088 * Returns a predicate and value builder for the outbound body on an
089 * exchange
090 */
091 public static ValueBuilder outBody() {
092 return Builder.outBody();
093 }
094
095 /**
096 * Returns a predicate and value builder for the outbound message body as a
097 * specific type
098 */
099 public static <T> ValueBuilder outBodyAs(Class<T> type) {
100 return Builder.outBodyAs(type);
101 }
102
103 /**
104 * Returns a predicate and value builder for the fault body on an
105 * exchange
106 */
107 public static ValueBuilder faultBody() {
108 return Builder.faultBody();
109 }
110
111 /**
112 * Returns a predicate and value builder for the fault message body as a
113 * specific type
114 */
115 public static <T> ValueBuilder faultBodyAs(Class<T> type) {
116 return Builder.faultBodyAs(type);
117 }
118
119 /**
120 * Returns a value builder for the given system property
121 */
122 public static ValueBuilder systemProperty(String name) {
123 return Builder.systemProperty(name);
124 }
125
126 /**
127 * Returns a value builder for the given system property
128 */
129 public static ValueBuilder systemProperty(String name, String defaultValue) {
130 return Builder.systemProperty(name, defaultValue);
131 }
132
133 // Assertions
134 // -----------------------------------------------------------------------
135
136 public static <T> T assertIsInstanceOf(Class<T> expectedType, Object value) {
137 assertNotNull(value, "Expected an instance of type: " + expectedType.getName() + " but was null");
138 assertTrue(expectedType.isInstance(value), "object should be a " + expectedType.getName()
139 + " but was: " + value + " with type: " + value.getClass().getName());
140 return expectedType.cast(value);
141 }
142
143 public static void assertEndpointUri(Endpoint endpoint, String uri) {
144 assertNotNull(endpoint, "Endpoint is null when expecting endpoint for: " + uri);
145 assertEquals(endpoint.getEndpointUri(), uri, "Endoint uri for: " + endpoint);
146 }
147
148 /**
149 * Asserts the In message on the exchange contains the expected value
150 */
151 public static Object assertInMessageHeader(Exchange exchange, String name, Object expected) {
152 return assertMessageHeader(exchange.getIn(), name, expected);
153 }
154
155 /**
156 * Asserts the Out message on the exchange contains the expected value
157 */
158 public static Object assertOutMessageHeader(Exchange exchange, String name, Object expected) {
159 return assertMessageHeader(exchange.getOut(), name, expected);
160 }
161
162 /**
163 * Asserts that the given exchange has an OUT message of the given body value
164 *
165 * @param exchange the exchange which should have an OUT message
166 * @param expected the expected value of the OUT message
167 * @throws InvalidPayloadException is thrown if the payload is not the expected class type
168 */
169 public static void assertInMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException {
170 assertNotNull(exchange, "Should have a response exchange!");
171
172 Object actual;
173 if (expected == null) {
174 actual = ExchangeHelper.getMandatoryInBody(exchange);
175 assertEquals(actual, expected, "in body of: " + exchange);
176 } else {
177 actual = ExchangeHelper.getMandatoryInBody(exchange, expected.getClass());
178 }
179 assertEquals(actual, expected, "in body of: " + exchange);
180
181 LOG.debug("Received response: " + exchange + " with in: " + exchange.getIn());
182 }
183
184 /**
185 * Asserts that the given exchange has an OUT message of the given body value
186 *
187 * @param exchange the exchange which should have an OUT message
188 * @param expected the expected value of the OUT message
189 * @throws InvalidPayloadException is thrown if the payload is not the expected class type
190 */
191 public static void assertOutMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException {
192 assertNotNull(exchange, "Should have a response exchange!");
193
194 Object actual;
195 if (expected == null) {
196 actual = ExchangeHelper.getMandatoryOutBody(exchange);
197 assertEquals(actual, expected, "output body of: " + exchange);
198 } else {
199 actual = ExchangeHelper.getMandatoryOutBody(exchange, expected.getClass());
200 }
201 assertEquals(actual, expected, "output body of: " + exchange);
202
203 LOG.debug("Received response: " + exchange + " with out: " + exchange.getOut());
204 }
205
206 public static Object assertMessageHeader(Message message, String name, Object expected) {
207 Object value = message.getHeader(name);
208 assertEquals(value, expected, "Header: " + name + " on Message: " + message);
209 return value;
210 }
211
212 /**
213 * Asserts that the given expression when evaluated returns the given answer
214 */
215 public static Object assertExpression(Expression expression, Exchange exchange, Object expected) {
216 Object value;
217 if (expected != null) {
218 value = expression.evaluate(exchange, expected.getClass());
219 } else {
220 value = expression.evaluate(exchange, Object.class);
221 }
222
223 LOG.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + value);
224
225 assertEquals(value, expected, "Expression: " + expression + " on Exchange: " + exchange);
226 return value;
227 }
228
229 /**
230 * Asserts that the predicate returns the expected value on the exchange
231 */
232 public static void assertPredicateMatches(Predicate predicate, Exchange exchange) {
233 assertPredicate(predicate, exchange, true);
234 }
235
236 /**
237 * Asserts that the predicate returns the expected value on the exchange
238 */
239 public static void assertPredicateDoesNotMatch(Predicate predicate, Exchange exchange) {
240 try {
241 PredicateAssertHelper.assertMatches(predicate, "Predicate should match: ", exchange);
242 } catch (AssertionError e) {
243 LOG.debug("Caught expected assertion error: " + e);
244 }
245 assertPredicate(predicate, exchange, false);
246 }
247
248 /**
249 * Asserts that the predicate returns the expected value on the exchange
250 */
251 public static boolean assertPredicate(final Predicate predicate, Exchange exchange, boolean expected) {
252 if (expected) {
253 PredicateAssertHelper.assertMatches(predicate, "Predicate failed: ", exchange);
254 }
255 boolean value = predicate.matches(exchange);
256
257 LOG.debug("Evaluated predicate: " + predicate + " on exchange: " + exchange + " result: " + value);
258
259 assertEquals(value, expected, "Predicate: " + predicate + " on Exchange: " + exchange);
260 return value;
261 }
262
263 /**
264 * Resolves an endpoint and asserts that it is found
265 */
266 public static Endpoint resolveMandatoryEndpoint(CamelContext context, String uri) {
267 Endpoint endpoint = context.getEndpoint(uri);
268
269 assertNotNull(endpoint, "No endpoint found for URI: " + uri);
270
271 return endpoint;
272 }
273
274 /**
275 * Resolves an endpoint and asserts that it is found
276 */
277 public static <T extends Endpoint> T resolveMandatoryEndpoint(CamelContext context, String uri,
278 Class<T> endpointType) {
279 T endpoint = context.getEndpoint(uri, endpointType);
280
281 assertNotNull(endpoint, "No endpoint found for URI: " + uri);
282
283 return endpoint;
284 }
285
286 /**
287 * Creates an exchange with the given body
288 */
289 protected Exchange createExchangeWithBody(CamelContext camelContext, Object body) {
290 Exchange exchange = new DefaultExchange(camelContext);
291 Message message = exchange.getIn();
292 message.setHeader("testClass", getClass().getName());
293 message.setBody(body);
294 return exchange;
295 }
296
297 public static <T> T assertOneElement(List<T> list) {
298 assertEquals(list.size(), 1, "Size of list should be 1: " + list);
299 return list.get(0);
300 }
301
302 /**
303 * Asserts that a list is of the given size
304 */
305 public static <T> List<T> assertListSize(List<T> list, int size) {
306 return assertListSize("List", list, size);
307 }
308
309 /**
310 * Asserts that a list is of the given size
311 */
312 public static <T> List<T> assertListSize(String message, List<T> list, int size) {
313 assertEquals(list.size(), size, message + " should be of size: " + size + " but is: " + list);
314 return list;
315 }
316
317 /**
318 * Asserts that a list is of the given size
319 */
320 public static <T> Collection<T> assertCollectionSize(Collection<T> list, int size) {
321 return assertCollectionSize("List", list, size);
322 }
323
324 /**
325 * Asserts that a list is of the given size
326 */
327 public static <T> Collection<T> assertCollectionSize(String message, Collection<T> list, int size) {
328 assertEquals(list.size(), size, message + " should be of size: " + size + " but is: " + list);
329 return list;
330 }
331
332 /**
333 * A helper method to create a list of Route objects for a given route builder
334 */
335 public static List<Route> getRouteList(RouteBuilder builder) throws Exception {
336 CamelContext context = new DefaultCamelContext();
337 context.addRoutes(builder);
338 context.start();
339 List<Route> answer = context.getRoutes();
340 context.stop();
341 return answer;
342 }
343
344 /**
345 * Asserts that the text contains the given string
346 *
347 * @param text the text to compare
348 * @param containedText the text which must be contained inside the other text parameter
349 */
350 public static void assertStringContains(String text, String containedText) {
351 assertNotNull(text, "Text should not be null!");
352 assertTrue(text.contains(containedText), "Text: " + text + " does not contain: " + containedText);
353 }
354
355 /**
356 * If a processor is wrapped with a bunch of DelegateProcessor or DelegateAsyncProcessor objects
357 * this call will drill through them and return the wrapped Processor.
358 */
359 public static Processor unwrap(Processor processor) {
360 while (true) {
361 if (processor instanceof DelegateProcessor) {
362 processor = ((DelegateProcessor)processor).getProcessor();
363 } else {
364 return processor;
365 }
366 }
367 }
368
369 /**
370 * If a processor is wrapped with a bunch of DelegateProcessor or DelegateAsyncProcessor objects
371 * this call will drill through them and return the Channel.
372 * <p/>
373 * Returns null if no channel is found.
374 */
375 public static Channel unwrapChannel(Processor processor) {
376 while (true) {
377 if (processor instanceof Channel) {
378 return (Channel) processor;
379 } else if (processor instanceof DelegateProcessor) {
380 processor = ((DelegateProcessor)processor).getProcessor();
381 } else {
382 return null;
383 }
384 }
385 }
386
387 /**
388 * Recursively delete a directory, useful to zapping test data
389 *
390 * @param file the directory to be deleted
391 */
392 public static void deleteDirectory(String file) {
393 deleteDirectory(new File(file));
394 }
395
396 /**
397 * Recursively delete a directory, useful to zapping test data
398 *
399 * @param file the directory to be deleted
400 */
401 public static void deleteDirectory(File file) {
402 if (file.isDirectory()) {
403 File[] files = file.listFiles();
404 for (int i = 0; i < files.length; i++) {
405 deleteDirectory(files[i]);
406 }
407 }
408 file.delete();
409 }
410
411 /**
412 * create the directory
413 *
414 * @param file the directory to be created
415 */
416 public static void createDirectory(String file) {
417 File dir = new File(file);
418 dir.mkdirs();
419 }
420
421 /**
422 * To be used for folder/directory comparison that works across different platforms such
423 * as Window, Mac and Linux.
424 */
425 public static void assertDirectoryEquals(String expected, String actual) {
426 assertDirectoryEquals(null, expected, actual);
427 }
428
429 /**
430 * To be used for folder/directory comparison that works across different platforms such
431 * as Window, Mac and Linux.
432 */
433 public static void assertDirectoryEquals(String message, String expected, String actual) {
434 // must use single / as path separators
435 String expectedPath = expected.replace('\\', '/');
436 String actualPath = actual.replace('\\', '/');
437
438 if (message != null) {
439 assertEquals(actualPath, expectedPath, message);
440 } else {
441 assertEquals(actualPath, expectedPath);
442 }
443 }
444
445 /**
446 * To be used to check is a file is found in the file system
447 */
448 public static void assertFileExists(String filename) {
449 File file = new File(filename).getAbsoluteFile();
450 assertTrue(file.exists(), "File " + filename + " should exist");
451 }
452
453 /**
454 * Is this OS the given platform.
455 * <p/>
456 * Uses <tt>os.name</tt> from the system properties to determine the OS.
457 *
458 * @param platform such as Windows
459 * @return <tt>true</tt> if its that platform.
460 */
461 public static boolean isPlatform(String platform) {
462 String osName = System.getProperty("os.name").toLowerCase(Locale.US);
463 return osName.indexOf(platform.toLowerCase(Locale.US)) > -1;
464 }
465
466 /**
467 * Is this Java by the given vendor.
468 * <p/>
469 * Uses <tt>java.vendor</tt> from the system properties to determine the vendor.
470 *
471 * @param vendor such as IBM
472 * @return <tt>true</tt> if its that vendor.
473 */
474 public static boolean isJavaVendor(String vendor) {
475 String javaVendor = System.getProperty("java.vendor").toLowerCase(Locale.US);
476 return javaVendor.indexOf(vendor.toLowerCase(Locale.US)) > -1;
477 }
478
479 /**
480 * Is this Java 1.5
481 *
482 * @return <tt>true</tt> if its Java 1.5, <tt>false</tt> if its not (for example Java 1.6 or better)
483 * @deprecated will be removed in the near future as Camel now requires JDK1.6+
484 */
485 @Deprecated
486 public static boolean isJava15() {
487 String javaVersion = System.getProperty("java.version").toLowerCase(Locale.US);
488 return javaVersion.startsWith("1.5");
489 }
490
491 public String getTestMethodName() {
492 return "";
493 }
494
495 }