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.jbi.messaging.NormalizedMessageImpl; 32 import org.w3c.dom.Document ; 33 import org.xml.sax.InputSource ; 34 35 41 public class PropertyValueResolverTest extends TestCase { 42 43 private final transient Log logger = LogFactory.getLog(getClass()); 44 45 private final static String TEST_STRING = "PROP_TEST_STRING"; 46 47 private final static String PROPNAME = "property1"; 48 49 private final static String SAMPLE_MSG_XML = "<sample><get x='911'>me</get></sample>"; 50 51 private final static String XML_EXISTING_PROP = "<existing-property/>"; 52 53 private final static String XML_EXISTING_PROP_OTHER_PROP = "<existing-property name=\"newname\"/>"; 54 55 private final static String XML_STATICVAL = "<static-value><![CDATA[a value in the raw]]></static-value>"; 56 57 private final static String XML_XPATH = "<xpath-expression><![CDATA[//get[@x='911']]]></xpath-expression>"; 58 59 private Document makeDocument(String xml) { 60 DocumentBuilder db; 61 try { 62 db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 63 return db.parse(new InputSource (new StringReader (xml))); 64 } catch (Exception e) { 65 fail(e.getLocalizedMessage()); 66 return null; 67 } 68 } 69 70 75 private NormalizedMessage makeTestMessage(String 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 db; 83 db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 84 Document dom = db.parse(new InputSource (new StringReader (xml))); 85 message.setContent(new DOMSource (dom)); 86 } catch (Exception e) { 87 fail(e.getLocalizedMessage()); 88 } 89 return message; 90 } 91 92 96 public void testStaticStringAsFirst() { 97 String propertyNode = (new StringBuffer ("<").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 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 125 public void testXPath() { 126 String propertyNode = (new StringBuffer ("<").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 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 156 public void testExistCopier() { 157 String propertyNode = (new StringBuffer ("<").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 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 |