JBoss Remoting SVN: r6116 - in remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test: stream and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-24 21:41:02 -0400 (Fri, 24 Sep 2010)
New Revision: 6116
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/stream/
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/stream/StreamsTestCase.java
Removed:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java
Log:
JBREM-1228: Moved to org.jboss.remoting3.test.stream and added a bunch of methods.
Deleted: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java 2010-09-24 16:11:58 UTC (rev 6115)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/StreamsTestCase.java 2010-09-25 01:41:02 UTC (rev 6116)
@@ -1,115 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, 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.remoting3.test;
-
-import java.io.EOFException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.Vector;
-import org.jboss.remoting3.stream.ObjectSink;
-import org.jboss.remoting3.stream.ObjectSource;
-import org.jboss.remoting3.stream.Streams;
-import org.testng.annotations.Test;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
-
-/**
- *
- */
-@Test(suiteName = "streams")
-public final class StreamsTestCase {
-
- public void testCollectionObjectSink() throws Throwable {
- final ArrayList<String> strings = new ArrayList<String>();
- final ObjectSink<String> sink = Streams.getCollectionObjectSink(strings);
- sink.accept("Instance 1");
- sink.accept("Instance 2");
- sink.accept("Instance 3");
- sink.accept("Instance 4");
- sink.accept("Instance 5");
- sink.close();
- final Iterator<String> i = strings.iterator();
- assertEquals(i.next(), "Instance 1");
- assertEquals(i.next(), "Instance 2");
- assertEquals(i.next(), "Instance 3");
- assertEquals(i.next(), "Instance 4");
- assertEquals(i.next(), "Instance 5");
- assertFalse(i.hasNext());
- }
-
- public void testIteratorObjectSource() throws Throwable {
- final ObjectSource<String> source = Streams.getIteratorObjectSource(Arrays.asList("One", "Two", "Three", "Four", "Five").iterator());
- assertTrue(source.hasNext());
- assertEquals(source.next(), "One");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Two");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Three");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Four");
- assertTrue(source.hasNext());
- assertTrue(source.hasNext()); // tricky!
- assertEquals(source.next(), "Five");
- assertFalse(source.hasNext());
- assertFalse(source.hasNext()); // also tricky!
- source.close();
- assertFalse(source.hasNext()); // also also tricky!
- assertFalse(source.hasNext()); // also also also tricky!
- try {
- source.next();
- } catch (EOFException t) {
- return;
- }
- fail("No exception thrown at end of iterator");
- }
-
- public void testEnumerationObjectSource() throws Throwable {
- final ObjectSource<String> source = Streams.getEnumerationObjectSource(new Vector<String>(Arrays.asList("One", "Two", "Three", "Four", "Five")).elements());
- assertTrue(source.hasNext());
- assertEquals(source.next(), "One");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Two");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Three");
- assertTrue(source.hasNext());
- assertEquals(source.next(), "Four");
- assertTrue(source.hasNext());
- assertTrue(source.hasNext()); // tricky!
- assertEquals(source.next(), "Five");
- assertFalse(source.hasNext());
- assertFalse(source.hasNext()); // also tricky!
- source.close();
- assertFalse(source.hasNext()); // also also tricky!
- assertFalse(source.hasNext()); // also also also tricky!
- try {
- source.next();
- } catch (EOFException t) {
- return;
- }
- fail("No exception thrown at end of iterator");
- }
-}
Added: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/stream/StreamsTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/stream/StreamsTestCase.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/stream/StreamsTestCase.java 2010-09-25 01:41:02 UTC (rev 6116)
@@ -0,0 +1,502 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, 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.remoting3.test.stream;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.Vector;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.jboss.marshalling.ByteBufferInput;
+import org.jboss.marshalling.ByteInput;
+import org.jboss.marshalling.OutputStreamByteOutput;
+import org.jboss.marshalling.Pair;
+import org.jboss.remoting3.stream.ObjectSink;
+import org.jboss.remoting3.stream.ObjectSource;
+import org.jboss.remoting3.stream.Streams;
+import org.jboss.xnio.IoFuture;
+import org.jboss.xnio.log.Logger;
+import org.testng.annotations.Test;
+
+/**
+ *
+ */
+@Test(suiteName = "streams")
+public final class StreamsTestCase {
+
+ private static Logger log = Logger.getLogger(StreamsTestCase.class);
+
+ public void testCollectionObjectSink() throws Throwable {
+ final ArrayList<String> strings = new ArrayList<String>();
+ final ObjectSink<String> sink = Streams.getCollectionObjectSink(strings);
+ sink.accept("Instance 1");
+ sink.accept("Instance 2");
+ sink.accept("Instance 3");
+ sink.accept("Instance 4");
+ sink.accept("Instance 5");
+ sink.close();
+ final Iterator<String> i = strings.iterator();
+ assertEquals(i.next(), "Instance 1");
+ assertEquals(i.next(), "Instance 2");
+ assertEquals(i.next(), "Instance 3");
+ assertEquals(i.next(), "Instance 4");
+ assertEquals(i.next(), "Instance 5");
+ assertFalse(i.hasNext());
+ }
+
+ public void testCollectionObjectSinkRealTime() throws Throwable {
+ final ArrayList<String> strings = new ArrayList<String>();
+ final ObjectSink<String> sink = Streams.getCollectionObjectSink(strings);
+ sink.accept("Instance 1");
+ assertEquals(strings.get(0), "Instance 1");
+ sink.accept("Instance 2");
+ assertEquals(strings.get(1), "Instance 2");
+ sink.accept("Instance 3");
+ assertEquals(strings.get(2), "Instance 3");
+ sink.accept("Instance 4");
+ assertEquals(strings.get(3), "Instance 4");
+ sink.accept("Instance 5");
+ assertEquals(strings.get(4), "Instance 5");
+ sink.close();
+ }
+
+ public void testIteratorObjectSource() throws Throwable {
+ final ObjectSource<String> source = Streams.getIteratorObjectSource(Arrays.asList("One", "Two", "Three", "Four", "Five").iterator());
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "One");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Two");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Three");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Four");
+ assertTrue(source.hasNext());
+ assertTrue(source.hasNext()); // tricky!
+ assertEquals(source.next(), "Five");
+ assertFalse(source.hasNext());
+ assertFalse(source.hasNext()); // also tricky!
+ source.close();
+ assertFalse(source.hasNext()); // also also tricky!
+ assertFalse(source.hasNext()); // also also also tricky!
+ try {
+ source.next();
+ } catch (EOFException t) {
+ return;
+ }
+ fail("No exception thrown at end of iterator");
+ }
+
+ public void testCollectionObjectSource() throws Throwable {
+ final ObjectSource<String> source = Streams.getCollectionObjectSource(Arrays.asList("One", "Two", "Three", "Four", "Five"));
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "One");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Two");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Three");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Four");
+ assertTrue(source.hasNext());
+ assertTrue(source.hasNext()); // tricky!
+ assertEquals(source.next(), "Five");
+ assertFalse(source.hasNext());
+ assertFalse(source.hasNext()); // also tricky!
+ source.close();
+ assertFalse(source.hasNext()); // also also tricky!
+ assertFalse(source.hasNext()); // also also also tricky!
+ try {
+ source.next();
+ } catch (EOFException t) {
+ return;
+ }
+ fail("No exception thrown at end of iterator");
+ }
+
+ public void testMapObjectSink() throws Throwable {
+ final HashMap<String, String> map = new HashMap<String, String>();
+ final ObjectSink<Pair<String, String>> sink = Streams.getMapObjectSink(map);
+ sink.accept(new Pair<String, String>("k1", "v1"));
+ sink.accept(new Pair<String, String>("k2", "v2"));
+ sink.accept(new Pair<String, String>("k3", "v3"));
+ sink.accept(new Pair<String, String>("k4", "v4"));
+ sink.accept(new Pair<String, String>("k5", "v5"));
+ sink.close();
+
+ assertEquals(5, map.keySet().size());
+ assertEquals(map.get("k1"), "v1");
+ assertEquals(map.get("k2"), "v2");
+ assertEquals(map.get("k3"), "v3");
+ assertEquals(map.get("k4"), "v4");
+ assertEquals(map.get("k5"), "v5");
+ }
+
+
+ public void testMapObjectSinkRealTime() throws Throwable {
+ final HashMap<String, String> map = new HashMap<String, String>();
+ final ObjectSink<Pair<String, String>> sink = Streams.getMapObjectSink(map);
+ sink.accept(new Pair<String, String>("k1", "v1"));
+ assertEquals(1, map.keySet().size());
+ assertEquals(map.get("k1"), "v1");
+ sink.accept(new Pair<String, String>("k2", "v2"));
+ assertEquals(2, map.keySet().size());
+ assertEquals(map.get("k2"), "v2");
+ sink.accept(new Pair<String, String>("k3", "v3"));
+ assertEquals(3, map.keySet().size());
+ assertEquals(map.get("k3"), "v3");
+ sink.accept(new Pair<String, String>("k4", "v4"));
+ assertEquals(4, map.keySet().size());
+ assertEquals(map.get("k4"), "v4");
+ sink.accept(new Pair<String, String>("k5", "v5"));
+ assertEquals(5, map.keySet().size());
+ assertEquals(map.get("k5"), "v5");
+ sink.close();
+ }
+
+ class A{
+ String s;
+ public A(String s) {
+ this.s = s;
+ }
+ public int hashCode() {
+ return s.hashCode();
+ }
+ public boolean equals(Object obj) {
+ if (obj == null || !(obj instanceof A)) {
+ return false;
+ }
+ return s.equals(((A) obj).s);
+ }
+ };
+
+ class B extends A {
+ public B(String s) {
+ super(s);
+ }
+ };
+
+ public void testCheckedObjectSink() throws Throwable {
+
+ final HashSet<A> set = new HashSet<A>();
+ final ObjectSink<A> collectionObjectSink = Streams.getCollectionObjectSink(set);
+ final ObjectSink<A> sink = Streams.getCheckedObjectSink(collectionObjectSink, B.class);
+
+ sink.accept(new B("Instance 1"));
+ sink.accept(new B("Instance 2"));
+ sink.accept(new B("Instance 3"));
+ sink.accept(new B("Instance 4"));
+ sink.accept(new B("Instance 5"));
+ sink.close();
+
+ assertEquals(5, set.size());
+ assertTrue(set.contains(new B("Instance 1")));
+ assertTrue(set.contains(new B("Instance 2")));
+ assertTrue(set.contains(new B("Instance 3")));
+ assertTrue(set.contains(new B("Instance 4")));
+ assertTrue(set.contains(new B("Instance 5")));
+
+ try {
+ sink.accept(new A("isn't a B"));
+ fail("sink shouldn't accept an A");
+ } catch (ClassCastException e) {
+ log.info(this + " got expected ClassCastException");
+ } catch (Throwable t) {
+ log.error(this + " expected ClassCastException, got " + t);
+ }
+ }
+
+ public void testCheckedObjectSinkRealTime() throws Throwable {
+
+ final HashSet<A> set = new HashSet<A>();
+ final ObjectSink<A> collectionObjectSink = Streams.getCollectionObjectSink(set);
+ final ObjectSink<A> sink = Streams.getCheckedObjectSink(collectionObjectSink, B.class);
+
+ sink.accept(new B("Instance 1"));
+ assertEquals(1, set.size());
+ assertTrue(set.contains(new B("Instance 1")));
+ sink.accept(new B("Instance 2"));
+ assertEquals(2, set.size());
+ assertTrue(set.contains(new B("Instance 2")));
+ sink.accept(new B("Instance 3"));
+ assertEquals(3, set.size());
+ assertTrue(set.contains(new B("Instance 3")));
+ sink.accept(new B("Instance 4"));
+ assertEquals(4, set.size());
+ assertTrue(set.contains(new B("Instance 4")));
+ sink.accept(new B("Instance 5"));
+ assertEquals(5, set.size());
+ assertTrue(set.contains(new B("Instance 5")));
+
+ try {
+ sink.accept(new A("isn't a B"));
+ fail("sink shouldn't accept an A");
+ } catch (ClassCastException e) {
+ log.info(this + " got expected ClassCastException");
+ } catch (Throwable t) {
+ log.error(this + " expected ClassCastException, got " + t);
+ }
+
+ sink.close();
+ }
+
+ public void testMapEntryIteratorObjectSource() throws Throwable {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("1", "a");
+ map.put("2", "b");
+ map.put("3", "c");
+ map.put("4", "d");
+ map.put("5", "e");
+ final ObjectSource<Pair<String, String>> source = Streams.getMapEntryIteratorObjectSource(map.entrySet().iterator());
+
+ assertTrue(source.hasNext());
+ Pair<String, String> entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertFalse(source.hasNext());
+ assertFalse(source.hasNext()); // also tricky!
+ source.close();
+ assertFalse(source.hasNext()); // also also tricky!
+ assertFalse(source.hasNext()); // also also also tricky!
+ try {
+ source.next();
+// } catch (EOFException t) {
+ } catch (NoSuchElementException t) {
+ return;
+ }
+ fail("No exception thrown at end of iterator");
+ }
+
+ public void testMapObjectSource() throws Throwable {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("1", "a");
+ map.put("2", "b");
+ map.put("3", "c");
+ map.put("4", "d");
+ map.put("5", "e");
+ final ObjectSource<Pair<String, String>> source = Streams.getMapObjectSource(map);
+
+ assertTrue(source.hasNext());
+ Pair<String, String> entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertTrue(source.hasNext());
+ entry = source.next();
+ assertEquals(map.get(entry.getA()), entry.getB());
+ assertFalse(source.hasNext());
+ assertFalse(source.hasNext()); // also tricky!
+ source.close();
+ assertFalse(source.hasNext()); // also also tricky!
+ assertFalse(source.hasNext()); // also also also tricky!
+ try {
+ source.next();
+// } catch (EOFException t) {
+ } catch (NoSuchElementException t) {
+ return;
+ }
+ fail("No exception thrown at end of iterator");
+ }
+
+ public void testGetCollection() throws Throwable {
+ Set<String> set = new HashSet<String>();
+ set.add("1");
+ set.add("2");
+ set.add("3");
+ set.add("4");
+ set.add("5");
+ ObjectSource<String> source = Streams.getCollectionObjectSource(set);
+ source.close();
+ Collection<String> collection = Streams.getCollection(new ArrayList<String>(), source);
+ assertEquals(5, collection.size());
+ assertTrue(collection.contains("1"));
+ assertTrue(collection.contains("2"));
+ assertTrue(collection.contains("3"));
+ assertTrue(collection.contains("4"));
+ assertTrue(collection.contains("5"));
+ }
+
+ public void testGetMap() throws Throwable {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("1", "a");
+ map.put("2", "b");
+ map.put("3", "c");
+ map.put("4", "d");
+ map.put("5", "e");
+ ObjectSource<Pair<String, String>> source = Streams.getMapObjectSource(map);
+ source.close();
+ Map<String, String> newMap = Streams.getMap(new TreeMap<String, String>(), source);
+ assertEquals(5, newMap.entrySet().size());
+ assertEquals("a", newMap.get("1"));
+ assertEquals("b", newMap.get("2"));
+ assertEquals("c", newMap.get("3"));
+ assertEquals("d", newMap.get("4"));
+ assertEquals("e", newMap.get("5"));
+ }
+
+ public void testGetFutureCollection() throws Throwable {
+ Set<String> set = new HashSet<String>();
+ set.add("1");
+ set.add("2");
+ set.add("3");
+ set.add("4");
+ set.add("5");
+ ObjectSource<String> source = Streams.getCollectionObjectSource(set);
+ source.close();
+ Executor executor = new ThreadPoolExecutor(8, 64, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(64));
+ IoFuture<? extends Collection<String>> future = Streams.getFutureCollection(executor, new ArrayList<String>(), source);
+ Collection<String> collection= future.get();
+ assertEquals(5, collection.size());
+ assertTrue(collection.contains("1"));
+ assertTrue(collection.contains("2"));
+ assertTrue(collection.contains("3"));
+ assertTrue(collection.contains("4"));
+ assertTrue(collection.contains("5"));
+ }
+
+ public void testGetFutureMap() throws Throwable {
+ Map<String, String> map = new HashMap<String, String>();
+ map.put("1", "a");
+ map.put("2", "b");
+ map.put("3", "c");
+ map.put("4", "d");
+ map.put("5", "e");
+ ObjectSource<Pair<String, String>> source = Streams.getMapObjectSource(map);
+ source.close();
+ Executor executor = new ThreadPoolExecutor(8, 64, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(64));
+ IoFuture<? extends Map<String, String>> future = Streams.getFutureMap(executor, new TreeMap<String, String>(), source);
+ Map<String, String> newMap = future.get();
+ assertEquals(5, newMap.entrySet().size());
+ assertEquals("a", newMap.get("1"));
+ assertEquals("b", newMap.get("2"));
+ assertEquals("c", newMap.get("3"));
+ assertEquals("d", newMap.get("4"));
+ assertEquals("e", newMap.get("5"));
+ }
+
+ public void testEnumerationObjectSource() throws Throwable {
+ final ObjectSource<String> source = Streams.getEnumerationObjectSource(new Vector<String>(Arrays.asList("One", "Two", "Three", "Four", "Five")).elements());
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "One");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Two");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Three");
+ assertTrue(source.hasNext());
+ assertEquals(source.next(), "Four");
+ assertTrue(source.hasNext());
+ assertTrue(source.hasNext()); // tricky!
+ assertEquals(source.next(), "Five");
+ assertFalse(source.hasNext());
+ assertFalse(source.hasNext()); // also tricky!
+ source.close();
+ assertFalse(source.hasNext()); // also also tricky!
+ assertFalse(source.hasNext()); // also also also tricky!
+ try {
+ source.next();
+ } catch (EOFException t) {
+ return;
+ }
+ fail("No exception thrown at end of iterator");
+ }
+
+ public void testCopyStream() throws Throwable {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Streams.copyStream(new ByteArrayInputStream("This is a test!!!".getBytes()), baos);
+ assertEquals(new String(baos.toByteArray()), "This is a test!!!");
+ }
+
+ public void testCopyStreamFromByteInput() throws Throwable {
+ ByteInput byteInput = new TestByteInput("This is a test!!!".getBytes());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ OutputStreamByteOutput outputStreamByteOutput = new OutputStreamByteOutput(baos);
+ Streams.copyStream(byteInput, outputStreamByteOutput);
+ assertEquals(new String(baos.toByteArray()), "This is a test!!!");
+ }
+
+ static class TestByteInput implements ByteInput {
+ private ByteBufferInput byteBufferInput;
+
+ public TestByteInput(byte[] bytes) {
+ ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
+ byteBuffer.put(bytes);
+ byteBuffer.flip();
+ byteBufferInput = new ByteBufferInput(byteBuffer);
+ }
+ public int available() throws IOException {
+ return byteBufferInput.available();
+ }
+ public int read() throws IOException {
+ return byteBufferInput.read();
+ }
+ public int read(byte[] b) throws IOException {
+ return byteBufferInput.read(b);
+ }
+ public int read(byte[] b, int off, int len) throws IOException {
+ return byteBufferInput.read(b, off, len);
+ }
+ public long skip(long n) throws IOException {
+ return byteBufferInput.skip(n);
+ }
+ public void close() throws IOException {
+ byteBufferInput.close();
+ }
+ }
+}
14 years, 3 months
JBoss Remoting SVN: r6115 - remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-24 12:11:58 -0400 (Fri, 24 Sep 2010)
New Revision: 6115
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java
Log:
JBREM-1228: Fixed javadoc.
Modified: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java 2010-09-24 15:59:42 UTC (rev 6114)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ClientConnectorMisuseTestCase.java 2010-09-24 16:11:58 UTC (rev 6115)
@@ -69,9 +69,7 @@
}
/**
- * Distinct Executors, Endpoints, Xnio's, TcpServers, RequestListeners.
- * One instance of one service in each Endpoint.
- * Two Connections per TcpServer, two Clients per Connection.
+ * Verifies that a Connector can't be used until it has been transported over the network.
*/
@Test
public void testClientConnectorMisuse() throws Exception {
14 years, 3 months
JBoss Remoting SVN: r6114 - in remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test: remote and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-24 11:59:42 -0400 (Fri, 24 Sep 2010)
New Revision: 6114
Added:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java
Log:
JBREM-1228: Moved a couple of methods from RemotingTestBase to RemotingLocalTestBase.
Added: remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java
===================================================================
--- remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java (rev 0)
+++ remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/remote/RemoteMessageHandlerTestCase.java 2010-09-24 15:59:42 UTC (rev 6114)
@@ -0,0 +1,346 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.remoting3.test.remote;
+
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.net.URISyntaxException;
+import java.util.Date;
+import java.util.Random;
+
+import org.jboss.remoting3.Client;
+import org.jboss.remoting3.Connection;
+import org.jboss.remoting3.RemoteExecutionException;
+import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.ServiceOpenException;
+import org.jboss.remoting3.test.RemotingTestBase;
+import org.jboss.xnio.Option;
+import org.jboss.xnio.OptionMap;
+import org.jboss.xnio.log.Logger;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright September 16, 2010
+ */
+@Test(suiteName = "RemoteMessageHandler")
+public class RemoteMessageHandlerTestCase extends RemotingTestBase {
+
+ private static final Option<BadClass> BAD_OPTION = Option.simple(RemoteMessageHandlerTestCase.class, "BAD_OPTION", BadClass.class);
+ private static final String SERVICE_TYPE = "servicetyype";
+ private static final String INSTANCE_NAME = "instancename";
+ private static final String WRITE_EXCEPTION_MSG = "can't write this";
+
+ private static final Logger log = Logger.getLogger(RemoteMessageHandlerTestCase.class);
+ private static int counter = new Random(new Date().getTime()).nextInt();
+
+ private boolean useBadOption;
+
+ @BeforeMethod
+ public void setUp() {
+ useBadOption = false;
+ }
+
+ @AfterMethod
+ public void tearDown() throws IOException {
+ }
+
+ @Test
+ public void testServiceError() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ useBadOption = true;
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ try {
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+ } catch (ServiceOpenException e) {
+ if ("Remote side failed to open service".equals(e.getMessage())) {
+ log.info(this + " got expected ServiceOpenException(\"Remote side failed to open service\"");
+ } else {
+ log.error(this + " got ServiceOpenException but didn't get expected message \"Remote side failed to open service\": " + e.getMessage());
+ fail(" got ServiceOpenException but didn't get expected message \"Remote side failed to open service\": " + e.getMessage());
+ }
+ } catch (Throwable t) {
+ log.error("expected ServiceOpenException(\"Remote side failed to open service\"");
+ fail("expected ServiceOpenException(\"Remote side failed to open service\"");
+ }
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+ @Test
+ public void testRequestAbort() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ try {
+ client.invoke(new UnwriteableObject());
+ } catch (TestException e) {
+ if (WRITE_EXCEPTION_MSG.equals(e.getMessage())) {
+ log.info(this + " got expected TestException");
+ } else {
+ fail(" expected TestException message: " + WRITE_EXCEPTION_MSG);
+ }
+ } catch (Throwable t) {
+ fail(" expected TestException(" + WRITE_EXCEPTION_MSG + ")");
+ }
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+ @Test
+ public void testLongMessage() throws Exception {
+ enter();
+
+ ServerPackage sp0 = null;
+ ServerPackage sp1 = null;
+ Connection connection = null;
+ Client<Object, Object> client = null;
+
+ try {
+ int id0 = counter++;
+ int id1 = counter++;
+
+ // Set up services.
+ sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+ sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+
+ // Set up connection and client.
+ connection = setupConnection(sp0, sp1);
+ client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+
+ // Send long message.
+ log.info(this + " sending long message");
+// byte[] bytes = new byte[1024 * 8 - 5];
+ byte[] bytes = new byte[1024 * 1024 * 16];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = 0x01;
+ }
+ client.invoke(bytes);
+ log.info(this + " sent long message");
+
+ log.info(getName() + " PASSES");
+ } finally {
+ if (client != null) {
+ client.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ if (sp0 != null) {
+ sp0.close();
+ }
+ if (sp1 != null) {
+ sp1.close();
+ }
+ exit();
+ }
+ }
+
+// @Test
+// public void testReplyExceptionAbort() throws Exception {
+// enter();
+//
+// ServerPackage sp0 = null;
+// ServerPackage sp1 = null;
+// Connection connection = null;
+// Client<Object, Object> client = null;
+//
+// try {
+// int id0 = counter++;
+// int id1 = counter++;
+//
+// // Set up services.
+// sp0 = setupServer(null, id0, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+// sp1 = setupServer(null, id1, new TestRequestListener(), SERVICE_TYPE, INSTANCE_NAME);
+//
+// // Set up connection and client.
+// connection = setupConnection(sp0, sp1);
+// client = setupClient(connection, SERVICE_TYPE, INSTANCE_NAME);
+//
+// client.invoke(new TestException("testReplyExceptionAbort"));
+//
+// log.info(getName() + " PASSES");
+// } finally {
+// if (client != null) {
+// client.close();
+// }
+// if (connection != null) {
+// connection.close();
+// }
+// if (sp0 != null) {
+// sp0.close();
+// }
+// if (sp1 != null) {
+// sp1.close();
+// }
+// exit();
+// }
+// }
+
+ public Connection setupConnection(ServerPackage localServerPackage, ServerPackage remoteServerPackage) throws IOException, URISyntaxException {
+ return super.setupConnection(localServerPackage, remoteServerPackage);
+ }
+
+ protected OptionMap getConnectionOptionMap() {
+ if (useBadOption) {
+ return OptionMap.builder().set(BAD_OPTION, new BadClass()).getMap();
+ } else {
+ return OptionMap.EMPTY;
+ }
+ }
+
+ protected OptionMap getClientOptionMap() {
+ if (useBadOption) {
+ return OptionMap.builder().set(BAD_OPTION, new BadClass()).getMap();
+ } else {
+ return OptionMap.EMPTY;
+ }
+ }
+
+ public Client<Object, Object> setupClient(Connection connection, String serviceType, String instanceName) throws IOException, URISyntaxException {
+ Client<Object, Object> client = getFutureResult(connection.openClient(serviceType, instanceName, Object.class, Object.class, getClientOptionMap()), 1000000, "unable to open client to " + serviceType + ":" + instanceName);
+ return client;
+ }
+
+ static class TestRequestListener implements RequestListener<Object, Object> {
+ public void handleRequest(RequestContext<Object> context, Object request) throws RemoteExecutionException {
+ try {
+ context.sendReply(request);
+ } catch (IOException e) {
+ log.error(this + " unable to send reply", e);
+ e.printStackTrace();
+ }
+ }
+ }
+
+ static class BadClass implements Serializable {
+ private static final long serialVersionUID = -2289052108857883020L;
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ log.info(this + ".readObject()");
+ stream.defaultReadObject();
+ throw new RuntimeException("BadClass.readObject()");
+ }
+ }
+
+ static class UnwriteableObject implements Serializable {
+ private static final long serialVersionUID = 7626170116737760955L;
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ throw new TestException(WRITE_EXCEPTION_MSG);
+ }
+ }
+
+ static class TestException extends IOException {
+ private static final long serialVersionUID = -5729262735391226111L;
+ private int counter;
+
+ public TestException(String msg) {
+ super(msg);
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ log.info(this + ".readObject()");
+ stream.defaultReadObject();
+ counter++;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ log.info(this + ".writeObject()");
+ if (counter > 0) {
+ throw new TestException(WRITE_EXCEPTION_MSG);
+ }
+ stream.defaultWriteObject();
+ }
+ }
+}
14 years, 3 months
JBoss Remoting SVN: r6113 - remoting2/branches.
by jboss-remoting-commits@lists.jboss.org
Author: jbertram(a)redhat.com
Date: 2010-09-24 11:40:35 -0400 (Fri, 24 Sep 2010)
New Revision: 6113
Added:
remoting2/branches/2.5.2.SP2_JBREM-1245/
Log:
JBPAPP-5137
Copied: remoting2/branches/2.5.2.SP2_JBREM-1245 (from rev 6112, remoting2/tags/2.5.2.SP2)
14 years, 3 months
JBoss Remoting SVN: r6112 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-24 10:23:40 -0400 (Fri, 24 Sep 2010)
New Revision: 6112
Modified:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
Log:
JBREM-1245: Reduced log level to INFO.
Modified: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-24 14:23:06 UTC (rev 6111)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-24 14:23:40 UTC (rev 6112)
@@ -80,7 +80,7 @@
if (firstTime)
{
firstTime = false;
- Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE);
+ Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
PatternLayout layout = new PatternLayout(pattern);
14 years, 3 months
JBoss Remoting SVN: r6111 - remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-24 10:23:06 -0400 (Fri, 24 Sep 2010)
New Revision: 6111
Modified:
remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
Log:
JBREM-1245: Reduced log level to INFO.
Modified: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
===================================================================
--- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-24 01:33:38 UTC (rev 6110)
+++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-24 14:23:06 UTC (rev 6111)
@@ -80,7 +80,7 @@
if (firstTime)
{
firstTime = false;
- Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE);
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
PatternLayout layout = new PatternLayout(pattern);
14 years, 3 months
JBoss Remoting SVN: r6110 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-23 21:33:38 -0400 (Thu, 23 Sep 2010)
New Revision: 6110
Modified:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
Log:
JBREM-1245: Changed Level.TRACE to XLevel.TRACE.
Modified: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-23 04:09:51 UTC (rev 6109)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-24 01:33:38 UTC (rev 6110)
@@ -39,6 +39,7 @@
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
import org.jboss.remoting.Client;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
@@ -79,7 +80,7 @@
if (firstTime)
{
firstTime = false;
- Logger.getLogger("org.jboss.remoting").setLevel(Level.TRACE);
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE);
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
PatternLayout layout = new PatternLayout(pattern);
14 years, 3 months
JBoss Remoting SVN: r6109 - in remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket: retriable and 1 other directory.
by jboss-remoting-commits@lists.jboss.org
Author: ron.sigal(a)jboss.com
Date: 2010-09-23 00:09:51 -0400 (Thu, 23 Sep 2010)
New Revision: 6109
Added:
remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/
remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
Log:
JBREM-1245: New unit test.
Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java
===================================================================
--- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java (rev 0)
+++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket/retriable/SocketGeneralizedExceptionTestCase.java 2010-09-23 04:09:51 UTC (rev 6109)
@@ -0,0 +1,341 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.test.remoting.transport.socket.retriable;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLException;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+
+
+/**
+ * Unit test for JBREM-1245.
+ *
+ * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
+ * @version $Rev$
+ * <p>
+ * Copyright Sep 22, 2010
+ * </p>
+ */
+public class SocketGeneralizedExceptionTestCase extends TestCase
+{
+ private static Logger log = Logger.getLogger(SocketGeneralizedExceptionTestCase.class);
+
+ private static boolean firstTime = true;
+ protected static IOException exceptionToThrow;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+
+ TestOutputStream.counter = 0;
+ TestOutputStream.threwException = false;
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testSSLException() throws Throwable
+ {
+ log.info("entering " + getName());
+ exceptionToThrow = new SSLException("Connection has been shutdown");
+ doTest();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testConnectionResetException() throws Throwable
+ {
+ log.info("entering " + getName());
+ exceptionToThrow = new IOException("Connection reset by peer");
+ doTest();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testConnectionClosedException() throws Throwable
+ {
+ log.info("entering " + getName());
+ exceptionToThrow = new IOException("Connection is closed");
+ doTest();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testBrokenPipeException() throws Throwable
+ {
+ log.info("entering " + getName());
+ exceptionToThrow = new IOException("Broken pipe");
+ doTest();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected void doTest() throws Throwable
+ {
+ // Start server.
+ setupServer();
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(2));
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Verify invocation works in spite of exception.
+ assertEquals("xyz", client.invoke("xyz"));
+ assertTrue(TestOutputStream.threwException);
+
+ client.disconnect();
+ shutdownServer();
+ }
+
+
+ protected String getTransport()
+ {
+ return "socket";
+ }
+
+ protected void addExtraServerConfig(Map config) {}
+ protected void addExtraClientConfig(Map config) {}
+
+
+ protected void setupServer() throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port + "/?generalizeSocketException=true";
+ String metadata = System.getProperty("remoting.metadata");
+ if (metadata != null)
+ {
+ locatorURI += "&" + metadata;
+ }
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ public void addListener(InvokerCallbackHandler callbackHandler) {}
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ return invocation.getParameter();
+ }
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ public static class TestSocketFactory extends SocketFactory
+ {
+ int initialSuccesses = 1;
+
+ public TestSocketFactory()
+ {
+ }
+ public TestSocketFactory(int initialSuccesses)
+ {
+ this.initialSuccesses = initialSuccesses;
+ }
+ public Socket createSocket()
+ {
+ Socket s = new TestSocket(initialSuccesses);
+ log.info("returning " + s);
+ return s;
+ }
+ public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
+ {
+ Socket s = new TestSocket(arg0, arg1, initialSuccesses);
+ log.info("returning " + s);
+ return s;
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1) throws IOException
+ {
+ Socket s = new TestSocket(arg0, arg1, initialSuccesses);
+ log.info("returning " + s);
+ return s;
+ }
+
+ public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
+ {
+ Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
+ log.info("returning " + s);
+ return s;
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
+ {
+ Socket s = new TestSocket(arg0, arg1, arg2, arg3, initialSuccesses);
+ log.info("returning " + s);
+ return s;
+ }
+ }
+
+
+ static class TestSocket extends Socket
+ {
+ int initialSuccesses;
+
+ public TestSocket(int initialWrites)
+ {
+ this.initialSuccesses = initialWrites;
+ }
+ public TestSocket(String host, int port, int initialWrites) throws UnknownHostException, IOException
+ {
+ super(host, port);
+ this.initialSuccesses = initialWrites;
+ }
+ public TestSocket(InetAddress address, int port, int initialWrites) throws IOException
+ {
+ super(address, port);
+ this.initialSuccesses = initialWrites;
+ }
+ public TestSocket(String host, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
+ {
+ super(host, port, localAddr, localPort);
+ this.initialSuccesses = initialWrites;
+ }
+ public TestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int initialWrites) throws IOException
+ {
+ super(address, port, localAddr, localPort);
+ this.initialSuccesses = initialWrites;
+ }
+ public OutputStream getOutputStream() throws IOException
+ {
+ return new TestOutputStream(super.getOutputStream(), initialSuccesses);
+ }
+ public String toString()
+ {
+ return "TestSocket[" + getLocalPort() + "->" + getPort() + "]";
+ }
+ }
+
+
+ static class TestOutputStream extends OutputStream
+ {
+ public static int counter;
+ public static boolean threwException;
+
+ OutputStream os;
+ boolean closed;
+ int initialWrites;
+ boolean doThrow = true;
+
+ public TestOutputStream(OutputStream os, int initialWrites)
+ {
+ this.os = os;
+ this.initialWrites = initialWrites;
+ }
+ public void write(int b) throws IOException
+ {
+ if (doThrow && ++counter == initialWrites)
+ {
+ log.info("throwing " + exceptionToThrow);
+ threwException = true;
+ throw exceptionToThrow;
+ }
+ os.write(b);
+ }
+ public void write(byte b[], int off, int len) throws IOException
+ {
+ log.info("TestOutputStream: counter = " + (counter + 1) + ", initialWrites = " + initialWrites);
+ if (++counter == initialWrites)
+ {
+ log.info("throwing " + exceptionToThrow);
+ threwException = true;
+ throw exceptionToThrow;
+ }
+ log.info(this + " calling write()");
+ doThrow = false;
+ os.write(b, off, len);
+ os.flush();
+ doThrow = true;
+ log.info(this + " back from write()");
+ }
+ }
+}
\ No newline at end of file
14 years, 3 months