Move MarshallingWithCachingTest from core module to soaktest module.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@384690 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-03-10 03:05:38 +00:00
parent 2f1dcea144
commit 5e8f85df32
1 changed files with 142 additions and 141 deletions

View File

@ -1,141 +1,142 @@
/** /**
* *
* Copyright 2005-2006 The Apache Software Foundation * Copyright 2005-2006 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.activemq.openwire; package org.apache.activemq.soaktest;
import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.DataStructure; import org.apache.activemq.command.DataStructure;
import org.apache.activemq.command.ProducerId; import org.apache.activemq.command.ProducerId;
import org.apache.activemq.command.ProducerInfo; import org.apache.activemq.command.ProducerInfo;
import org.apache.activemq.openwire.OpenWireFormat;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.DataOutputStream;
import java.io.IOException;
import junit.framework.TestCase;
import junit.framework.TestCase;
/**
* /**
* @version $Revision$ *
*/ * @version $Revision: 383541 $
public class MarshallingWithCachingTest extends TestCase { */
public class MarshallingWithCachingTest extends TestCase {
protected long commandCount = Short.MAX_VALUE;
protected String connectionId = "Cheese"; protected long commandCount = Short.MAX_VALUE;
protected ActiveMQDestination destination = new ActiveMQQueue("Foo"); protected String connectionId = "Cheese";
protected int endOfStreamMarker = 0x12345678; protected ActiveMQDestination destination = new ActiveMQQueue("Foo");
protected int endOfStreamMarker = 0x12345678;
protected ByteArrayOutputStream buffer = new ByteArrayOutputStream();
protected DataOutputStream ds = new DataOutputStream(buffer); protected ByteArrayOutputStream buffer = new ByteArrayOutputStream();
protected OpenWireFormat openWireformat; protected DataOutputStream ds = new DataOutputStream(buffer);
protected long logGroup = 10000; protected OpenWireFormat openWireformat;
protected long logGroup = 10000;
public void testReadAndWriteLotsOfCommands() throws Exception {
System.out.println("Marshalling: " + commandCount + " objects"); public void testReadAndWriteLotsOfCommands() throws Exception {
for (long i = 0; i < commandCount ; i++) { System.out.println("Marshalling: " + commandCount + " objects");
logProgress("Marshalling", i); for (long i = 0; i < commandCount ; i++) {
DataStructure object = createDataStructure(i); logProgress("Marshalling", i);
writeObject(object); DataStructure object = createDataStructure(i);
} writeObject(object);
ds.writeInt(endOfStreamMarker); }
ds.writeInt(endOfStreamMarker);
// now lets read from the stream
ds.close(); // now lets read from the stream
ds.close();
System.out.println("Unmarshalling: " + commandCount + " objects");
System.out.println("Unmarshalling: " + commandCount + " objects");
ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
DataInputStream dis = new DataInputStream(in); ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
for (long i = 0; i < commandCount ; i++) { DataInputStream dis = new DataInputStream(in);
logProgress("Unmarshalling", i); for (long i = 0; i < commandCount ; i++) {
DataStructure command = null; logProgress("Unmarshalling", i);
try { DataStructure command = null;
command = (DataStructure) openWireformat.unmarshal(dis); try {
} command = (DataStructure) openWireformat.unmarshal(dis);
catch (Exception e) { }
e.printStackTrace(); catch (Exception e) {
fail("Failed to unmarshal object: " + i + ". Reason: " + e); e.printStackTrace();
} fail("Failed to unmarshal object: " + i + ". Reason: " + e);
assertDataStructureExpected(command, i); }
} assertDataStructureExpected(command, i);
}
int marker = dis.readInt();
assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker)); int marker = dis.readInt();
assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
// lets try read and we should get an exception
try { // lets try read and we should get an exception
dis.readByte(); try {
fail("Should have reached the end of the stream"); dis.readByte();
} fail("Should have reached the end of the stream");
catch (IOException e) { }
// worked! catch (IOException e) {
} // worked!
} }
}
protected void logProgress(String message, long i) {
if (i % logGroup == 0) { protected void logProgress(String message, long i) {
System.out.println(message + " at object: " + i); if (i % logGroup == 0) {
} System.out.println(message + " at object: " + i);
} }
}
protected void setUp() throws Exception {
super.setUp(); protected void setUp() throws Exception {
openWireformat = createOpenWireFormat(); super.setUp();
} openWireformat = createOpenWireFormat();
}
protected OpenWireFormat createOpenWireFormat() {
OpenWireFormat wf = new OpenWireFormat(); protected OpenWireFormat createOpenWireFormat() {
wf.setCacheEnabled(true); OpenWireFormat wf = new OpenWireFormat();
wf.setStackTraceEnabled(true); wf.setCacheEnabled(true);
wf.setVersion(1); wf.setStackTraceEnabled(true);
return wf; wf.setVersion(1);
} return wf;
}
private void writeObject(Object object) throws IOException {
openWireformat.marshal(object, ds); private void writeObject(Object object) throws IOException {
} openWireformat.marshal(object, ds);
}
protected DataStructure createDataStructure(long index) {
ProducerId id = new ProducerId(); protected DataStructure createDataStructure(long index) {
id.setConnectionId(connectionId); ProducerId id = new ProducerId();
id.setSessionId(index); id.setConnectionId(connectionId);
id.setValue(index); id.setSessionId(index);
id.setValue(index);
ProducerInfo object = new ProducerInfo();
object.setProducerId(id); ProducerInfo object = new ProducerInfo();
object.setDestination(destination); object.setProducerId(id);
return object; object.setDestination(destination);
} return object;
}
protected void assertDataStructureExpected(DataStructure object, long i) {
assertEquals("Type of object for index: " + i, ProducerInfo.class, object.getClass()); protected void assertDataStructureExpected(DataStructure object, long i) {
ProducerInfo command = (ProducerInfo) object; assertEquals("Type of object for index: " + i, ProducerInfo.class, object.getClass());
ProducerInfo command = (ProducerInfo) object;
ProducerId id = command.getProducerId();
assertNotNull("ProducerID for object at index: " + i, id); ProducerId id = command.getProducerId();
assertNotNull("ProducerID for object at index: " + i, id);
assertEquals("connection ID in object: "+ i, connectionId, id.getConnectionId());
String actual = Long.toHexString(id.getValue()); assertEquals("connection ID in object: "+ i, connectionId, id.getConnectionId());
String expected = Long.toHexString(i); String actual = Long.toHexString(id.getValue());
assertEquals("value of object: "+ i + " was: " + actual, expected, actual); String expected = Long.toHexString(i);
assertEquals("value of object: "+ i + " was: " + actual, i, id.getSessionId()); assertEquals("value of object: "+ i + " was: " + actual, expected, actual);
} assertEquals("value of object: "+ i + " was: " + actual, i, id.getSessionId());
} }
}