1 #! /bin/sh
   2 
   3 # Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
   4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 #
   6 # This code is free software; you can redistribute it and/or modify it
   7 # under the terms of the GNU General Public License version 2 only, as
   8 # published by the Free Software Foundation.
   9 #
  10 # This code is distributed in the hope that it will be useful, but WITHOUT
  11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 # version 2 for more details (a copy is included in the LICENSE file that
  14 # accompanied this code).
  15 #
  16 # You should have received a copy of the GNU General Public License version
  17 # 2 along with this work; if not, write to the Free Software Foundation,
  18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 #
  20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 # or visit www.oracle.com if you need additional information or have any
  22 # questions.
  23 
  24 OS=`uname -s`
  25 DASH_P="-p"
  26 # gawk is not available on Solaris and Windows
  27 # set to the proper nawk/awk/gawk 
  28 case "$OS" in
  29   SunOS )
  30     PS=":"
  31     FS="/"
  32     AWK=nawk
  33     ;;
  34   Linux )
  35     AWK=gawk
  36     PS=":"
  37     FS="/"
  38     ;;
  39   Windows* )
  40     AWK=awk
  41     DASH_P=""
  42     PS=";"
  43     FS="\\"
  44     ;;
  45   CYGWIN* )
  46     AWK=awk
  47     PS=";"
  48     FS="/"
  49     isCygwin=true
  50     ;;
  51   * )
  52     echo "Unrecognized system!"
  53     exit 1;
  54     ;;
  55 esac
  56 
  57 BIN=${TESTJAVA:-../../../../../build}/bin
  58 SRC=${TESTSRC:-.}
  59 
  60 tsrc=$1
  61 dir=`dirname $1`
  62 if [ $tsrc = "-" ] ; then
  63   tsrc=""
  64 elif [ $dir = "." ] ; then
  65   tsrc="../$tsrc"
  66 fi
  67 
  68 # clean up the working directory
  69 rm -rf z.*
  70 mkdir z.test
  71 cd z.test
  72 
  73 # parse the input $tsrc file and write source files in
  74 # an appropriate directory (modulepath or package name hierarchy) 
  75 #
  76 # MKS awk system("mkdir") command looks like invoking the 
  77 # DOS mkdir command rather than the MKS mkdir when running from
  78 # jtreg harness (although system("which mkdir") shows it's MKS
  79 # mkdir.exe.  It doesn't accept "-p" option nor forward slashes.
  80 # It will create a directory named "-p" instead but it does create
  81 # all non-existing subdirectories.  So the file separator and
  82 # mkdir -p option are passed as variables to the awk script
  83 # to handle MKS specially.
  84 #
  85 # MKS awk does not accept plan `{` and so the escape sequence
  86 # `\{' is used that causes the warning on other platforms.
  87 #
  88 $AWK -v sep="$FS" -v dash_p="$DASH_P" '
  89   function toPath(dir, name) {
  90     return sprintf("%s%s%s", dir, sep, name)
  91   }
  92   function mkdir(dir) {
  93     system("mkdir " dash_p " " dir);
  94   }
  95 
  96   /^:/ {
  97     test = $2; status = $3; how = $4;
  98     tdir = sprintf("%03d%s", tcount++, $2);
  99     mkdir(tdir);
 100     if (NF == 3) e = $3; else e = $3 " " $4;
 101     efile = toPath(tdir, "expected");
 102     print e >efile;
 103     close(efile);
 104     msdir = toPath(tdir, "src");
 105     mkdir(msdir);
 106   }
 107 
 108   /^module / {
 109     module = $2;
 110     mname = module;
 111     mdir = toPath(msdir, mname);
 112     mkdir(mdir);
 113     if (mfile != "") close(mfile);
 114     mfile = toPath(mdir, "module-info.java");
 115     print $0 >mfile;
 116     next;
 117   }
 118 
 119   /^package / {
 120     pkgpre = $0;
 121     pname = $2;
 122     gsub("\\.", sep, pname);
 123     gsub("(;| )", "", pname);
 124     pdir = toPath(mdir, pname);
 125     mkdir(pdir);
 126     module = 0;
 127     next;
 128   }
 129 
 130   /^import / {
 131     pkgpre = pkgpre "\n" $0;
 132     next;
 133   }
 134 
 135   /.* (class|interface) .* *\{ *\}? *$/ {
 136     class = $0;
 137     sub(" +\{ *\}? *", "", class);
 138     sub(".*class +", "", class);
 139     if (cfile != "") close(cfile);
 140     cfile = toPath(pdir, class ".java");
 141     print pkgpre >>cfile;
 142     print $0 >>cfile;
 143     pkgpre = "";
 144     next;
 145   }
 146 
 147   /^./ {
 148     if (module) {
 149       print $0 >>mfile;
 150       if (match($0, /^ +class +([a-zA-Z.]+) *;/)) print module >toPath(tdir, "main");
 151     }
 152     if (class) print $0 >>cfile;
 153     next;
 154   }
 155 
 156   /^#?$/ { module = 0; class = 0; }
 157 
 158   /.+/ { if (module || class) print $0; }
 159 
 160 ' $tsrc
 161 
 162 tests=`echo * | wc -w`
 163 if [ $tests = 1 ]; then
 164   d=`echo *`
 165   mv $d/* .
 166   rmdir $d
 167 fi
 168 
 169 failures=0
 170 fail() {
 171   echo "FAIL: $*"
 172   failures=`expr $failures + 1`
 173 }
 174 
 175 compile() {
 176   $BIN/javac -d modules -modulepath modules \
 177      `find src -name '*.java'`
 178 }
 179 
 180 install() {
 181   mlist=`cd modules; echo *`
 182   $BIN/jmod create -L z.mlib \
 183   && $BIN/jmod -J-esa $VM_FLAGS_INSTALL install modules $mlist -L z.mlib
 184 #  && $BIN/jmod list -L z.mlib
 185 }
 186 
 187 catfile() {
 188   tr -d '\r' < $1
 189 }
 190 
 191 invoke() {
 192   if [ -f main ] ; then
 193     modulename=`catfile main`
 194     $BIN/java $VM_FLAGS -esa \
 195               -Dtest.src=${TESTSRC} -Dtest.classes=${TESTCLASSES} \
 196               -ea -L z.mlib -m $modulename
 197   else
 198     true 
 199   fi
 200 }
 201 
 202 step() {
 203   if $1; then
 204     if [ $2 = fail ]; then
 205       fail $1 did not fail as expected
 206       return 1
 207     fi
 208   else
 209     if [ $2 = pass ]; then
 210       fail $1 failed
 211       return 1
 212     fi
 213   fi
 214 }
 215 
 216 run() {
 217   test=$1
 218   e=`catfile expected`
 219   [ $tests = 1 ] || echo "-- $test $e"
 220   mkdir modules
 221   case "$e" in
 222     'fail compile')
 223       step compile fail;;
 224     'fail install')
 225       step compile pass && step install fail;;
 226     'fail invoke')
 227       step compile pass && step install pass && step invoke fail;;
 228     'pass setup')
 229       ;;
 230     'pass compile')
 231       step compile pass;;
 232     pass)
 233       step compile pass && step install pass && step invoke pass;;
 234     *)
 235       fail "Unhandled expected result: $e";;
 236   esac
 237 }
 238 
 239 if [ $tests = 1 ]; then
 240   run singleton || /bin/true
 241 else
 242   pdir=`echo $BIN | cut -c1-3`
 243   if [ -z "$TESTSRC" -a "$pdir" = "../" ]; then
 244     BIN=../$BIN;
 245   fi
 246   for t in *; do
 247     cd $t
 248     run `echo $t | cut -c4-` || /bin/true
 249     cd ..
 250   done
 251 fi
 252 
 253 if [ $tests -gt 1 ]; then
 254   echo
 255   echo "== $tests tests, $failures failure(s)"
 256 fi
 257 
 258 exit $failures