jdk/test/tools/jlink/plugins/SorterPluginTest.java
changeset 37916 627154540b60
parent 37915 be4ff50b6cb6
parent 37914 a2f43d835fa1
child 37925 3560b89fb532
equal deleted inserted replaced
37915:be4ff50b6cb6 37916:627154540b60
     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 sorter 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 SorterPluginTest
       
    31  */
       
    32 
       
    33 import java.io.File;
       
    34 import java.nio.file.Files;
       
    35 import java.util.Arrays;
       
    36 import java.util.Collection;
       
    37 import java.util.HashMap;
       
    38 import java.util.Map;
       
    39 import jdk.tools.jlink.internal.PoolImpl;
       
    40 
       
    41 import jdk.tools.jlink.internal.plugins.SortResourcesPlugin;
       
    42 import jdk.tools.jlink.plugin.Pool;
       
    43 import jdk.tools.jlink.plugin.Pool.ModuleData;
       
    44 import jdk.tools.jlink.plugin.TransformerPlugin;
       
    45 
       
    46 public class SorterPluginTest {
       
    47 
       
    48     public static void main(String[] args) throws Exception {
       
    49         new SorterPluginTest().test();
       
    50     }
       
    51 
       
    52     public void test() throws Exception {
       
    53         ModuleData[] array = {
       
    54                 Pool.newResource("/module1/toto1", new byte[0]),
       
    55                 Pool.newResource("/module2/toto1", new byte[0]),
       
    56                 Pool.newResource("/module3/toto1", new byte[0]),
       
    57                 Pool.newResource("/module3/toto1/module-info.class", new byte[0]),
       
    58                 Pool.newResource("/zazou/toto1", new byte[0]),
       
    59                 Pool.newResource("/module4/zazou", new byte[0]),
       
    60                 Pool.newResource("/module5/toto1", new byte[0]),
       
    61                 Pool.newResource("/module6/toto1/module-info.class", new byte[0])
       
    62         };
       
    63 
       
    64         ModuleData[] sorted = {
       
    65                 Pool.newResource("/zazou/toto1", new byte[0]),
       
    66                 Pool.newResource("/module3/toto1/module-info.class", new byte[0]),
       
    67                 Pool.newResource("/module6/toto1/module-info.class", new byte[0]),
       
    68                 Pool.newResource("/module1/toto1", new byte[0]),
       
    69                 Pool.newResource("/module2/toto1", new byte[0]),
       
    70                 Pool.newResource("/module3/toto1", new byte[0]),
       
    71                 Pool.newResource("/module4/zazou", new byte[0]),
       
    72                 Pool.newResource("/module5/toto1", new byte[0]),
       
    73 };
       
    74 
       
    75         ModuleData[] sorted2 = {
       
    76             Pool.newResource("/module5/toto1", new byte[0]),
       
    77             Pool.newResource("/module6/toto1/module-info.class", new byte[0]),
       
    78             Pool.newResource("/module4/zazou", new byte[0]),
       
    79             Pool.newResource("/module3/toto1", new byte[0]),
       
    80             Pool.newResource("/module3/toto1/module-info.class", new byte[0]),
       
    81             Pool.newResource("/module1/toto1", new byte[0]),
       
    82             Pool.newResource("/module2/toto1", new byte[0]),
       
    83             Pool.newResource("/zazou/toto1", new byte[0]),};
       
    84 
       
    85         Pool resources = new PoolImpl();
       
    86         for (ModuleData r : array) {
       
    87             resources.add(r);
       
    88         }
       
    89 
       
    90         {
       
    91             Pool out = new PoolImpl();
       
    92             Map<String, String> config = new HashMap<>();
       
    93             config.put(SortResourcesPlugin.NAME, "/zazou/*,*/module-info.class");
       
    94             TransformerPlugin p = new SortResourcesPlugin();
       
    95             p.configure(config);
       
    96             p.visit(resources, out);
       
    97             check(out.getContent(), sorted);
       
    98         }
       
    99 
       
   100         {
       
   101             // Order of resources in the file, then un-ordered resources.
       
   102             File order = new File("resources.order");
       
   103             order.createNewFile();
       
   104             StringBuilder builder = new StringBuilder();
       
   105             // 5 first resources come from file
       
   106             for (int i = 0; i < 5; i++) {
       
   107                 builder.append(sorted2[i].getPath()).append("\n");
       
   108             }
       
   109             Files.write(order.toPath(), builder.toString().getBytes());
       
   110 
       
   111             Pool out = new PoolImpl();
       
   112             Map<String, String> config = new HashMap<>();
       
   113             config.put(SortResourcesPlugin.NAME, order.getAbsolutePath());
       
   114             TransformerPlugin p = new SortResourcesPlugin();
       
   115             p.configure(config);
       
   116             p.visit(resources, out);
       
   117             check(out.getContent(), sorted2);
       
   118 
       
   119         }
       
   120     }
       
   121 
       
   122     private void check(Collection<ModuleData> outResources,
       
   123             ModuleData[] sorted) {
       
   124         if (outResources.size() != sorted.length) {
       
   125             throw new AssertionError("Wrong number of resources:\n"
       
   126                     + "expected: " + Arrays.toString(sorted) + ",\n"
       
   127                     + "     got: " + outResources);
       
   128         }
       
   129         int i = 0;
       
   130         for (ModuleData r : outResources) {
       
   131             System.err.println("Resource: " + r);
       
   132             if (!sorted[i].getPath().equals(r.getPath())) {
       
   133                 throw new AssertionError("Resource not properly sorted, difference at: " + i + "\n"
       
   134                         + "expected: " + Arrays.toString(sorted) + ",\n"
       
   135                         + "     got: " + outResources);
       
   136             }
       
   137             i++;
       
   138         }
       
   139     }
       
   140 }