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 exclude plugin
  27  * @author Jean-Francois Denise
  28  * @modules jdk.jlink/jdk.tools.jlink.internal
  29  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  30  *          jdk.jlink/jdk.tools.jlink.plugin
  31  * @run main ExcludePluginTest
  32  */
  33 
  34 import java.io.File;
  35 import java.nio.file.Files;
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import jdk.tools.jlink.internal.ResourcePoolManager;
  39 
  40 import jdk.tools.jlink.internal.plugins.ExcludePlugin;
  41 import jdk.tools.jlink.plugin.ResourcePool;
  42 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  43 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  44 
  45 public class ExcludePluginTest {
  46 
  47     public static void main(String[] args) throws Exception {
  48         new ExcludePluginTest().test();
  49     }
  50 
  51     public void test() throws Exception {
  52         check("**.jcov", "/num/toto.jcov", true);
  53         check("**.jcov", "/toto.jcov/", true);
  54         check("**.jcov", "/toto.jcov/tutu/tata", false);
  55         check("/java.base/*.jcov", "/java.base/toto.jcov", true);
  56         check("/java.base/toto.jcov", "/tjava.base/iti.jcov", false);
  57         check("/java.base/*/toto.jcov", "/java.base/toto.jcov", false);
  58         check("/java.base/*/toto.jcov", "/java.base/tutu/toto.jcov", true);
  59         check("**/java.base/*/toto.jcov", "/tutu/java.base/tutu/toto.jcov", true);
  60         check("/META-INF/**", "/META-INF/services/  MyProvider ", true);
  61         check("/META-INF/**", "/META-INF/services/MyProvider", true);
  62         check("**/META-INF", "/ META-INF/services/MyProvider", false);
  63         check("**/META-INF/**", "/java.base//META-INF/services/MyProvider", true);
  64         check("/java.base/*/Toto$Titi.class", "/java.base/tutu/Toto$Titi.class", true);
  65         check("/**$**.class", "/java.base/tutu/Toto$Titi.class", true);
  66         check("**$**.class", "/java.base/tutu/Toto$Titi.class", true);
  67 
  68         // Excluded resource list in a file
  69         File order = new File("resources.exc");
  70         order.createNewFile();
  71         Files.write(order.toPath(), "**.jcov".getBytes());
  72         check("@" + order.getAbsolutePath(), "/num/toto.jcov", true);
  73     }
  74 
  75     public void check(String s, String sample, boolean exclude) throws Exception {
  76         Map<String, String> prop = new HashMap<>();
  77         prop.put(ExcludePlugin.NAME, s);
  78         ExcludePlugin excludePlugin = new ExcludePlugin();
  79         excludePlugin.configure(prop);
  80         ResourcePoolManager resourcesMgr = new ResourcePoolManager();
  81         ResourcePoolEntry resource = ResourcePoolEntry.create(sample, new byte[0]);
  82         resourcesMgr.add(resource);
  83         ResourcePoolManager resultMgr = new ResourcePoolManager();
  84         ResourcePool resPool = excludePlugin.transform(resourcesMgr.resourcePool(),
  85                 resultMgr.resourcePoolBuilder());
  86         if (exclude) {
  87             if (resPool.contains(resource)) {
  88                 throw new AssertionError(sample + " should be excluded by " + s);
  89             }
  90         } else {
  91             if (!resPool.contains(resource)) {
  92                 throw new AssertionError(sample + " shouldn't be excluded by " + s);
  93             }
  94         }
  95     }
  96 }