/*
* JBoss, Home of Professional Open Source
* Copyright 2005, 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.serial.invocationtest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import junit.framework.TestCase;
import org.jboss.serial.DataSerializationTest;
import org.jboss.serial.data.NonSerializableTest;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;
import org.jboss.serial.util.StringUtilBuffer;
public class TestInvocationTestCase extends DataSerializationTest
{
public TestInvocationTestCase()
{
//super("TestWithBigDecimal.class");
super();
}
private static final int MAX_LOOP = 1000;
private static final int START_LOOP = 100;
private static final boolean SAVE_FILE = System.getProperties()
.getProperty("SizeBenchmarkTestCase.saveFiles") != null;
static ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
public void executTest(Object dataObject) throws Throwable
{
if (dataObject instanceof NonSerializableTest)
{
return;
}
testJava(dataObject);
testJBoss(dataObject);
}
public void testJava(Object dataObject) throws Throwable {
Object invocationObject = TestInvocation.createTestInstance("java",dataObject);
System.out.println();
System.out.println("Testing " + dataObject.getClass().getName()+ " on java");
try {
doTestJavaSerialization(invocationObject);
} catch (Throwable e) {
e.printStackTrace(System.out);
throw new Exception("Failed at " + dataObject.getClass().getName(),
e);
}
}
public void testJBoss(Object dataObject) throws Throwable {
Object invocationObject = TestInvocation.createTestInstance("jboss",dataObject);
System.out.println();
System.out.println("Testing " + dataObject.getClass().getName()+ " on jboss");
try {
doTestJBossSerialization(invocationObject);
} catch (Throwable e) {
e.printStackTrace(System.out);
// throw new Exception("Failed at " +
// dataObject.getClass().getName(),e);
}
}
public void doTestJBossSerialization(Object myTest) throws Exception {
StringUtilBuffer buffer = new StringUtilBuffer(10 * 1024, 10 * 1024);
long startTime=0;
byte[] byteArray=null;
for (int exec = 0; exec < MAX_LOOP; exec++) {
byteOut.reset();
if (exec==START_LOOP)
{
startTime=System.currentTimeMillis();
}
JBossObjectOutputStream out = new JBossObjectOutputStream(byteOut,
buffer);
out.writeObject(myTest);
out.flush();
byteArray = byteOut.toByteArray();
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteArray);
JBossObjectInputStream input = new JBossObjectInputStream(
byteInput, buffer);
Object value = input.readObject();
assertNotSame(myTest, value);
assertSame(myTest.getClass(), value.getClass());
assertEquals(myTest, value);
}
System.out.println("Time on JBossSerialization for invocation:" + (System.currentTimeMillis() - startTime));
saveFile("/tmp/jboss-test-" + myTest.getClass().getName() + ".bin",
byteArray);
System.out.println(myTest.getClass().getName() + " produced "
+ byteArray.length + " on JBossSerialization");
}
public void doTestJavaSerialization(Object myTest) throws Exception {
long startTime=0;
byte[] byteArray=null;
for (int exec = 0; exec < MAX_LOOP; exec++) {
byteOut.reset();
if (exec==START_LOOP)
{
startTime=System.currentTimeMillis();
}
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(myTest);
out.flush();
byteArray = byteOut.toByteArray();
ByteArrayInputStream byteInput = new ByteArrayInputStream(byteArray);
ObjectInputStream input = new ObjectInputStream(byteInput);
Object value = input.readObject();
assertNotSame(myTest, value);
assertSame(myTest.getClass(), value.getClass());
assertEquals(myTest, value);
}
System.out.println("Time on JavaSerialization for invocation:" + (System.currentTimeMillis() - startTime));
saveFile("/tmp/jboss-test-" + myTest.getClass().getName() + ".bin",
byteArray);
System.out.println(myTest.getClass().getName() + " produced "
+ byteArray.length + " on JavaSerialization");
}
private static void saveFile(String fileName, byte[] bytes)
throws Exception {
if (SAVE_FILE) {
File file = new File(fileName);
DataOutputStream dataout = new DataOutputStream(
new FileOutputStream(file));
dataout.write(bytes);
dataout.close();
}
}
}