/*
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.internal.soa.esb.services.routing.cbr.bom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.xpath.XPathExpressionException;
import org.jboss.internal.soa.esb.services.routing.cbr.DslHelper;
import org.jboss.internal.soa.esb.util.StreamUtils;
import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Test class to verify support of messages with content containing a Byte Order
* Mark (BOM).
*
* @author Martin Weiler
*
*/
public class DslHelperBomUnitTest {
/*
* message with xml content as String body
*/
private static Message strMsg;
/*
* message with xml content as Bytes body
*/
private static Message bytesMsg;
/*
* Tests the DslHelper with a message containing a String payload
*/
@Test
public void selectAsStringBomStringMsg()
throws UnsupportedEncodingException, XPathExpressionException {
assertEquals("2", DslHelper.selectAsString(strMsg, "Order/@orderId"));
}
/*
* Tests the DslHelper with a message containing a byte[] payload
*/
@Test
public void selectAsStringBomBytesMsg()
throws UnsupportedEncodingException, XPathExpressionException {
assertEquals("2", DslHelper.selectAsString(bytesMsg, "Order/@orderId"));
}
/*
* Helper class to create a byte[] containing an xml snippet, preceded by a
* BOM
*/
private static byte[] readBytes() throws IOException {
FileOutputStream fos = new FileOutputStream("bom.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
osw.write(0xFEFF); // write the byte order mark
osw.write("");
osw.write("");
osw.close();
FileInputStream fis = new FileInputStream("bom.txt");
byte[] msgBytes = StreamUtils.readStream(fis);
assertNotNull(msgBytes);
return msgBytes;
}
/*
* Creates a message with the BOM content as String payload
*/
@BeforeClass
public static void createStrMsg() throws IOException {
strMsg = MessageFactory.getInstance().getMessage();
String payloadString = new String(readBytes(), "utf-8");
strMsg.getBody().add(Body.DEFAULT_LOCATION, payloadString);
}
/*
* Creates a message with the BOM content as byte[] payload
*/
@BeforeClass
public static void createBytesMsg() throws IOException {
bytesMsg = MessageFactory.getInstance().getMessage();
bytesMsg.getBody().add(Body.DEFAULT_LOCATION, readBytes());
}
}