tests: rename 'tests/' => 't/', '*.test' => '*.sh'
[platform/upstream/automake.git] / t / java-compile-run-nested.sh
1 #! /bin/sh
2 # Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # Test on compilation and execution of Java class files.
18 # Also meddle with wrapper scripts, as would be probably seen in a real
19 # "UNIX-style" use case.
20 # This test uses a typical "nested" source tree setup (i.e., different
21 # components/aspects are separated into different subdirectories),
22 # and uses the 'parallel-tests' testsuite driver.  The sister test
23 # 'java-compile-run-flat.test' do similar checks with a "flat" setup
24 # (i.e., everything in the top-level directory), and forcing the use
25 # of the older non-parallel 'simple-tests' driver.
26
27 required='java javac'
28 am_parallel_tests=yes
29 . ./defs || Exit 1
30
31 echo "AC_SUBST([PATH_SEPARATOR], ['$PATH_SEPARATOR'])" >> configure.ac
32
33 cat >> configure.ac <<'END'
34 AC_SUBST([JAVA], [java])
35 AC_CONFIG_SRCDIR([jprog/PkgLocation.jin])
36 AC_SUBST([jprogdatadir],  ['${pkgdatadir}'])
37 AC_SUBST([jprogclassdir], ['${jprogdatadir}/jprog'])
38 AC_CONFIG_FILES([jprog/PkgLocation.java:jprog/PkgLocation.jin])
39 AC_CONFIG_FILES([jprog/Makefile bin/Makefile tests/Makefile])
40 AC_OUTPUT
41 END
42
43 ## TOP-LEVEL SETUP AND TARGETS ##
44
45 cat > Makefile.am <<'END'
46 SUBDIRS = bin jprog tests
47
48 test-built:
49         ls -l $(srcdir)/*             ;: For debugging.
50         test $(srcdir) = . || ls -l * ;: Likewise.
51         test -f $(srcdir)/jprog/Main.java
52         test -f $(srcdir)/jprog/HelloStream.java
53         test -f $(srcdir)/jprog/PkgLocation.jin
54         test -f jprog/PkgLocation.java
55         test -f jprog/HelloStream.class
56         test -f jprog/Main.class
57         test -f jprog/PkgLocation.class
58         test -f jprog/classjprogclass.stamp
59
60 test-installed:
61         ls -l $(jprogclassdir) ;: For debugging.
62         test -f $(jprogclassdir)/HelloStream.class
63         test -f $(jprogclassdir)/Main.class
64         test -f $(jprogclassdir)/PkgLocation.class
65         if find $(prefix) | grep '\.stamp$$'; then exit 1; else :; fi
66
67 run-installed:
68         jprog_doing_installcheck=yes $(MAKE) $(AM_MAKEFLAGS) check
69
70 check-local: test-built
71 installcheck-local: test-installed run-installed
72
73 .PHONY: test-built test-installed run-installed
74 END
75
76 ## WRAPPER SCRIPT ##
77
78 mkdir bin
79
80 cat > bin/Makefile.am <<'END'
81 bin_SCRIPTS = jprog
82
83 edit_script = sed -e 's|[@]JAVA@|$(JAVA)|g' \
84                   -e 's|[@]jprogdatadir@|$(jprogdatadir)|g' \
85                   -e 's|[@]SHELL@|$(SHELL)|g' \
86                   -e 's|[@]PATH_SEPARATOR@|$(PATH_SEPARATOR)|g'
87
88 jprog: jprog.sh
89         rm -f $@ $@-t
90         $(edit_script) `test -f '$@.sh' || echo $(srcdir)/`$@.sh >$@-t
91         chmod a-w $@-t && chmod a+x $@-t && mv -f $@-t $@
92         sed 's/^/ | /' $@ ;: for debugging.
93
94 EXTRA_DIST = jprog.sh
95 CLEANFILES = jprog
96 END
97
98 cat > bin/jprog.sh <<'END'
99 #!@SHELL@
100 CLASSPATH=${jprog_classpath-'@jprogdatadir@'}${CLASSPATH+"@PATH_SEPARATOR@$CLASSPATH"}
101 export CLASSPATH
102 case $# in
103   0) exec @JAVA@ jprog.Main;;
104   *) exec @JAVA@ jprog.Main "$@";;
105 esac
106 END
107
108 ## JAVA SOURCES ##
109
110 mkdir jprog
111
112 cat > jprog/Makefile.am <<'END'
113 dist_jprogclass_JAVA = Main.java HelloStream.java
114 nodist_jprogclass_JAVA = PkgLocation.java
115 END
116
117 cat > jprog/PkgLocation.jin <<'END'
118 package jprog;
119 public class PkgLocation {
120     public static String prefix() {
121         return new String("@prefix@");
122     }
123 }
124 END
125
126 cat > jprog/Main.java <<'END'
127 package jprog;
128 import jprog.PkgLocation;
129 import jprog.HelloStream;
130 public class Main {
131     public static void main(String[] args) {
132         for (int i = 0; i < args.length; i++) {
133             if (args[i].equals("--print-prefix")) {
134                 System.out.println(PkgLocation.prefix());
135             } else if (args[i].equals("--hello-stdout")) {
136                 HelloStream.to(System.out);
137             } else if (args[i].equals("--hello-stderr")) {
138                 HelloStream.to(System.err);
139             } else {
140                System.err.println("jprog: invalid option '" + args[i] +
141                                   "'");
142                System.exit(2);
143             }
144         }
145         System.exit(0);
146     }
147 }
148 END
149
150 cat > jprog/HelloStream.java <<'END'
151 package jprog;
152 import java.io.PrintStream;
153 class HelloStream {
154     public static void to(PrintStream stream) {
155         stream.println("Hello, Stream!");
156     }
157 }
158 END
159
160 ## TESTS ##
161
162 mkdir tests
163
164 cat > tests/Makefile.am <<'END'
165 AM_TESTS_ENVIRONMENT = \
166     if test x"$$jprog_doing_installcheck" != x"yes"; then \
167         jprog_classpath='$(abs_top_builddir):$(abs_top_srcdir)'; \
168         export jprog_classpath; \
169         PATH='$(abs_top_builddir)/bin$(PATH_SEPARATOR)'$$PATH; \
170         export PATH; \
171     else \
172         unset jprog_classpath || :; \
173         PATH='$(prefix)/bin$(PATH_SEPARATOR)'$$PATH; \
174         export PATH; \
175     fi; \
176     config_time_prefix='@prefix@'; export config_time_prefix;
177
178 TESTS = \
179   simple.test \
180   prefix.test \
181   stdout.test \
182   stderr.test \
183   badarg.test
184
185 XFAIL_TESTS = badarg.test
186
187 EXTRA_DIST = $(TESTS)
188 END
189
190 cat > tests/simple.test <<'END'
191 #!/bin/sh
192 jprog
193 END
194
195 cat > tests/prefix.test <<'END'
196 #!/bin/sh
197 jprefix=`jprog --print-prefix` || exit 1
198 echo "$0: exp prefix: $config_time_prefix"
199 echo "$0: got prefix: $jprefix"
200 test x"$jprefix" = x"$config_time_prefix"
201 END
202
203 cat > tests/stdout.test <<'END'
204 #!/bin/sh
205 rc=0
206 jprog --hello-stdout >stdout.out 2>stdout.err || { echo \$?=$?; rc=1; }
207 sed 's/^/out:/' <stdout.out      # For debugging.
208 sed 's/^/err:/' <stdout.err >&2  # Likewise.
209 test -s stdout.err && rc=1
210 test "`cat stdout.out`" = 'Hello, Stream!' || rc=1
211 rm -f stdout.out stdout.err || rc=1
212 exit $rc
213 END
214
215 cat > tests/stderr.test <<'END'
216 #!/bin/sh
217 rc=0
218 jprog --hello-stderr >stderr.out 2>stderr.err || { echo \$?=$?; rc=1; }
219 sed 's/^/out:/' <stderr.out      # For debugging.
220 sed 's/^/err:/' <stderr.err >&2  # Likewise.
221 test -s stderr.out && rc=1
222 test "`cat stderr.err`" = 'Hello, Stream!' || rc=1
223 rm -f stderr.out stderr.err || rc=1
224 exit $rc
225 END
226
227 cat > tests/badarg.test <<'END'
228 #!/bin/sh
229 jprog --bad-argument
230 END
231
232 chmod a+x tests/*.test
233
234 ## DO CHECKS ##
235
236 $ACLOCAL
237 $AUTOCONF
238 $AUTOMAKE -a
239
240 # To have the parallel testsuite more verbose.
241 VERBOSE=yes; export VERBOSE
242
243 ./configure --prefix="`pwd`/_inst"
244 cat jprog/PkgLocation.java # For debugging.
245 $MAKE check
246 $MAKE install
247 $MAKE test-installed
248 $MAKE run-installed
249 $MAKE distcheck
250
251 :