From 951e7e24800c0f0c4cfcdef5d634b1e16536eed0 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Fri, 26 Mar 2010 05:05:59 +0000 Subject: [PATCH] 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 --- lib/java/build.xml | 31 +- lib/java/ivy.xml | 1 + .../apache/thrift/server/ServerTestBase.java | 462 ++++++++++++++++++ .../apache/thrift/server/TestHsHaServer.java | 11 + .../thrift/server/TestNonblockingServer.java | 58 +++ .../org/apache/thrift/test/TestClient.java | 423 ---------------- .../thrift/test/TestNonblockingServer.java | 73 --- .../org/apache/thrift/test/TestServer.java | 306 ------------ 8 files changed, 562 insertions(+), 803 deletions(-) create mode 100644 lib/java/test/org/apache/thrift/server/ServerTestBase.java create mode 100644 lib/java/test/org/apache/thrift/server/TestHsHaServer.java create mode 100644 lib/java/test/org/apache/thrift/server/TestNonblockingServer.java delete mode 100644 lib/java/test/org/apache/thrift/test/TestClient.java delete mode 100644 lib/java/test/org/apache/thrift/test/TestNonblockingServer.java delete mode 100644 lib/java/test/org/apache/thrift/test/TestServer.java diff --git a/lib/java/build.xml b/lib/java/build.xml index e8e5d78f..4cf7c8f4 100644 --- a/lib/java/build.xml +++ b/lib/java/build.xml @@ -163,7 +163,34 @@ - + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + diff --git a/lib/java/ivy.xml b/lib/java/ivy.xml index ca6ecb33..aafc0da4 100644 --- a/lib/java/ivy.xml +++ b/lib/java/ivy.xml @@ -20,5 +20,6 @@ + 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 00000000..05d0d8da --- /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 testMap(Map 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 testSet(Set 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 testList(List 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> testMapMap(int hello) { + System.out.print("testMapMap(" + hello + ")\n"); + Map> mapmap = + new HashMap>(); + + HashMap pos = new HashMap(); + HashMap neg = new HashMap(); + 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> 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(); + crazy.xtructs = new ArrayList(); + + 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 first_map = new HashMap(); + HashMap second_map = new HashMap();; + + first_map.put(Numberz.TWO, crazy); + first_map.put(Numberz.THREE, crazy); + + second_map.put(Numberz.SIX, looney); + + Map> insane = + new HashMap>(); + insane.put((long)1, first_map); + insane.put((long)2, second_map); + + return insane; + } + + public Xtruct testMulti(byte arg0, int arg1, long arg2, Map 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 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(); + 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(); + insane.xtructs.add(truck); + System.out.print("testInsanity()"); + Map> whoa = + testClient.testInsanity(insane); + System.out.print(" = {"); + for (long key : whoa.keySet()) { + Map val = whoa.get(key); + System.out.print(key + " => {"); + + for (Numberz k2 : val.keySet()) { + Insanity v2 = val.get(k2); + System.out.print(k2 + " => {"); + Map 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 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 listout = new ArrayList(); + for (int i = -2; i < 3; ++i) { + listout.add(i); + } + List listin = testClient.testList(listout); + assertEquals(listout, listin); + } + + private void testMap(ThriftTest.Client testClient) throws TException { + Map mapout = new HashMap(); + for (int i = 0; i < 5; ++i) { + mapout.put(i, i-10); + } + Map mapin = testClient.testMap(mapout); + assertEquals(mapout, mapin); + } + + private void testNestedMap(ThriftTest.Client testClient) throws TException { + Map> mm = + testClient.testMapMap(1); + Map> mapmap = + new HashMap>(); + + HashMap pos = new HashMap(); + HashMap neg = new HashMap(); + 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 setout = new HashSet(); + for (int i = -2; i < 3; ++i) { + setout.add(i); + } + Set 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 00000000..c355d3ef --- /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 00000000..3aac5736 --- /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 aee3bbfe..00000000 --- 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 mapout = new HashMap(); - 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 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 setout = new HashSet(); - 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 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 listout = new ArrayList(); - 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 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> mm = - testClient.testMapMap(1); - System.out.print(" = {"); - for (int key : mm.keySet()) { - System.out.print(key + " => {"); - Map 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(); - 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(); - insane.xtructs.add(truck); - System.out.print("testInsanity()"); - Map> whoa = - testClient.testInsanity(insane); - System.out.print(" = {"); - for (long key : whoa.keySet()) { - Map val = whoa.get(key); - System.out.print(key + " => {"); - - for (Numberz k2 : val.keySet()) { - Insanity v2 = val.get(k2); - System.out.print(k2 + " => {"); - Map 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 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 2e6a1780..00000000 --- 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 304d8a3d..00000000 --- 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 testMap(Map 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 testSet(Set 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 testList(List 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> testMapMap(int hello) { - System.out.print("testMapMap(" + hello + ")\n"); - Map> mapmap = - new HashMap>(); - - HashMap pos = new HashMap(); - HashMap neg = new HashMap(); - 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> 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(); - crazy.xtructs = new ArrayList(); - - 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 first_map = new HashMap(); - HashMap second_map = new HashMap();; - - first_map.put(Numberz.TWO, crazy); - first_map.put(Numberz.THREE, crazy); - - second_map.put(Numberz.SIX, looney); - - Map> insane = - new HashMap>(); - insane.put((long)1, first_map); - insane.put((long)2, second_map); - - return insane; - } - - public Xtruct testMulti(byte arg0, int arg1, long arg2, Map 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."); - } -} -- 2.17.1