1 17 package org.apache.servicemix.tck; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.container.SpringJBIContainer; 22 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 23 import org.apache.servicemix.jbi.util.DOMUtil; 24 import org.apache.xpath.CachedXPathAPI; 25 import org.springframework.context.support.AbstractXmlApplicationContext; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.traversal.NodeIterator; 29 import org.xml.sax.SAXException ; 30 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.stream.StreamSource ; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 import junit.framework.TestCase; 44 45 48 public abstract class SpringTestSupport extends TestCase { 49 protected transient Log log = LogFactory.getLog(getClass()); 50 51 protected AbstractXmlApplicationContext context; 52 protected SourceTransformer transformer; 53 protected int messageCount = 20; 54 protected SpringJBIContainer jbi; 55 56 protected void setUp() throws Exception { 57 transformer = new SourceTransformer(); 58 context = createBeanFactory(); 59 jbi = (SpringJBIContainer) context.getBean("jbi"); 60 assertNotNull("JBI Container not found in spring!", jbi); 61 } 62 63 protected void tearDown() throws Exception { 64 if (context != null) { 65 log.info("Closing down the spring context"); 66 context.destroy(); 67 } 68 } 69 70 protected Object getBean(String name) { 71 Object answer = null; 72 if (jbi != null) { 73 answer = jbi.getBean(name); 74 } 75 if (answer == null) { 76 answer = context.getBean(name); 77 } 78 assertNotNull("Could not find object in Spring for key: " + name, answer); 79 return answer; 80 } 81 82 protected abstract AbstractXmlApplicationContext createBeanFactory(); 83 84 92 protected String textValueOfXPath(Node node, String xpath) throws TransformerException { 93 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI(); 94 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath); 95 Node root = iterator.nextNode(); 96 if (root instanceof Element ) { 97 Element element = (Element ) root; 98 if (element == null) { 99 return ""; 100 } 101 String text = DOMUtil.getElementText(element); 102 return text; 103 } 104 else if (root != null) { 105 return root.getNodeValue(); 106 } else { 107 return null; 108 } 109 } 110 111 protected Source getSourceFromClassPath(String fileOnClassPath) { 112 InputStream stream = getClass().getResourceAsStream(fileOnClassPath); 113 assertNotNull("Could not find file: " + fileOnClassPath + " on the classpath", stream); 114 Source content = new StreamSource (stream); 115 return content; 116 } 117 118 protected void assertMessagesReceived(MessageList messageList, int messageCount) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 119 messageList.assertMessagesReceived(messageCount); 120 List list = messageList.getMessages(); 121 int counter = 0; 122 for (Iterator iter = list.iterator(); iter.hasNext();) { 123 NormalizedMessage message = (NormalizedMessage) iter.next(); 124 log.info("Message " + (counter++) + " is: " + message); 125 log.info(transformer.contentToString(message)); 126 } 127 } 128 } 129 | Popular Tags |