diff options
author | rsuen | 2009-04-28 20:48:24 +0000 |
---|---|---|
committer | rsuen | 2009-04-28 20:48:24 +0000 |
commit | 7d9c699821d6019613fec987bf01da4f4e669a57 (patch) | |
tree | 6117da4a2c0cf3d673d0cf9b32f5120c338824c8 | |
parent | b887e89669e60302457552fb4840f5d391141803 (diff) | |
download | org.eclipse.ecf-7d9c699821d6019613fec987bf01da4f4e669a57.tar.gz org.eclipse.ecf-7d9c699821d6019613fec987bf01da4f4e669a57.tar.xz org.eclipse.ecf-7d9c699821d6019613fec987bf01da4f4e669a57.zip |
Bug 273357 - Abstract implementation of datashare APIs using Java 1.4 NIO
9 files changed, 489 insertions, 0 deletions
diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.classpath b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.classpath new file mode 100644 index 000000000..2fbb7a23e --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.project b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.project new file mode 100644 index 000000000..e83e50a93 --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.ecf.tests.provider.datashare.nio</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.settings/org.eclipse.jdt.core.prefs b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..eb543c71c --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +#Tue Apr 28 16:47:06 EDT 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 +org.eclipse.jdt.core.compiler.compliance=1.4 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning +org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning +org.eclipse.jdt.core.compiler.source=1.3 diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/META-INF/MANIFEST.MF b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/META-INF/MANIFEST.MF new file mode 100644 index 000000000..a3cc0d0db --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/META-INF/MANIFEST.MF @@ -0,0 +1,8 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: NIO Datashare Implementation Tests +Bundle-SymbolicName: org.eclipse.ecf.tests.provider.datashare.nio +Bundle-Version: 1.0.0.qualifier +Fragment-Host: org.eclipse.ecf.provider.datashare.nio;bundle-version="1.0.0" +Bundle-RequiredExecutionEnvironment: J2SE-1.4 +Require-Bundle: org.junit;bundle-version="3.8.2" diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/build.properties b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/build.properties new file mode 100644 index 000000000..34d2e4d2d --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIOChannel.java b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIOChannel.java new file mode 100644 index 000000000..c57571b82 --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIOChannel.java @@ -0,0 +1,43 @@ +/****************************************************************************** + * Copyright (c) 2009 Remy Chi Jian Suen and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Remy Chi Jian Suen - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.provider.datashare.nio; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.ecf.datashare.IChannelListener; +import org.eclipse.ecf.provider.datashare.nio.NIOChannel; +import org.eclipse.ecf.provider.datashare.nio.NIODatashareContainer; + +public class ConcreteNIOChannel extends NIOChannel { + + public ConcreteNIOChannel(NIODatashareContainer datashareContainer, + ID userId, ID id, IChannelListener listener) throws ECFException { + super(datashareContainer, userId, id, listener); + } + + protected void log(IStatus status) { + System.err.println(status.getMessage()); + Throwable t = status.getException(); + if (t != null) { + t.printStackTrace(System.err); + } + } + + public int getPort() { + return getLocalPort(); + } + + protected void sendRequest(ID receiver) throws ECFException { + // nothing to do + } + +} diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIODatashareContainer.java b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIODatashareContainer.java new file mode 100644 index 000000000..cf1caa84d --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ConcreteNIODatashareContainer.java @@ -0,0 +1,57 @@ +/****************************************************************************** + * Copyright (c) 2009 Remy Chi Jian Suen and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Remy Chi Jian Suen - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.provider.datashare.nio; + +import java.util.Map; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.ecf.core.IContainer; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.identity.Namespace; +import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.ecf.datashare.IChannelConfig; +import org.eclipse.ecf.datashare.IChannelListener; +import org.eclipse.ecf.provider.datashare.nio.NIOChannel; +import org.eclipse.ecf.provider.datashare.nio.NIODatashareContainer; + +public class ConcreteNIODatashareContainer extends NIODatashareContainer { + + private IContainer container; + + public ConcreteNIODatashareContainer(IContainer container) { + super(container); + this.container = container; + } + + protected void log(IStatus status) { + System.err.println(status.getMessage()); + Throwable t = status.getException(); + if (t != null) { + t.printStackTrace(System.err); + } + } + + protected NIOChannel createNIOChannel(ID channelId, + IChannelListener listener, Map properties) throws ECFException { + return new ConcreteNIOChannel(this, container.getConnectedID(), + channelId, listener); + } + + protected NIOChannel createNIOChannel(IChannelConfig newChannelConfig) + throws ECFException { + return null; + } + + public Namespace getChannelNamespace() { + return null; + } + +} diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ContainerImpl.java b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ContainerImpl.java new file mode 100644 index 000000000..bbe0f3a5f --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/ContainerImpl.java @@ -0,0 +1,49 @@ +/****************************************************************************** + * Copyright (c) 2009 Remy Chi Jian Suen and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Remy Chi Jian Suen - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.provider.datashare.nio; + +import org.eclipse.ecf.core.BaseContainer; +import org.eclipse.ecf.core.ContainerConnectException; +import org.eclipse.ecf.core.events.ContainerConnectedEvent; +import org.eclipse.ecf.core.events.ContainerDisconnectedEvent; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.identity.IDFactory; +import org.eclipse.ecf.core.security.IConnectContext; + +public class ContainerImpl extends BaseContainer { + + private static int counter = 0; + + private ID connectedId; + + public ContainerImpl() { + super(IDFactory.getDefault().createStringID("A" + counter)); //$NON-NLS-1$ + connectedId = IDFactory.getDefault().createStringID("B" + counter); //$NON-NLS-1$ + + counter++; + } + + public void connect(ID targetId, IConnectContext connectContext) + throws ContainerConnectException { + fireContainerEvent(new ContainerConnectedEvent(getID(), + getConnectedID())); + } + + public void disconnect() { + fireContainerEvent(new ContainerDisconnectedEvent(getID(), + getConnectedID())); + } + + public ID getConnectedID() { + return connectedId; + } + +} diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/NIODatashareTest.java b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/NIODatashareTest.java new file mode 100644 index 000000000..974bf8c25 --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.datashare.nio/src/org/eclipse/ecf/tests/provider/datashare/nio/NIODatashareTest.java @@ -0,0 +1,286 @@ +/****************************************************************************** + * Copyright (c) 2009 Remy Chi Jian Suen and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Remy Chi Jian Suen - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.provider.datashare.nio; + +import java.net.InetSocketAddress; + +import junit.framework.TestCase; + +import org.eclipse.ecf.core.IContainer; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.identity.IDFactory; +import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.ecf.datashare.IChannel; +import org.eclipse.ecf.datashare.IChannelContainerAdapter; +import org.eclipse.ecf.datashare.IChannelListener; +import org.eclipse.ecf.datashare.events.IChannelConnectEvent; +import org.eclipse.ecf.datashare.events.IChannelDisconnectEvent; +import org.eclipse.ecf.datashare.events.IChannelEvent; +import org.eclipse.ecf.datashare.events.IChannelMessageEvent; + +public class NIODatashareTest extends TestCase { + + private static final String LOCALHOST = "127.0.0.1"; //$NON-NLS-1$ + // private static final String LOCALHOST = "localhost"; //$NON-NLS-1$ + // private static final String LOCALHOST = "0.0.0.0"; //$NON-NLS-1$ + + private static final String CHANNEL_NAME = "channel"; //$NON-NLS-1$ + + private Object waitObject = new Object(); + + private Exception exception; + + private IContainer containerA = new ContainerImpl(); + private IContainer containerB = new ContainerImpl(); + + private ConcreteNIODatashareContainer channelContainerA; + private ConcreteNIODatashareContainer channelContainerB; + + private ConcreteNIOChannel channelA; + private ConcreteNIOChannel channelB; + + private static void assertEquals(byte[] expected, byte[] actual) { + // don't use Arrays.equals(byte[], byte[]) to get more accurate failure + // information + assertNotNull(expected); + assertNotNull(actual); + + assertEquals(expected.length, actual.length); + + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], actual[i]); + } + } + + private static ConcreteNIOChannel createChannel( + IChannelContainerAdapter channelContainer) throws ECFException { + return createChannel(channelContainer, null); + } + + private static ConcreteNIOChannel createChannel( + IChannelContainerAdapter channelContainer, IChannelListener listener) + throws ECFException { + return (ConcreteNIOChannel) channelContainer.createChannel(IDFactory + .getDefault().createStringID(CHANNEL_NAME), listener, null); + } + + protected void setUp() throws Exception { + super.setUp(); + + channelContainerA = new ConcreteNIODatashareContainer(containerA); + channelContainerB = new ConcreteNIODatashareContainer(containerB); + + exception = null; + } + + protected void tearDown() throws Exception { + containerA.disconnect(); + containerB.disconnect(); + + channelContainerA = null; + channelContainerB = null; + + channelA = null; + channelB = null; + + // just to make sure we don't have any pending threads + synchronized (waitObject) { + waitObject.notifyAll(); + } + + super.tearDown(); + } + + private void waitForCompletion(long timeout) throws Exception { + synchronized (waitObject) { + waitObject.wait(timeout); + } + + if (exception != null) { + throw exception; + } + } + + private void send(IChannel channel, ID receiver, byte[] data) { + try { + channel.sendMessage(receiver, data); + } catch (ECFException e) { + exception = e; + + synchronized (waitObject) { + waitObject.notify(); + } + } + } + + public void testChannelConnectEvent() throws Exception { + final ID[] eventIds = new ID[2]; + + channelA = createChannel(channelContainerA, new IChannelListener() { + public void handleChannelEvent(IChannelEvent e) { + if (e instanceof IChannelConnectEvent) { + IChannelConnectEvent event = (IChannelConnectEvent) e; + eventIds[0] = event.getChannelID(); + eventIds[1] = event.getTargetID(); + } + } + }); + + containerA.connect(null, null); + + assertEquals(channelA.getID(), eventIds[0]); + assertEquals(containerA.getConnectedID(), eventIds[1]); + } + + /** + * Test that while the container fires events, the datashare implementation + * is ignoring them successfully as it does not have a listener and is not + * throwing runtime exceptions. + */ + public void testChannelConnectEventWithoutListeners() throws Exception { + channelA = createChannel(channelContainerA); + containerA.connect(null, null); + } + + public void testChannelDisconnectEvent() throws Exception { + final ID[] eventIds = new ID[2]; + + channelA = createChannel(channelContainerA, new IChannelListener() { + public void handleChannelEvent(IChannelEvent e) { + if (e instanceof IChannelDisconnectEvent) { + IChannelDisconnectEvent event = (IChannelDisconnectEvent) e; + eventIds[0] = event.getChannelID(); + eventIds[1] = event.getTargetID(); + } + } + }); + + containerA.disconnect(); + + assertEquals(channelA.getID(), eventIds[0]); + // technically, getConnectedID() should return null when a container has + // disconnected, but anyway... + assertEquals(containerA.getConnectedID(), eventIds[1]); + } + + /** + * Test that while the container fires events, the datashare implementation + * is ignoring them successfully as it does not have a listener and is not + * throwing runtime exceptions. + */ + public void testChannelDisconnectEventWithoutListeners() throws Exception { + channelA = createChannel(channelContainerA, null); + containerA.disconnect(); + } + + public void testOneWaySend() throws Exception { + final byte[][] actual = new byte[1][]; + + channelA = createChannel(channelContainerA); + + int targetPort = channelA.getPort(); + + channelB = createChannel(channelContainerB, new IChannelListener() { + public void handleChannelEvent(IChannelEvent event) { + if (event instanceof IChannelMessageEvent) { + actual[0] = ((IChannelMessageEvent) event).getData(); + + synchronized (waitObject) { + waitObject.notify(); + } + } + } + }); + + byte[] expected = { 1, 2, 3 }; + + channelA.sendMessage(containerB.getConnectedID(), expected); + + channelContainerB.enqueue(new InetSocketAddress(LOCALHOST, targetPort)); + + waitForCompletion(5000); + + assertEquals(expected, actual[0]); + } + + public void testOneWaySend16k() throws Exception { + final byte[][] actual = new byte[1][]; + + channelA = createChannel(channelContainerA); + + int targetPort = channelA.getPort(); + + channelB = createChannel(channelContainerB, new IChannelListener() { + public void handleChannelEvent(IChannelEvent event) { + if (event instanceof IChannelMessageEvent) { + actual[0] = ((IChannelMessageEvent) event).getData(); + + synchronized (waitObject) { + waitObject.notify(); + } + } + } + }); + + byte[] expected = new byte[16384]; + for (int i = 0; i < expected.length; i++) { + expected[i] = (byte) (i % 128); + } + + channelA.sendMessage(containerB.getConnectedID(), expected); + + channelContainerB.enqueue(new InetSocketAddress(LOCALHOST, targetPort)); + + waitForCompletion(10000); + + assertEquals(expected, actual[0]); + } + + public void testSendAndReply() throws Exception { + final byte[] expected1 = { 1, 2, 3 }; + final byte[] expected2 = { 4, 5, 6 }; + + final byte[][] actual = new byte[2][]; + + channelA = createChannel(channelContainerA, new IChannelListener() { + public void handleChannelEvent(IChannelEvent event) { + if (event instanceof IChannelMessageEvent) { + actual[1] = ((IChannelMessageEvent) event).getData(); + + synchronized (waitObject) { + waitObject.notify(); + } + } + } + }); + + int targetPort = channelA.getPort(); + + channelB = createChannel(channelContainerB, new IChannelListener() { + public void handleChannelEvent(IChannelEvent event) { + if (event instanceof IChannelMessageEvent) { + actual[0] = ((IChannelMessageEvent) event).getData(); + + send(channelB, containerA.getConnectedID(), expected2); + } + } + }); + + channelA.sendMessage(containerB.getConnectedID(), expected1); + + channelContainerB.enqueue(new InetSocketAddress(LOCALHOST, targetPort)); + + waitForCompletion(5000); + + assertEquals(expected1, actual[0]); + assertEquals(expected2, actual[1]); + } +} |