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 order of plugins
  27  * @author Jean-Francois Denise
  28  * @library ../../lib
  29  * @modules java.base/jdk.internal.jimage
  30  *          jdk.jdeps/com.sun.tools.classfile
  31  *          jdk.jlink/jdk.tools.jlink.internal
  32  *          jdk.jlink/jdk.tools.jmod
  33  *          jdk.jlink/jdk.tools.jimage
  34  *          jdk.compiler
  35  * @build tests.*
  36  * @run main/othervm PluginOrderTest
  37  */
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.HashSet;
  41 import java.util.List;
  42 import java.util.Map;
  43 import java.util.Set;
  44 
  45 import jdk.tools.jlink.internal.PluginOrderingGraph;
  46 import jdk.tools.jlink.plugin.Plugin;
  47 import jdk.tools.jlink.plugin.Plugin.Category;
  48 import jdk.tools.jlink.plugin.ModulePool;
  49 
  50 public class PluginOrderTest {
  51 
  52     public static void main(String[] args) throws Exception {
  53 
  54         validGraph0();
  55         validGraph1();
  56 
  57         boolean failed = false;
  58 
  59         try {
  60             withCycles0();
  61             failed = true;
  62         } catch (Exception ex) {
  63             //ok
  64             System.err.println(ex.getMessage());
  65         }
  66         if (failed) {
  67             throw new Exception("Should have failed");
  68         }
  69 
  70         try {
  71             withCycles1();
  72             failed = true;
  73         } catch (Exception ex) {
  74             //ok
  75             System.err.println(ex.getMessage());
  76         }
  77         if (failed) {
  78             throw new Exception("Should have failed");
  79         }
  80 
  81         try {
  82             withCycles2();
  83             failed = true;
  84         } catch (Exception ex) {
  85             //ok
  86             System.err.println(ex.getMessage());
  87         }
  88         if (failed) {
  89             throw new Exception("Should have failed");
  90         }
  91     }
  92 
  93     private static void validGraph0() throws Exception {
  94         Set<String> set = new HashSet<>();
  95         set.add("plug2");
  96         List<Plugin> plugins = new ArrayList<>();
  97         plugins.add(new Plug("plug2", Collections.emptySet(), Collections.emptySet(),
  98                 Category.TRANSFORMER));
  99         plugins.add(new Plug("plug1", set, Collections.emptySet(), Category.TRANSFORMER));
 100         List<Plugin> ordered = PluginOrderingGraph.sort(plugins);
 101         if (ordered.get(0) != plugins.get(1) || ordered.get(1) != plugins.get(0)) {
 102             throw new Exception("Invalid sorting");
 103         }
 104     }
 105 
 106     private static void validGraph1() {
 107         Set<String> lst1 = new HashSet<>();
 108         lst1.add("plug2");
 109         lst1.add("plug3");
 110         Plugin p1 = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
 111 
 112         Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 113 
 114         Set<String> lst3 = new HashSet<>();
 115         lst3.add("plug4");
 116         lst3.add("plug6");
 117         Plugin p3 = new Plug("plug3", lst3, Collections.emptySet(), Category.TRANSFORMER);
 118 
 119         Plugin p4 = new Plug("plug4", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 120 
 121         Set<String> lst5 = new HashSet<>();
 122         lst5.add("plug3");
 123         lst5.add("plug1");
 124         lst5.add("plug2");
 125         lst5.add("plug6");
 126         Plugin p5 = new Plug("plug5", lst5, Collections.emptySet(), Category.TRANSFORMER);
 127 
 128         Set<String> lst6 = new HashSet<>();
 129         lst6.add("plug4");
 130         lst6.add("plug2");
 131         Plugin p6 = new Plug("plug6", lst6, Collections.emptySet(), Category.TRANSFORMER);
 132 
 133         Plugin p7 = new Plug("plug7", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 134 
 135         Plugin p8 = new Plug("plug8", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 136 
 137         List<Plugin> plugins = new ArrayList<>();
 138         plugins.add(p1);
 139         plugins.add(p2);
 140         plugins.add(p3);
 141         plugins.add(p4);
 142         plugins.add(p5);
 143         plugins.add(p6);
 144         plugins.add(p7);
 145         plugins.add(p8);
 146 
 147         PluginOrderingGraph.sort(plugins);
 148     }
 149 
 150     private static void withCycles0() throws Exception {
 151         Set<String> set2 = new HashSet<>();
 152         set2.add("plug1");
 153         List<Plugin> plugins = new ArrayList<>();
 154         plugins.add(new Plug("plug2", set2, Collections.emptySet(),
 155                 Category.TRANSFORMER));
 156 
 157         Set<String> set1 = new HashSet<>();
 158         set1.add("plug2");
 159         plugins.add(new Plug("plug1", set1, Collections.emptySet(), Category.TRANSFORMER));
 160         PluginOrderingGraph.sort(plugins);
 161 
 162     }
 163 
 164     private static void withCycles2() {
 165         Set<String> lst1 = new HashSet<>();
 166         lst1.add("plug2");
 167         lst1.add("plug3");
 168         Plugin p1 = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
 169 
 170         Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 171 
 172         Set<String> lst3 = new HashSet<>();
 173         lst3.add("plug4");
 174         lst3.add("plug6");
 175         Plugin p3 = new Plug("plug3", lst3, Collections.emptySet(), Category.TRANSFORMER);
 176 
 177         Plugin p4 = new Plug("plug4", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 178 
 179         Set<String> lst5 = new HashSet<>();
 180         lst5.add("plug3");
 181         lst5.add("plug1");
 182         lst5.add("plug2");
 183         Plugin p5 = new Plug("plug5", lst5, Collections.emptySet(), Category.TRANSFORMER);
 184 
 185         Set<String> lst6 = new HashSet<>();
 186         lst6.add("plug4");
 187         lst6.add("plug1");
 188         Plugin p6 = new Plug("plug6", lst6, Collections.emptySet(), Category.TRANSFORMER);
 189 
 190         Plugin p7 = new Plug("plug7", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 191 
 192         Plugin p8 = new Plug("plug8", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 193 
 194         List<Plugin> plugins = new ArrayList<>();
 195         plugins.add(p1);
 196         plugins.add(p2);
 197         plugins.add(p3);
 198         plugins.add(p4);
 199         plugins.add(p5);
 200         plugins.add(p6);
 201         plugins.add(p7);
 202         plugins.add(p8);
 203         PluginOrderingGraph.sort(plugins);
 204     }
 205 
 206     private static void withCycles1() {
 207         Set<String> lst1 = new HashSet<>();
 208         lst1.add("plug2");
 209         lst1.add("plug3");
 210         Plugin p = new Plug("plug1", lst1, Collections.emptySet(), Category.TRANSFORMER);
 211         Plugin p2 = new Plug("plug2", Collections.emptySet(), Collections.emptySet(), Category.TRANSFORMER);
 212 
 213         Set<String> lst3 = new HashSet<>();
 214         lst3.add("plug2");
 215 
 216         Set<String> lst4 = new HashSet<>();
 217         lst4.add("plug1");
 218 
 219         Plugin p3 = new Plug("plug3", lst4, lst3, Category.TRANSFORMER);
 220         List<Plugin> plugins = new ArrayList<>();
 221         plugins.add(p);
 222         plugins.add(p2);
 223         plugins.add(p3);
 224         PluginOrderingGraph.sort(plugins);
 225     }
 226 
 227     private static class Plug implements Plugin {
 228 
 229         private final Set<String> isBefore;
 230         private final Set<String> isAfter;
 231         private final Category category;
 232         private final String name;
 233 
 234         private Plug(String name, Set<String> isBefore, Set<String> isAfter, Category category) {
 235             this.name = name;
 236             this.isBefore = isBefore;
 237             this.isAfter = isAfter;
 238             this.category = category;
 239         }
 240 
 241         @Override
 242         public Set<String> isAfter() {
 243             return isAfter;
 244         }
 245 
 246         @Override
 247         public Set<String> isBefore() {
 248             return isBefore;
 249         }
 250 
 251         @Override
 252         public String toString() {
 253             return name;
 254         }
 255 
 256         @Override
 257         public void visit(ModulePool in, ModulePool out) {
 258 
 259         }
 260 
 261         @Override
 262         public Category getType() {
 263             return category;
 264         }
 265 
 266         @Override
 267         public String getName() {
 268             return name;
 269         }
 270     }
 271 }