1#!/bin/sh
2
3#
4# Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6#
7# This code is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License version 2 only, as
9# published by the Free Software Foundation.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26
27# @test
28# @bug 4212732
29# @summary Test handling of the Class-Path attribute in jar file manifests
30# @author Martin Buchholz
31#
32# @run shell Class-Path.sh
33
34# To run this test manually, simply do ./Class-Path.sh
35
36. ${TESTSRC-.}/Util.sh
37
38set -u
39
40Cleanup() {
41    Sys rm -rf pkg Main.java Main.class Main.jar jars
42    Sys rm -rf MANIFEST.MF A.jar B.zip
43}
44
45Cleanup
46Sys mkdir pkg
47
48#----------------------------------------------------------------
49# Create mutually referential jar files
50#----------------------------------------------------------------
51cat >pkg/A.java <<EOF
52package pkg;
53import pkg.B;
54public class A {
55    public static int f() { return B.g(); }
56    public static int g() { return 0; }
57}
58EOF
59
60cat >pkg/B.java <<EOF
61package pkg;
62import pkg.A;
63public class B {
64    public static int f() { return A.g(); }
65    public static int g() { return 0; }
66}
67EOF
68
69Sys "$javac" ${TESTTOOLVMOPTS} pkg/A.java pkg/B.java
70
71MkManifestWithClassPath "B.zip"
72Sys "$jar" cmf MANIFEST.MF A.jar pkg/A.class
73
74MkManifestWithClassPath "A.jar"
75Sys "$jar" cmf MANIFEST.MF B.zip pkg/B.class
76
77cat >Main.java <<EOF
78import pkg.*;
79public class Main {
80    public static void main(String []a) { System.exit(A.f() + B.f()); }
81}
82EOF
83
84Success "$javac" ${TESTTOOLVMOPTS} -cp "A.jar" Main.java
85Success "$javac" ${TESTTOOLVMOPTS} -cp "B.zip" Main.java
86Success "$java" ${TESTVMOPTS}  -cp "A.jar${PS}." Main
87Success "$java" ${TESTVMOPTS}  -cp "B.zip${PS}." Main
88
89#----------------------------------------------------------------
90# Jar file Class-Path expanded only for jars found on user class path
91#----------------------------------------------------------------
92Sys mkdir jars
93Sys mv A.jar B.zip jars/.
94Success "$javac" ${TESTTOOLVMOPTS} -cp "jars/A.jar"       Main.java
95Success "$java" ${TESTVMOPTS}  -cp "jars/A.jar${PS}." Main
96
97Success "$javac" ${TESTTOOLVMOPTS} -cp "jars/B.zip"       Main.java
98Success "$java" ${TESTVMOPTS}  -cp "jars/B.zip${PS}." Main
99
100# Success "$javac" ${TESTTOOLVMOPTS} -extdirs "jars"        -cp None Main.java
101# Success "$javac" ${TESTTOOLVMOPTS} -Djava.ext.dirs="jars" -cp None Main.java
102# Success "$java" ${TESTVMOPTS}  -Djava.ext.dirs="jars" -cp .    Main
103
104# Success "$javac" ${TESTTOOLVMOPTS} -endorseddirs "jars"        -cp None Main.java
105# Success "$javac" ${TESTTOOLVMOPTS} -Djava.endorsed.dirs="jars" -cp None Main.java
106# Success "$java" ${TESTVMOPTS}  -Djava.endorsed.dirs="jars" -cp .    Main
107
108Failure "$java" ${TESTVMOPTS}  -Xbootclasspath/p:"jars/A.jar" -cp .    Main
109Failure "$java" ${TESTVMOPTS}  -Xbootclasspath/a:"jars/B.zip" -cp .    Main
110Failure "$javac" ${TESTTOOLVMOPTS} -Xbootclasspath/p:"jars/A.jar" -cp None Main.java
111Failure "$javac" ${TESTTOOLVMOPTS} -Xbootclasspath/a:"jars/B.zip" -cp None Main.java
112Sys mv jars/A.jar jars/B.zip .
113
114MkManifestWithClassPath "A.jar"
115echo "Main-Class: Main" >> MANIFEST.MF
116Sys "$jar" cmf MANIFEST.MF Main.jar Main.class
117
118Success "$java" ${TESTVMOPTS} -jar Main.jar
119
120MkManifestWithClassPath "."
121Sys "$jar" cmf MANIFEST.MF A.jar pkg/A.class
122
123Success "$javac" ${TESTTOOLVMOPTS} -cp "A.jar" Main.java
124Success "$java" ${TESTVMOPTS} -jar Main.jar
125
126MkManifestWithClassPath ""
127Sys "$jar" cmf MANIFEST.MF A.jar pkg/A.class
128
129Failure "$javac" ${TESTTOOLVMOPTS} -cp "A.jar" Main.java
130Failure "$java" ${TESTVMOPTS} -jar Main.jar
131
132#----------------------------------------------------------------
133# Test new flag -e (application entry point)
134#----------------------------------------------------------------
135
136cat > Hello.java <<EOF
137import pkg.*;
138public class Hello {
139    public static void main(String []a) { System.out.println("Hello World!"); }
140}
141EOF
142
143cat > Bye.java <<EOF
144import pkg.*;
145public class Bye {
146    public static void main(String []a) { System.out.println("Good Bye!"); }
147}
148EOF
149
150Success "$javac" ${TESTTOOLVMOPTS} Hello.java Bye.java
151
152# test jar creation without manifest
153#
154Success "$jar" cfe "Hello.jar" "Hello" Hello.class
155Success "$java" ${TESTVMOPTS} -jar Hello.jar
156
157# test for overriding the manifest during jar creation
158#
159echo "Main-Class: Hello" >> MANIFEST.MF
160
161# test for error: " 'e' flag and manifest with the 'Main-Class' 
162# attribute cannot be specified together, during creation
163Failure "$jar" cmfe  MANIFEST.MF "Bye.jar" "Bye" Bye.class
164
165# test for overriding the manifest when updating the jar
166#
167Success "$jar" cfe "greetings.jar" "Hello" Hello.class
168Success "$jar" ufe "greetings.jar" "Bye" Bye.class
169Success "$java" ${TESTVMOPTS} -jar greetings.jar
170
171# test for error: " 'e' flag and manifest with the 'Main-Class'
172# attribute cannot be specified together, during update
173Failure "$jar" umfe  MANIFEST.MF "greetings.jar" "Hello"
174
175# test jar updation when there are no inputfiles 
176#
177Success "$jar" ufe "Hello.jar" "Bye"
178Failure "$java" ${TESTVMOPTS} -jar Hello.jar
179Success "$jar" umf  MANIFEST.MF "Hello.jar"
180
181# test creating jar when the to-be-archived files
182# do not contain the specified main class, there is no check done
183# for the presence of the main class, so the test will pass
184#
185Success "$jar" cfe "Hello.jar" "Hello" Bye.class
186
187# Jar creation and update when there is no manifest and inputfiles 
188# specified
189Failure "$jar" cvf "A.jar"
190Failure "$jar" uvf "A.jar"
191
192# error: no such file or directory
193Failure "$jar" cvf "A.jar" non-existing.file
194Failure "$jar" uvf "A.jar" non-existing.file
195
196Cleanup
197
198Bottom Line
199