001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: *
024: * $Id: DataMessageUtilTest.java 3531 2006-12-08 09:28:11Z sji $
025: */
026: package com.bostechcorp.cbesb.runtime.component.util;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.UnsupportedEncodingException;
031:
032: import javax.jbi.messaging.MessageExchange;
033: import javax.jbi.messaging.NormalizedMessage;
034: import javax.xml.namespace.QName;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.TransformerException;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.dom.DOMSource;
040: import javax.xml.transform.stream.StreamResult;
041: import javax.xml.transform.stream.StreamSource;
042:
043: import junit.framework.TestCase;
044:
045: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
046: import org.apache.servicemix.jbi.messaging.InOnlyImpl;
047: import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
048:
049: import com.bostechcorp.cbesb.common.util.FileUtil;
050:
051: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.*;
052:
053: public class DataMessageUtilTest extends TestCase {
054:
055: protected static final String STRING_DATA = "This is a test StringSource record (non-xml).";
056: protected static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Foo><bar>test1</bar><bar>test2</bar><bar>test3</bar></Foo>";
057:
058: /**
059: * Creates a new NormalizedMessage, adds src as a record using DataMessageUtil.
060: * Then creates a second DataMessageUtil from the existing NormalizedMessage
061: * and retrieves the record and returns it as a Source.
062: * @param src
063: * @return
064: * @throws Exception
065: */
066: protected Source testAddGetRecord(Source src) throws Exception {
067: MessageExchange me = new InOnlyImpl("exchangeId");
068: me.setOperation(new QName("uri", "op"));
069: me.setProperty("myProp", "myValue");
070: NormalizedMessage msg = me.createMessage();
071:
072: //Create an instance of DataMessageUtil to wrap the NormalizedMessage
073: NormalizedMessageHandler dataMessage1 = new NormalizedMessageHandler(
074: msg);
075: //Add the Source as a record
076: dataMessage1.addRecord(src);
077: assertTrue(dataMessage1.getRecordCount() == 1);
078: dataMessage1.debugContents();
079:
080: //Get the NormalizedMessage back
081: msg = dataMessage1.generateMessageContent();
082: assertNotNull(((NormalizedMessageImpl) msg).getBody());
083:
084: //Create a new DataMessageUtil from this NormalizedMessage
085: NormalizedMessageHandler dataMessage2 = new NormalizedMessageHandler(
086: msg);
087: dataMessage2.debugContents();
088: assertTrue(dataMessage2.getRecordCount() == 1);
089:
090: Source src2 = dataMessage2.getRecordAtIndex(0);
091: assertNotNull(src2);
092:
093: return src2;
094: }
095:
096: public void testAddGetWithStringSource() throws Exception {
097: System.out.println("---StringSource---");
098: Source src = new StringSource(STRING_DATA);
099: Source result = testAddGetRecord(src);
100: assertTrue(result instanceof StringSource);
101: String resultStr = ((StringSource) result).getText();
102: assertTrue(resultStr.equals(STRING_DATA));
103:
104: }
105:
106: public void testAddGetWithByteArraySource() throws Exception {
107: System.out.println("---ByteArraySource---");
108: byte[] data = FileUtil
109: .readBytesFromFile("src/resources/bostech.gif");
110:
111: Source src = new ByteArraySource(data);
112: Source result = testAddGetRecord(src);
113: assertTrue(result instanceof ByteArraySource);
114: byte[] resultData = ((ByteArraySource) result).getBytes();
115: assertTrue(data.length == resultData.length);
116: for (int i = 0; i < data.length; i++) {
117: assertTrue(data[i] == resultData[i]);
118: }
119: }
120:
121: public void testAddGetWithStreamSource() throws Exception {
122: System.out.println("---StreamSource---");
123: Source src = new StreamSource(new ByteArrayInputStream(XML_DATA
124: .getBytes()));
125: Source result = testAddGetRecord(src);
126: assertTrue(result instanceof DOMSource);
127:
128: String resultStr = getStringFromSource(result);
129: assertTrue(resultStr.equals(XML_DATA));
130:
131: }
132:
133: public void testAddGetWithDomSource() throws Exception {
134: System.out.println("---DOMSource---");
135: Source src = new SourceTransformer()
136: .toDOMSource(new StringSource(XML_DATA));
137: Source result = testAddGetRecord(src);
138: assertTrue(result instanceof DOMSource);
139:
140: String resultStr = getStringFromSource(result);
141: assertTrue(resultStr.equals(XML_DATA));
142: }
143:
144: public void testAddGetWithSaxSource() throws Exception {
145: System.out.println("---SaxSource---");
146: Source src = new SourceTransformer()
147: .toSAXSource(new StringSource(XML_DATA));
148: Source result = testAddGetRecord(src);
149: assertTrue(result instanceof DOMSource);
150:
151: String resultStr = getStringFromSource(result);
152: assertTrue(resultStr.equals(XML_DATA));
153:
154: }
155:
156: protected String getStringFromSource(Source src) {
157: String result = null;
158: try {
159: TransformerFactory tFactory = TransformerFactory
160: .newInstance();
161: Transformer transformer = tFactory.newTransformer();
162: ByteArrayOutputStream baos = new ByteArrayOutputStream();
163: StreamResult resultStream = new StreamResult(baos);
164: transformer.transform(src, resultStream);
165: result = new String(baos.toByteArray(), "UTF-8");
166: } catch (TransformerException te) {
167: System.err.println("Error converting Source into String");
168: System.err.println(te.getMessage());
169: } catch (UnsupportedEncodingException e) {
170: System.err.println("Error converting Source into String");
171: System.err.println(e.getMessage());
172: }
173:
174: return result;
175: }
176: }
|