1 #! /bin/sh
   2 
   3 # Copyright (c) 2012, 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 # @test
  25 # @summary jmod remove tests
  26 
  27 set -e
  28 
  29 BIN=${TESTJAVA:-../../../../../build}/bin
  30 SRC=${TESTSRC:-.}
  31 VMOPTS="${TESTVMOPTS} -esa -ea"
  32 test=000jmod-rm
  33 
  34 mk() {
  35   d=`dirname $1`
  36   if [ ! -d $d ]; then mkdir -p $d; fi
  37   cat - >$1
  38 }
  39 
  40 compare() {
  41   if [ "$1" != "$2" ]; then
  42     echo "FAIL: expected $1, got $2"
  43     exit 1
  44   fi
  45 }
  46 
  47 rm -rf $test/z.*
  48 
  49 mk $test/z.src/foo/module-info.java <<EOF
  50 module foo@1.0 {
  51     exports com.foo;
  52     class com.foo.Main;
  53     view foo.internal {
  54         exports com.foo.internal;
  55         permits qux;
  56     }
  57 }
  58 EOF
  59 
  60 mk $test/z.src/foo/com/foo/Main.java <<EOF
  61 package com.foo;
  62 public class Main {
  63     public static void main(String[] args) {
  64         System.out.println("Hello from " + name());
  65     }
  66 
  67     public static String name() {
  68         return "foo";
  69     }
  70 }
  71 EOF
  72 
  73 mk $test/z.src/foo/com/foo/internal/Hello.java <<EOF
  74 package com.foo.internal;
  75 public class Hello {
  76     public static String name() {
  77         return "foo.internal";
  78     }
  79 }
  80 EOF
  81 
  82 mk $test/z.src/bar/module-info.java <<EOF
  83 module bar@1.0 {
  84     class com.bar.Main;
  85 }
  86 EOF
  87 
  88 mk $test/z.src/bar/com/bar/Main.java <<EOF
  89 package com.bar;
  90 public class Main {
  91     public static void main(String[] args) {
  92         System.out.println("Hello from bar");
  93     }
  94 }
  95 EOF
  96 
  97 mk $test/z.src/baz/module-info.java <<EOF
  98 module baz@1.0 {
  99     requires foo @ 1.0;
 100     class com.baz.Main;
 101 }
 102 EOF
 103 
 104 mk $test/z.src/baz/com/baz/Main.java <<EOF
 105 package com.baz;
 106 public class Main {
 107     public static void main(String[] args) {
 108         System.out.println("Hello from baz, and hello from " + com.foo.Main.name());
 109     }
 110 }
 111 EOF
 112 
 113 mk $test/z.src/qux/module-info.java <<EOF
 114 module qux@1.0 {
 115     requires foo.internal @ 1.0;
 116     class com.qux.Main;
 117 }
 118 EOF
 119 
 120 mk $test/z.src/qux/com/qux/Main.java <<EOF
 121 package com.qux;
 122 import com.foo.internal.Hello;
 123 public class Main {
 124     public static void main(String[] args) {
 125         System.out.println("Hello from qux, and hello from " + Hello.name());
 126     }
 127 }
 128 EOF
 129 
 130 mkdir $test/z.modules $test/z.classes
 131 
 132 $BIN/javac -source 8 -d $test/z.modules -modulepath $test/z.modules \
 133     `find $test/z.src -name '*.java'`
 134 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib create
 135 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib install $test/z.modules foo
 136 ## Check foo@1.0 is installed
 137 compare foo@1.0 `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 138 ## Check jmod remove -d (--dry)
 139 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib remove -d foo@1.0
 140 compare foo@1.0 `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 141 ## Check jmod remove (not a dry run!)
 142 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib remove foo@1.0
 143 compare "" `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 144 rm -rf $test/z.lib
 145 
 146 ## Check jmod remove with additional non dependent root modules
 147 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib create
 148 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib install $test/z.modules foo bar
 149 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm foo@1.0
 150 compare bar@1.0 `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 151 rm -rf $test/z.lib
 152 
 153 ## Check with foo and dependent baz, should fail to remove foo
 154 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib create
 155 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib install $test/z.modules foo baz
 156 ## We expect the remove to fail. Temporarily disable '-e' ( script exits
 157 ## immediately if a command exits with a  non-zero exit status ).
 158 set +e
 159 if `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm foo@1.0 > /dev/null 2>&1`; then
 160   echo "FAIL: foo@1.0 should not be removed as baz@1.0 depends on it"
 161   exit 1
 162 fi
 163 set -e
 164 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm foo@1.0 baz@1.0
 165 compare "" `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 166 rm -rf $test/z.lib
 167 
 168 ## Check views, foo.internal and dependent qux, should fail to remove foo
 169 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib create
 170 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib install $test/z.modules foo qux
 171 ## Expect next command to fail
 172 set +e
 173 if `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm foo@1.0 > /dev/null 2>&1`; then
 174   echo "FAIL: foo@1.0 should not be removed as qux@1.0 depends on it"
 175   exit 1
 176 fi
 177 set -e
 178 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm qux@1.0 foo@1.0
 179 compare "" `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 180 rm -rf $test/z.lib
 181 
 182 ## Check -f (--force), foo and dependent baz, should remove foo
 183 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib create
 184 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib install $test/z.modules foo baz
 185 $BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib rm -f foo@1.0
 186 compare baz@1.0 `$BIN/jmod ${TESTTOOLVMOPTS} -L $test/z.lib list`
 187 rm -rf $test/z.lib