THRIFT-961 JavaScript TestSuite using ant/ivy and Java's ServerTestBase Handler



git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1026899 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/js/test/RunTestServer.sh b/lib/js/test/RunTestServer.sh
deleted file mode 100755
index 4c41ac1..0000000
--- a/lib/js/test/RunTestServer.sh
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/bin/bash 
-
-#
-# 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.
-#
-
-LOG4J="../../java/build/ivy/lib/slf4j-api-1.5.8.jar:../../java/build/ivy/lib/log4j-1.2.14.jar:../../java/build/ivy/lib/slf4j-log4j12-1.5.8.jar"
-HTTPCORE="./httpcore-4.0.1.jar"
-
-if [ -f ${HTTPCORE} ]
-then
-    echo "compiling test..."
-else
-    echo "try to download httpcore..."
-
-    wget http://archive.apache.org/dist/httpcomponents/httpcore/binary/httpcomponents-core-4.0.1-bin.tar.gz
-    # extract required jar file
-    tar -xzf httpcomponents-core-4.0.1-bin.tar.gz httpcomponents-core-4.0.1/lib/httpcore-4.0.1.jar --to-stdout > ${HTTPCORE}
-
-    if [ ! -f ${HTTPCORE} ]
-    then
-        echo "Missing required file ${HTTPCORE}"
-        echo "You can download this from http://archive.apache.org/dist/httpcomponents/httpcore/binary/httpcomponents-core-4.0.1-bin.tar.gz"
-        echo "Place the jar in this directory and try again."
-        exit
-    fi
-fi
-
-../../../compiler/cpp/thrift --gen java ../../../test/ThriftTest.thrift
-../../../compiler/cpp/thrift --gen js ../../../test/ThriftTest.thrift
-
-javac -cp ${LOG4J}:../../java/libthrift.jar gen-java/thrift/test/*.java
-javac -cp ${LOG4J}:${HTTPCORE}:../../java/libthrift.jar:gen-java/ src/test/*.java 
-java -cp  ${LOG4J}:${HTTPCORE}:../../java/libthrift.jar:gen-java:src test.Httpd ../
diff --git a/lib/js/test/build.xml b/lib/js/test/build.xml
new file mode 100644
index 0000000..ee38edb
--- /dev/null
+++ b/lib/js/test/build.xml
@@ -0,0 +1,157 @@
+<!--
+ 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.
+-->
+<project name="Java Script Test" default="test" basedir="."
+  xmlns:ivy="antlib:org.apache.ivy.ant"
+  xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+  <description>Java Script Test based on Thrift Java Library</description>
+
+  <property name="src" location="src" />
+  <property name="genjava" location="gen-java" />
+  <property name="genjs" location="gen-js" />
+  <property name="build" location="build" />
+  <property name="jar.file" location="${basedir}/jstest.jar" /> 
+
+  <!-- the root directory, where you unpack thrift distibution (e.g. thrift-0.x.x.tar.gz) -->
+  <property name="thrift.dir" location="../../../" />
+  <property name="thrift.java.dir" location="${thrift.dir}/lib/java" />
+
+  <property name="thrift.compiler" location="${thrift.dir}/compiler/cpp/thrift" />
+
+  <!-- take ivy from java, test depends anyway on java! -->
+  <property name="ivy.dir" location="${thrift.java.dir}/build/ivy" />
+  <property name="ivy.lib.dir" location="${ivy.dir}/lib" />
+
+  <path id="libs.classpath">
+    <pathelement path="${thrift.java.dir}/libthrift.jar" />
+    <pathelement path="${thrift.java.dir}/libthrift-test.jar" />
+    <fileset dir="${thrift.java.dir}/build/ivy/lib">
+      <include name="*.jar" />
+    </fileset>
+  </path>
+
+  <path id="test.classpath">
+    <path refid="libs.classpath" />
+    <pathelement location="${jar.file}" />
+  </path>
+
+  <target name="dependencies">
+    <fail>
+      <condition>
+        <not>
+          <resourcecount count="1">
+            <fileset id="fs" dir="${thrift.java.dir}" includes="libthrift.jar"/>
+          </resourcecount>
+        </not>
+      </condition>
+      You need libthrift.jar located at
+      ${thrift.java.dir}
+      Did you compile Thrift Java library and its test suite by "ant compile-test"?
+    </fail>
+    <fail>
+      <condition>
+        <not>
+          <resourcecount count="1">
+            <fileset id="fs" dir="${ivy.dir}" includes="ivy-2*.jar"/>
+          </resourcecount>
+        </not>
+      </condition>
+      ivy is missing at ${ivy.dir}
+    </fail>
+    <fail>
+      <condition>
+        <not>
+          <resourcecount count="1">
+            <fileset id="fs" dir="${thrift.dir}" includes="compiler/cpp/thrift"/>
+          </resourcecount>
+        </not>
+      </condition>
+      Thrift compiler is missing !
+    </fail>
+  </target>
+
+  <target name="init" depends="dependencies">
+    <tstamp />
+    <mkdir dir="${build}"/>
+  </target>
+
+  <target name="compile" description="compile the test suite" depends="init, generate, resolve">
+    <javac srcdir="${genjava}" destdir="${build}" classpathref="libs.classpath" />
+    <javac srcdir="${src}" destdir="${build}" classpathref="libs.classpath" />
+  </target>
+
+  <target name="jstest" description="" depends="compile">
+    <jar jarfile="${jar.file}" basedir="${build}"/>
+  </target>
+
+  <target name="test" description="run the test server" depends="jstest">
+    <java classname="test.Httpd" fork="true"
+      classpathref="test.classpath" failonerror="true">
+      <arg value="../" />
+    </java>
+  </target>
+
+  <target name="generate">
+    <exec executable="${thrift.compiler}" failonerror="true">
+      <arg line="--gen java ${thrift.dir}/test/ThriftTest.thrift" />
+    </exec>
+    <exec executable="${thrift.compiler}" failonerror="true">
+      <arg line="--gen js ${thrift.dir}/test/ThriftTest.thrift" />
+    </exec>
+  </target>
+
+  <target name="clean">
+    <delete dir="${build}" />
+    <delete dir="${genjava}" />
+    <delete dir="${genjs}" />
+    <delete file="${jar.file}" />
+  </target>
+
+  <!-- ivy tasks from java build.xml ... don't know how to import them instead of copying -->
+  <target name="resolve" depends="ivy-init-antlib" description="retrieve dependencies with ivy" unless="noivy">
+    <ivy:retrieve />
+  </target>
+
+  <target name="ivy-probe-antlib">
+    <condition property="ivy.found">
+      <typefound uri="antlib:org.apache.ivy.ant" name="cleancache"/>
+    </condition>
+  </target>
+
+  <target name="ivy-init-antlib" depends="ivy-probe-antlib" unless="ivy.found">
+    <typedef uri="antlib:org.apache.ivy.ant" onerror="fail"
+      loaderRef="ivyLoader">
+      <classpath>
+        <fileset dir="${ivy.dir}">
+          <include name="ivy-2.*.jar" />
+        </fileset>
+      </classpath>
+    </typedef>
+    <fail>
+      <condition >
+        <not>
+          <typefound uri="antlib:org.apache.ivy.ant" name="cleancache"/>
+        </not>
+      </condition>
+      You need Apache Ivy 2.0 or later from http://ant.apache.org/
+      It could not be loaded from ${ivy_repo_url}
+    </fail>
+  </target>
+
+</project>
diff --git a/lib/js/test/ivy.xml b/lib/js/test/ivy.xml
new file mode 100644
index 0000000..fdd2d90
--- /dev/null
+++ b/lib/js/test/ivy.xml
@@ -0,0 +1,22 @@
+<!--
+   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.
+-->
+<ivy-module version="2.0">
+    <info organisation="org.apache.thrift" module="jstest"/>
+    <dependencies>
+       <dependency org="org.apache.httpcomponents" name="httpclient" rev="4.0.1" conf="* -> *,!sources,!javadoc"/>
+    </dependencies>
+</ivy-module>
diff --git a/lib/js/test/src/test/Httpd.java b/lib/js/test/src/test/Httpd.java
index 155a8b8..e3aad7d 100644
--- a/lib/js/test/src/test/Httpd.java
+++ b/lib/js/test/src/test/Httpd.java
@@ -70,6 +70,7 @@
 import org.apache.thrift.transport.TMemoryBuffer;
 
 import thrift.test.ThriftTest;
+import org.apache.thrift.server.ServerTestBase.TestHandler;
 
 /**
  * Basic, yet fully functional and spec compliant, HTTP/1.1 file server.
diff --git a/lib/js/test/src/test/TestHandler.java b/lib/js/test/src/test/TestHandler.java
deleted file mode 100644
index f2c944f..0000000
--- a/lib/js/test/src/test/TestHandler.java
+++ /dev/null
@@ -1,141 +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 test;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.thrift.TException;
-
-import thrift.test.Insanity;
-import thrift.test.ThriftTest;
-import thrift.test.Xception;
-import thrift.test.Xception2;
-import thrift.test.Xtruct;
-import thrift.test.Xtruct2;
-import thrift.test.Numberz;
-
-public class TestHandler implements ThriftTest.Iface {
-
-    public byte testByte(byte thing) throws TException {
-        return thing;
-    }
-
-    public double testDouble(double thing) throws TException {
-        return thing;
-    }
-
-    public Numberz testEnum(Numberz thing) throws TException {
-        return thing;
-    }
-
-    public void testException(String arg) throws Xception, TException {
-        throw new Xception(1,arg);       
-    }
-
-    public int testI32(int thing) throws TException {
-        return thing;
-    }
-
-    public long testI64(long thing) throws TException {
-        return thing;
-    }
-
-    public Map<Long, Map<Numberz, Insanity>> testInsanity(Insanity argument) throws TException {
-        Map<Long, Map<Numberz, Insanity>> result = new HashMap<Long, Map<Numberz,Insanity>>();
-        
-        result.put(Long.valueOf(1), new HashMap<Numberz,Insanity>());
-        result.get(Long.valueOf(1)).put(Numberz.ONE, argument);
-        
-        result.put(Long.valueOf(2), new HashMap<Numberz,Insanity>());
-        result.get(Long.valueOf(2)).put(Numberz.ONE, argument);
-        
-        return result;
-    }
-
-    public List<Integer> testList(List<Integer> thing) throws TException {
-        return thing;
-    }
-
-    public Map<Integer, Integer> testMap(Map<Integer, Integer> thing) throws TException {
-        return thing;
-    }
-
-    public Map<Integer, Map<Integer, Integer>> testMapMap(int hello) throws TException {
-        Map<Integer, Map<Integer,Integer>> result = new HashMap<Integer, Map<Integer,Integer>>();
-        
-        result.put(Integer.valueOf(1), new HashMap<Integer,Integer>());
-        result.get(Integer.valueOf(1)).put(Integer.valueOf(1), Integer.valueOf(1));
-        result.get(Integer.valueOf(1)).put(Integer.valueOf(2), Integer.valueOf(2));
-        result.get(Integer.valueOf(2)).put(Integer.valueOf(1), Integer.valueOf(1));
-    
-        return result;
-    }
-
-    public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short, String> arg3, Numberz arg4, long arg5) throws TException {
-        Xtruct xtr = new Xtruct();
-        
-        xtr.byte_thing = arg0;
-        xtr.i32_thing  = arg1;
-        xtr.i64_thing  = arg2;
-        xtr.string_thing = "server string";
-        
-        return xtr;
-    }
-
-    public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2, TException {
-        Xtruct xtr = new Xtruct();
-        xtr.setString_thing(arg0);
-        throw new Xception2(1,xtr);
-    }
-
-    public Xtruct2 testNest(Xtruct2 thing) throws TException {
-       return thing;
-    }
-
-    public void testOneway(int secondsToSleep) throws TException {
-        try{
-            Thread.sleep(secondsToSleep * 1000);
-        }catch(InterruptedException e){
-            
-        }
-    }
-
-    public Set<Integer> testSet(Set<Integer> thing) throws TException {
-        return thing;
-    }
-
-    public String testString(String thing) throws TException {
-        return thing;
-    }
-
-    public Xtruct testStruct(Xtruct thing) throws TException {
-        return thing;
-    }
-
-    public long testTypedef(long thing) throws TException {
-        return thing;
-    }
-
-    public void testVoid() throws TException {
-        
-    }
-}
diff --git a/lib/js/test/test.html b/lib/js/test/test.html
index 8a62bf8..c5f4326 100644
--- a/lib/js/test/test.html
+++ b/lib/js/test/test.html
@@ -200,17 +200,8 @@
       try{
         client.testException("Xception");
       }catch(e){
-        equals(e.errorCode, 1);
-        equals(e.message, "Xception");
-      }
-    });
-
-    test("ApplicationException", function() {
-      expect(1);
-      try{
-        client.testException("ApplicationException");
-      }catch(e){
-        equals(e.message, "ApplicationException");
+        equals(e.errorCode, 1001);
+        equals(e.message, "This is an Xception");
       }
     });