maint: Update HACKING
[automake.git] / t / java-compile-run-flat.sh
blob1f74a3c62c4bcd773f52e8484c488b433702e820
1 #! /bin/sh
2 # Copyright (C) 2011-2017 Free Software Foundation, Inc.
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.
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.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
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 "flat" setup for the source tree (i.e., everything in
21 # the top-level directory), and forces the use of the old, non-parallel
22 # testsuite driver. The sister test 'java-compile-run-nested.sh' do
23 # similar checks with a more usual, "nested" setup, and using the older
24 # 'serial-tests' driver.
26 required='java javac'
27 am_serial_tests=yes
28 . test-init.sh
30 echo "AC_SUBST([PATH_SEPARATOR], ['$PATH_SEPARATOR'])" >> configure.ac
32 cat >> configure.ac <<'END'
33 AC_SUBST([JAVA], [java])
34 AC_CONFIG_SRCDIR([PkgLocation.jin])
35 AC_CONFIG_FILES([PkgLocation.java:PkgLocation.jin])
36 AC_OUTPUT
37 END
39 ## TOP-LEVEL SETUP AND TARGETS ##
41 cat > Makefile.am <<'END'
42 EXTRA_DIST = # Will be updated later.
44 test-built:
45 ls -l $(srcdir) ;: For debugging.
46 test $(srcdir) = . || ls -l . ;: Likewise.
47 test -f $(srcdir)/Main.java
48 test -f $(srcdir)/HelloStream.java
49 test -f $(srcdir)/PkgLocation.jin
50 test -f PkgLocation.java
51 test -f HelloStream.class
52 test -f PkgLocation.class
53 test -f Main.class
54 test -f classjava.stamp
56 test-installed:
57 ls -l $(javadir) ;: For debugging.
58 test -f $(javadir)/HelloStream.class
59 test -f $(javadir)/PkgLocation.class
60 test -f $(javadir)/Main.class
61 if find $(prefix) | grep '\.stamp$$'; then exit 1; else :; fi
63 run-installed:
64 jprog_doing_installcheck=yes $(MAKE) $(AM_MAKEFLAGS) check
66 check-local: test-built
67 installcheck-local: test-installed run-installed
69 .PHONY: test-built test-installed run-installed
70 END
72 ## WRAPPER SCRIPT ##
74 cat >> Makefile.am <<'END'
75 bin_SCRIPTS = jprog
77 edit_script = sed -e 's|[@]JAVA@|$(JAVA)|g' \
78 -e 's|[@]javadir@|$(javadir)|g' \
79 -e 's|[@]SHELL@|$(SHELL)|g' \
80 -e 's|[@]PATH_SEPARATOR@|$(PATH_SEPARATOR)|g'
82 jprog: jprog.sh
83 rm -f $@ $@-t
84 $(edit_script) `test -f '$@.sh' || echo $(srcdir)/`$@.sh >$@-t
85 chmod a-w $@-t && chmod a+x $@-t && mv -f $@-t $@
86 sed 's/^/ | /' $@ ;: for debugging.
88 EXTRA_DIST += jprog.sh
89 CLEANFILES = jprog
90 END
92 cat > jprog.sh <<'END'
93 #!@SHELL@
94 CLASSPATH=${jprog_classpath-'@javadir@'}${CLASSPATH+"@PATH_SEPARATOR@$CLASSPATH"}
95 export CLASSPATH
96 case $# in
97 0) exec @JAVA@ Main;;
98 *) exec @JAVA@ Main "$@";;
99 esac
102 ## JAVA SOURCES ##
104 cat >> Makefile.am <<'END'
105 javadir = $(pkgdatadir)/java
107 dist_java_JAVA = Main.java HelloStream.java
108 nodist_java_JAVA = PkgLocation.java
111 cat > PkgLocation.jin <<'END'
112 public class PkgLocation {
113 public static String prefix() {
114 return new String("@prefix@");
119 cat > Main.java <<'END'
120 public class Main {
121 public static void main(String[] args) {
122 for (int i = 0; i < args.length; i++) {
123 if (args[i].equals("--print-prefix")) {
124 System.out.println(PkgLocation.prefix());
125 } else if (args[i].equals("--hello-stdout")) {
126 HelloStream.to(System.out);
127 } else if (args[i].equals("--hello-stderr")) {
128 HelloStream.to(System.err);
129 } else {
130 System.err.println("jprog: invalid option '" + args[i] +
131 "'");
132 System.exit(2);
135 System.exit(0);
140 cat > HelloStream.java <<'END'
141 import java.io.PrintStream;
142 class HelloStream {
143 public static void to(PrintStream stream) {
144 stream.println("Hello, Stream!");
149 ## TESTS ##
151 cat >> Makefile.am <<'END'
152 ## We must use 'TESTS_ENVIRONMENT', not 'AM_TESTS_ENVIRONMENT',
153 ## because the latter is not hnoured by the old serial test
154 ## harness.
155 TESTS_ENVIRONMENT = \
156 if test x"$$jprog_doing_installcheck" != x"yes"; then \
157 jprog_classpath='$(abs_top_builddir):$(abs_top_srcdir)'; \
158 export jprog_classpath; \
159 PATH='$(abs_top_builddir)$(PATH_SEPARATOR)'$$PATH; \
160 export PATH; \
161 else \
162 unset jprog_classpath || :; \
163 PATH='$(prefix)/bin$(PATH_SEPARATOR)'$$PATH; \
164 export PATH; \
165 fi; \
166 config_time_prefix='@prefix@'; export config_time_prefix;
168 TESTS = \
169 simple.test \
170 prefix.test \
171 stdout.test \
172 stderr.test \
173 badarg.test
175 XFAIL_TESTS = badarg.test
177 EXTRA_DIST += $(TESTS)
180 cat > simple.test <<'END'
181 #!/bin/sh
182 jprog
185 cat > prefix.test <<'END'
186 #!/bin/sh
187 jprefix=`jprog --print-prefix` || exit 1
188 echo "$0: exp prefix: $config_time_prefix"
189 echo "$0: got prefix: $jprefix"
190 test x"$jprefix" = x"$config_time_prefix"
193 cat > stdout.test <<'END'
194 #!/bin/sh
195 rc=0
196 jprog --hello-stdout >stdout.out 2>stdout.err || { echo \$?=$?; rc=1; }
197 sed 's/^/out:/' <stdout.out # For debugging.
198 sed 's/^/err:/' <stdout.err >&2 # Likewise.
199 test -s stdout.err && rc=1
200 test "`cat stdout.out`" = 'Hello, Stream!' || rc=1
201 rm -f stdout.out stdout.err || rc=1
202 exit $rc
205 cat > stderr.test <<'END'
206 #!/bin/sh
207 rc=0
208 jprog --hello-stderr >stderr.out 2>stderr.err || { echo \$?=$?; rc=1; }
209 sed 's/^/out:/' <stderr.out # For debugging.
210 sed 's/^/err:/' <stderr.err >&2 # Likewise.
211 test -s stderr.out && rc=1
212 test "`cat stderr.err`" = 'Hello, Stream!' || rc=1
213 rm -f stderr.out stderr.err || rc=1
214 exit $rc
217 cat > badarg.test <<'END'
218 #!/bin/sh
219 jprog --bad-argument
222 chmod a+x *.test
224 ## DO CHECKS ##
226 $ACLOCAL
227 $AUTOCONF
228 $AUTOMAKE
230 # To have the parallel testsuite more verbose.
231 VERBOSE=yes; export VERBOSE
233 ./configure --prefix="$(pwd)/_inst"
234 cat PkgLocation.java # For debugging.
235 $MAKE check
236 $MAKE install
237 $MAKE test-installed
238 $MAKE run-installed
239 $MAKE distcheck