KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > mps > PropertyValueTest


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.servicemix.components.mps;
18
19 import java.io.StringReader JavaDoc;
20
21 import javax.jbi.JBIException;
22 import javax.jbi.messaging.NormalizedMessage;
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.transform.dom.DOMSource JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32
33 /**
34  * Test cases for the PeoprtyValue type Objects
35  * @author rbuckland
36  *
37  */

38 public class PropertyValueTest extends TestCase {
39
40     private final static String JavaDoc TEST_STRING = "PROP_TEST_STRING";
41     private final static String JavaDoc PROPNAME = "property1";
42     /**
43      * helper method to return a new JBI NormalizedMessage
44      * when we need one
45      * @return
46      */

47     private NormalizedMessage getTestMessage() {
48         NormalizedMessage message = new NormalizedMessageImpl();
49         message.setProperty(PROPNAME,TEST_STRING);
50         String JavaDoc xml = "<this><is><some attr='1234'>xml123</some></is></this>";
51         try {
52             DocumentBuilder JavaDoc db;
53             db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
54             Document JavaDoc dom = db.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
55             message.setContent(new DOMSource JavaDoc(dom));
56         } catch (Exception JavaDoc e) {
57             fail(e.getLocalizedMessage());
58         }
59         return message;
60     }
61
62     /**
63      * Test that a static string, does return a static string
64      *
65      */

66     public void testStaticString() {
67         PropertyValue pv = new StaticStringPropertyValue("someValueX");
68         try {
69             assertTrue(pv.getPropertyValue(getTestMessage()).equals("someValueX"));
70         } catch (JBIException e) {
71             fail(e.getLocalizedMessage());
72         }
73     }
74
75     /**
76      * Test that a existing property copier works
77      *
78      */

79     public void testExistingCopier() {
80         PropertyValue pv = new ExistingPropertyCopier(PROPNAME);
81         try {
82             assertTrue(pv.getPropertyValue(getTestMessage()).equals(TEST_STRING));
83         } catch (JBIException e) {
84             fail(e.getLocalizedMessage());
85         }
86     }
87
88     /**
89      * Test that a existing property copier works
90      *
91      */

92     public void testXPathElementPropertyValue() {
93         PropertyValue pv = new XPathContentMessagePropertyValue("/this/is/some");
94         try {
95             assertTrue(pv.getPropertyValue(getTestMessage()).equals("xml123"));
96         } catch (JBIException e) {
97             fail(e.getLocalizedMessage());
98         }
99     }
100
101     /**
102      * Test that a existing property copier works
103      *
104      */

105     public void testXPathAttrPropertyValue() {
106         PropertyValue pv = new XPathContentMessagePropertyValue("/this/is/some/@attr");
107         try {
108             assertTrue(pv.getPropertyValue(getTestMessage()).equals("1234"));
109         } catch (JBIException e) {
110             fail(e.getLocalizedMessage());
111         }
112     }
113
114     /**
115      * Test that a existing property copier works
116      *
117      */

118     public void testXPathConcatPropertyValue() {
119         PropertyValue pv = new XPathContentMessagePropertyValue("concat('x','y')");
120         try {
121             assertTrue(pv.getPropertyValue(getTestMessage()).equals("xy"));
122         } catch (JBIException e) {
123             fail(e.getLocalizedMessage());
124         }
125     }
126
127 }
128
Popular Tags