001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.tck;
018
019 import java.io.IOException;
020 import java.io.InputStream;
021 import java.util.Iterator;
022 import java.util.List;
023
024 import javax.jbi.messaging.ExchangeStatus;
025 import javax.jbi.messaging.MessageExchange;
026 import javax.jbi.messaging.MessagingException;
027 import javax.jbi.messaging.NormalizedMessage;
028 import javax.xml.parsers.ParserConfigurationException;
029 import javax.xml.transform.Source;
030 import javax.xml.transform.TransformerException;
031 import javax.xml.transform.stream.StreamSource;
032
033 import org.w3c.dom.Element;
034 import org.w3c.dom.Node;
035 import org.w3c.dom.traversal.NodeIterator;
036
037 import org.xml.sax.SAXException;
038
039 import junit.framework.TestCase;
040
041 import org.apache.commons.logging.Log;
042 import org.apache.commons.logging.LogFactory;
043 import org.apache.servicemix.jbi.container.SpringJBIContainer;
044 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
045 import org.apache.servicemix.jbi.util.DOMUtil;
046 import org.apache.xpath.CachedXPathAPI;
047 import org.springframework.context.support.AbstractXmlApplicationContext;
048
049 /**
050 * @version $Revision: 678656 $
051 */
052 public abstract class SpringTestSupport extends TestCase {
053 protected transient Log log = LogFactory.getLog(getClass());
054
055 protected AbstractXmlApplicationContext context;
056
057 protected SourceTransformer transformer;
058
059 protected int messageCount = 20;
060
061 protected SpringJBIContainer jbi;
062
063 protected SpringTestSupport(String name) {
064 super(name);
065 }
066
067 protected SpringTestSupport() {
068 }
069
070 protected void setUp() throws Exception {
071 transformer = new SourceTransformer();
072 context = createBeanFactory();
073 jbi = (SpringJBIContainer) context.getBean("jbi");
074 assertNotNull("JBI Container not found in spring!", jbi);
075 }
076
077 protected void tearDown() throws Exception {
078 if (context != null) {
079 log.info("Closing down the spring context");
080 context.destroy();
081 }
082 }
083
084 protected Object getBean(String name) {
085 Object answer = null;
086 if (jbi != null) {
087 answer = jbi.getBean(name);
088 }
089 if (answer == null) {
090 answer = context.getBean(name);
091 }
092 assertNotNull("Could not find object in Spring for key: " + name, answer);
093 return answer;
094 }
095
096 protected abstract AbstractXmlApplicationContext createBeanFactory();
097
098 /**
099 * Performs an XPath expression and returns the Text content of the root
100 * node.
101 *
102 * @param node
103 * @param xpath
104 * @return
105 * @throws TransformerException
106 */
107 protected String textValueOfXPath(Node node, String xpath) throws TransformerException {
108 CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
109 NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node, xpath);
110 Node root = iterator.nextNode();
111 if (root instanceof Element) {
112 Element element = (Element) root;
113 if (element == null) {
114 return "";
115 }
116 return DOMUtil.getElementText(element);
117 } else if (root != null) {
118 return root.getNodeValue();
119 } else {
120 return null;
121 }
122 }
123
124 protected Source getSourceFromClassPath(String fileOnClassPath) {
125 InputStream stream = getClass().getResourceAsStream(fileOnClassPath);
126 assertNotNull("Could not find file: " + fileOnClassPath + " on the classpath", stream);
127 return new StreamSource(stream);
128 }
129
130 protected void assertMessagesReceived(MessageList messageList, int count) throws MessagingException, TransformerException,
131 ParserConfigurationException, IOException, SAXException {
132 messageList.assertMessagesReceived(count);
133 List list = messageList.getMessages();
134 int counter = 0;
135 for (Iterator iter = list.iterator(); iter.hasNext();) {
136 NormalizedMessage message = (NormalizedMessage) iter.next();
137 log.info("Message " + (counter++) + " is: " + message);
138 log.info(transformer.contentToString(message));
139 }
140 }
141
142 protected void assertExchangeWorked(MessageExchange me) throws Exception {
143 if (me.getStatus() == ExchangeStatus.ERROR) {
144 if (me.getError() != null) {
145 throw me.getError();
146 } else {
147 fail("Received ERROR status");
148 }
149 } else if (me.getFault() != null) {
150 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
151 }
152 }
153 }