1 #! /bin/sh 2 3 # Copyright (c) 2011, 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. Oracle designates this 9 # particular file as subject to the "Classpath" exception as provided 10 # by Oracle in the LICENSE file that accompanied this code. 11 # 12 # This code is distributed in the hope that it will be useful, but WITHOUT 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 # version 2 for more details (a copy is included in the LICENSE file that 16 # accompanied this code). 17 # 18 # You should have received a copy of the GNU General Public License version 19 # 2 along with this work; if not, write to the Free Software Foundation, 20 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 # 22 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 # or visit www.oracle.com if you need additional information or have any 24 # questions. 25 26 # @test 27 # @summary Basic test for requires/provides service 28 # @run shell view.sh 29 30 set -e 31 32 BIN=${TESTJAVA:-../../../../build}/bin 33 SRC=${TESTSRC:-.} 34 35 mk() { 36 d=`dirname $1` 37 if [ ! -d $d ]; then mkdir -p $d; fi 38 cat - >$1 39 } 40 41 rm -rf z.* 42 43 mk z.src/foo/module-info.java <<EOF 44 module foo@1.0 { 45 exports com.foo.*; 46 } 47 EOF 48 49 mk z.src/foo/com/foo/Provider.java <<EOF 50 package com.foo; 51 public abstract class Provider { 52 protected Provider() { } 53 } 54 EOF 55 56 mk z.src/bar/module-info.java <<EOF 57 module bar@1.0 { 58 requires foo; 59 view bar.provider1 { 60 provides service com.foo.Provider with com.bar.MyProvider1; 61 } 62 view bar.provider2 { 63 provides service com.foo.Provider with com.bar.MyProvider2; 64 } 65 } 66 EOF 67 68 mk z.src/bar/com/bar/MyProvider1.java <<EOF 69 package com.bar; 70 import com.foo.Provider; 71 public class MyProvider1 extends Provider { 72 public MyProvider1() { } 73 } 74 EOF 75 76 mk z.src/bar/com/bar/MyProvider2.java <<EOF 77 package com.bar; 78 import com.foo.Provider; 79 public class MyProvider2 extends Provider { 80 public MyProvider2() { } 81 } 82 EOF 83 84 mk z.src/gus/module-info.java <<EOF 85 module gus @ 1.0 { 86 requires foo; 87 requires service com.foo.Provider; 88 class com.gus.Main; 89 } 90 EOF 91 92 mk z.src/gus/com/gus/Main.java <<EOF 93 package com.gus; 94 import java.util.*; 95 import com.foo.Provider; 96 public class Main { 97 public static void main(String[] args) { 98 int expected = Integer.parseInt(args[0]); 99 ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class); 100 int count = 0; 101 for (Provider p: sl) { 102 System.out.format("%s loaded by %s%n", p.getClass(), p.getClass().getClassLoader()); 103 count++; 104 } 105 if (count != expected) 106 throw new RuntimeException(expected + " providers expected"); 107 } 108 } 109 EOF 110 111 mkdir z.modules z.classes 112 113 $BIN/javac -source 8 -d z.modules -modulepath z.modules \ 114 `find z.src -name '*.java'` 115 $BIN/jmod -J-ea -L z.lib create 116 $BIN/jmod -J-ea -L z.lib install z.modules `ls z.src` 117 $BIN/jmod -J-ea -L z.lib ls -v 118 $BIN/java -ea -L z.lib -m gus 2