1 /*
   2  * Copyright (c) 2020, 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 package jdk.jpackage.tests;
  25 
  26 import java.util.Collection;
  27 import java.util.List;
  28 import jdk.jpackage.test.Annotations.Parameters;
  29 import jdk.jpackage.test.Annotations.Test;
  30 import jdk.jpackage.test.JPackageCommand;
  31 import jdk.jpackage.test.CfgFile;
  32 import jdk.jpackage.test.TKit;
  33 
  34 /*
  35  * @test
  36  * @summary jpackage application version testing
  37  * @library ../../../../helpers
  38  * @build jdk.jpackage.test.*
  39  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  40  * @compile JLinkOptionsTest.java
  41  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  42  *  --jpt-run=jdk.jpackage.tests.JLinkOptionsTest
  43  */
  44 
  45 public final class JLinkOptionsTest {
  46 
  47     @Parameters
  48     public static Collection input() {
  49         return List.of(new Object[][]{
  50             // default but with strip-native-commands removed
  51             {"Hello", new String[]{
  52                     "--jlink-options",
  53                     "--strip-debug --no-man-pages --no-header-files",
  54                     },
  55                     // non modular should have everything
  56                     new String[]{"jdk.jartool", "jdk.unsupported"},
  57                     null,
  58                     },
  59             // multiple jlink-options
  60             {"com.other/com.other.Hello", new String[]{
  61                     "--jlink-options",
  62                     "--strip-debug --no-man-pages --no-header-files",
  63                     "--jlink-options",
  64                     "--bind-services",
  65                     },
  66                     // with bind-services should have some services
  67                     new String[]{"java.smartcardio", "jdk.crypto.ec"},
  68                     null,
  69                     },
  70             // bind-services
  71             {"Hello", new String[]{
  72                     "--jlink-options", "--bind-services",
  73                     },
  74                     // non modular should have everything
  75                     new String[]{"jdk.jartool", "jdk.unsupported"},
  76                     null,
  77                     },
  78 
  79             // bind-services and jpackage option --bind-services (deprecated)
  80             {"com.other/com.other.Hello", new String[]{
  81                     "--bind-services",
  82                     "--jlink-options", "--bind-services",
  83                     },
  84                     // with bind-services should have some services
  85                     new String[]{"java.smartcardio", "jdk.crypto.ec"},
  86                     null,
  87                     },
  88 
  89             // limit modules
  90             {"com.other/com.other.Hello", new String[]{
  91                     "--jlink-options",
  92                     "--limit-modules java.base,java.datatransfer,java.xml,java.prefs,java.desktop,com.other",
  93                     },
  94                     // should have whatever it needs
  95                     new String[]{"java.base", "com.other"},
  96                     // should not have whatever it doesn't need
  97                     new String[]{"jdk.incubator.jpackage"},
  98                     },
  99 
 100             // bind-services and limit-options
 101             {"com.other/com.other.Hello", new String[]{
 102                     "--bind-services",
 103                     "--jlink-options",
 104                     "--limit-modules java.base,java.datatransfer,java.xml,java.prefs,java.desktop,com.other,java.smartcardio",
 105                     },
 106                     // with bind-services should have some services
 107                     new String[]{"java.smartcardio"},
 108                     // but not limited
 109                     new String[]{"jdk.crypto.ec"},
 110                     },
 111 
 112         });
 113     }
 114 
 115     public JLinkOptionsTest(String javaAppDesc, String[] jpackageArgs, String[] required, String[] prohibited) {
 116         this.required = required;
 117         this.prohibited = prohibited;
 118         cmd = JPackageCommand.helloAppImage(javaAppDesc);
 119         if (jpackageArgs != null) {
 120             cmd.addArguments(jpackageArgs);
 121         }
 122     }
 123 
 124     @Test
 125     public void test() {
 126         cmd.executeAndAssertHelloAppImageCreated();
 127 
 128         List<String> release = cmd.readRuntimeReleaseFile();
 129         List<String> mods = List.of(release.get(1));
 130         if (required != null) {
 131             for (String s : required) {
 132                 TKit.assertTextStream(s).label("mods").apply(mods.stream());
 133             }
 134         }
 135         if (prohibited != null) {
 136             for (String s : prohibited) {
 137                 TKit.assertTextStream(s).label("mods").negate().apply(mods.stream());
 138             }
 139         }
 140     }
 141 
 142     private final String[] required;
 143     private final String[] prohibited;
 144     private final JPackageCommand cmd;
 145 }