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.servicemix.components.mps;
018:
019: import java.io.StringReader;
020:
021: import javax.jbi.JBIException;
022: import javax.jbi.messaging.NormalizedMessage;
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.transform.dom.DOMSource;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
030: import org.w3c.dom.Document;
031: import org.xml.sax.InputSource;
032:
033: /**
034: * Test cases for the PeoprtyValue type Objects
035: * @author rbuckland
036: *
037: */
038: public class PropertyValueTest extends TestCase {
039:
040: private final static String TEST_STRING = "PROP_TEST_STRING";
041: private final static String PROPNAME = "property1";
042:
043: /**
044: * helper method to return a new JBI NormalizedMessage
045: * when we need one
046: * @return
047: */
048: private NormalizedMessage getTestMessage() {
049: NormalizedMessage message = new NormalizedMessageImpl();
050: message.setProperty(PROPNAME, TEST_STRING);
051: String xml = "<this><is><some attr='1234'>xml123</some></is></this>";
052: try {
053: DocumentBuilder db;
054: db = DocumentBuilderFactory.newInstance()
055: .newDocumentBuilder();
056: Document dom = db.parse(new InputSource(new StringReader(
057: xml)));
058: message.setContent(new DOMSource(dom));
059: } catch (Exception e) {
060: fail(e.getLocalizedMessage());
061: }
062: return message;
063: }
064:
065: /**
066: * Test that a static string, does return a static string
067: *
068: */
069: public void testStaticString() {
070: PropertyValue pv = new StaticStringPropertyValue("someValueX");
071: try {
072: assertTrue(pv.getPropertyValue(getTestMessage()).equals(
073: "someValueX"));
074: } catch (JBIException e) {
075: fail(e.getLocalizedMessage());
076: }
077: }
078:
079: /**
080: * Test that a existing property copier works
081: *
082: */
083: public void testExistingCopier() {
084: PropertyValue pv = new ExistingPropertyCopier(PROPNAME);
085: try {
086: assertTrue(pv.getPropertyValue(getTestMessage()).equals(
087: TEST_STRING));
088: } catch (JBIException e) {
089: fail(e.getLocalizedMessage());
090: }
091: }
092:
093: /**
094: * Test that a existing property copier works
095: *
096: */
097: public void testXPathElementPropertyValue() {
098: PropertyValue pv = new XPathContentMessagePropertyValue(
099: "/this/is/some");
100: try {
101: assertTrue(pv.getPropertyValue(getTestMessage()).equals(
102: "xml123"));
103: } catch (JBIException e) {
104: fail(e.getLocalizedMessage());
105: }
106: }
107:
108: /**
109: * Test that a existing property copier works
110: *
111: */
112: public void testXPathAttrPropertyValue() {
113: PropertyValue pv = new XPathContentMessagePropertyValue(
114: "/this/is/some/@attr");
115: try {
116: assertTrue(pv.getPropertyValue(getTestMessage()).equals(
117: "1234"));
118: } catch (JBIException e) {
119: fail(e.getLocalizedMessage());
120: }
121: }
122:
123: /**
124: * Test that a existing property copier works
125: *
126: */
127: public void testXPathConcatPropertyValue() {
128: PropertyValue pv = new XPathContentMessagePropertyValue(
129: "concat('x','y')");
130: try {
131: assertTrue(pv.getPropertyValue(getTestMessage()).equals(
132: "xy"));
133: } catch (JBIException e) {
134: fail(e.getLocalizedMessage());
135: }
136: }
137:
138: }
|