java-compile-run-nested.sh 6.5 KB

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