java-compile-run-flat.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #! /bin/sh
  2. # Copyright (C) 2011-2017 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. # Test on compilation and execution of Java class files.
  17. # Also meddle with wrapper scripts, as would be probably seen in a real
  18. # "UNIX-style" use case.
  19. # This test uses a "flat" setup for the source tree (i.e., everything in
  20. # the top-level directory), and forces the use of the old, non-parallel
  21. # testsuite driver. The sister test 'java-compile-run-nested.sh' do
  22. # similar checks with a more usual, "nested" setup, and using the older
  23. # 'serial-tests' driver.
  24. required='java javac'
  25. am_serial_tests=yes
  26. . test-init.sh
  27. echo "AC_SUBST([PATH_SEPARATOR], ['$PATH_SEPARATOR'])" >> configure.ac
  28. cat >> configure.ac <<'END'
  29. AC_SUBST([JAVA], [java])
  30. AC_CONFIG_SRCDIR([PkgLocation.jin])
  31. AC_CONFIG_FILES([PkgLocation.java:PkgLocation.jin])
  32. AC_OUTPUT
  33. END
  34. ## TOP-LEVEL SETUP AND TARGETS ##
  35. cat > Makefile.am <<'END'
  36. EXTRA_DIST = # Will be updated later.
  37. test-built:
  38. ls -l $(srcdir) ;: For debugging.
  39. test $(srcdir) = . || ls -l . ;: Likewise.
  40. test -f $(srcdir)/Main.java
  41. test -f $(srcdir)/HelloStream.java
  42. test -f $(srcdir)/PkgLocation.jin
  43. test -f PkgLocation.java
  44. test -f HelloStream.class
  45. test -f PkgLocation.class
  46. test -f Main.class
  47. test -f classjava.stamp
  48. test-installed:
  49. ls -l $(javadir) ;: For debugging.
  50. test -f $(javadir)/HelloStream.class
  51. test -f $(javadir)/PkgLocation.class
  52. test -f $(javadir)/Main.class
  53. if find $(prefix) | grep '\.stamp$$'; then exit 1; else :; fi
  54. run-installed:
  55. jprog_doing_installcheck=yes $(MAKE) $(AM_MAKEFLAGS) check
  56. check-local: test-built
  57. installcheck-local: test-installed run-installed
  58. .PHONY: test-built test-installed run-installed
  59. END
  60. ## WRAPPER SCRIPT ##
  61. cat >> Makefile.am <<'END'
  62. bin_SCRIPTS = jprog
  63. edit_script = sed -e 's|[@]JAVA@|$(JAVA)|g' \
  64. -e 's|[@]javadir@|$(javadir)|g' \
  65. -e 's|[@]SHELL@|$(SHELL)|g' \
  66. -e 's|[@]PATH_SEPARATOR@|$(PATH_SEPARATOR)|g'
  67. jprog: jprog.sh
  68. rm -f $@ $@-t
  69. $(edit_script) `test -f '$@.sh' || echo $(srcdir)/`$@.sh >$@-t
  70. chmod a-w $@-t && chmod a+x $@-t && mv -f $@-t $@
  71. sed 's/^/ | /' $@ ;: for debugging.
  72. EXTRA_DIST += jprog.sh
  73. CLEANFILES = jprog
  74. END
  75. cat > jprog.sh <<'END'
  76. #!@SHELL@
  77. CLASSPATH=${jprog_classpath-'@javadir@'}${CLASSPATH+"@PATH_SEPARATOR@$CLASSPATH"}
  78. export CLASSPATH
  79. case $# in
  80. 0) exec @JAVA@ Main;;
  81. *) exec @JAVA@ Main "$@";;
  82. esac
  83. END
  84. ## JAVA SOURCES ##
  85. cat >> Makefile.am <<'END'
  86. javadir = $(pkgdatadir)/java
  87. dist_java_JAVA = Main.java HelloStream.java
  88. nodist_java_JAVA = PkgLocation.java
  89. END
  90. cat > PkgLocation.jin <<'END'
  91. public class PkgLocation {
  92. public static String prefix() {
  93. return new String("@prefix@");
  94. }
  95. }
  96. END
  97. cat > Main.java <<'END'
  98. public class Main {
  99. public static void main(String[] args) {
  100. for (int i = 0; i < args.length; i++) {
  101. if (args[i].equals("--print-prefix")) {
  102. System.out.println(PkgLocation.prefix());
  103. } else if (args[i].equals("--hello-stdout")) {
  104. HelloStream.to(System.out);
  105. } else if (args[i].equals("--hello-stderr")) {
  106. HelloStream.to(System.err);
  107. } else {
  108. System.err.println("jprog: invalid option '" + args[i] +
  109. "'");
  110. System.exit(2);
  111. }
  112. }
  113. System.exit(0);
  114. }
  115. }
  116. END
  117. cat > HelloStream.java <<'END'
  118. import java.io.PrintStream;
  119. class HelloStream {
  120. public static void to(PrintStream stream) {
  121. stream.println("Hello, Stream!");
  122. }
  123. }
  124. END
  125. ## TESTS ##
  126. cat >> Makefile.am <<'END'
  127. ## We must use 'TESTS_ENVIRONMENT', not 'AM_TESTS_ENVIRONMENT',
  128. ## because the latter is not hnoured by the old serial test
  129. ## harness.
  130. TESTS_ENVIRONMENT = \
  131. if test x"$$jprog_doing_installcheck" != x"yes"; then \
  132. jprog_classpath='$(abs_top_builddir):$(abs_top_srcdir)'; \
  133. export jprog_classpath; \
  134. PATH='$(abs_top_builddir)$(PATH_SEPARATOR)'$$PATH; \
  135. export PATH; \
  136. else \
  137. unset jprog_classpath || :; \
  138. PATH='$(prefix)/bin$(PATH_SEPARATOR)'$$PATH; \
  139. export PATH; \
  140. fi; \
  141. config_time_prefix='@prefix@'; export config_time_prefix;
  142. TESTS = \
  143. simple.test \
  144. prefix.test \
  145. stdout.test \
  146. stderr.test \
  147. badarg.test
  148. XFAIL_TESTS = badarg.test
  149. EXTRA_DIST += $(TESTS)
  150. END
  151. cat > simple.test <<'END'
  152. #!/bin/sh
  153. jprog
  154. END
  155. cat > prefix.test <<'END'
  156. #!/bin/sh
  157. jprefix=`jprog --print-prefix` || exit 1
  158. echo "$0: exp prefix: $config_time_prefix"
  159. echo "$0: got prefix: $jprefix"
  160. test x"$jprefix" = x"$config_time_prefix"
  161. END
  162. cat > stdout.test <<'END'
  163. #!/bin/sh
  164. rc=0
  165. jprog --hello-stdout >stdout.out 2>stdout.err || { echo \$?=$?; rc=1; }
  166. sed 's/^/out:/' <stdout.out # For debugging.
  167. sed 's/^/err:/' <stdout.err >&2 # Likewise.
  168. test -s stdout.err && rc=1
  169. test "`cat stdout.out`" = 'Hello, Stream!' || rc=1
  170. rm -f stdout.out stdout.err || rc=1
  171. exit $rc
  172. END
  173. cat > stderr.test <<'END'
  174. #!/bin/sh
  175. rc=0
  176. jprog --hello-stderr >stderr.out 2>stderr.err || { echo \$?=$?; rc=1; }
  177. sed 's/^/out:/' <stderr.out # For debugging.
  178. sed 's/^/err:/' <stderr.err >&2 # Likewise.
  179. test -s stderr.out && rc=1
  180. test "`cat stderr.err`" = 'Hello, Stream!' || rc=1
  181. rm -f stderr.out stderr.err || rc=1
  182. exit $rc
  183. END
  184. cat > badarg.test <<'END'
  185. #!/bin/sh
  186. jprog --bad-argument
  187. END
  188. chmod a+x *.test
  189. ## DO CHECKS ##
  190. $ACLOCAL
  191. $AUTOCONF
  192. $AUTOMAKE
  193. # To have the parallel testsuite more verbose.
  194. VERBOSE=yes; export VERBOSE
  195. ./configure --prefix="$(pwd)/_inst"
  196. cat PkgLocation.java # For debugging.
  197. $MAKE check
  198. $MAKE install
  199. $MAKE test-installed
  200. $MAKE run-installed
  201. $MAKE distcheck
  202. :