summaryrefslogtreecommitdiff
path: root/java/test/Ice/stream/Client.java
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2004-10-20 00:04:40 +0000
committerMark Spruiell <mes@zeroc.com>2004-10-20 00:04:40 +0000
commit486c95495b7457fa035001ba026b1b9b848b794b (patch)
tree3bf070fb9ec87b39ab3423823fdad0efb94c7013 /java/test/Ice/stream/Client.java
parentWin32 fixes (diff)
downloadice-486c95495b7457fa035001ba026b1b9b848b794b.tar.bz2
ice-486c95495b7457fa035001ba026b1b9b848b794b.tar.xz
ice-486c95495b7457fa035001ba026b1b9b848b794b.zip
adding streaming interface
Diffstat (limited to 'java/test/Ice/stream/Client.java')
-rw-r--r--java/test/Ice/stream/Client.java457
1 files changed, 457 insertions, 0 deletions
diff --git a/java/test/Ice/stream/Client.java b/java/test/Ice/stream/Client.java
new file mode 100644
index 00000000000..7782183ac29
--- /dev/null
+++ b/java/test/Ice/stream/Client.java
@@ -0,0 +1,457 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved.
+//
+// This copy of Ice is licensed to you under the terms described in the
+// ICE_LICENSE file included in this distribution.
+//
+// **********************************************************************
+
+public class Client
+{
+ private static void
+ test(boolean b)
+ {
+ if(!b)
+ {
+ throw new RuntimeException();
+ }
+ }
+
+ private static class TestObjectWriter extends Ice.ObjectWriter
+ {
+ TestObjectWriter(Test.MyClass obj)
+ {
+ this.obj = obj;
+ }
+
+ public void
+ write(Ice.OutputStream out)
+ {
+ obj.__write(out);
+ called = true;
+ }
+
+ Test.MyClass obj;
+ boolean called = false;
+ }
+
+ private static class TestObjectReader extends Ice.ObjectReader
+ {
+ public void
+ read(Ice.InputStream in, boolean rid)
+ {
+ obj = new Test.MyClass();
+ obj.__read(in, rid);
+ called = true;
+ }
+
+ Test.MyClass obj;
+ boolean called = false;
+ }
+
+ private static class TestObjectFactory extends Ice.LocalObjectImpl implements Ice.ObjectFactory
+ {
+ public Ice.Object
+ create(String type)
+ {
+ assert(type.equals(Test.MyClass.ice_staticId()));
+ return new TestObjectReader();
+ }
+
+ public void
+ destroy()
+ {
+ }
+ }
+
+ private static class TestReadObjectCallback implements Ice.ReadObjectCallback
+ {
+ public void
+ invoke(Ice.Object obj)
+ {
+ this.obj = obj;
+ }
+
+ Ice.Object obj;
+ }
+
+ private static int
+ run(String[] args, Ice.Communicator communicator)
+ {
+ Ice.InputStream in;
+ Ice.OutputStream out;
+
+ System.out.print("testing primitive types... ");
+ System.out.flush();
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeBool(true);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readBool());
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeByte((byte)1);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readByte() == (byte)1);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeShort((short)2);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readShort() == (short)2);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeInt(3);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readInt() == 3);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeLong(4);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readLong() == 4);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeFloat((float)5.0);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readFloat() == (float)5.0);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeDouble(6.0);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readDouble() == 6.0);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ out.writeString("hello world");
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(in.readString().equals("hello world"));
+ in.finished();
+ }
+
+ System.out.println("ok");
+
+ System.out.print("testing constructed types... ");
+ System.out.flush();
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ Test.MyEnum.enum3.__write(out);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ test(Test.MyEnum.__read(in) == Test.MyEnum.enum3);
+ in.finished();
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ Test.SmallStruct s = new Test.SmallStruct();
+ s.bo = true;
+ s.by = (byte)1;
+ s.sh = (short)2;
+ s.i = 3;
+ s.l = 4;
+ s.f = (float)5.0;
+ s.d = 6.0;
+ s.str = "7";
+ s.e = Test.MyEnum.enum2;
+ s.p = Test.MyClassPrxHelper.uncheckedCast(communicator.stringToProxy("test:default"));
+ s.__write(out);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ Test.SmallStruct s2 = new Test.SmallStruct();
+ s2.__read(in);
+ in.finished();
+ test(s2.equals(s));
+ }
+
+ {
+ final boolean[] arr =
+ {
+ true,
+ false,
+ true,
+ false
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.BoolSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ boolean[] arr2 = Test.BoolSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final byte[] arr =
+ {
+ (byte)0x01,
+ (byte)0x11,
+ (byte)0x12,
+ (byte)0x22
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.ByteSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ byte[] arr2 = Test.ByteSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final short[] arr =
+ {
+ (short)0x01,
+ (short)0x11,
+ (short)0x12,
+ (short)0x22
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.ShortSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ short[] arr2 = Test.ShortSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final int[] arr =
+ {
+ 0x01,
+ 0x11,
+ 0x12,
+ 0x22
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.IntSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ int[] arr2 = Test.IntSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final long[] arr =
+ {
+ 0x01,
+ 0x11,
+ 0x12,
+ 0x22
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.LongSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ long[] arr2 = Test.LongSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final float[] arr =
+ {
+ (float)1,
+ (float)2,
+ (float)3,
+ (float)4
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.FloatSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ float[] arr2 = Test.FloatSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final double[] arr =
+ {
+ (double)1,
+ (double)2,
+ (double)3,
+ (double)4
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.DoubleSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ double[] arr2 = Test.DoubleSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final String[] arr =
+ {
+ "string1",
+ "string2",
+ "string3",
+ "string4"
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.StringSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ String[] arr2 = Test.StringSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ final Test.MyEnum[] arr =
+ {
+ Test.MyEnum.enum3,
+ Test.MyEnum.enum2,
+ Test.MyEnum.enum1,
+ Test.MyEnum.enum2
+ };
+ out = Ice.Util.createOutputStream(communicator);
+ Test.MyEnumSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ Test.MyEnum[] arr2 = Test.MyEnumSHelper.read(in);
+ in.finished();
+ test(java.util.Arrays.equals(arr2, arr));
+ }
+
+ {
+ Test.MyClass[] arr = new Test.MyClass[4];
+ for(int i = 0; i < arr.length; ++i)
+ {
+ arr[i] = new Test.MyClass();
+ arr[i].c = arr[i];
+ arr[i].o = arr[i];
+ arr[i].s = new Test.SmallStruct();
+ arr[i].s.e = Test.MyEnum.enum2;
+ arr[i].seq1 = new boolean[] { true, false, true, false };
+ arr[i].seq2 = new byte[] { (byte)1, (byte)2, (byte)3, (byte)4 };
+ arr[i].seq3 = new short[] { (short)1, (short)2, (short)3, (short)4 };
+ arr[i].seq4 = new int[] { 1, 2, 3, 4 };
+ arr[i].seq5 = new long[] { 1, 2, 3, 4 };
+ arr[i].seq6 = new float[] { (float)1, (float)2, (float)3, (float)4 };
+ arr[i].seq7 = new double[] { (double)1, (double)2, (double)3, (double)4 };
+ arr[i].seq8 = new String[] { "string1", "string2", "string3", "string4" };
+ arr[i].seq9 = new Test.MyEnum[] { Test.MyEnum.enum3, Test.MyEnum.enum2, Test.MyEnum.enum1 };
+ arr[i].seq10 = new Test.MyClass[4]; // null elements.
+ arr[i].d = new java.util.HashMap();
+ arr[i].d.put("hi", arr[i]);
+ }
+ out = Ice.Util.createOutputStream(communicator);
+ Test.MyClassSHelper.write(out, arr);
+ byte[] data = out.finished();
+ in = Ice.Util.createInputStream(communicator, data);
+ Test.MyClass[] arr2 = Test.MyClassSHelper.read(in);
+ in.finished();
+ test(arr2.length == arr.length);
+ for(int i = 0; i < arr2.length; ++i)
+ {
+ test(arr2[i] != null);
+ test(arr2[i].c == arr2[i]);
+ test(arr2[i].o == arr2[i]);
+ test(arr2[i].s.e == Test.MyEnum.enum2);
+ test(java.util.Arrays.equals(arr2[i].seq1, arr[i].seq1));
+ test(java.util.Arrays.equals(arr2[i].seq2, arr[i].seq2));
+ test(java.util.Arrays.equals(arr2[i].seq3, arr[i].seq3));
+ test(java.util.Arrays.equals(arr2[i].seq4, arr[i].seq4));
+ test(java.util.Arrays.equals(arr2[i].seq5, arr[i].seq5));
+ test(java.util.Arrays.equals(arr2[i].seq6, arr[i].seq6));
+ test(java.util.Arrays.equals(arr2[i].seq7, arr[i].seq7));
+ test(java.util.Arrays.equals(arr2[i].seq8, arr[i].seq8));
+ test(java.util.Arrays.equals(arr2[i].seq9, arr[i].seq9));
+ test(arr2[i].d.get("hi") == arr2[i]);
+ }
+ }
+
+ {
+ out = Ice.Util.createOutputStream(communicator);
+ Test.MyClass obj = new Test.MyClass();
+ obj.s = new Test.SmallStruct();
+ obj.s.e = Test.MyEnum.enum2;
+ TestObjectWriter writer = new TestObjectWriter(obj);
+ out.writeObject(writer);
+ byte[] data = out.finished();
+ test(writer.called);
+ communicator.removeObjectFactory(Test.MyClass.ice_staticId()); // Default factory.
+ communicator.addObjectFactory(new TestObjectFactory(), Test.MyClass.ice_staticId());
+ in = Ice.Util.createInputStream(communicator, data);
+ TestReadObjectCallback cb = new TestReadObjectCallback();
+ in.readObject(cb);
+ in.finished();
+ test(cb.obj != null);
+ test(cb.obj instanceof TestObjectReader);
+ TestObjectReader reader = (TestObjectReader)cb.obj;
+ test(reader.called);
+ test(reader.obj != null);
+ test(reader.obj.s.e == Test.MyEnum.enum2);
+ }
+
+ System.out.println("ok");
+
+ return 0;
+ }
+
+ public static void
+ main(String[] args)
+ {
+ int status = 0;
+ Ice.Communicator communicator = null;
+
+ try
+ {
+ communicator = Ice.Util.initialize(args);
+ status = run(args, communicator);
+ }
+ catch(Ice.LocalException ex)
+ {
+ ex.printStackTrace();
+ status = 1;
+ }
+
+ if(communicator != null)
+ {
+ try
+ {
+ communicator.destroy();
+ }
+ catch(Ice.LocalException ex)
+ {
+ ex.printStackTrace();
+ status = 1;
+ }
+ }
+
+ System.exit(status);
+ }
+}