1 17 package org.apache.servicemix.components.mps; 18 19 import java.io.StringReader ; 20 21 import javax.jbi.JBIException; 22 import javax.jbi.messaging.NormalizedMessage; 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.transform.dom.DOMSource ; 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 ; 35 import org.xml.sax.InputSource ; 36 37 public class PropertySetTest extends TestCase { 38 39 private final transient Log logger = LogFactory.getLog(getClass()); 40 private final static String TEST_STRING = "PROP_TEST_STRING"; 41 private final static String PROPNAME = "property1"; 42 private final static String SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>"; 43 private final static String SAMPLE_MSG_XML2 = "<this><is><some attr='1234'>xml123</some></is></this>"; 44 45 private final static String XML_EXISTING_PROP = "<existing-property/>"; 46 private final static String XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>"; 47 private final static String XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>"; 48 private final static String XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>"; 49 50 private Document makeDocument(String xml) { 51 DocumentBuilder db; 52 try { 53 db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 54 return db.parse(new InputSource (new StringReader (xml))); 55 } catch (Exception e) { 56 fail(e.getLocalizedMessage()); 57 return null; 58 } 59 } 60 61 66 private NormalizedMessage makeTestMessage(String 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 db; 74 db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 75 Document dom = db.parse(new InputSource (new StringReader (xml))); 76 message.setContent(new DOMSource (dom)); 77 } catch (Exception e) { 78 fail(e.getLocalizedMessage()); 79 } 80 return message; 81 } 82 83 92 public void testSimpleStaticValue() { 93 String xmlConfig = new StringBuffer ("<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 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()); e.printStackTrace(); 111 } 112 } 113 114 123 public void testSimpleExistingPropInvalidXPath() { 124 String xmlConfig = new StringBuffer ("<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 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()); e.printStackTrace(); 143 } 144 } 145 146 155 public void testStaticValueDefaultWithBadXpath() { 156 String xmlConfig = new StringBuffer ("<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 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()); e.printStackTrace(); 175 } 176 } 177 186 public void testSimpleValidXPath() { 187 String xmlConfig = new StringBuffer ("<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 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 220 public void testInvalidXPathSingle() { 221 String xmlConfig = new StringBuffer ("<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 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 252 public void testXPATH2MessagesOneHasOneHasnt() { 253 String xmlConfig = new StringBuffer ("<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 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 265 NormalizedMessage inMessage2 = makeTestMessage("<this><is><some>xml123</some></is></this>"); 266 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 293 public void testManyProperties() { 294 String xmlConfig = new StringBuffer ("<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 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 |