2
|
1 |
#
|
|
2 |
# Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
#
|
|
5 |
# This code is free software; you can redistribute it and/or modify it
|
|
6 |
# under the terms of the GNU General Public License version 2 only, as
|
|
7 |
# published by the Free Software Foundation.
|
|
8 |
#
|
|
9 |
# This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
# version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
# accompanied this code).
|
|
14 |
#
|
|
15 |
# You should have received a copy of the GNU General Public License version
|
|
16 |
# 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
#
|
|
19 |
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
# CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
# have any questions.
|
|
22 |
#
|
|
23 |
|
|
24 |
# @test
|
|
25 |
# @bug 4640520 6354623
|
|
26 |
# @summary Unit test for java.util.ServiceLoader
|
|
27 |
#
|
|
28 |
# @build Basic Load FooService FooProvider1 FooProvider2 FooProvider3
|
|
29 |
# @run shell basic.sh
|
|
30 |
|
|
31 |
# Command-line usage: sh basic.sh /path/to/build
|
|
32 |
|
|
33 |
if [ -z "$TESTJAVA" ]; then
|
|
34 |
if [ $# -lt 1 ]; then exit 1; fi
|
|
35 |
TESTJAVA="$1"; shift
|
|
36 |
TESTSRC="`pwd`"
|
|
37 |
TESTCLASSES="`pwd`"
|
|
38 |
fi
|
|
39 |
|
|
40 |
JAVA="$TESTJAVA/bin/java"
|
|
41 |
JAR="$TESTJAVA/bin/jar"
|
|
42 |
|
|
43 |
OS=`uname -s`
|
|
44 |
case "$OS" in
|
|
45 |
SunOS )
|
|
46 |
SEP=':' ;;
|
|
47 |
Linux )
|
|
48 |
SEP=':' ;;
|
|
49 |
* )
|
|
50 |
SEP='\;' ;;
|
|
51 |
esac
|
|
52 |
|
|
53 |
JARD=`pwd`/x.jar
|
|
54 |
EXTD=`pwd`/x.ext
|
|
55 |
TESTD=`pwd`/x.test
|
|
56 |
|
|
57 |
if [ \! -d $EXTD ]; then
|
|
58 |
# Initialize
|
|
59 |
echo Initializing...
|
|
60 |
rm -rf $JARD $EXTD $TESTD
|
|
61 |
mkdir -p $JARD $EXTD $TESTD
|
|
62 |
|
|
63 |
for n in 2 3; do
|
|
64 |
rm -rf $JARD/*; mkdir -p $JARD/META-INF/services
|
|
65 |
echo FooProvider$n \
|
|
66 |
>$JARD/META-INF/services/FooService
|
|
67 |
cp $TESTCLASSES/FooProvider$n.class $JARD
|
|
68 |
if [ $n = 3 ]; then
|
|
69 |
cp $TESTCLASSES/FooService.class $JARD
|
|
70 |
fi
|
|
71 |
(cd $JARD; "$JAR" -cf ../p$n.jar *)
|
|
72 |
done
|
|
73 |
|
|
74 |
mv p3.jar $EXTD
|
|
75 |
|
|
76 |
cp $TESTCLASSES/Load.class $TESTD
|
|
77 |
cp $TESTCLASSES/FooService.class $TESTD
|
|
78 |
cp $TESTCLASSES/FooProvider1.class $TESTD
|
|
79 |
mkdir -p $TESTD/META-INF/services
|
|
80 |
echo FooProvider1 \
|
|
81 |
>$TESTD/META-INF/services/FooService
|
|
82 |
|
|
83 |
# This gives us:
|
|
84 |
# $TESTD: FooProvider1
|
|
85 |
# . : FooProvider2, in p2.jar
|
|
86 |
# $EXTD: FooProvider3, in p3.jar
|
|
87 |
|
|
88 |
fi
|
|
89 |
|
|
90 |
failures=0
|
|
91 |
|
|
92 |
go() {
|
|
93 |
echo ''
|
|
94 |
cp="$1"; shift
|
|
95 |
if [ -z "$cp" ]; then cp="$TESTCLASSES"; else cp="$TESTCLASSES$SEP$cp"; fi
|
|
96 |
vmargs="$1"; shift
|
|
97 |
sh -xc "'$JAVA' -cp $cp $vmargs $T $*" 2>&1
|
|
98 |
if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
|
|
99 |
}
|
|
100 |
|
|
101 |
|
|
102 |
# Java-level tests
|
|
103 |
|
|
104 |
T=Basic
|
|
105 |
go ".${SEP}$TESTD${SEP}p2.jar" "-Djava.ext.dirs=$EXTD"
|
|
106 |
|
|
107 |
|
|
108 |
# Success cases
|
|
109 |
|
|
110 |
T=Load
|
|
111 |
|
|
112 |
go "$TESTD" "" FooProvider1
|
|
113 |
|
|
114 |
go ".${SEP}p2.jar" "" FooProvider2
|
|
115 |
|
|
116 |
go "" "-Djava.ext.dirs=$EXTD" FooProvider3
|
|
117 |
|
|
118 |
go "$TESTD${SEP}p2.jar" "" FooProvider1 FooProvider2
|
|
119 |
|
|
120 |
go "$TESTD" "-Djava.ext.dirs=$EXTD" FooProvider3 FooProvider1
|
|
121 |
|
|
122 |
go "$TESTD${SEP}p2.jar" "-Djava.ext.dirs=$EXTD" \
|
|
123 |
FooProvider3 FooProvider1 FooProvider2
|
|
124 |
|
|
125 |
# Should only find the installed provider
|
|
126 |
go "$TESTD${SEP}p2.jar" "-Djava.ext.dirs=$EXTD" -i FooProvider3
|
|
127 |
|
|
128 |
|
|
129 |
# Failure cases
|
|
130 |
|
|
131 |
mkdir -p x.meta/META-INF/services
|
|
132 |
|
|
133 |
# Simple failures
|
|
134 |
for p in FooProvider42 'blah blah' 9234 'X!' java.lang.Object; do
|
|
135 |
echo $p >x.meta/META-INF/services/FooService
|
|
136 |
go ".${SEP}x.meta" "" fail
|
|
137 |
done
|
|
138 |
|
|
139 |
# Failures followed by successes
|
|
140 |
echo FooProvider42 >x.meta/META-INF/services/FooService
|
|
141 |
go "$TESTD${SEP}x.meta" "" FooProvider1 fail
|
|
142 |
go "x.meta${SEP}$TESTD" "" fail FooProvider1
|
|
143 |
go "$TESTD${SEP}x.meta${SEP}${SEP}p2.jar" "-Djava.ext.dirs=$EXTD" \
|
|
144 |
FooProvider3 FooProvider1 fail FooProvider2
|
|
145 |
|
|
146 |
|
|
147 |
# Summary
|
|
148 |
|
|
149 |
echo ''
|
|
150 |
if [ $failures -gt 0 ];
|
|
151 |
then echo "$failures case(s) failed";
|
|
152 |
else echo "All cases passed"; fi
|
|
153 |
exit $failures
|