java: Add JUnit to ivy config. Convert Nonblocking server tests to use JUnit. Framework laid to convert the remainder of the tests.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@927693 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/build.xml b/lib/java/build.xml
index e8e5d78..4cf7c8f 100644
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -163,7 +163,34 @@
<javac debug="true" srcdir="${src.test}" destdir="${build.test}" classpathref="test.classpath" />
</target>
- <target name="test" description="Run the full test suite" depends="compile-test">
+ <property name="build.test" location="${build.dir}/test"/>
+ <property name="test.junit.output.format" value="plain"/>
+ <property name="test.timeout" value="2000000"/>
+ <property name="test.src.dir" location="${basedir}/test"/>
+ <property name="test.log.dir" value="${build.test}/log"/>
+
+ <target name="junit-test" description="Run the JUnit test suite" depends="compile-test">
+ <mkdir dir="${test.log.dir}"/>
+ <junit
+ printsummary="yes" showoutput="${test.output}"
+ haltonfailure="no" fork="yes" forkmode="once" maxmemory="512m"
+ errorProperty="tests.failed" failureProperty="tests.failed"
+ timeout="${test.timeout}"
+ >
+ <sysproperty key="build.test" value="${build.test}"/>
+ <classpath refid="test.classpath"/>
+ <formatter type="${test.junit.output.format}" />
+ <batchtest todir="${test.log.dir}" unless="testcase">
+ <fileset dir="${test.src.dir}" includes="**/Test*.java" />
+ </batchtest>
+ <batchtest todir="${test.log.dir}" if="testcase">
+ <fileset dir="${test.src.dir}" includes="**/${testcase}.java" />
+ </batchtest>
+ </junit>
+ <fail if="tests.failed">Tests failed!</fail>
+ </target>
+
+ <target name="deprecated-test" description="Run the non-JUnit test suite" depends="compile-test">
<java classname="org.apache.thrift.test.JSONProtoTest"
classpathref="test.classpath" failonerror="true" />
<java classname="org.apache.thrift.test.TCompactProtocolTest"
@@ -192,6 +219,8 @@
classpathref="test.classpath" failonerror="true" />
</target>
+ <target name="test" description="Run the full test suite" depends="junit-test,deprecated-test"/>
+
<target name="testclient" description="Run a test client">
<java classname="org.apache.thrift.test.TestClient"
classpathref="test.classpath" failonerror="true">
diff --git a/lib/java/ivy.xml b/lib/java/ivy.xml
index ca6ecb3..aafc0da 100644
--- a/lib/java/ivy.xml
+++ b/lib/java/ivy.xml
@@ -20,5 +20,6 @@
<dependency org="org.slf4j" name="slf4j-api" rev="1.5.8" conf="* -> *,!sources,!javadoc"/>
<dependency org="org.slf4j" name="slf4j-simple" rev="1.5.8" conf="* -> *,!sources,!javadoc"/>
<dependency org="commons-lang" name="commons-lang" rev="2.4" conf="* -> *,!sources,!javadoc"/>
+ <dependency org="junit" name="junit" rev="4.4" conf="* -> *,!sources,!javadoc"/>
</dependencies>
</ivy-module>
diff --git a/lib/java/test/org/apache/thrift/server/ServerTestBase.java b/lib/java/test/org/apache/thrift/server/ServerTestBase.java
new file mode 100644
index 0000000..05d0d8d
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/server/ServerTestBase.java
@@ -0,0 +1,462 @@
+package org.apache.thrift.server;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.TProcessor;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.transport.TFramedTransport;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+
+import thrift.test.Insanity;
+import thrift.test.Numberz;
+import thrift.test.ThriftTest;
+import thrift.test.Xception;
+import thrift.test.Xception2;
+import thrift.test.Xtruct;
+import thrift.test.Xtruct2;
+
+public abstract class ServerTestBase extends TestCase {
+
+ public static class TestHandler implements ThriftTest.Iface {
+
+ public TestHandler() {}
+
+ public void testVoid() {
+ System.out.print("testVoid()\n");
+ }
+
+ public String testString(String thing) {
+ System.out.print("testString(\"" + thing + "\")\n");
+ return thing;
+ }
+
+ public byte testByte(byte thing) {
+ System.out.print("testByte(" + thing + ")\n");
+ return thing;
+ }
+
+ public int testI32(int thing) {
+ System.out.print("testI32(" + thing + ")\n");
+ return thing;
+ }
+
+ public long testI64(long thing) {
+ System.out.print("testI64(" + thing + ")\n");
+ return thing;
+ }
+
+ public double testDouble(double thing) {
+ System.out.print("testDouble(" + thing + ")\n");
+ return thing;
+ }
+
+ public Xtruct testStruct(Xtruct thing) {
+ System.out.print("testStruct({" +
+ "\"" + thing.string_thing + "\", " +
+ thing.byte_thing + ", " +
+ thing.i32_thing + ", " +
+ thing.i64_thing + "})\n");
+ return thing;
+ }
+
+ public Xtruct2 testNest(Xtruct2 nest) {
+ Xtruct thing = nest.struct_thing;
+ System.out.print("testNest({" +
+ nest.byte_thing + ", {" +
+ "\"" + thing.string_thing + "\", " +
+ thing.byte_thing + ", " +
+ thing.i32_thing + ", " +
+ thing.i64_thing + "}, " +
+ nest.i32_thing + "})\n");
+ return nest;
+ }
+
+ public Map<Integer,Integer> testMap(Map<Integer,Integer> thing) {
+ System.out.print("testMap({");
+ boolean first = true;
+ for (int key : thing.keySet()) {
+ if (first) {
+ first = false;
+ } else {
+ System.out.print(", ");
+ }
+ System.out.print(key + " => " + thing.get(key));
+ }
+ System.out.print("})\n");
+ return thing;
+ }
+
+ public Set<Integer> testSet(Set<Integer> thing) {
+ System.out.print("testSet({");
+ boolean first = true;
+ for (int elem : thing) {
+ if (first) {
+ first = false;
+ } else {
+ System.out.print(", ");
+ }
+ System.out.print(elem);
+ }
+ System.out.print("})\n");
+ return thing;
+ }
+
+ public List<Integer> testList(List<Integer> thing) {
+ System.out.print("testList({");
+ boolean first = true;
+ for (int elem : thing) {
+ if (first) {
+ first = false;
+ } else {
+ System.out.print(", ");
+ }
+ System.out.print(elem);
+ }
+ System.out.print("})\n");
+ return thing;
+ }
+
+ public Numberz testEnum(Numberz thing) {
+ System.out.print("testEnum(" + thing + ")\n");
+ return thing;
+ }
+
+ public long testTypedef(long thing) {
+ System.out.print("testTypedef(" + thing + ")\n");
+ return thing;
+ }
+
+ public Map<Integer,Map<Integer,Integer>> testMapMap(int hello) {
+ System.out.print("testMapMap(" + hello + ")\n");
+ Map<Integer,Map<Integer,Integer>> mapmap =
+ new HashMap<Integer,Map<Integer,Integer>>();
+
+ HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
+ HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
+ for (int i = 1; i < 5; i++) {
+ pos.put(i, i);
+ neg.put(-i, -i);
+ }
+
+ mapmap.put(4, pos);
+ mapmap.put(-4, neg);
+
+ return mapmap;
+ }
+
+ public Map<Long, Map<Numberz,Insanity>> testInsanity(Insanity argument) {
+ System.out.print("testInsanity()\n");
+
+ Xtruct hello = new Xtruct();
+ hello.string_thing = "Hello2";
+ hello.byte_thing = 2;
+ hello.i32_thing = 2;
+ hello.i64_thing = 2;
+
+ Xtruct goodbye = new Xtruct();
+ goodbye.string_thing = "Goodbye4";
+ goodbye.byte_thing = (byte)4;
+ goodbye.i32_thing = 4;
+ goodbye.i64_thing = (long)4;
+
+ Insanity crazy = new Insanity();
+ crazy.userMap = new HashMap<Numberz, Long>();
+ crazy.xtructs = new ArrayList<Xtruct>();
+
+ crazy.userMap.put(Numberz.EIGHT, (long)8);
+ crazy.xtructs.add(goodbye);
+
+ Insanity looney = new Insanity();
+ crazy.userMap.put(Numberz.FIVE, (long)5);
+ crazy.xtructs.add(hello);
+
+ HashMap<Numberz,Insanity> first_map = new HashMap<Numberz, Insanity>();
+ HashMap<Numberz,Insanity> second_map = new HashMap<Numberz, Insanity>();;
+
+ first_map.put(Numberz.TWO, crazy);
+ first_map.put(Numberz.THREE, crazy);
+
+ second_map.put(Numberz.SIX, looney);
+
+ Map<Long,Map<Numberz,Insanity>> insane =
+ new HashMap<Long, Map<Numberz,Insanity>>();
+ insane.put((long)1, first_map);
+ insane.put((long)2, second_map);
+
+ return insane;
+ }
+
+ public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short,String> arg3, Numberz arg4, long arg5) {
+ System.out.print("testMulti()\n");
+
+ Xtruct hello = new Xtruct();;
+ hello.string_thing = "Hello2";
+ hello.byte_thing = arg0;
+ hello.i32_thing = arg1;
+ hello.i64_thing = arg2;
+ return hello;
+ }
+
+ public void testException(String arg) throws Xception {
+ System.out.print("testException("+arg+")\n");
+ if (arg.equals("Xception")) {
+ Xception x = new Xception();
+ x.errorCode = 1001;
+ x.message = "This is an Xception";
+ throw x;
+ }
+ return;
+ }
+
+ public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
+ System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
+ if (arg0.equals("Xception")) {
+ Xception x = new Xception();
+ x.errorCode = 1001;
+ x.message = "This is an Xception";
+ throw x;
+ } else if (arg0.equals("Xception2")) {
+ Xception2 x = new Xception2();
+ x.errorCode = 2002;
+ x.struct_thing = new Xtruct();
+ x.struct_thing.string_thing = "This is an Xception2";
+ throw x;
+ }
+
+ Xtruct result = new Xtruct();
+ result.string_thing = arg1;
+ return result;
+ }
+
+ public void testOneway(int sleepFor) {
+ System.out.println("testOneway(" + Integer.toString(sleepFor) +
+ ") => sleeping...");
+ try {
+ Thread.sleep(sleepFor * 1000);
+ System.out.println("Done sleeping!");
+ } catch (InterruptedException ie) {
+ throw new RuntimeException(ie);
+ }
+ }
+ } // class TestHandler
+
+ private static final List<TProtocolFactory> PROTOCOLS = Arrays.asList(
+ new TBinaryProtocol.Factory(),
+ new TCompactProtocol.Factory());
+
+ protected static final String HOST = "localhost";
+ protected static final int PORT = 9090;
+ protected static final int SOCKET_TIMEOUT = 1000;
+ private static final Xtruct XSTRUCT = new Xtruct("Zero", (byte) 1, -3, -5);
+ private static final Xtruct2 XSTRUCT2 = new Xtruct2((byte)1, XSTRUCT, 5);
+
+ public abstract void startServer(TProcessor processor, TProtocolFactory protoFactory) throws Exception;
+
+ public abstract void stopServer() throws Exception;
+
+ public abstract TTransport getTransport() throws Exception;
+
+ private void testByte(ThriftTest.Client testClient) throws TException {
+ byte i8 = testClient.testByte((byte)1);
+ assertEquals(1, i8);
+ }
+
+ private void testDouble(ThriftTest.Client testClient) throws TException {
+ double dub = testClient.testDouble(5.325098235);
+ assertEquals(5.325098235, dub);
+ }
+
+ private void testEnum(ThriftTest.Client testClient) throws TException {
+ assertEquals(Numberz.ONE, testClient.testEnum(Numberz.ONE));
+ assertEquals(Numberz.TWO, testClient.testEnum(Numberz.TWO));
+ assertEquals(Numberz.THREE, testClient.testEnum(Numberz.THREE));
+ assertEquals(Numberz.FIVE, testClient.testEnum(Numberz.FIVE));
+ assertEquals(Numberz.EIGHT, testClient.testEnum(Numberz.EIGHT));
+ }
+
+ private void testI32(ThriftTest.Client testClient) throws TException {
+ int i32 = testClient.testI32(-1);
+ assertEquals(i32, -1);
+ }
+
+ private void testI64(ThriftTest.Client testClient) throws TException {
+ long i64 = testClient.testI64(-34359738368L);
+ assertEquals(i64, -34359738368L);
+ }
+
+ // todo: add assertions
+ private void testInsanity(ThriftTest.Client testClient) throws TException {
+ Insanity insane;
+
+ insane = new Insanity();
+ insane.userMap = new HashMap<Numberz, Long>();
+ insane.userMap.put(Numberz.FIVE, (long)5000);
+ Xtruct truck = new Xtruct();
+ truck.string_thing = "Truck";
+ truck.byte_thing = (byte)8;
+ truck.i32_thing = 8;
+ truck.i64_thing = 8;
+ insane.xtructs = new ArrayList<Xtruct>();
+ insane.xtructs.add(truck);
+ System.out.print("testInsanity()");
+ Map<Long,Map<Numberz,Insanity>> whoa =
+ testClient.testInsanity(insane);
+ System.out.print(" = {");
+ for (long key : whoa.keySet()) {
+ Map<Numberz,Insanity> val = whoa.get(key);
+ System.out.print(key + " => {");
+
+ for (Numberz k2 : val.keySet()) {
+ Insanity v2 = val.get(k2);
+ System.out.print(k2 + " => {");
+ Map<Numberz, Long> userMap = v2.userMap;
+ System.out.print("{");
+ if (userMap != null) {
+ for (Numberz k3 : userMap.keySet()) {
+ System.out.print(k3 + " => " + userMap.get(k3) + ", ");
+ }
+ }
+ System.out.print("}, ");
+
+ List<Xtruct> xtructs = v2.xtructs;
+ System.out.print("{");
+ if (xtructs != null) {
+ for (Xtruct x : xtructs) {
+ System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
+ }
+ }
+ System.out.print("}");
+
+ System.out.print("}, ");
+ }
+ System.out.print("}, ");
+ }
+ System.out.print("}\n");
+ }
+
+ public void testIt() throws Exception {
+
+ for (TProtocolFactory protoFactory : PROTOCOLS) {
+ TestHandler handler = new TestHandler();
+ ThriftTest.Processor processor = new ThriftTest.Processor(handler);
+
+ startServer(processor, protoFactory);
+
+ TTransport transport;
+
+ TSocket socket = new TSocket(HOST, PORT);
+ socket.setTimeout(SOCKET_TIMEOUT);
+ transport = socket;
+ transport = new TFramedTransport(transport);
+
+ TProtocol protocol = protoFactory.getProtocol(transport);
+ ThriftTest.Client testClient = new ThriftTest.Client(protocol);
+
+ transport.open();
+ testVoid(testClient);
+ testString(testClient);
+ testByte(testClient);
+ testI32(testClient);
+ testI64(testClient);
+ testDouble(testClient);
+ testStruct(testClient);
+ testNestedStruct(testClient);
+ testMap(testClient);
+ testSet(testClient);
+ testList(testClient);
+ testEnum(testClient);
+ testTypedef(testClient);
+ testNestedMap(testClient);
+ testInsanity(testClient);
+ testOneway(testClient);
+ transport.close();
+
+ stopServer();
+ }
+ }
+
+ private void testList(ThriftTest.Client testClient) throws TException {
+ List<Integer> listout = new ArrayList<Integer>();
+ for (int i = -2; i < 3; ++i) {
+ listout.add(i);
+ }
+ List<Integer> listin = testClient.testList(listout);
+ assertEquals(listout, listin);
+ }
+
+ private void testMap(ThriftTest.Client testClient) throws TException {
+ Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
+ for (int i = 0; i < 5; ++i) {
+ mapout.put(i, i-10);
+ }
+ Map<Integer,Integer> mapin = testClient.testMap(mapout);
+ assertEquals(mapout, mapin);
+ }
+
+ private void testNestedMap(ThriftTest.Client testClient) throws TException {
+ Map<Integer,Map<Integer,Integer>> mm =
+ testClient.testMapMap(1);
+ Map<Integer,Map<Integer,Integer>> mapmap =
+ new HashMap<Integer,Map<Integer,Integer>>();
+
+ HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
+ HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
+ for (int i = 1; i < 5; i++) {
+ pos.put(i, i);
+ neg.put(-i, -i);
+ }
+
+ mapmap.put(4, pos);
+ mapmap.put(-4, neg);
+ assertEquals(mapmap, mm);
+ }
+
+ private void testNestedStruct(ThriftTest.Client testClient) throws TException {
+ Xtruct2 in2 = testClient.testNest(XSTRUCT2);
+ assertEquals(XSTRUCT2, in2);
+ }
+
+ private void testOneway(ThriftTest.Client testClient) throws Exception {
+ testClient.testOneway(3);
+ }
+
+ private void testSet(ThriftTest.Client testClient) throws TException {
+ Set<Integer> setout = new HashSet<Integer>();
+ for (int i = -2; i < 3; ++i) {
+ setout.add(i);
+ }
+ Set<Integer> setin = testClient.testSet(setout);
+ assertEquals(setout, setin);
+ }
+
+ private void testString(ThriftTest.Client testClient) throws TException {
+ String s = testClient.testString("Test");
+ assertEquals("Test", s);
+ }
+
+ private void testStruct(ThriftTest.Client testClient) throws TException {
+ assertEquals(XSTRUCT, testClient.testStruct(XSTRUCT));
+ }
+
+ private void testTypedef(ThriftTest.Client testClient) throws TException {
+ assertEquals(309858235082523L, testClient.testTypedef(309858235082523L));
+ }
+
+ private void testVoid(ThriftTest.Client testClient) throws TException {
+ testClient.testVoid();
+ }
+
+}
diff --git a/lib/java/test/org/apache/thrift/server/TestHsHaServer.java b/lib/java/test/org/apache/thrift/server/TestHsHaServer.java
new file mode 100644
index 0000000..c355d3e
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/server/TestHsHaServer.java
@@ -0,0 +1,11 @@
+package org.apache.thrift.server;
+
+import org.apache.thrift.TProcessor;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.transport.TNonblockingServerSocket;
+
+public class TestHsHaServer extends TestNonblockingServer {
+ protected TServer getServer(TProcessor processor, TNonblockingServerSocket socket, TProtocolFactory protoFactory) {
+ return new THsHaServer(processor, socket, protoFactory);
+ }
+}
diff --git a/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java b/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java
new file mode 100644
index 0000000..3aac573
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java
@@ -0,0 +1,58 @@
+package org.apache.thrift.server;
+
+
+import org.apache.thrift.TProcessor;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.transport.TFramedTransport;
+import org.apache.thrift.transport.TNonblockingServerSocket;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+
+public class TestNonblockingServer extends ServerTestBase {
+
+ private Thread serverThread;
+ private TServer server;
+
+ protected TServer getServer(TProcessor processor, TNonblockingServerSocket socket, TProtocolFactory protoFactory) {
+ return new TNonblockingServer(processor, socket, protoFactory);
+ }
+
+ @Override
+ public void startServer(final TProcessor processor, final TProtocolFactory protoFactory) throws Exception {
+ serverThread = new Thread() {
+ public void run() {
+ try {
+ // Transport
+ TNonblockingServerSocket tServerSocket =
+ new TNonblockingServerSocket(PORT);
+
+ server = getServer(processor, tServerSocket, protoFactory);
+
+ // Run it
+ System.out.println("Starting the server on port " + PORT + "...");
+ server.serve();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
+ };
+ serverThread.start();
+ Thread.sleep(1000);
+ }
+
+ @Override
+ public void stopServer() throws Exception {
+ server.stop();
+ try {
+ serverThread.join();
+ } catch (InterruptedException e) {}
+ }
+
+ @Override
+ public TTransport getTransport() throws Exception {
+ TSocket socket = new TSocket(HOST, PORT);
+ socket.setTimeout(SOCKET_TIMEOUT);
+ return new TFramedTransport(socket);
+ }
+}
diff --git a/lib/java/test/org/apache/thrift/test/TestClient.java b/lib/java/test/org/apache/thrift/test/TestClient.java
deleted file mode 100644
index aee3bbf..0000000
--- a/lib/java/test/org/apache/thrift/test/TestClient.java
+++ /dev/null
@@ -1,423 +0,0 @@
-/*
- * 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.thrift.test;
-
-// Generated code
-import thrift.test.*;
-
-import org.apache.thrift.TApplicationException;
-import org.apache.thrift.TSerializer;
-import org.apache.thrift.transport.TTransport;
-import org.apache.thrift.transport.TSocket;
-import org.apache.thrift.transport.THttpClient;
-import org.apache.thrift.transport.TFramedTransport;
-import org.apache.thrift.transport.TTransportException;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TSimpleJSONProtocol;
-
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.List;
-import java.util.ArrayList;
-
-/**
- * Test Java client for thrift. Essentially just a copy of the C++ version,
- * this makes a variety of requests to enable testing for both performance and
- * correctness of the output.
- *
- */
-public class TestClient {
- public static void main(String [] args) {
- try {
- String host = "localhost";
- int port = 9090;
- String url = null;
- int numTests = 1;
- boolean framed = false;
-
- int socketTimeout = 1000;
-
- try {
- for (int i = 0; i < args.length; ++i) {
- if (args[i].equals("-h")) {
- String[] hostport = (args[++i]).split(":");
- host = hostport[0];
- port = Integer.valueOf(hostport[1]);
- } else if (args[i].equals("-f") || args[i].equals("-framed")) {
- framed = true;
- } else if (args[i].equals("-u")) {
- url = args[++i];
- } else if (args[i].equals("-n")) {
- numTests = Integer.valueOf(args[++i]);
- } else if (args[i].equals("-timeout")) {
- socketTimeout = Integer.valueOf(args[++i]);
- }
- }
- } catch (Exception x) {
- x.printStackTrace();
- }
-
- TTransport transport;
-
- if (url != null) {
- transport = new THttpClient(url);
- } else {
- TSocket socket = new TSocket(host, port);
- socket.setTimeout(socketTimeout);
- transport = socket;
- if (framed) {
- transport = new TFramedTransport(transport);
- }
- }
-
- TBinaryProtocol binaryProtocol =
- new TBinaryProtocol(transport);
- ThriftTest.Client testClient =
- new ThriftTest.Client(binaryProtocol);
- Insanity insane = new Insanity();
-
- long timeMin = 0;
- long timeMax = 0;
- long timeTot = 0;
-
- for (int test = 0; test < numTests; ++test) {
-
- /**
- * CONNECT TEST
- */
- System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
- try {
- transport.open();
- } catch (TTransportException ttx) {
- System.out.println("Connect failed: " + ttx.getMessage());
- continue;
- }
-
- long start = System.nanoTime();
-
- /**
- * VOID TEST
- */
- try {
- System.out.print("testVoid()");
- testClient.testVoid();
- System.out.print(" = void\n");
- } catch (TApplicationException tax) {
- tax.printStackTrace();
- }
-
- /**
- * STRING TEST
- */
- System.out.print("testString(\"Test\")");
- String s = testClient.testString("Test");
- System.out.print(" = \"" + s + "\"\n");
-
- /**
- * BYTE TEST
- */
- System.out.print("testByte(1)");
- byte i8 = testClient.testByte((byte)1);
- System.out.print(" = " + i8 + "\n");
-
- /**
- * I32 TEST
- */
- System.out.print("testI32(-1)");
- int i32 = testClient.testI32(-1);
- System.out.print(" = " + i32 + "\n");
-
- /**
- * I64 TEST
- */
- System.out.print("testI64(-34359738368)");
- long i64 = testClient.testI64(-34359738368L);
- System.out.print(" = " + i64 + "\n");
-
- /**
- * DOUBLE TEST
- */
- System.out.print("testDouble(5.325098235)");
- double dub = testClient.testDouble(5.325098235);
- System.out.print(" = " + dub + "\n");
-
- /**
- * STRUCT TEST
- */
- System.out.print("testStruct({\"Zero\", 1, -3, -5})");
- Xtruct out = new Xtruct();
- out.string_thing = "Zero";
- out.byte_thing = (byte) 1;
- out.i32_thing = -3;
- out.i64_thing = -5;
- Xtruct in = testClient.testStruct(out);
- System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
-
- /**
- * NESTED STRUCT TEST
- */
- System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
- Xtruct2 out2 = new Xtruct2();
- out2.byte_thing = (short)1;
- out2.struct_thing = out;
- out2.i32_thing = 5;
- Xtruct2 in2 = testClient.testNest(out2);
- in = in2.struct_thing;
- System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");
-
- /**
- * MAP TEST
- */
- Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
- for (int i = 0; i < 5; ++i) {
- mapout.put(i, i-10);
- }
- System.out.print("testMap({");
- boolean first = true;
- for (int key : mapout.keySet()) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(key + " => " + mapout.get(key));
- }
- System.out.print("})");
- Map<Integer,Integer> mapin = testClient.testMap(mapout);
- System.out.print(" = {");
- first = true;
- for (int key : mapin.keySet()) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(key + " => " + mapout.get(key));
- }
- System.out.print("}\n");
-
- /**
- * SET TEST
- */
- Set<Integer> setout = new HashSet<Integer>();
- for (int i = -2; i < 3; ++i) {
- setout.add(i);
- }
- System.out.print("testSet({");
- first = true;
- for (int elem : setout) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("})");
- Set<Integer> setin = testClient.testSet(setout);
- System.out.print(" = {");
- first = true;
- for (int elem : setin) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("}\n");
-
- /**
- * LIST TEST
- */
- List<Integer> listout = new ArrayList<Integer>();
- for (int i = -2; i < 3; ++i) {
- listout.add(i);
- }
- System.out.print("testList({");
- first = true;
- for (int elem : listout) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("})");
- List<Integer> listin = testClient.testList(listout);
- System.out.print(" = {");
- first = true;
- for (int elem : listin) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("}\n");
-
- /**
- * ENUM TEST
- */
- System.out.print("testEnum(ONE)");
- Numberz ret = testClient.testEnum(Numberz.ONE);
- System.out.print(" = " + ret + "\n");
-
- System.out.print("testEnum(TWO)");
- ret = testClient.testEnum(Numberz.TWO);
- System.out.print(" = " + ret + "\n");
-
- System.out.print("testEnum(THREE)");
- ret = testClient.testEnum(Numberz.THREE);
- System.out.print(" = " + ret + "\n");
-
- System.out.print("testEnum(FIVE)");
- ret = testClient.testEnum(Numberz.FIVE);
- System.out.print(" = " + ret + "\n");
-
- System.out.print("testEnum(EIGHT)");
- ret = testClient.testEnum(Numberz.EIGHT);
- System.out.print(" = " + ret + "\n");
-
- /**
- * TYPEDEF TEST
- */
- System.out.print("testTypedef(309858235082523)");
- long uid = testClient.testTypedef(309858235082523L);
- System.out.print(" = " + uid + "\n");
-
- /**
- * NESTED MAP TEST
- */
- System.out.print("testMapMap(1)");
- Map<Integer,Map<Integer,Integer>> mm =
- testClient.testMapMap(1);
- System.out.print(" = {");
- for (int key : mm.keySet()) {
- System.out.print(key + " => {");
- Map<Integer,Integer> m2 = mm.get(key);
- for (int k2 : m2.keySet()) {
- System.out.print(k2 + " => " + m2.get(k2) + ", ");
- }
- System.out.print("}, ");
- }
- System.out.print("}\n");
-
- /**
- * INSANITY TEST
- */
- insane = new Insanity();
- insane.userMap = new HashMap<Numberz, Long>();
- insane.userMap.put(Numberz.FIVE, (long)5000);
- Xtruct truck = new Xtruct();
- truck.string_thing = "Truck";
- truck.byte_thing = (byte)8;
- truck.i32_thing = 8;
- truck.i64_thing = 8;
- insane.xtructs = new ArrayList<Xtruct>();
- insane.xtructs.add(truck);
- System.out.print("testInsanity()");
- Map<Long,Map<Numberz,Insanity>> whoa =
- testClient.testInsanity(insane);
- System.out.print(" = {");
- for (long key : whoa.keySet()) {
- Map<Numberz,Insanity> val = whoa.get(key);
- System.out.print(key + " => {");
-
- for (Numberz k2 : val.keySet()) {
- Insanity v2 = val.get(k2);
- System.out.print(k2 + " => {");
- Map<Numberz, Long> userMap = v2.userMap;
- System.out.print("{");
- if (userMap != null) {
- for (Numberz k3 : userMap.keySet()) {
- System.out.print(k3 + " => " + userMap.get(k3) + ", ");
- }
- }
- System.out.print("}, ");
-
- List<Xtruct> xtructs = v2.xtructs;
- System.out.print("{");
- if (xtructs != null) {
- for (Xtruct x : xtructs) {
- System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
- }
- }
- System.out.print("}");
-
- System.out.print("}, ");
- }
- System.out.print("}, ");
- }
- System.out.print("}\n");
-
- // Test oneway
- System.out.print("testOneway(3)...");
- long startOneway = System.nanoTime();
- testClient.testOneway(3);
- long onewayElapsedMillis = (System.nanoTime() - startOneway) / 1000000;
- if (onewayElapsedMillis > 200) {
- throw new Exception("Oneway test failed: took " +
- Long.toString(onewayElapsedMillis) +
- "ms");
- } else {
- System.out.println("Success - took " +
- Long.toString(onewayElapsedMillis) +
- "ms");
- }
-
-
- long stop = System.nanoTime();
- long tot = stop-start;
-
- System.out.println("Total time: " + tot/1000 + "us");
-
- if (timeMin == 0 || tot < timeMin) {
- timeMin = tot;
- }
- if (tot > timeMax) {
- timeMax = tot;
- }
- timeTot += tot;
-
- transport.close();
- }
-
- long timeAvg = timeTot / numTests;
-
- System.out.println("Min time: " + timeMin/1000 + "us");
- System.out.println("Max time: " + timeMax/1000 + "us");
- System.out.println("Avg time: " + timeAvg/1000 + "us");
-
- String json = (new TSerializer(new TSimpleJSONProtocol.Factory())).toString(insane);
-
- System.out.println("\nFor good meausre here is some JSON:\n" + json);
-
- } catch (Exception x) {
- x.printStackTrace();
- }
-
- }
-
-}
diff --git a/lib/java/test/org/apache/thrift/test/TestNonblockingServer.java b/lib/java/test/org/apache/thrift/test/TestNonblockingServer.java
deleted file mode 100644
index 2e6a178..0000000
--- a/lib/java/test/org/apache/thrift/test/TestNonblockingServer.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.thrift.test;
-
-import org.apache.thrift.server.THsHaServer;
-import org.apache.thrift.server.TNonblockingServer;
-import org.apache.thrift.server.TServer;
-import org.apache.thrift.transport.TNonblockingServerSocket;
-
-import thrift.test.ThriftTest;
-
-
-public class TestNonblockingServer extends TestServer {
- public static void main(String [] args) {
- try {
- int port = 9090;
- boolean hsha = false;
-
- for (int i = 0; i < args.length; i++) {
- if (args[i].equals("-p")) {
- port = Integer.valueOf(args[i++]);
- } else if (args[i].equals("-hsha")) {
- hsha = true;
- }
- }
-
- // Processor
- TestHandler testHandler =
- new TestHandler();
- ThriftTest.Processor testProcessor =
- new ThriftTest.Processor(testHandler);
-
- // Transport
- TNonblockingServerSocket tServerSocket =
- new TNonblockingServerSocket(port);
-
- TServer serverEngine;
-
- if (hsha) {
- // HsHa Server
- serverEngine = new THsHaServer(testProcessor, tServerSocket);
- } else {
- // Nonblocking Server
- serverEngine = new TNonblockingServer(testProcessor, tServerSocket);
- }
-
- // Run it
- System.out.println("Starting the server on port " + port + "...");
- serverEngine.serve();
-
- } catch (Exception x) {
- x.printStackTrace();
- }
- System.out.println("done.");
- }
-}
diff --git a/lib/java/test/org/apache/thrift/test/TestServer.java b/lib/java/test/org/apache/thrift/test/TestServer.java
deleted file mode 100644
index 304d8a3..0000000
--- a/lib/java/test/org/apache/thrift/test/TestServer.java
+++ /dev/null
@@ -1,306 +0,0 @@
-/*
- * 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.thrift.test;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TProtocolFactory;
-import org.apache.thrift.server.TServer;
-import org.apache.thrift.server.TThreadPoolServer;
-import org.apache.thrift.transport.TServerSocket;
-
-import thrift.test.Insanity;
-import thrift.test.Numberz;
-import thrift.test.ThriftTest;
-import thrift.test.Xception;
-import thrift.test.Xception2;
-import thrift.test.Xtruct;
-import thrift.test.Xtruct2;
-
-public class TestServer {
-
- public static class TestHandler implements ThriftTest.Iface {
-
- public TestHandler() {}
-
- public void testVoid() {
- System.out.print("testVoid()\n");
- }
-
- public String testString(String thing) {
- System.out.print("testString(\"" + thing + "\")\n");
- return thing;
- }
-
- public byte testByte(byte thing) {
- System.out.print("testByte(" + thing + ")\n");
- return thing;
- }
-
- public int testI32(int thing) {
- System.out.print("testI32(" + thing + ")\n");
- return thing;
- }
-
- public long testI64(long thing) {
- System.out.print("testI64(" + thing + ")\n");
- return thing;
- }
-
- public double testDouble(double thing) {
- System.out.print("testDouble(" + thing + ")\n");
- return thing;
- }
-
- public Xtruct testStruct(Xtruct thing) {
- System.out.print("testStruct({" +
- "\"" + thing.string_thing + "\", " +
- thing.byte_thing + ", " +
- thing.i32_thing + ", " +
- thing.i64_thing + "})\n");
- return thing;
- }
-
- public Xtruct2 testNest(Xtruct2 nest) {
- Xtruct thing = nest.struct_thing;
- System.out.print("testNest({" +
- nest.byte_thing + ", {" +
- "\"" + thing.string_thing + "\", " +
- thing.byte_thing + ", " +
- thing.i32_thing + ", " +
- thing.i64_thing + "}, " +
- nest.i32_thing + "})\n");
- return nest;
- }
-
- public Map<Integer,Integer> testMap(Map<Integer,Integer> thing) {
- System.out.print("testMap({");
- boolean first = true;
- for (int key : thing.keySet()) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(key + " => " + thing.get(key));
- }
- System.out.print("})\n");
- return thing;
- }
-
- public Set<Integer> testSet(Set<Integer> thing) {
- System.out.print("testSet({");
- boolean first = true;
- for (int elem : thing) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("})\n");
- return thing;
- }
-
- public List<Integer> testList(List<Integer> thing) {
- System.out.print("testList({");
- boolean first = true;
- for (int elem : thing) {
- if (first) {
- first = false;
- } else {
- System.out.print(", ");
- }
- System.out.print(elem);
- }
- System.out.print("})\n");
- return thing;
- }
-
- public Numberz testEnum(Numberz thing) {
- System.out.print("testEnum(" + thing + ")\n");
- return thing;
- }
-
- public long testTypedef(long thing) {
- System.out.print("testTypedef(" + thing + ")\n");
- return thing;
- }
-
- public Map<Integer,Map<Integer,Integer>> testMapMap(int hello) {
- System.out.print("testMapMap(" + hello + ")\n");
- Map<Integer,Map<Integer,Integer>> mapmap =
- new HashMap<Integer,Map<Integer,Integer>>();
-
- HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
- HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
- for (int i = 1; i < 5; i++) {
- pos.put(i, i);
- neg.put(-i, -i);
- }
-
- mapmap.put(4, pos);
- mapmap.put(-4, neg);
-
- return mapmap;
- }
-
- public Map<Long, Map<Numberz,Insanity>> testInsanity(Insanity argument) {
- System.out.print("testInsanity()\n");
-
- Xtruct hello = new Xtruct();
- hello.string_thing = "Hello2";
- hello.byte_thing = 2;
- hello.i32_thing = 2;
- hello.i64_thing = 2;
-
- Xtruct goodbye = new Xtruct();
- goodbye.string_thing = "Goodbye4";
- goodbye.byte_thing = (byte)4;
- goodbye.i32_thing = 4;
- goodbye.i64_thing = (long)4;
-
- Insanity crazy = new Insanity();
- crazy.userMap = new HashMap<Numberz, Long>();
- crazy.xtructs = new ArrayList<Xtruct>();
-
- crazy.userMap.put(Numberz.EIGHT, (long)8);
- crazy.xtructs.add(goodbye);
-
- Insanity looney = new Insanity();
- crazy.userMap.put(Numberz.FIVE, (long)5);
- crazy.xtructs.add(hello);
-
- HashMap<Numberz,Insanity> first_map = new HashMap<Numberz, Insanity>();
- HashMap<Numberz,Insanity> second_map = new HashMap<Numberz, Insanity>();;
-
- first_map.put(Numberz.TWO, crazy);
- first_map.put(Numberz.THREE, crazy);
-
- second_map.put(Numberz.SIX, looney);
-
- Map<Long,Map<Numberz,Insanity>> insane =
- new HashMap<Long, Map<Numberz,Insanity>>();
- insane.put((long)1, first_map);
- insane.put((long)2, second_map);
-
- return insane;
- }
-
- public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short,String> arg3, Numberz arg4, long arg5) {
- System.out.print("testMulti()\n");
-
- Xtruct hello = new Xtruct();;
- hello.string_thing = "Hello2";
- hello.byte_thing = arg0;
- hello.i32_thing = arg1;
- hello.i64_thing = arg2;
- return hello;
- }
-
- public void testException(String arg) throws Xception {
- System.out.print("testException("+arg+")\n");
- if (arg.equals("Xception")) {
- Xception x = new Xception();
- x.errorCode = 1001;
- x.message = "This is an Xception";
- throw x;
- }
- return;
- }
-
- public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
- System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
- if (arg0.equals("Xception")) {
- Xception x = new Xception();
- x.errorCode = 1001;
- x.message = "This is an Xception";
- throw x;
- } else if (arg0.equals("Xception2")) {
- Xception2 x = new Xception2();
- x.errorCode = 2002;
- x.struct_thing = new Xtruct();
- x.struct_thing.string_thing = "This is an Xception2";
- throw x;
- }
-
- Xtruct result = new Xtruct();
- result.string_thing = arg1;
- return result;
- }
-
- public void testOneway(int sleepFor) {
- System.out.println("testOneway(" + Integer.toString(sleepFor) +
- ") => sleeping...");
- try {
- Thread.sleep(sleepFor * 1000);
- System.out.println("Done sleeping!");
- } catch (InterruptedException ie) {
- throw new RuntimeException(ie);
- }
- }
-
- } // class TestHandler
-
- public static void main(String [] args) {
- try {
- int port = 9090;
- if (args.length > 1) {
- port = Integer.valueOf(args[0]);
- }
-
- // Processor
- TestHandler testHandler =
- new TestHandler();
- ThriftTest.Processor testProcessor =
- new ThriftTest.Processor(testHandler);
-
- // Transport
- TServerSocket tServerSocket =
- new TServerSocket(port);
-
- // Protocol factory
- TProtocolFactory tProtocolFactory =
- new TBinaryProtocol.Factory();
-
- TServer serverEngine;
-
- // Simple Server
- // serverEngine = new TSimpleServer(testProcessor, tServerSocket);
-
- // ThreadPool Server
- serverEngine = new TThreadPoolServer(testProcessor, tServerSocket, tProtocolFactory);
-
- // Run it
- System.out.println("Starting the server on port " + port + "...");
- serverEngine.serve();
-
- } catch (Exception x) {
- x.printStackTrace();
- }
- System.out.println("done.");
- }
-}