1 /*
   2  * Copyright (c) 2015, 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  * @summary Test a pool containing external files.
  27  * @author Andrei Eremeev
  28  * @modules jdk.jlink/jdk.tools.jlink.internal
  29  * @run build ImageFilePoolTest
  30  * @run main ImageFilePoolTest
  31  */
  32 
  33 import java.io.ByteArrayInputStream;
  34 import java.util.Optional;
  35 import java.util.function.Function;
  36 import jdk.tools.jlink.internal.ResourcePoolEntryFactory;
  37 import jdk.tools.jlink.internal.ResourcePoolManager;
  38 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  39 import jdk.tools.jlink.plugin.ResourcePool;
  40 
  41 public class ImageFilePoolTest {
  42     public static void main(String[] args) throws Exception {
  43         new ImageFilePoolTest().test();
  44     }
  45 
  46     public void test() throws Exception {
  47         checkNegative();
  48         checkVisitor();
  49     }
  50 
  51     private static final String SUFFIX = "END";
  52 
  53     private void checkVisitor() throws Exception {
  54         ResourcePoolManager input = new ResourcePoolManager();
  55         for (int i = 0; i < 1000; ++i) {
  56             String module = "module" + (i / 100);
  57             input.add(newInMemoryImageFile("/" + module + "/java/class" + i,
  58                     ResourcePoolEntry.Type.CONFIG, "class" + i));
  59         }
  60         if (input.entryCount() != 1000) {
  61             throw new AssertionError();
  62         }
  63         ResourcePoolManager output = new ResourcePoolManager();
  64         ResourceVisitor visitor = new ResourceVisitor();
  65         input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder());
  66         if (visitor.getAmountBefore() == 0) {
  67             throw new AssertionError("Resources not found");
  68         }
  69         if (visitor.getAmountBefore() != input.entryCount()) {
  70             throw new AssertionError("Number of visited resources. Expected: " +
  71                     visitor.getAmountBefore() + ", got: " + input.entryCount());
  72         }
  73         if (visitor.getAmountAfter() != output.entryCount()) {
  74             throw new AssertionError("Number of added resources. Expected: " +
  75                     visitor.getAmountAfter() + ", got: " + output.entryCount());
  76         }
  77         output.entries().forEach(outFile -> {
  78             String path = outFile.path().replaceAll(SUFFIX + "$", "");
  79             Optional<ResourcePoolEntry> inFile = input.findEntry(path);
  80             if (!inFile.isPresent()) {
  81                 throw new AssertionError("Unknown resource: " + path);
  82             }
  83         });
  84     }
  85 
  86     private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> {
  87 
  88         private int amountBefore;
  89         private int amountAfter;
  90 
  91         @Override
  92         public ResourcePoolEntry apply(ResourcePoolEntry file) {
  93             int index = ++amountBefore % 3;
  94             switch (index) {
  95                 case 0:
  96                     ++amountAfter;
  97                     return newInMemoryImageFile(file.path() + SUFFIX,
  98                             file.type(), file.path());
  99                 case 1:
 100                     ++amountAfter;
 101                     return newInMemoryImageFile(file.path(),
 102                             file.type(), file.path());
 103             }
 104             return null;
 105         }
 106 
 107         public int getAmountAfter() {
 108             return amountAfter;
 109         }
 110 
 111         public int getAmountBefore() {
 112             return amountBefore;
 113         }
 114     }
 115 
 116     private void checkNegative() throws Exception {
 117         ResourcePoolManager input = new ResourcePoolManager();
 118         try {
 119             input.add(null);
 120             throw new AssertionError("NullPointerException is not thrown");
 121         } catch (NullPointerException e) {
 122             // expected
 123         }
 124         try {
 125             input.contains(null);
 126             throw new AssertionError("NullPointerException is not thrown");
 127         } catch (NullPointerException e) {
 128             // expected
 129         }
 130         if (input.findEntry("unknown").isPresent()) {
 131             throw new AssertionError("ImageFileResourcePool does not return null for unknown file");
 132         }
 133         if (input.contains(newInMemoryImageFile("/unknown/foo", ResourcePoolEntry.Type.CONFIG, "unknown"))) {
 134             throw new AssertionError("'contain' returns true for /unknown/foo file");
 135         }
 136         input.add(newInMemoryImageFile("/aaa/bbb", ResourcePoolEntry.Type.CONFIG, ""));
 137         try {
 138             input.add(newInMemoryImageFile("/aaa/bbb", ResourcePoolEntry.Type.CONFIG, ""));
 139             throw new AssertionError("Exception expected");
 140         } catch (Exception e) {
 141             // expected
 142         }
 143     }
 144 
 145     private static ResourcePoolEntry newInMemoryImageFile(String path,
 146             ResourcePoolEntry.Type type, String content) {
 147         return ResourcePoolEntryFactory.create(path, type, content.getBytes());
 148     }
 149 }