1 /* 2 * Copyright (c) 2003, 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4408526 6854795 27 * @summary Index the non-meta files in META-INF, such as META-INF/services. 28 */ 29 30 import java.io.*; 31 import java.util.Arrays; 32 import java.util.jar.*; 33 import sun.tools.jar.Main; 34 import java.util.zip.ZipFile; 35 36 public class MetaInf { 37 38 static String jarName = "a.jar"; 39 static String INDEX = "META-INF/INDEX.LIST"; 40 static String SERVICES = "META-INF/services"; 41 static String contents = 42 System.getProperty("test.src") + File.separatorChar + "jarcontents"; 43 44 static void run(String ... args) { 45 if (! new Main(System.out, System.err, "jar").run(args)) 46 throw new Error("jar failed: args=" + Arrays.toString(args)); 47 } 48 49 static void copy(File from, File to) throws IOException { 50 FileInputStream in = new FileInputStream(from); 51 FileOutputStream out = new FileOutputStream(to); 52 try { 53 byte[] buf = new byte[8192]; 54 int n; 55 while ((n = in.read(buf)) != -1) 56 out.write(buf, 0, n); 57 } finally { 58 in.close(); 59 out.close(); 60 } 61 } 62 63 static boolean contains(File jarFile, String entryName) 64 throws IOException { 65 return new ZipFile(jarFile).getEntry(entryName) != null; 66 } 67 68 static void checkContains(File jarFile, String entryName) 69 throws IOException { 70 if (! contains(jarFile, entryName)) 71 throw new Error(String.format("expected jar %s to contain %s", 72 jarFile, entryName)); 73 } 74 75 static void testIndex(String jarName) throws IOException { 76 System.err.printf("jarName=%s%n", jarName); 77 78 File jar = new File(jarName); 79 80 // Create a jar to be indexed. 81 run("cf", jarName, "-C", contents, SERVICES); 82 83 for (int i = 0; i < 2; i++) { 84 run("i", jarName); 85 checkContains(jar, INDEX); 86 checkContains(jar, SERVICES); 87 } 88 89 JarFile f = new JarFile(jarName); 90 BufferedReader index = 91 new BufferedReader( 92 new InputStreamReader( 93 f.getInputStream(f.getJarEntry(INDEX)))); 94 String line; 95 while ((line = index.readLine()) != null) { 96 if (line.equals(SERVICES)) { 97 return; 98 } 99 } 100 throw new Error(SERVICES + " not indexed."); 101 } 102 103 public static void main(String[] args) throws IOException { 104 testIndex("a.jar"); // a path with parent == null 105 testIndex("./a.zip"); // a path with parent != null 106 107 // Try indexing a jar in the default temp directory. 108 File tmpFile = File.createTempFile("MetaInf", null, null); 109 try { 110 testIndex(tmpFile.getPath()); 111 } finally { 112 tmpFile.delete(); 113 } 114 } 115 }