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: 564900 $
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 void setUp() throws Exception {
064: transformer = new SourceTransformer();
065: context = createBeanFactory();
066: jbi = (SpringJBIContainer) context.getBean("jbi");
067: assertNotNull("JBI Container not found in spring!", jbi);
068: }
069:
070: protected void tearDown() throws Exception {
071: if (context != null) {
072: log.info("Closing down the spring context");
073: context.destroy();
074: }
075: }
076:
077: protected Object getBean(String name) {
078: Object answer = null;
079: if (jbi != null) {
080: answer = jbi.getBean(name);
081: }
082: if (answer == null) {
083: answer = context.getBean(name);
084: }
085: assertNotNull("Could not find object in Spring for key: "
086: + name, answer);
087: return answer;
088: }
089:
090: protected abstract AbstractXmlApplicationContext createBeanFactory();
091:
092: /**
093: * Performs an XPath expression and returns the Text content of the root
094: * node.
095: *
096: * @param node
097: * @param xpath
098: * @return
099: * @throws TransformerException
100: */
101: protected String textValueOfXPath(Node node, String xpath)
102: throws TransformerException {
103: CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
104: NodeIterator iterator = cachedXPathAPI.selectNodeIterator(node,
105: xpath);
106: Node root = iterator.nextNode();
107: if (root instanceof Element) {
108: Element element = (Element) root;
109: if (element == null) {
110: return "";
111: }
112: return DOMUtil.getElementText(element);
113: } else if (root != null) {
114: return root.getNodeValue();
115: } else {
116: return null;
117: }
118: }
119:
120: protected Source getSourceFromClassPath(String fileOnClassPath) {
121: InputStream stream = getClass().getResourceAsStream(
122: fileOnClassPath);
123: assertNotNull("Could not find file: " + fileOnClassPath
124: + " on the classpath", stream);
125: return new StreamSource(stream);
126: }
127:
128: protected void assertMessagesReceived(MessageList messageList,
129: int count) throws MessagingException, TransformerException,
130: ParserConfigurationException, IOException, SAXException {
131: messageList.assertMessagesReceived(count);
132: List list = messageList.getMessages();
133: int counter = 0;
134: for (Iterator iter = list.iterator(); iter.hasNext();) {
135: NormalizedMessage message = (NormalizedMessage) iter.next();
136: log.info("Message " + (counter++) + " is: " + message);
137: log.info(transformer.contentToString(message));
138: }
139: }
140:
141: protected void assertExchangeWorked(MessageExchange me)
142: 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: "
151: + new SourceTransformer().toString(me.getFault()
152: .getContent()));
153: }
154: }
155: }
|