1 /*
   2  * Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  */
   5 package com.oracle.appbundlers.tests.functionality.jdk9test;
   6 
   7 import static com.oracle.appbundlers.utils.installers.AbstractBundlerUtils.CHECK_MODULE_IN_JAVA_EXECUTABLE;
   8 import static com.oracle.appbundlers.utils.installers.AbstractBundlerUtils.OUTPUT_CONTAINS;
   9 import static java.util.stream.Collectors.toSet;
  10 
  11 import java.io.File;
  12 import java.io.IOException;
  13 import java.nio.file.Path;
  14 import java.util.HashMap;
  15 import java.util.Map;
  16 import java.util.concurrent.ExecutionException;
  17 import java.util.stream.Collectors;
  18 
  19 import com.oracle.appbundlers.tests.functionality.TestBase;
  20 import com.oracle.appbundlers.tests.functionality.functionalinterface.AdditionalParams;
  21 import com.oracle.appbundlers.tests.functionality.functionalinterface.BasicParams;
  22 import com.oracle.appbundlers.tests.functionality.functionalinterface.VerifiedOptions;
  23 import com.oracle.appbundlers.tests.functionality.parameters.GenericModuleParameters;
  24 import com.oracle.appbundlers.utils.AppWrapper;
  25 import com.oracle.appbundlers.utils.ExtensionType;
  26 import com.oracle.appbundlers.utils.SourceFactory;
  27 import com.oracle.appbundlers.utils.Utils;
  28 import com.oracle.tools.packager.RelativeFileSet;
  29 import com.sun.javafx.tools.packager.bundlers.BundleParams;
  30 
  31 /**
  32  * @author Ramesh BG Example 3 in chris list Example 3: Unnamed Module + Entire
  33  *         JRE + 3rd party modules -srcfiles hello.world.jar -appClass
  34  *         HelloWorld -BmainJar=hello.world.jar -addmods 3rd.party -modulepath
  35  *         <path to 3rd party JARs>
  36  */
  37 public class UnnamedModuleDependsOn3rdPartyModulesBundledWithEntireJreTest
  38         extends TestBase {
  39 
  40     protected AppWrapper getApp() throws IOException {
  41         return new AppWrapper(Utils.getTempSubDir(WORK_DIRECTORY),
  42                 COM_GREETINGS_APP1_QUALIFIED_CLASS_NAME,
  43                 "-XaddExports:custom.util/testapp.util=ALL-UNNAMED",
  44                 SourceFactory.get_custom_util_module(),
  45                 SourceFactory.get_com_greetings_app_unnamed_module(
  46                         new HashMap<String, String>() {
  47                             private static final long serialVersionUID = 2076100253408663958L;
  48 
  49                             {
  50                                 put(PRINTLN_STATEMENT,
  51                                         CUSTOM_UTIL_PRINTLN_STATEMENT);
  52                             }
  53                         }));
  54     }
  55 
  56     public VerifiedOptions getVerifiedOptions() {
  57         return () -> {
  58             Map<String, Object> hashMap = new HashMap<String, Object>();
  59             hashMap.put(OUTPUT_CONTAINS, HELLO_WORLD_OUTPUT);
  60             hashMap.put(CHECK_MODULE_IN_JAVA_EXECUTABLE,
  61                     CUSTOM_UTIL_MODULE_NAME);
  62             return hashMap;
  63         };
  64     }
  65 
  66     public AdditionalParams getAdditionalParams() {
  67         return () -> {
  68             Map<String, Object> hashMap = new HashMap<String, Object>();
  69             hashMap.put(ADD_MODS,
  70                     this.currentParameter.getApp().getAllModuleNamesSeparatedByPathSeparator());
  71             return hashMap;
  72         };
  73     }
  74 
  75     @Override
  76     protected void prepareTestEnvironment() throws Exception {
  77         for (ExtensionType intermediate : ExtensionType.getModuleTypes()) {
  78             this.currentParameter = this.intermediateToParametersMap
  79                     .get(intermediate);
  80             overrideParameters(intermediate);
  81             initializeAndPrepareApp();
  82         }
  83     }
  84 
  85     @Override
  86     public void overrideParameters(ExtensionType intermediate)
  87             throws IOException {
  88         this.currentParameter.setApp(getApp());
  89         this.currentParameter.setBasicParams(getBasicParams());
  90         this.currentParameter.setAdditionalParams(getAdditionalParams());
  91         this.currentParameter.setVerifiedOptions(getVerifiedOptions());
  92     }
  93 
  94     @Override
  95     protected void prepareApp(AppWrapper app, ExtensionType extension)
  96             throws IOException, ExecutionException {
  97         app.preinstallApp(
  98                 new ExtensionType[] { extension, ExtensionType.NormalJar });
  99         app.writeSourcesToAppDirectory();
 100         app.compileAndCreateJavaExtensionProduct(extension);
 101     }
 102 
 103     @Override
 104     protected void initializeAndPrepareApp() throws Exception {
 105         prepareApp(this.currentParameter.getApp(),
 106                 this.currentParameter.getExtension());
 107     }
 108 
 109     public BasicParams getBasicParams() throws IOException {
 110         /*
 111          * there is no main module in this test since unnamed module depending
 112          * on named module via -XaddExports
 113          */
 114         return (AppWrapper app) -> {
 115             Map<String, Object> basicParams = new HashMap<String, Object>();
 116             basicParams.put(BundleParams.PARAM_APP_RESOURCES,
 117                     new RelativeFileSet(
 118                             this.currentParameter.getApp().getJarDir().toFile(),
 119                             app.getJarFilesList().stream().map(Path::toFile)
 120                                     .collect(toSet())));
 121             basicParams.put(APPLICATION_CLASS,
 122                     this.currentParameter.getApp().getMainClass());
 123             basicParams.put(CLASSPATH,
 124                     this.currentParameter.getApp().getJarFilesList().stream()
 125                             .map(Path::getFileName).map(Path::toString)
 126                             .collect(Collectors.joining(File.pathSeparator)));
 127             basicParams.put(MODULEPATH, String.join(File.pathSeparator,
 128                     JMODS_PATH_IN_JDK, ((GenericModuleParameters) this.currentParameter).getModulePath()));
 129             return basicParams;
 130         };
 131     }
 132 
 133     @Override
 134     public ExtensionType[] getExtensionArray() {
 135         return ExtensionType.getModuleTypes();
 136     }
 137 }