KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > tck > SpringTestSupport


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.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 JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.traversal.NodeIterator;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import javax.jbi.messaging.MessagingException;
32 import javax.jbi.messaging.NormalizedMessage;
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 import junit.framework.TestCase;
44
45 /**
46  * @version $Revision: 426415 $
47  */

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 JavaDoc {
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 JavaDoc {
64         if (context != null) {
65             log.info("Closing down the spring context");
66             context.destroy();
67         }
68     }
69
70     protected Object JavaDoc getBean(String JavaDoc name) {
71         Object JavaDoc 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     /**
85      * Performs an XPath expression and returns the Text content of the root node.
86      *
87      * @param node
88      * @param xpath
89      * @return
90      * @throws TransformerException
91      */

92     protected String JavaDoc textValueOfXPath(Node JavaDoc node, String JavaDoc xpath) throws TransformerException JavaDoc {
93         CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
94         NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath);
95         Node JavaDoc root = iterator.nextNode();
96         if (root instanceof Element JavaDoc) {
97             Element JavaDoc element = (Element JavaDoc) root;
98             if (element == null) {
99                 return "";
100             }
101             String JavaDoc 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 JavaDoc getSourceFromClassPath(String JavaDoc fileOnClassPath) {
112         InputStream JavaDoc stream = getClass().getResourceAsStream(fileOnClassPath);
113         assertNotNull("Could not find file: " + fileOnClassPath + " on the classpath", stream);
114         Source JavaDoc content = new StreamSource JavaDoc(stream);
115         return content;
116     }
117
118     protected void assertMessagesReceived(MessageList messageList, int messageCount) throws MessagingException, TransformerException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
119         messageList.assertMessagesReceived(messageCount);
120         List JavaDoc 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