tests: cosmetic changes in t/extra-sources.sh
[platform/upstream/automake.git] / t / java-compile-run-flat.sh
1 #! /bin/sh
2 # Copyright (C) 2011-2013 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 "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.
25
26 required='java javac'
27 am_serial_tests=yes
28 . test-init.sh
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([PkgLocation.jin])
35 AC_CONFIG_FILES([PkgLocation.java:PkgLocation.jin])
36 AC_OUTPUT
37 END
38
39 ## TOP-LEVEL SETUP AND TARGETS ##
40
41 cat > Makefile.am <<'END'
42 EXTRA_DIST = # Will be updated later.
43
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
55
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
62
63 run-installed:
64         jprog_doing_installcheck=yes $(MAKE) $(AM_MAKEFLAGS) check
65
66 check-local: test-built
67 installcheck-local: test-installed run-installed
68
69 .PHONY: test-built test-installed run-installed
70 END
71
72 ## WRAPPER SCRIPT ##
73
74 cat >> Makefile.am <<'END'
75 bin_SCRIPTS = jprog
76
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'
81
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.
87
88 EXTRA_DIST += jprog.sh
89 CLEANFILES = jprog
90 END
91
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
100 END
101
102 ## JAVA SOURCES ##
103
104 cat >> Makefile.am <<'END'
105 javadir = $(pkgdatadir)/java
106
107 dist_java_JAVA = Main.java HelloStream.java
108 nodist_java_JAVA = PkgLocation.java
109 END
110
111 cat > PkgLocation.jin <<'END'
112 public class PkgLocation {
113     public static String prefix() {
114         return new String("@prefix@");
115     }
116 }
117 END
118
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);
133             }
134         }
135         System.exit(0);
136     }
137 }
138 END
139
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!");
145     }
146 }
147 END
148
149 ## TESTS ##
150
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;
167
168 TESTS = \
169   simple.test \
170   prefix.test \
171   stdout.test \
172   stderr.test \
173   badarg.test
174
175 XFAIL_TESTS = badarg.test
176
177 EXTRA_DIST += $(TESTS)
178 END
179
180 cat > simple.test <<'END'
181 #!/bin/sh
182 jprog
183 END
184
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"
191 END
192
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
203 END
204
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
215 END
216
217 cat > badarg.test <<'END'
218 #!/bin/sh
219 jprog --bad-argument
220 END
221
222 chmod a+x *.test
223
224 ## DO CHECKS ##
225
226 $ACLOCAL
227 $AUTOCONF
228 $AUTOMAKE
229
230 # To have the parallel testsuite more verbose.
231 VERBOSE=yes; export VERBOSE
232
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
240
241 :