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 ZipFile zf = new ZipFile(jarFile); 66 if ( zf != null ) { 67 boolean result = zf.getEntry(entryName) != null; 68 zf.close(); 69 return result; 70 } 71 return false; 72 } 73 74 static void checkContains(File jarFile, String entryName) 75 throws IOException { 76 if (! contains(jarFile, entryName)) 77 throw new Error(String.format("expected jar %s to contain %s", 78 jarFile, entryName)); 79 } 80 81 static void testIndex(String jarName) throws IOException { 82 System.err.printf("jarName=%s%n", jarName); 83 84 File jar = new File(jarName); 85 86 // Create a jar to be indexed. 87 run("cf", jarName, "-C", contents, SERVICES); 88 89 for (int i = 0; i < 2; i++) { 90 run("i", jarName); 91 checkContains(jar, INDEX); 92 checkContains(jar, SERVICES); 93 } 94 95 JarFile f = new JarFile(jarName); 96 BufferedReader index = 97 new BufferedReader( 98 new InputStreamReader( 99 f.getInputStream(f.getJarEntry(INDEX)))); 100 String line; 101 while ((line = index.readLine()) != null) { 102 if (line.equals(SERVICES)) { 103 index.close(); 104 f.close(); 105 return; 106 } 107 } 108 index.close(); 109 f.close(); 110 throw new Error(SERVICES + " not indexed."); 111 } 112 113 public static void main(String[] args) throws IOException { 114 testIndex("a.jar"); // a path with parent == null 115 testIndex("./a.zip"); // a path with parent != null 116 117 // Try indexing a jar in the default temp directory. 118 File tmpFile = File.createTempFile("MetaInf", null, null); 119 try { 120 testIndex(tmpFile.getPath()); 121 } finally { 122 tmpFile.delete(); 123 } 124 } 125 }