You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2010/03/26 06:05:59 UTC

svn commit: r927693 - in /incubator/thrift/trunk/lib/java: ./ test/org/apache/thrift/server/ test/org/apache/thrift/test/

Author: bryanduxbury
Date: Fri Mar 26 05:05:59 2010
New Revision: 927693

URL: http://svn.apache.org/viewvc?rev=927693&view=rev
Log:
java: Add JUnit to ivy config. Convert Nonblocking server tests to use JUnit. Framework laid to convert the remainder of the tests.

Added:
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/ServerTestBase.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestHsHaServer.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java
Removed:
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TestClient.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TestNonblockingServer.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/TestServer.java
Modified:
    incubator/thrift/trunk/lib/java/build.xml
    incubator/thrift/trunk/lib/java/ivy.xml

Modified: incubator/thrift/trunk/lib/java/build.xml
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/build.xml?rev=927693&r1=927692&r2=927693&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/build.xml (original)
+++ incubator/thrift/trunk/lib/java/build.xml Fri Mar 26 05:05:59 2010
@@ -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">

Modified: incubator/thrift/trunk/lib/java/ivy.xml
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/ivy.xml?rev=927693&r1=927692&r2=927693&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/ivy.xml (original)
+++ incubator/thrift/trunk/lib/java/ivy.xml Fri Mar 26 05:05:59 2010
@@ -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>

Added: incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/ServerTestBase.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/ServerTestBase.java?rev=927693&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/ServerTestBase.java (added)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/ServerTestBase.java Fri Mar 26 05:05:59 2010
@@ -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();
+  }
+
+}

Added: incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestHsHaServer.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestHsHaServer.java?rev=927693&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestHsHaServer.java (added)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestHsHaServer.java Fri Mar 26 05:05:59 2010
@@ -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);
+  }
+}

Added: incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java?rev=927693&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java (added)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/server/TestNonblockingServer.java Fri Mar 26 05:05:59 2010
@@ -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);
+  }
+}