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