KickJava   Java API By Example, From Geeks To Geeks.

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


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.components.mps.ConfigNotSupportedException;
32 import org.apache.servicemix.components.mps.PropertySet;
33 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
34 import org.w3c.dom.Document JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 public class PropertySetTest extends TestCase {
38
39     private final transient Log logger = LogFactory.getLog(getClass());
40     private final static String JavaDoc TEST_STRING = "PROP_TEST_STRING";
41     private final static String JavaDoc PROPNAME = "property1";
42     private final static String JavaDoc SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>";
43     private final static String JavaDoc SAMPLE_MSG_XML2 = "<this><is><some attr='1234'>xml123</some></is></this>";
44     
45     private final static String JavaDoc XML_EXISTING_PROP = "<existing-property/>";
46     private final static String JavaDoc XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>";
47     private final static String JavaDoc XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>";
48     private final static String JavaDoc XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>";
49     
50     private Document JavaDoc makeDocument(String JavaDoc xml) {
51         DocumentBuilder JavaDoc db;
52         try {
53             db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
54             return db.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
55         } catch (Exception JavaDoc e) {
56             fail(e.getLocalizedMessage());
57             return null;
58         }
59     }
60         
61     /**
62      * helper method to return a new JBI NormalizedMessage
63      * when we need one
64      * @return
65      */

66     private NormalizedMessage makeTestMessage(String JavaDoc xml) {
67         NormalizedMessage message = new NormalizedMessageImpl();
68         message.setProperty(PROPNAME,TEST_STRING);
69         if (xml == null) {
70             xml = SAMPLE_MSG_XML2;
71         }
72         try {
73             DocumentBuilder JavaDoc db;
74             db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
75             Document JavaDoc dom = db.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)));
76             message.setContent(new DOMSource JavaDoc(dom));
77         } catch (Exception JavaDoc e) {
78             fail(e.getLocalizedMessage());
79         }
80         return message;
81     }
82     
83     /**
84      * Test
85      * <property-set name="somename">
86      * <property name="newProperty">
87      * <existing-property name="property1"/>
88      * </property>
89      * </property-set>
90      *
91      */

92     public void testSimpleStaticValue() {
93         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
94                                 .append("<property name='testSimpleStaticValue.property'>")
95                                 .append("<existing-property name='property1'/>")
96                                 .append("</property>")
97                                 .append("</property-set>").toString();
98         Document JavaDoc propertySet = makeDocument(xmlConfig);
99         try {
100             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
101             NormalizedMessage inMessage = makeTestMessage(null);
102             NormalizedMessage outMessage = makeTestMessage(null);
103             ps.applyProperties(inMessage,outMessage);
104             
105             assertTrue(TEST_STRING.equals(outMessage.getProperty("testSimpleStaticValue.property")));
106         } catch (ConfigNotSupportedException e) {
107             fail(e.getLocalizedMessage());
108         } catch (JBIException e) {
109             fail(e.getLocalizedMessage());// TODO Auto-generated catch block
110
e.printStackTrace();
111         }
112     }
113     
114     /**
115      * Test
116      * <property-set name="somename">
117      * <property name="newProperty">
118      * <existing-property name="property1"/>
119      * </property>
120      * </property-set>
121      *
122      */

123     public void testSimpleExistingPropInvalidXPath() {
124         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
125                                 .append("<property name='testSimpleExistingPropInvalidXPath.property'>")
126                                 .append("<xpath-expression><![CDATA[/someexpath/statement]]></xpath-expression>")
127                                 .append("<existing-property name='property1'/>")
128                                 .append("</property>")
129                                 .append("</property-set>").toString();
130         Document JavaDoc propertySet = makeDocument(xmlConfig);
131         try {
132             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
133             NormalizedMessage inMessage = makeTestMessage(null);
134             NormalizedMessage outMessage = makeTestMessage(null);
135             ps.applyProperties(inMessage,outMessage);
136             
137             assertTrue(TEST_STRING.equals(outMessage.getProperty("testSimpleExistingPropInvalidXPath.property")));
138         } catch (ConfigNotSupportedException e) {
139             fail(e.getLocalizedMessage());
140         } catch (JBIException e) {
141             fail(e.getLocalizedMessage());// TODO Auto-generated catch block
142
e.printStackTrace();
143         }
144     }
145     
146     /**
147      * Test
148      * <property-set name="somename">
149      * <property name="newProperty">
150      * <existing-property name="property1"/>
151      * </property>
152      * </property-set>
153      *
154      */

155     public void testStaticValueDefaultWithBadXpath() {
156         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
157                                 .append("<property name='testStaticValueDefaultWithBadXpath.property'>")
158                                 .append("<xpath-expression><![CDATA[/someexpath/statement]]></xpath-expression>")
159                                 .append("<static-value>myvalue</static-value>")
160                                 .append("</property>")
161                                 .append("</property-set>").toString();
162         Document JavaDoc propertySet = makeDocument(xmlConfig);
163         try {
164             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
165             NormalizedMessage inMessage = makeTestMessage(null);
166             NormalizedMessage outMessage = makeTestMessage(null);
167             ps.applyProperties(inMessage,outMessage);
168             
169             assertTrue("myvalue".equals(outMessage.getProperty("testStaticValueDefaultWithBadXpath.property")));
170         } catch (ConfigNotSupportedException e) {
171             fail(e.getLocalizedMessage());
172         } catch (JBIException e) {
173             fail(e.getLocalizedMessage());// TODO Auto-generated catch block
174
e.printStackTrace();
175         }
176     }
177     /**
178      * Test
179      * <property-set name="somename">
180      * <property name="newProperty">
181      * <existing-property name="property1"/>
182      * </property>
183      * </property-set>
184      *
185      */

186     public void testSimpleValidXPath() {
187         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
188                                 .append("<property name='testSimpleValidXPath.property'>")
189                                 .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
190                                 .append("<existing-property name='property1'/>")
191                                 .append("</property>")
192                                 .append("</property-set>").toString();
193         Document JavaDoc propertySet = makeDocument(xmlConfig);
194         try {
195             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
196             NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
197             NormalizedMessage outMessage = makeTestMessage(null);
198             ps.applyProperties(inMessage,outMessage);
199             
200             assertTrue(outMessage.getProperty("testSimpleValidXPath.property").toString().equals("1234"));
201         } catch (ConfigNotSupportedException e) {
202             e.printStackTrace();
203             fail(e.getLocalizedMessage());
204         } catch (JBIException e) {
205             e.printStackTrace();
206             fail(e.getLocalizedMessage());
207         }
208     }
209     
210     
211     /**
212      * Test
213      * <property-set name="somename">
214      * <property name="newProperty">
215      * <existing-property name="property1"/>
216      * </property>
217      * </property-set>
218      *
219      */

220     public void testInvalidXPathSingle() {
221         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
222                                 .append("<property name='testInvalidXPathSingle.property'>")
223                                 .append("<xpath-expression><![CDATA[/NoNode]]></xpath-expression>")
224                                 .append("</property>")
225                                 .append("</property-set>").toString();
226         Document JavaDoc propertySet = makeDocument(xmlConfig);
227         try {
228             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
229             NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
230             NormalizedMessage outMessage = makeTestMessage(null);
231             ps.applyProperties(inMessage,outMessage);
232             
233             assertTrue("Node is not null",outMessage.getProperty("testInvalidXPathSingle.property") == null);
234         } catch (ConfigNotSupportedException e) {
235             e.printStackTrace();
236             fail(e.getLocalizedMessage());
237         } catch (JBIException e) {
238             e.printStackTrace();
239             fail(e.getLocalizedMessage());
240         }
241     }
242     
243     /**
244      * Test
245      * <property-set name="somename">
246      * <property name="newProperty">
247      * <existing-property name="property1"/>
248      * </property>
249      * </property-set>
250      *
251      */

252     public void testXPATH2MessagesOneHasOneHasnt() {
253         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
254                                 .append("<property name='testXPATH2MessagesOneHasOneHasnt.property'>")
255                                 .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
256                                 .append("<existing-property name='property1'/>")
257                                 .append("</property>")
258                                 .append("</property-set>").toString();
259         Document JavaDoc propertySet = makeDocument(xmlConfig);
260         try {
261             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
262             NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
263             // inMessage will have a property1=TEST_STRING
264

265             NormalizedMessage inMessage2 = makeTestMessage("<this><is><some>xml123</some></is></this>");
266             // inMessage2 will have a property1=TEST_STRING
267
NormalizedMessage outMessage = makeTestMessage(null);
268
269             ps.applyProperties(inMessage,outMessage);
270             assertTrue(outMessage.getProperty("testXPATH2MessagesOneHasOneHasnt.property").toString().equals("1234"));
271
272             ps.applyProperties(inMessage2,outMessage);
273             assertTrue(outMessage.getProperty("testXPATH2MessagesOneHasOneHasnt.property").toString().equals(TEST_STRING));
274
275         } catch (ConfigNotSupportedException e) {
276             e.printStackTrace();
277             fail(e.getLocalizedMessage());
278         } catch (JBIException e) {
279             e.printStackTrace();
280             fail(e.getLocalizedMessage());
281         }
282     }
283     
284     /**
285      * Test
286      * <property-set name="somename">
287      * <property name="newProperty">
288      * <existing-property name="property1"/>
289      * </property>
290      * </property-set>
291      *
292      */

293     public void testManyProperties() {
294         String JavaDoc xmlConfig = new StringBuffer JavaDoc("<property-set name='somename'>")
295                                 .append("<property name='newProperty1'>")
296                                    .append("<xpath-expression><![CDATA[/this//some/@attr]]></xpath-expression>")
297                                    .append("<existing-property name='property1'/>")
298                                 .append("</property>")
299                                 .append("<property name='newProperty2'>")
300                                    .append("<xpath-expression><![CDATA[/this//some/@attrNoExisting]]></xpath-expression>")
301                                    .append("<static-value><![CDATA[Some String I expect to be Set]]></static-value>")
302                                 .append("</property>")
303                                 .append("<property name='newProperty3'>")
304                                    .append("<existing-property name='missingProperty'/>")
305                                    .append("<existing-property name='missingProperty2'/>")
306                                    .append("<static-value><![CDATA[Some String I expect to be Set]]></static-value>")
307                                 .append("</property>")
308                                 .append("<property name='newProperty4'>")
309                                    .append("<xpath-expression><![CDATA[/this//some/nothere/@attr]]></xpath-expression>")
310                                    .append("<existing-property name='property1WhichIsNoteThere'/>")
311                                    .append("<existing-property name='testProperty'/>")
312                                 .append("</property>")
313                                 .append("<property name='newProperty5'>")
314                                    .append("<static-value><![CDATA[tagged]]></static-value>")
315                                    .append("<static-value><![CDATA[taggedNotSet]]></static-value>")
316                                 .append("</property>")
317                                 .append("<property name='newProperty6'>")
318                                    .append("<xpath-expression><![CDATA[/this//some/@attrNoExisting]]></xpath-expression>")
319                                    .append("</property>")
320                                 .append("</property-set>").toString();
321         Document JavaDoc propertySet = makeDocument(xmlConfig);
322         try {
323             PropertySet ps = new PropertySet("somename",propertySet.getDocumentElement());
324             NormalizedMessage inMessage = makeTestMessage("<this><is><some attr='1234'>xml123</some></is></this>");
325             inMessage.setProperty("testProperty","value123");
326             NormalizedMessage outMessage = makeTestMessage(null);
327             ps.applyProperties(inMessage,outMessage);
328             
329             assertTrue("newProperty1 was not set to expected value",outMessage.getProperty("newProperty1").toString().equals("1234"));
330             assertTrue("newProperty2 was not set to expected value",outMessage.getProperty("newProperty2").toString().equals("Some String I expect to be Set"));
331             assertTrue("newProperty3 was not set to expected value",outMessage.getProperty("newProperty3").toString().equals("Some String I expect to be Set"));
332             assertTrue("newProperty4 was not set to expected value",outMessage.getProperty("newProperty4").toString().equals("value123"));
333             assertTrue("newProperty5 was not set to expected value",outMessage.getProperty("newProperty5").toString().equals("tagged"));
334             assertTrue("newProperty5 was not set to expected value",outMessage.getProperty("newProperty6") == null);
335         } catch (ConfigNotSupportedException e) {
336             e.printStackTrace();
337             fail(e.getLocalizedMessage());
338         } catch (JBIException e) {
339             e.printStackTrace();
340             fail(e.getLocalizedMessage());
341         }
342     }
343     
344 }
345
Popular Tags