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.test.junit4;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.Expression;
021 import org.apache.camel.spi.Language;
022
023 /**
024 * A useful base class for testing the language plugins in Camel
025 */
026 public abstract class LanguageTestSupport extends ExchangeTestSupport {
027
028 protected abstract String getLanguageName();
029
030 /**
031 * Asserts that the given predicate expression evaluated on the current language and message
032 * exchange evaluates to true
033 */
034 protected void assertPredicate(String expression) {
035 assertPredicate(exchange, expression, true);
036 }
037
038 /**
039 * Asserts that the given predicate expression evaluated on the current language and message
040 * exchange evaluates to false
041 */
042 protected void assertPredicateFails(String expression) {
043 assertPredicate(exchange, expression, false);
044 }
045
046 /**
047 * Asserts that the given predicate expression evaluated on the current language and message
048 * exchange evaluates to the expected value
049 */
050 protected void assertPredicate(String expression, boolean expected) {
051 assertPredicate(exchange, expression, expected);
052 }
053
054 protected void assertPredicate(Exchange exchange, String expression, boolean expected) {
055 assertPredicate(getLanguageName(), expression, exchange, expected);
056 }
057
058
059 /**
060 * Asserts that this language expression evaluates to the given value on the given exchange
061 */
062 protected void assertExpression(Exchange exchange, String expressionText, Object expectedValue) {
063 assertExpression(exchange, getLanguageName(), expressionText, expectedValue);
064 }
065
066 /**
067 * Asserts that this language expression evaluates to the given value on the current exchange
068 */
069 protected void assertExpression(String expressionText, Object expectedValue) {
070 assertExpression(exchange, expressionText, expectedValue);
071 }
072
073 /**
074 * Asserts that the expression evaluates to one of the two given values
075 */
076 protected void assertExpression(String expressionText, String expectedValue, String orThisExpectedValue) {
077 Language language = assertResolveLanguage(getLanguageName());
078
079 Expression expression = language.createExpression(expressionText);
080 assertNotNull("No Expression could be created for text: " + expressionText + " language: " + language, expression);
081
082 Object value;
083 if (expectedValue != null) {
084 value = expression.evaluate(exchange, expectedValue.getClass());
085 } else {
086 value = expression.evaluate(exchange, Object.class);
087 }
088
089 log.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + value);
090
091 assertTrue("Expression: " + expression + " on Exchange: " + exchange,
092 expectedValue.equals(value) || orThisExpectedValue.equals(value));
093 }
094
095 }