tests: rework tests on AM_PATH_PYTHON
[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.sh' 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 . ./defs || exit 1
29
30 echo "AC_SUBST([PATH_SEPARATOR], ['$PATH_SEPARATOR'])" >> configure.ac
31
32 cat >> configure.ac <<'END'
33 AC_SUBST([JAVA], [java])
34 AC_CONFIG_SRCDIR([jprog/PkgLocation.jin])
35 AC_SUBST([jprogdatadir],  ['${pkgdatadir}'])
36 AC_SUBST([jprogclassdir], ['${jprogdatadir}/jprog'])
37 AC_CONFIG_FILES([jprog/PkgLocation.java:jprog/PkgLocation.jin])
38 AC_CONFIG_FILES([jprog/Makefile bin/Makefile tests/Makefile])
39 AC_OUTPUT
40 END
41
42 ## TOP-LEVEL SETUP AND TARGETS ##
43
44 cat > Makefile.am <<'END'
45 SUBDIRS = bin jprog tests
46
47 test-built:
48         ls -l $(srcdir)/*             ;: For debugging.
49         test $(srcdir) = . || ls -l * ;: Likewise.
50         test -f $(srcdir)/jprog/Main.java
51         test -f $(srcdir)/jprog/HelloStream.java
52         test -f $(srcdir)/jprog/PkgLocation.jin
53         test -f jprog/PkgLocation.java
54         test -f jprog/HelloStream.class
55         test -f jprog/Main.class
56         test -f jprog/PkgLocation.class
57         test -f jprog/classjprogclass.stamp
58
59 test-installed:
60         ls -l $(jprogclassdir) ;: For debugging.
61         test -f $(jprogclassdir)/HelloStream.class
62         test -f $(jprogclassdir)/Main.class
63         test -f $(jprogclassdir)/PkgLocation.class
64         if find $(prefix) | grep '\.stamp$$'; then exit 1; else :; fi
65
66 run-installed:
67         jprog_doing_installcheck=yes $(MAKE) $(AM_MAKEFLAGS) check
68
69 check-local: test-built
70 installcheck-local: test-installed run-installed
71
72 .PHONY: test-built test-installed run-installed
73 END
74
75 ## WRAPPER SCRIPT ##
76
77 mkdir bin
78
79 cat > bin/Makefile.am <<'END'
80 bin_SCRIPTS = jprog
81
82 edit_script = sed -e 's|[@]JAVA@|$(JAVA)|g' \
83                   -e 's|[@]jprogdatadir@|$(jprogdatadir)|g' \
84                   -e 's|[@]SHELL@|$(SHELL)|g' \
85                   -e 's|[@]PATH_SEPARATOR@|$(PATH_SEPARATOR)|g'
86
87 jprog: jprog.sh
88         rm -f $@ $@-t
89         $(edit_script) `test -f '$@.sh' || echo $(srcdir)/`$@.sh >$@-t
90         chmod a-w $@-t && chmod a+x $@-t && mv -f $@-t $@
91         sed 's/^/ | /' $@ ;: for debugging.
92
93 EXTRA_DIST = jprog.sh
94 CLEANFILES = jprog
95 END
96
97 cat > bin/jprog.sh <<'END'
98 #!@SHELL@
99 CLASSPATH=${jprog_classpath-'@jprogdatadir@'}${CLASSPATH+"@PATH_SEPARATOR@$CLASSPATH"}
100 export CLASSPATH
101 case $# in
102   0) exec @JAVA@ jprog.Main;;
103   *) exec @JAVA@ jprog.Main "$@";;
104 esac
105 END
106
107 ## JAVA SOURCES ##
108
109 mkdir jprog
110
111 cat > jprog/Makefile.am <<'END'
112 dist_jprogclass_JAVA = Main.java HelloStream.java
113 nodist_jprogclass_JAVA = PkgLocation.java
114 END
115
116 cat > jprog/PkgLocation.jin <<'END'
117 package jprog;
118 public class PkgLocation {
119     public static String prefix() {
120         return new String("@prefix@");
121     }
122 }
123 END
124
125 cat > jprog/Main.java <<'END'
126 package jprog;
127 import jprog.PkgLocation;
128 import jprog.HelloStream;
129 public class Main {
130     public static void main(String[] args) {
131         for (int i = 0; i < args.length; i++) {
132             if (args[i].equals("--print-prefix")) {
133                 System.out.println(PkgLocation.prefix());
134             } else if (args[i].equals("--hello-stdout")) {
135                 HelloStream.to(System.out);
136             } else if (args[i].equals("--hello-stderr")) {
137                 HelloStream.to(System.err);
138             } else {
139                System.err.println("jprog: invalid option '" + args[i] +
140                                   "'");
141                System.exit(2);
142             }
143         }
144         System.exit(0);
145     }
146 }
147 END
148
149 cat > jprog/HelloStream.java <<'END'
150 package jprog;
151 import java.io.PrintStream;
152 class HelloStream {
153     public static void to(PrintStream stream) {
154         stream.println("Hello, Stream!");
155     }
156 }
157 END
158
159 ## TESTS ##
160
161 mkdir tests
162
163 cat > tests/Makefile.am <<'END'
164 AM_TESTS_ENVIRONMENT = \
165     if test x"$$jprog_doing_installcheck" != x"yes"; then \
166         jprog_classpath='$(abs_top_builddir):$(abs_top_srcdir)'; \
167         export jprog_classpath; \
168         PATH='$(abs_top_builddir)/bin$(PATH_SEPARATOR)'$$PATH; \
169         export PATH; \
170     else \
171         unset jprog_classpath || :; \
172         PATH='$(prefix)/bin$(PATH_SEPARATOR)'$$PATH; \
173         export PATH; \
174     fi; \
175     config_time_prefix='@prefix@'; export config_time_prefix;
176
177 TESTS = \
178   simple.test \
179   prefix.test \
180   stdout.test \
181   stderr.test \
182   badarg.test
183
184 XFAIL_TESTS = badarg.test
185
186 EXTRA_DIST = $(TESTS)
187 END
188
189 cat > tests/simple.test <<'END'
190 #!/bin/sh
191 jprog
192 END
193
194 cat > tests/prefix.test <<'END'
195 #!/bin/sh
196 jprefix=`jprog --print-prefix` || exit 1
197 echo "$0: exp prefix: $config_time_prefix"
198 echo "$0: got prefix: $jprefix"
199 test x"$jprefix" = x"$config_time_prefix"
200 END
201
202 cat > tests/stdout.test <<'END'
203 #!/bin/sh
204 rc=0
205 jprog --hello-stdout >stdout.out 2>stdout.err || { echo \$?=$?; rc=1; }
206 sed 's/^/out:/' <stdout.out      # For debugging.
207 sed 's/^/err:/' <stdout.err >&2  # Likewise.
208 test -s stdout.err && rc=1
209 test "`cat stdout.out`" = 'Hello, Stream!' || rc=1
210 rm -f stdout.out stdout.err || rc=1
211 exit $rc
212 END
213
214 cat > tests/stderr.test <<'END'
215 #!/bin/sh
216 rc=0
217 jprog --hello-stderr >stderr.out 2>stderr.err || { echo \$?=$?; rc=1; }
218 sed 's/^/out:/' <stderr.out      # For debugging.
219 sed 's/^/err:/' <stderr.err >&2  # Likewise.
220 test -s stderr.out && rc=1
221 test "`cat stderr.err`" = 'Hello, Stream!' || rc=1
222 rm -f stderr.out stderr.err || rc=1
223 exit $rc
224 END
225
226 cat > tests/badarg.test <<'END'
227 #!/bin/sh
228 jprog --bad-argument
229 END
230
231 chmod a+x tests/*.test
232
233 ## DO CHECKS ##
234
235 $ACLOCAL
236 $AUTOCONF
237 $AUTOMAKE -a
238
239 # To have the parallel testsuite more verbose.
240 VERBOSE=yes; export VERBOSE
241
242 ./configure --prefix="$(pwd)/_inst"
243 cat jprog/PkgLocation.java # For debugging.
244 $MAKE check
245 $MAKE install
246 $MAKE test-installed
247 $MAKE run-installed
248 $MAKE distcheck
249
250 :