test/tools/jar/index/MetaInf.java

Print this page




  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 }


  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 }