1 /*
   2  * Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  */
   5 
   6 /* @test
   7 
   8 @summary Testcases that show different possibilities of resource bundling.
   9 
  10 @library
  11 /appBundlerTests/lib/ant-javafx.jar
  12 /appBundlerTests/src
  13 /appBundlerTests/test
  14 
  15 @build
  16 BundlerTest
  17 com.oracle.appbundlers.utils.AppWrapper
  18 com.oracle.appbundlers.utils.Utils
  19 com.oracle.appbundlers.utils.Config
  20 com.oracle.appbundlers.utils.VerificationUtil
  21 com.oracle.appbundlers.utils.installers.Installer
  22 
  23 @run testng com.oracle.appbundlers.tests.functionality.TestBase
  24 */
  25 package com.oracle.appbundlers.tests.functionality;
  26 
  27 import static com.oracle.appbundlers.utils.Utils.isLinux;
  28 import static com.oracle.appbundlers.utils.Utils.isWindows;
  29 import static java.util.stream.Collectors.toList;
  30 
  31 import java.io.IOException;
  32 import java.lang.reflect.Method;
  33 import java.util.Arrays;
  34 import java.util.HashMap;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Map;
  38 import java.util.concurrent.ExecutionException;
  39 import java.util.concurrent.TimeUnit;
  40 import java.util.logging.Level;
  41 import java.util.logging.Logger;
  42 import java.util.stream.Collectors;
  43 import java.util.stream.Stream;
  44 
  45 import org.testng.Assert;
  46 import org.testng.annotations.AfterClass;
  47 import org.testng.annotations.BeforeClass;
  48 import org.testng.annotations.BeforeMethod;
  49 import org.testng.annotations.DataProvider;
  50 import org.testng.annotations.Test;
  51 
  52 import com.oracle.appbundlers.tests.BundlerProvider;
  53 import com.oracle.appbundlers.tests.functionality.parameters.ExplodedModuleParameters;
  54 import com.oracle.appbundlers.tests.functionality.parameters.JmodParameters;
  55 import com.oracle.appbundlers.tests.functionality.parameters.ModularJarParameters;
  56 import com.oracle.appbundlers.tests.functionality.parameters.NormalJarParameters;
  57 import com.oracle.appbundlers.tests.functionality.parameters.Parameters;
  58 import com.oracle.appbundlers.utils.AppWrapper;
  59 import com.oracle.appbundlers.utils.BundlerUtils;
  60 import com.oracle.appbundlers.utils.BundlingManager;
  61 import com.oracle.appbundlers.utils.BundlingManagers;
  62 import com.oracle.appbundlers.utils.Constants;
  63 import com.oracle.appbundlers.utils.ExtensionType;
  64 import com.oracle.appbundlers.utils.PackageTypeFilter;
  65 import com.oracle.appbundlers.utils.PackagerApiFilter;
  66 import com.oracle.appbundlers.utils.Utils;
  67 import com.oracle.appbundlers.utils.installers.AbstractBundlerUtils;
  68 import com.oracle.tools.packager.Bundler;
  69 import com.oracle.tools.packager.ConfigException;
  70 import com.oracle.tools.packager.Log;
  71 
  72 import javafx.util.Pair;
  73 
  74 /**
  75  * @author Dmitry Ginzburg <dmitry.x.ginzburg@oracle.com>
  76  * @author Dmitry Zinkevich <dmitry.zinkevich@oracle.com>
  77  */
  78 public abstract class TestBase implements Constants {
  79 
  80     protected BundlingManager bundlingManager;
  81     protected Parameters currentParameter;
  82     protected Map<ExtensionType, Parameters> intermediateToParametersMap = new HashMap<ExtensionType, Parameters>();
  83 
  84     protected static final Logger LOG = Logger
  85             .getLogger(TestBase.class.getName());
  86 
  87     protected Method testMethod = null;
  88     
  89     protected void populateIntermediateToParametersMap() {
  90         for (ExtensionType extension : getExtensionArray()) {
  91             switch (extension) {
  92             case NormalJar:
  93                 intermediateToParametersMap.put(extension,
  94                         new NormalJarParameters());
  95                 break;
  96             case ModularJar:
  97                 intermediateToParametersMap.put(extension,
  98                         new ModularJarParameters());
  99                 break;
 100             case ExplodedModules:
 101                 intermediateToParametersMap.put(extension,
 102                         new ExplodedModuleParameters());
 103                 break;
 104             case Jmods:
 105                 intermediateToParametersMap.put(extension,
 106                         new JmodParameters());
 107                 break;
 108             }
 109         }
 110     }
 111 
 112     // method block: should be overridden in some tests
 113     // all these implementations are just "default-values"
 114     protected BundlerUtils[] getBundlerUtils() {
 115         return BundlerUtils.values();
 116     }
 117 
 118     protected BundlingManagers[] getBundlingManagers() {
 119         return BundlingManagers.values();
 120     }
 121 
 122     /**
 123      * return App Name
 124      */
 125     public String getResultingAppName() {
 126         return this.getClass().getSimpleName();
 127     }
 128 
 129     protected boolean isConfigExceptionExpected(Bundler bundler) {
 130         return false;
 131     }
 132 
 133     protected void prepareApp(final AppWrapper app, ExtensionType extension)
 134             throws IOException, ExecutionException {
 135         app.preinstallApp(extension);
 136         app.writeSourcesToAppDirectory();
 137         app.compileApp(extension);
 138         app.jarApp(extension);
 139     }
 140 
 141     @BeforeClass
 142     public void setupApplication() throws Exception {
 143         Log.setLogger(new Log.Logger(true));
 144         populateIntermediateToParametersMap();
 145         prepareTestEnvironment();
 146         customBeforeClassHook();
 147     }
 148 
 149     public void customBeforeClassHook() throws Exception {
 150     }
 151 
 152     protected void prepareTestEnvironment() throws Exception {
 153         for (ExtensionType extension : getExtensionArray()) {
 154             if(!isTestCaseApplicableForExtensionType(extension)) {
 155                 continue;
 156             }
 157             this.currentParameter = this.intermediateToParametersMap
 158                     .get(extension);
 159             overrideParameters(extension);
 160             initializeAndPrepareApp();
 161         }
 162     }
 163 
 164     protected void initializeAndPrepareApp() throws Exception {
 165         if (this.currentParameter.getApp() == null) {
 166             this.currentParameter.initializeDefaultApp();
 167         }
 168         prepareApp(this.currentParameter.getApp(),
 169                 this.currentParameter.getExtension());
 170     }
 171 
 172     @BeforeMethod
 173     public void saveTestMethod(Method method) throws IOException {
 174         testMethod = method;
 175     }
 176 
 177     @Test(dataProvider = "getBundlers")
 178     public void runTest(BundlingManager bundlingManager) throws Exception {
 179         this.currentParameter = intermediateToParametersMap
 180                 .get(bundlingManager.getExtensionType());
 181         if (!isTestCaseApplicableForExtensionType(
 182                 bundlingManager.getExtensionType())) {
 183             return;
 184         }
 185 
 186         Map<String, Object> allParams = getAllParams();
 187         String testName = this.getClass().getName() + "::"
 188                 + testMethod.getName() + "$" + bundlingManager.toString();
 189         this.bundlingManager = bundlingManager;
 190         LOG.log(Level.INFO, "Starting test \"{0}\".", testName);
 191         try {
 192             validate();
 193             if (isConfigExceptionExpected(bundlingManager.getBundler())) {
 194                 Assert.fail("ConfigException is expected, but isn't thrown");
 195             }
 196         } catch (ConfigException ex) {
 197             if (isConfigExceptionExpected(bundlingManager.getBundler())) {
 198                 return;
 199             } else {
 200                 LOG.log(Level.SEVERE, "Configuration error: {0}.",
 201                         new Object[] { ex });
 202                 throw ex;
 203             }
 204         }
 205 
 206         try {
 207             bundlingManager.execute(allParams, this.currentParameter.getApp());
 208             String path = bundlingManager.install(
 209                     this.currentParameter.getApp(), getResultingAppName(),
 210                     false);
 211             LOG.log(Level.INFO, "Installed at: {0}", path);
 212 
 213             Pair<TimeUnit, Integer> tuple = getDelayAfterInstall();
 214             tuple.getKey().sleep(tuple.getValue());
 215             AppWrapper app2 = this.currentParameter.getApp();
 216             this.currentParameter.getVerifiedOptions()
 217                     .forEach((name, value) -> bundlingManager.verifyOption(name,
 218                             value, app2, getResultingAppName()));
 219         } finally {
 220             uninstallApp();
 221             LOG.log(Level.INFO, "Finished test: {0}", testName);
 222         }
 223     }
 224 
 225     public boolean isTestCaseApplicableForExtensionType(
 226             ExtensionType extension) {
 227         return true;
 228     }
 229 
 230     public ExtensionType[] getExtensionArray() {
 231         return ExtensionType.values();
 232     }
 233 
 234     protected void uninstallApp() throws Exception {
 235         if (this.bundlingManager != null) {
 236             String appName = this.bundlingManager.getAppName(getAllParams());
 237             this.bundlingManager.uninstall(this.currentParameter.getApp(), appName);
 238         }
 239     }
 240 
 241     @AfterClass
 242     protected void cleanUp() throws IOException {
 243         try {
 244             LOG.log(Level.INFO, "Removing temporary files: ");
 245         } finally {
 246             for (Parameters parameters : intermediateToParametersMap.values()) {
 247                 Utils.tryRemoveRecursive(parameters.getApp().getWorkDir());
 248             }
 249         }
 250     }
 251 
 252     public void overrideParameters(ExtensionType intermediate)
 253             throws Exception {
 254 
 255     }
 256 
 257     @DataProvider(name = "getBundlers")
 258     public Iterator<Object[]> getBundlers() {
 259 
 260         List<BundlingManagers> packagerInterfaces = Stream
 261                 .of(getBundlingManagers()).filter(PackagerApiFilter::accept)
 262                 .collect(Collectors.toList());
 263 
 264         final List<AbstractBundlerUtils> installationPackageTypes = Stream
 265                 .of(getBundlerUtils()).filter(BundlerUtils::isSupported)
 266                 .filter(PackageTypeFilter::accept)
 267                 .map(BundlerUtils::getBundlerUtils).collect(toList());
 268 
 269         return BundlerProvider.createBundlingManagers(installationPackageTypes,
 270                 packagerInterfaces, Arrays.asList(getExtensionArray()));
 271     }
 272 
 273     public boolean mustBeSupported(String bundlerId) {
 274         if (isLinux()) {
 275             return bundlerId.equals("linux.app") || bundlerId.equals("deb")
 276                     || bundlerId.equals("rpm");
 277         } else if (isWindows()) {
 278             return bundlerId.equals("windows.app") || bundlerId.equals("exe")
 279                     || bundlerId.equals("msi");
 280         } else {
 281             return bundlerId.equals("mac.app")
 282                     || bundlerId.equals("mac.appStore")
 283                     || bundlerId.equals("dmg");
 284         }
 285     }
 286 
 287     protected Pair<TimeUnit, Integer> getDelayAfterInstall() {
 288         return new Pair<TimeUnit, Integer>(TimeUnit.MILLISECONDS, 100);
 289     }
 290 
 291     protected Map<String, Object> getAllParams() throws Exception {
 292         Map<String, Object> basicParams = this.currentParameter
 293                 .getBasicParams();
 294         Map<String, Object> allParams = new HashMap<String, Object>();
 295         allParams.put(APP_NAME, getResultingAppName());
 296         allParams.putAll(basicParams);
 297         allParams.putAll(this.currentParameter.getAdditionalParams());
 298         return allParams;
 299     }
 300 
 301     public void validate() throws Exception {
 302         this.bundlingManager.validate(this.currentParameter.getBasicParams());
 303     }
 304 
 305     public Parameters getParameters() {
 306         return this.currentParameter;
 307     }
 308 }