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     moduleclassFound = 0;
 107   }
 108 
 109   /^module / {
 110     module = $2;
 111     mname = module;
 112     mdir = toPath(msdir, mname);
 113     mkdir(mdir);
 114     if (mfile != "") close(mfile);
 115     mfile = toPath(mdir, "module-info.java");
 116     print $0 >mfile;
 117     next;
 118   }
 119 
 120   /^package / {
 121     pkgpre = $0;
 122     pname = $2;
 123     gsub("\\.", sep, pname);
 124     gsub("(;| )", "", pname);
 125     pdir = toPath(mdir, pname);
 126     mkdir(pdir);
 127     module = 0;
 128     next;
 129   }
 130 
 131   /^import / {
 132     pkgpre = pkgpre "\n" $0;
 133     next;
 134   }
 135 
 136   /.* (class|interface) .* *\{ *\}? *$/ {
 137     class = $0;
 138     sub(" +\{ *\}? *", "", class);
 139     sub(".*class +", "", class);
 140     if (cfile != "") close(cfile);
 141     cfile = toPath(pdir, class ".java");
 142     print pkgpre >>cfile;
 143     print $0 >>cfile;
 144     pkgpre = "";
 145     next;
 146   }
 147 
 148   /^./ {
 149     if (module) {
 150       print $0 >>mfile;
 151       if (match($0, /^ +class +([a-zA-Z.]+) *;/)) {
 152           if (moduleclassFound == 0) {
 153               print module >toPath(tdir, "main");
 154               moduleclassFound = 1;
 155          }
 156       }
 157     }
 158     if (class) print $0 >>cfile;
 159     next;
 160   }
 161 
 162   /^#?$/ { module = 0; class = 0; }
 163 
 164   /.+/ { if (module || class) print $0; }
 165 
 166 ' $tsrc
 167 
 168 tests=`echo * | wc -w`
 169 if [ $tests = 1 ]; then
 170   d=`echo *`
 171   mv $d/* .
 172   rmdir $d
 173 fi
 174 
 175 failures=0
 176 fail() {
 177   echo "FAIL: $*"
 178   failures=`expr $failures + 1`
 179 }
 180 
 181 compile() {
 182   $BIN/javac -d modules -modulepath modules \
 183      `find src -name '*.java'`
 184 }
 185 
 186 install() {
 187   mlist=`cd modules; echo *`
 188   $BIN/jmod create -L z.mlib \
 189   && $BIN/jmod -J-esa $VM_FLAGS_INSTALL install modules $mlist -L z.mlib
 190 #  && $BIN/jmod list -L z.mlib
 191 }
 192 
 193 catfile() {
 194   tr -d '\r' < $1
 195 }
 196 
 197 invoke() {
 198   if [ -f main ] ; then
 199     modulename=`catfile main`
 200     $BIN/java $VM_FLAGS -esa \
 201               -Dtest.src=${TESTSRC} -Dtest.classes=${TESTCLASSES} \
 202               -ea -L z.mlib -m $modulename
 203   else
 204     true 
 205   fi
 206 }
 207 
 208 step() {
 209   if $1; then
 210     if [ $2 = fail ]; then
 211       fail $1 did not fail as expected
 212       return 1
 213     fi
 214   else
 215     if [ $2 = pass ]; then
 216       fail $1 failed
 217       return 1
 218     fi
 219   fi
 220 }
 221 
 222 run() {
 223   test=$1
 224   e=`catfile expected`
 225   [ $tests = 1 ] || echo "-- $test $e"
 226   mkdir modules
 227   case "$e" in
 228     'fail compile')
 229       step compile fail;;
 230     'fail install')
 231       step compile pass && step install fail;;
 232     'fail invoke')
 233       step compile pass && step install pass && step invoke fail;;
 234     'pass setup')
 235       ;;
 236     'pass compile')
 237       step compile pass;;
 238     pass)
 239       step compile pass && step install pass && step invoke pass;;
 240     *)
 241       fail "Unhandled expected result: $e";;
 242   esac
 243 }
 244 
 245 if [ $tests = 1 ]; then
 246   run singleton || /bin/true
 247 else
 248   pdir=`echo $BIN | cut -c1-3`
 249   if [ -z "$TESTSRC" -a "$pdir" = "../" ]; then
 250     BIN=../$BIN;
 251   fi
 252   for t in *; do
 253     if [ -d $t ] ; then
 254       cd $t
 255       run `echo $t | cut -c4-` || /bin/true
 256       cd ..
 257     fi
 258   done
 259 fi
 260 
 261 if [ $tests -gt 1 ]; then
 262   echo
 263   echo "== $tests tests, $failures failure(s)"
 264 fi
 265 
 266 exit $failures