THRIFT-892 - refactor erlang library to build using rebar, move tests into lib/erl/test directory and run several via make check

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1137870 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configure.ac b/configure.ac
index f70ace2..48f93b5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -464,7 +464,7 @@
   lib/c_glib/test/Makefile
   lib/csharp/Makefile
   lib/erl/Makefile
-  lib/erl/src/Makefile
+  lib/erl/src/thrift.app.src
   lib/hs/Makefile
   lib/java/Makefile
   lib/js/test/Makefile
diff --git a/lib/erl/Makefile.am b/lib/erl/Makefile.am
index 0722f12..fd780a2 100644
--- a/lib/erl/Makefile.am
+++ b/lib/erl/Makefile.am
@@ -17,7 +17,23 @@
 # under the License.
 #
 
-SUBDIRS = src
+THRIFT = ../../compiler/cpp/thrift
+#THRIFT_FILES = ../../test/ThriftTest.thrift \
+#			   ../../test/Stress.thrift
+THRIFT_FILES = $(wildcard ../../test/*.thrift) \
+			   $(wildcard test/*.thrift)
+
+.generated: $(THRIFT_FILES)
+	for f in $(THRIFT_FILES) ; do \
+	  $(THRIFT) --gen erl -out test $$f ; \
+	done ; \
+	touch .generated
+
+all: .generated
+	./rebar compile
+
+check: .generated
+	./rebar eunit
 
 install: all
 	mkdir -p $(DESTDIR)$(ERLANG_INSTALL_LIB_DIR_thrift) ; \
@@ -31,10 +47,36 @@
 uninstall:
 	rm -rf $(DESTDIR)$(ERLANG_INSTALL_LIB_DIR_thrift)
 
+clean:
+	rm .generated
+	./rebar clean
 
 maintainer-clean-local:
+	rm -f test/secondService_* \
+		  test/aService_* \
+		  test/serviceForExceptionWithAMap_* \
+		  test/annotationTest_* \
+		  test/service_* \
+		  test/constantsDemo_* \
+		  test/smallService_* \
+		  test/smallTest_* \
+		  test/debugProtoTest_* \
+		  test/srv_* \
+		  test/denseLinkingTest_* \
+		  test/stressTest_* \
+		  test/docTest_* \
+		  test/emptyService_* \
+		  test/inherited_* \
+		  test/javaBeansTest_* \
+		  test/thrift1151_* \
+		  test/javaBeansTest_* \
+		  test/manyTypedefs_* \
+		  test/thriftTest_* \
+		  test/optionalRequiredTest_* \
+		  test/yowza_* \
+		  test/reverseOrderService_*
 	rm -rf ebin
 
-EXTRA_DIST = include src
+EXTRA_DIST = include src rebar rebar.config
 
 MAINTAINERCLEANFILES = Makefile.in
diff --git a/lib/erl/rebar b/lib/erl/rebar
new file mode 100755
index 0000000..53419ea
--- /dev/null
+++ b/lib/erl/rebar
Binary files differ
diff --git a/lib/erl/rebar.config b/lib/erl/rebar.config
new file mode 100644
index 0000000..7eb26b2
--- /dev/null
+++ b/lib/erl/rebar.config
@@ -0,0 +1,3 @@
+{erl_opts, [debug_info]}.
+% {pre_hooks, [{compile, "./scripts/rebar-pre-compile"},
+%             {clean, "./scripts/rebar-clean"}]}.
diff --git a/lib/erl/src/Makefile.am b/lib/erl/src/Makefile.am
deleted file mode 100644
index 9d6fcdb..0000000
--- a/lib/erl/src/Makefile.am
+++ /dev/null
@@ -1,83 +0,0 @@
-# ----------------------------------------------------
-# Application version
-# ----------------------------------------------------
-
-APP_NAME=$(PACKAGE_NAME)
-VSN=$(PACKAGE_VERSION)
-
-# ----------------------------------------------------
-# FLAGS
-# ----------------------------------------------------
-
-ERL_FLAGS =
-ERL_INCLUDE = -I../include
-ERL_BEHAV_PATH = -pz ../ebin
-ERL_COMPILE_FLAGS = $(ERL_INCLUDE) $(ERL_BEHAV_PATH)
-
-EBIN ?= ../ebin
-ESRC ?= .
-EMULATOR = beam
-
-ERLC_WFLAGS = -W
-ERLC = erlc $(ERLC_WFLAGS) $(ERLC_FLAGS)
-ERL = erl -boot start_clean
-
-$(EBIN)/%.beam: $(ESRC)/%.erl
-	@echo "   ERLC  $<"
-	@$(ERLC) $(ERL_FLAGS) $(ERL_COMPILE_FLAGS) -o$(EBIN) $<
-
-.erl.beam:
-	$(ERLC) $(ERL_FLAGS) $(ERL_COMPILE_FLAGS) -o$(dir $@) $<
-
-# ----------------------------------------------------
-# Target Specs
-# ----------------------------------------------------
-
-MODULES = $(shell find . -name \*.erl | sed 's:^\./::' | sed 's/\.erl//')
-MODULES_STRING_LIST = $(shell find . -name \*.erl | sed 's:^\./::' | sed 's/\.erl/,/')
-BEHAV_MODULES = $(shell find . -name \*.erl | xargs grep -l behaviour_info | sed 's:^\./::' | sed 's/\.erl//')
-
-ERL_FILES= $(MODULES:%=%.erl)
-DOC_FILES=$(ERL_FILES)
-
-APP_FILE= $(APP_NAME).app
-APP_SRC= $(APP_FILE).src
-APP_TARGET= $(EBIN)/$(APP_FILE)
-
-BEAMS= $(MODULES:%=$(EBIN)/%.$(EMULATOR))
-BEHAV_BEAMS= $(BEHAV_MODULES:%=$(EBIN)/%.$(EMULATOR))
-TARGET_FILES= $(BEHAV_BEAMS) $(BEAMS) $(APP_TARGET) $(APPUP_TARGET)
-
-# ----------------------------------------------------
-# Targets
-# ----------------------------------------------------
-
-all debug opt: $(EBIN) $(TARGET_FILES)
-
-# Note: In the open-source build clean must not destroy the preloaded
-# beam files.
-clean:
-	rm -f $(TARGET_FILES)
-	rm -f *~
-	rm -f core
-	rm -rf $(EBIN)
-	rm -rf *html
-
-$(EBIN):
-	mkdir $(EBIN)
-
-dialyzer: $(TARGET_FILES)
-	dialyzer --src -r . $(ERL_INCLUDE)
-
-# ----------------------------------------------------
-# Special Build Targets
-# ----------------------------------------------------
-
-$(APP_TARGET): $(APP_SRC) $(BEAMS)
-	sed -e 's;%VSN%;$(VSN);' \
-		-e 's;%APP_NAME%;$(APP_NAME);' \
-		-e 's;%MODULES%;%MODULES%$(MODULES_STRING_LIST);' \
-		$< > $<".tmp"
-	sed -e 's/%MODULES%\(.*\),/\1/' \
-		$<".tmp" > $@
-	rm $<".tmp"
diff --git a/lib/erl/src/thrift.app.src b/lib/erl/src/thrift.app.src.in
similarity index 96%
rename from lib/erl/src/thrift.app.src
rename to lib/erl/src/thrift.app.src.in
index ebe38d3..176c4c1 100644
--- a/lib/erl/src/thrift.app.src
+++ b/lib/erl/src/thrift.app.src.in
@@ -17,17 +17,16 @@
 %% under the License.
 %%
 %%% -*- mode:erlang -*-
-{application, %APP_NAME%,
+{application, @PACKAGE_NAME@,
  [
   % A quick description of the application.
   {description, "Thrift bindings"},
 
   % The version of the applicaton
-  {vsn, "%VSN%"},
+  {vsn, "@PACKAGE_VERSION@"},
 
   % All modules used by the application.
   {modules, [
-       %MODULES%
    ]},
 
   % All of the registered names the application uses. This can be ignored.
diff --git a/test/erl/src/Thrift1151.thrift b/lib/erl/test/Thrift1151.thrift
similarity index 100%
rename from test/erl/src/Thrift1151.thrift
rename to lib/erl/test/Thrift1151.thrift
diff --git a/test/erl/src/stress_server.erl b/lib/erl/test/stress_server.erl
similarity index 100%
rename from test/erl/src/stress_server.erl
rename to lib/erl/test/stress_server.erl
diff --git a/test/erl/src/test_client.erl b/lib/erl/test/test_client.erl
similarity index 100%
rename from test/erl/src/test_client.erl
rename to lib/erl/test/test_client.erl
diff --git a/lib/erl/test/test_disklog.erl b/lib/erl/test/test_disklog.erl
new file mode 100644
index 0000000..17a27b0
--- /dev/null
+++ b/lib/erl/test/test_disklog.erl
@@ -0,0 +1,85 @@
+%%
+%% 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.
+%%
+
+-module(test_disklog).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+disklog_test() ->
+  {ok, TransportFactory} =
+    thrift_disk_log_transport:new_transport_factory(
+      test_disklog,
+      [{file, "/tmp/test_log"},
+       {size, {1024*1024, 10}}]),
+  {ok, ProtocolFactory} =
+    thrift_binary_protocol:new_protocol_factory( TransportFactory, []),
+  {ok, Proto} = ProtocolFactory(),
+  {ok, Client0} = thrift_client:new(Proto, thriftTest_thrift),
+
+  io:format("Client started~n"),
+
+  % We have to make oneway calls into this client only since otherwise it
+  % will try to read from the disklog and go boom.
+  {Client1, {ok, ok}} = thrift_client:call(Client0, testOneway, [16#deadbeef]),
+  io:format("Call written~n"),
+
+  % Use the send_call method to write a non-oneway call into the log
+  {Client2, ok} =
+    thrift_client:send_call(Client1, testString, [<<"hello world">>]),
+  io:format("Non-oneway call sent~n"),
+
+  {_Client3, ok} = thrift_client:close(Client2),
+  io:format("Client closed~n"),
+
+  ok.
+
+disklog_base64_test() ->
+  {ok, TransportFactory} =
+    thrift_disk_log_transport:new_transport_factory(
+      test_disklog,
+      [{file, "/tmp/test_b64_log"},
+       {size, {1024*1024, 10}}]),
+  {ok, B64Factory} =
+    thrift_base64_transport:new_transport_factory(TransportFactory),
+  {ok, BufFactory} =
+    thrift_buffered_transport:new_transport_factory(B64Factory),
+  {ok, ProtocolFactory} =
+    thrift_binary_protocol:new_protocol_factory(BufFactory, []),
+  {ok, Proto} = ProtocolFactory(),
+  {ok, Client0} = thrift_client:new(Proto, thriftTest_thrift),
+
+  io:format("Client started~n"),
+
+  % We have to make oneway calls into this client only since otherwise
+  % it will try to read from the disklog and go boom.
+  {Client1, {ok, ok}} = thrift_client:call(Client0, testOneway, [16#deadbeef]),
+  io:format("Call written~n"),
+
+  % Use the send_call method to write a non-oneway call into the log
+  {Client2, ok} =
+    thrift_client:send_call(Client1, testString, [<<"hello world">>]),
+  io:format("Non-oneway call sent~n"),
+
+  {_Client3, ok} = thrift_client:close(Client2),
+  io:format("Client closed~n"),
+
+  ok.
+
+-endif.
diff --git a/test/erl/src/test_membuffer.erl b/lib/erl/test/test_membuffer.erl
similarity index 94%
rename from test/erl/src/test_membuffer.erl
rename to lib/erl/test/test_membuffer.erl
index fcf993a..7a01143 100644
--- a/test/erl/src/test_membuffer.erl
+++ b/lib/erl/test/test_membuffer.erl
@@ -18,7 +18,9 @@
 %%
 
 -module(test_membuffer).
--export([t/0]).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
 
 -include("thriftTest_types.hrl").
 
@@ -30,7 +32,7 @@
     i64_thing = 12345678900
   }.
 
-t1() ->
+encode_decode_1_test() ->
   {ok, Transport} = thrift_memory_buffer:new(),
   {ok, Protocol0} = thrift_binary_protocol:new(Transport),
   TestData = test_data(),
@@ -42,8 +44,7 @@
     'xtruct'),
   Result = TestData.
 
-
-t2() ->
+encode_decode_2_test() ->
   {ok, Transport} = thrift_memory_buffer:new(),
   {ok, Protocol0} = thrift_binary_protocol:new(Transport),
   TestData = test_data(),
@@ -60,7 +61,7 @@
     i64_thing = TestData#xtruct.i64_thing}.
 
 
-t3() ->
+encode_decode_3_test() ->
   {ok, Transport} = thrift_memory_buffer:new(),
   {ok, Protocol0} = thrift_binary_protocol:new(Transport),
   TestData = #bools{im_true = true, im_false = false},
@@ -75,7 +76,7 @@
   true = TestData#bools.im_false =:= Result#bools.im_false.
 
 
-t4() ->
+encode_decode_4_test() ->
   {ok, Transport} = thrift_memory_buffer:new(),
   {ok, Protocol0} = thrift_binary_protocol:new(Transport),
   TestData = #insanity{xtructs=[]},
@@ -88,7 +89,7 @@
 
   TestData = Result.
 
-t5() ->
+encode_decode_5_test() ->
   % test writing to a buffer, getting the bytes out, putting them
   % in a new buffer and reading them
 
@@ -111,10 +112,4 @@
 
   Result = TestData.
 
-t() ->
-  t1(),
-  t2(),
-  t3(),
-  t4(),
-  t5().
-
+-endif.
diff --git a/test/erl/src/test_server.erl b/lib/erl/test/test_server.erl
similarity index 100%
rename from test/erl/src/test_server.erl
rename to lib/erl/test/test_server.erl
diff --git a/lib/erl/test/test_thrift_1151.erl b/lib/erl/test/test_thrift_1151.erl
new file mode 100644
index 0000000..9cee486
--- /dev/null
+++ b/lib/erl/test/test_thrift_1151.erl
@@ -0,0 +1,24 @@
+-module(test_thrift_1151).
+
+-include("thrift1151_types.hrl").
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+unmatched_struct_test() ->
+  S1 = #structC{x=#structB{x=1}},
+  {ok, Transport} = thrift_memory_buffer:new(),
+  {ok, Protocol} = thrift_binary_protocol:new(Transport),
+  ?assertException (error, struct_unmatched,
+    thrift_protocol:write(Protocol,
+      {{struct, element(2, thrift1151_types:struct_info('structC'))}, S1})).
+
+badarg_test() ->
+  S2 = #structC{x=#structA{x="1"}},
+  {ok, Transport} = thrift_memory_buffer:new(),
+  {ok, Protocol} = thrift_binary_protocol:new(Transport),
+  ?assertException (error, badarg,
+    thrift_protocol:write(Protocol,
+      {{struct, element(2, thrift1151_types:struct_info('structC'))}, S2})).
+
+-endif.
diff --git a/test/Makefile.am b/test/Makefile.am
index 39ea85a..ae11404 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -41,7 +41,6 @@
 	rb \
 	perl \
 	php \
-	erl \
 	hs \
 	ocaml \
 	AnnotationTest.thrift \
diff --git a/test/erl/Makefile b/test/erl/Makefile
deleted file mode 100644
index bfb1d00..0000000
--- a/test/erl/Makefile
+++ /dev/null
@@ -1,68 +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.
-#
-
-GENDIR=gen
-GEN_INCLUDEDIR=$(GENDIR)/include
-GEN_SRCDIR=$(GENDIR)/src
-GEN_TARGETDIR=$(GENDIR)/ebin
-
-INCLUDEDIR=include
-TARGETDIR=ebin
-SRCDIR=src
-
-ALL_INCLUDEDIR=$(GEN_INCLUDEDIR) $(INCLUDEDIR) ../../lib/erl/include
-INCLUDEFLAGS=$(patsubst %,-I%, ${ALL_INCLUDEDIR})
-
-MODULES = stress_server test_server test_client test_disklog test_membuffer test_thrift_1151
-
-INCLUDES = 
-TARGETS = $(patsubst %,${TARGETDIR}/%.beam,${MODULES})
-HEADERS = $(patsubst %,${INCLUDEDIR}/%.hrl,${INCLUDES})
-
-all: ${GEN_TARGETDIR}/ ${TARGETS}
-
-TEST_RPCFILE = ../ThriftTest.thrift
-STRESS_RPCFILE = ../StressTest.thrift
-THRIFT_1151_FILE = src/Thrift1151.thrift
-THRIFT = ../../compiler/cpp/thrift
-
-${GENDIR}/: ${RPCFILE}
-	rm -rf ${GENDIR}
-	${THRIFT} --gen erl ${TEST_RPCFILE}
-	${THRIFT} --gen erl ${STRESS_RPCFILE}
-	${THRIFT} --gen erl ${THRIFT_1151_FILE}
-	mkdir -p ${GEN_INCLUDEDIR}
-	mkdir -p ${GEN_SRCDIR}
-	mkdir -p ${GEN_TARGETDIR}
-	mv gen-erl/*.hrl ${GEN_INCLUDEDIR}
-	mv gen-erl/*.erl ${GEN_SRCDIR}
-	rm -rf gen-erl
-
-${GEN_TARGETDIR}/: ${GENDIR}/
-	rm -rf ${GEN_TARGETDIR}
-	mkdir -p ${GEN_TARGETDIR}
-	erlc ${ERLC_FLAGS} ${INCLUDEFLAGS} -o ${GEN_TARGETDIR} ${GEN_SRCDIR}/*.erl
-
-$(TARGETS): ${TARGETDIR}/%.beam: ${SRCDIR}/%.erl ${GEN_INCLUDEDIR}/ ${HEADERS}
-	mkdir -p ${TARGETDIR}
-	erlc ${ERLC_FLAGS} ${INCLUDEFLAGS} -o ${TARGETDIR} $<
-
-clean:
-	rm -f ${TARGETDIR}/*.beam
-	rm -rf ${GENDIR}
diff --git a/test/erl/src/test_disklog.erl b/test/erl/src/test_disklog.erl
deleted file mode 100644
index fc0dcf8..0000000
--- a/test/erl/src/test_disklog.erl
+++ /dev/null
@@ -1,83 +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.
-%%
-
--module(test_disklog).
-
--compile(export_all).
-
-t() ->
-    {ok, TransportFactory} =
-        thrift_disk_log_transport:new_transport_factory(
-          test_disklog,
-          [{file, "/tmp/test_log"},
-           {size, {1024*1024, 10}}]),
-    {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory(
-                              TransportFactory, []),
-    {ok, Proto} = ProtocolFactory(),
-    {ok, Client0} = thrift_client:new(Proto, thriftTest_thrift),
-
-    io:format("Client started~n"),
-
-    % We have to make oneway calls into this client only since otherwise it will try
-    % to read from the disklog and go boom.
-    {Client1, {ok, ok}} = thrift_client:call(Client0, testOneway, [16#deadbeef]),
-    io:format("Call written~n"),
-
-    % Use the send_call method to write a non-oneway call into the log
-    {Client2, ok} = thrift_client:send_call(Client1, testString, [<<"hello world">>]),
-    io:format("Non-oneway call sent~n"),
-
-    {_Client3, ok} = thrift_client:close(Client2),
-    io:format("Client closed~n"),
-
-    ok.
-
-
-
-t_base64() ->
-    {ok, TransportFactory} =
-        thrift_disk_log_transport:new_transport_factory(
-          test_disklog,
-          [{file, "/tmp/test_b64_log"},
-           {size, {1024*1024, 10}}]),
-    {ok, B64Factory} =
-        thrift_base64_transport:new_transport_factory(TransportFactory),
-    {ok, BufFactory} =
-        thrift_buffered_transport:new_transport_factory(B64Factory),
-    {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory(
-                              BufFactory, []),
-    {ok, Proto} = ProtocolFactory(),
-    {ok, Client0} = thrift_client:new(Proto, thriftTest_thrift),
-
-    io:format("Client started~n"),
-
-    % We have to make oneway calls into this client only since otherwise it will try
-    % to read from the disklog and go boom.
-    {Client1, {ok, ok}} = thrift_client:call(Client0, testOneway, [16#deadbeef]),
-    io:format("Call written~n"),
-
-    % Use the send_call method to write a non-oneway call into the log
-    {Client2, ok} = thrift_client:send_call(Client1, testString, [<<"hello world">>]),
-    io:format("Non-oneway call sent~n"),
-
-    {_Client3, ok} = thrift_client:close(Client2),
-    io:format("Client closed~n"),
-
-    ok.
-
diff --git a/test/erl/src/test_thrift_1151.erl b/test/erl/src/test_thrift_1151.erl
deleted file mode 100644
index c50ddee..0000000
--- a/test/erl/src/test_thrift_1151.erl
+++ /dev/null
@@ -1,19 +0,0 @@
--module(test_thrift_1151).
-
--include("thrift1151_types.hrl").
-
--export([t/0, t1/0]).
-
-t() ->
-  S1 = #structC{x=#structB{x=1}},
-  {ok, Transport} = thrift_memory_buffer:new(),
-  {ok, Protocol} = thrift_binary_protocol:new(Transport),
-  thrift_protocol:write(Protocol,
-    {{struct, element(2, thrift1151_types:struct_info('structC'))}, S1}).
-
-t1() ->
-  S2 = #structC{x=#structA{x="1"}},
-  {ok, Transport} = thrift_memory_buffer:new(),
-  {ok, Protocol} = thrift_binary_protocol:new(Transport),
-  thrift_protocol:write(Protocol,
-    {{struct, element(2, thrift1151_types:struct_info('structC'))}, S2}).