diff options
author | Robert Gemmell <robbie@apache.org> | 2011-08-21 15:32:37 +0000 |
---|---|---|
committer | Robert Gemmell <robbie@apache.org> | 2011-08-21 15:32:37 +0000 |
commit | f26db6070ee7d82479ce88c0a8e40c9a8167cbb9 (patch) | |
tree | 8dbbdbd59cbf72e9d93ca60943836aab78ff8014 /java/client/src | |
parent | 29715191e2de1a62ff6eea04755c795a6969b88d (diff) | |
download | qpid-python-f26db6070ee7d82479ce88c0a8e40c9a8167cbb9.tar.gz |
QPID-3307: correct a couple style issues, add some basic unit testing for ObjectMessage and the new CLAOIS
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1160002 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/client/src')
4 files changed, 206 insertions, 6 deletions
diff --git a/java/client/src/main/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStream.java b/java/client/src/main/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStream.java index e89f0c5643..669a0f1abf 100644 --- a/java/client/src/main/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStream.java +++ b/java/client/src/main/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStream.java @@ -62,7 +62,6 @@ public class ClassLoadingAwareObjectInputStream extends ObjectInputStream return load(classDesc.getName(), cl); } - @Override protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException @@ -112,16 +111,16 @@ public class ClassLoadingAwareObjectInputStream extends ObjectInputStream if (clazz != null) { return clazz; - } else + } + else { return Class.forName(className, false, _ON_FAULT_CLASS_LOADER); } } } - - - static { + static + { _primitives.put("boolean", boolean.class); _primitives.put("byte", byte.class); _primitives.put("char", char.class); @@ -132,5 +131,4 @@ public class ClassLoadingAwareObjectInputStream extends ObjectInputStream _primitives.put("double", double.class); _primitives.put("void", void.class); } - } diff --git a/java/client/src/test/java/org/apache/qpid/client/message/TestMessageHelper.java b/java/client/src/test/java/org/apache/qpid/client/message/TestMessageHelper.java index 7ee991b63c..b5c31e7c5e 100644 --- a/java/client/src/test/java/org/apache/qpid/client/message/TestMessageHelper.java +++ b/java/client/src/test/java/org/apache/qpid/client/message/TestMessageHelper.java @@ -43,4 +43,9 @@ public class TestMessageHelper { return new JMSStreamMessage(AMQMessageDelegateFactory.FACTORY_0_8); } + + public static JMSObjectMessage newJMSObjectMessage() + { + return new JMSObjectMessage(AMQMessageDelegateFactory.FACTORY_0_8); + } } diff --git a/java/client/src/test/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStreamTest.java b/java/client/src/test/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStreamTest.java new file mode 100644 index 0000000000..93a4fb39f5 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/client/util/ClassLoadingAwareObjectInputStreamTest.java @@ -0,0 +1,87 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.client.util; + +import java.io.InputStream; +import java.io.ObjectOutputStream; +import java.lang.reflect.Proxy; +import java.util.Arrays; +import java.util.List; + +import org.apache.mina.common.ByteBuffer; +import org.apache.qpid.test.utils.QpidTestCase; + +public class ClassLoadingAwareObjectInputStreamTest extends QpidTestCase +{ + InputStream _in; + ClassLoadingAwareObjectInputStream _claOIS; + + protected void setUp() throws Exception + { + //Create a viable input stream for instantiating the CLA OIS + ByteBuffer buf = ByteBuffer.allocate(10); + buf.setAutoExpand(true); + + ObjectOutputStream out = new ObjectOutputStream(buf.asOutputStream()); + out.writeObject("testString"); + out.flush(); + out.close(); + + buf.rewind(); + _in = buf.asInputStream(); + + _claOIS = new ClassLoadingAwareObjectInputStream(_in); + } + + /** + * Test that the resolveProxyClass method returns a proxy class implementing the desired interface + */ + public void testResolveProxyClass() throws Exception + { + //try to proxy an interface + Class<?> clazz = _claOIS.resolveProxyClass(new String[]{"java.lang.CharSequence"}); + + //verify the proxy supports the expected interface (only) + List<Class<?>> interfaces = Arrays.asList(clazz.getInterfaces()); + assertTrue("Unexpected interfaces supported by proxy", interfaces.contains(CharSequence.class)); + assertEquals("Unexpected interfaces supported by proxy", 1, interfaces.size()); + } + + /** + * Test that the resolveProxyClass method throws a ClassNotFoundException wrapping an + * IllegalArgumentException if it is provided arguments which violate the restrictions allowed + * by Proxy.getProxyClass (as required by the ObjectInputStream.resolveProxyClass javadoc). + */ + public void testResolveProxyClassThrowsCNFEWrappingIAE() throws Exception + { + try + { + //try to proxy a *class* rather than an interface, which is illegal + _claOIS.resolveProxyClass(new String[]{"java.lang.String"}); + fail("should have thrown an exception"); + } + catch(ClassNotFoundException cnfe) + { + //expected, but must verify it is wrapping an IllegalArgumentException + assertTrue(cnfe.getCause() instanceof IllegalArgumentException); + } + } +} diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/client/message/ObjectMessageUnitTest.java b/java/client/src/test/java/org/apache/qpid/test/unit/client/message/ObjectMessageUnitTest.java new file mode 100644 index 0000000000..177dccea7e --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/test/unit/client/message/ObjectMessageUnitTest.java @@ -0,0 +1,110 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.test.unit.client.message; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.apache.qpid.client.message.JMSObjectMessage; +import org.apache.qpid.client.message.TestMessageHelper; +import org.apache.qpid.test.utils.QpidTestCase; + +public class ObjectMessageUnitTest extends QpidTestCase +{ + private JMSObjectMessage _om; + + protected void setUp() throws Exception + { + super.setUp(); + _om = TestMessageHelper.newJMSObjectMessage(); + } + + /** + * Test that setObject with a primitive works + */ + public void testSetObjectWithBooleanPrimitive() throws Exception + { + _om.setObject(true); + + //make the message readable + _om.reset(); + + Object object = _om.getObject(); + + assertTrue("Unexpected type returned", object instanceof Boolean); + assertEquals("Unexpected value returned", true, object); + } + + /** + * Test that setObject with a serializable Object works + */ + public void testSetObjectWithString() throws Exception + { + _om.setObject("test string"); + + //make the message readable + _om.reset(); + + Object object = _om.getObject(); + + assertTrue("Unexpected type returned", object instanceof String); + assertEquals("Unexpected value returned", "test string", object); + } + + /** + * Test that setObject with a Collection of serializable's works, returning + * the items in the list when deserialized and ignoring any values + * added to the collection after setObject() is called on the message. + */ + public void testSetObjectWithArrayListOfInteger() throws Exception + { + ArrayList<Integer> list = new ArrayList<Integer>(); + list.add(1234); + list.add(Integer.MIN_VALUE); + list.add(Integer.MAX_VALUE); + + _om.setObject(list); + + //add something extra to the list now, and check it isn't in the value read back + list.add(0); + + //make the message readable + _om.reset(); + + //retrieve the Object + Object object = _om.getObject(); + + ArrayList<?> returnedList = null; + if(object instanceof ArrayList<?>) + { + returnedList = (ArrayList<?>) object; + } + else + { + fail("returned object was not an ArrayList"); + } + + //verify the extra added Integer was not present, then remove it from original list again and compare contents with the returned list + assertFalse("returned list should not have had the value added after setObject() was used", returnedList.contains(0)); + list.remove(Integer.valueOf(0)); + assertTrue("list contents were not equal", Arrays.equals(list.toArray(), returnedList.toArray())); + } +} |