KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
32 import org.w3c.dom.Document JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34
35 /**
36  * Test cases for the PropertyValueResolver class
37  *
38  * @author rbuckland
39  *
40  */

41 public class PropertyValueResolverTest extends TestCase {
42
43     private final transient Log logger = LogFactory.getLog(getClass());
44
45     private final static String JavaDoc TEST_STRING = "PROP_TEST_STRING";
46
47     private final static String JavaDoc PROPNAME = "property1";
48
49     private final static String JavaDoc SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>";
50
51     private final static String JavaDoc XML_EXISTING_PROP = "<existing-property/>";
52
53     private final static String JavaDoc XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>";
54
55     private final static String JavaDoc XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>";
56
57     private final static String JavaDoc XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>";
58
59     private Document JavaDoc makeDocument(String JavaDoc xml) {
60         DocumentBuilder JavaDoc db;
61         try {
62             db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
63             return db.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
64         } catch (Exception JavaDoc e) {
65             fail(e.getLocalizedMessage());
66             return null;
67         }
68     }
69
70     /**
71      * helper method to return a new JBI NormalizedMessage when we need one
72      *
73      * @return
74      */

75     private NormalizedMessage makeTestMessage(String JavaDoc xml) {
76         NormalizedMessage message = new NormalizedMessageImpl();
77         message.setProperty(PROPNAME, TEST_STRING);
78         if (xml == null) {
79             xml = "<this><is><some attr='1234'>xml123</some></is></this>";
80         }
81         try {
82             DocumentBuilder JavaDoc db;
83             db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
84             Document JavaDoc dom = db.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
85             message.setContent(new DOMSource JavaDoc(dom));
86         } catch (Exception JavaDoc e) {
87             fail(e.getLocalizedMessage());
88         }
89         return message;
90     }
91
92     /**
93      * Test that a static string, does return a static string
94      *
95      */

96     public void testStaticStringAsFirst() {
97         String JavaDoc propertyNode = (new StringBuffer JavaDoc("<").append(
98                 PropertyValueResolver.XML_ELEMENT_NAME).append(
99                 " name='newproperty'>").append(XML_STATICVAL).append("</")
100                 .append(PropertyValueResolver.XML_ELEMENT_NAME).append(">"))
101                 .toString();
102
103         NormalizedMessage in = makeTestMessage(SAMPLE_MSG_XML);
104         NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
105         Document JavaDoc doc = makeDocument(propertyNode);
106         try {
107             PropertyValueResolver pvr = new PropertyValueResolver(
108                     "newproperty", doc.getDocumentElement());
109             pvr.setProperty(in, out);
110             logger.debug("prop = " + out.getProperty("newproperty"));
111             assertTrue(out.getProperty("newproperty").toString().equals(
112                     "a value in the raw"));
113
114         } catch (JBIException e) {
115             fail(e.getLocalizedMessage());
116         } catch (ConfigNotSupportedException e) {
117             fail(e.getLocalizedMessage());
118         }
119     }
120
121     /**
122      * Test the xpath PVR
123      *
124      */

125     public void testXPath() {
126         String JavaDoc propertyNode = (new StringBuffer JavaDoc("<").append(
127                 PropertyValueResolver.XML_ELEMENT_NAME).append(
128                 " name='newproperty'>").append(XML_XPATH).append(XML_STATICVAL)
129                 .append("</").append(PropertyValueResolver.XML_ELEMENT_NAME)
130                 .append(">")).toString();
131
132         NormalizedMessage in = makeTestMessage(PropertyValueResolverTest.SAMPLE_MSG_XML);
133         NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
134         Document JavaDoc doc = makeDocument(propertyNode);
135         try {
136             PropertyValueResolver pvr = new PropertyValueResolver(
137                     "newproperty", doc.getDocumentElement());
138             pvr.setProperty(in, out);
139             logger
140                     .debug("xpath:@newproperty = "
141                             + out.getProperty("newproperty"));
142             assertTrue(out.getProperty("newproperty") != null);
143             assertTrue(out.getProperty("newproperty").toString().equals("me"));
144
145         } catch (JBIException e) {
146             fail(e.getLocalizedMessage());
147         } catch (ConfigNotSupportedException e) {
148             fail(e.getLocalizedMessage());
149         }
150     }
151
152     /**
153      * Test that a existing copier thingo
154      *
155      */

156     public void testExistCopier() {
157         String JavaDoc propertyNode = (new StringBuffer JavaDoc("<").append(
158                 PropertyValueResolver.XML_ELEMENT_NAME).append(
159                 " name='prop.xyz'>").append(XML_EXISTING_PROP).append(
160                 XML_STATICVAL).append("</").append(
161                 PropertyValueResolver.XML_ELEMENT_NAME).append(">")).toString();
162
163         NormalizedMessage in = makeTestMessage(PropertyValueResolverTest.SAMPLE_MSG_XML);
164         in.setProperty("prop.xyz", "ahahaha");
165         NormalizedMessage out = makeTestMessage(SAMPLE_MSG_XML);
166         Document JavaDoc doc = makeDocument(propertyNode);
167         try {
168             PropertyValueResolver pvr = new PropertyValueResolver("prop.xyz",
169                     doc.getDocumentElement());
170             pvr.setProperty(in, out);
171             logger.debug("prop = " + out.getProperty("prop.xyz"));
172             assertTrue(out.getProperty("prop.xyz") != null);
173             assertTrue(out.getProperty("prop.xyz").toString().equals("ahahaha"));
174
175         } catch (JBIException e) {
176             fail(e.getLocalizedMessage());
177         } catch (ConfigNotSupportedException e) {
178             fail(e.getLocalizedMessage());
179         }
180     }
181
182 }
183
Popular Tags