1 /*
   2  * Copyright (c) 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager.windows;
  27 
  28 import static com.oracle.tools.packager.StandardBundlerParam.APP_NAME;
  29 import static com.oracle.tools.packager.StandardBundlerParam.BUILD_ROOT;
  30 import static com.oracle.tools.packager.StandardBundlerParam.VERBOSE;
  31 
  32 import org.junit.After;
  33 import org.junit.Assume;
  34 import org.junit.Before;
  35 import org.junit.BeforeClass;
  36 import org.junit.Test;
  37 import static org.junit.Assert.*;
  38 
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.nio.file.Files;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 
  45 import com.oracle.tools.packager.Bundler;
  46 import com.oracle.tools.packager.Log;
  47 import com.oracle.tools.packager.ConfigException;
  48 import com.oracle.tools.packager.UnsupportedPlatformException;
  49 
  50 public class WinServiceBundlerTest {
  51     
  52     static File tmpBase;
  53     static File workDir;
  54     static boolean retain = false;
  55     
  56     @BeforeClass
  57     public static void prepareApp() {
  58         // only run on windows
  59         Assume.assumeTrue(System.getProperty("os.name").toLowerCase().startsWith("win"));
  60 
  61         Log.setLogger(new Log.Logger(true));
  62 
  63         retain = Boolean.parseBoolean(System.getProperty("RETAIN_PACKAGER_TESTS"));
  64         workDir = new File("build/tmp/tests", "winservice");
  65     }
  66 
  67     @Before
  68     public void createTmpDir() throws IOException {
  69         if (retain) {
  70             tmpBase = new File("build/tmp/tests/winservice");
  71         } else {
  72             tmpBase = Files.createTempDirectory("fxpackagertests").toFile();
  73         }
  74         tmpBase.mkdir();
  75     }
  76 
  77     @After
  78     public void maybeCleanupTmpDir() {
  79         if (!retain) {
  80             attemptDelete(tmpBase);
  81         }
  82     }
  83     
  84     private void attemptDelete(File tmpBase) {
  85         if (tmpBase.isDirectory()) {
  86             File[] children = tmpBase.listFiles();
  87             if (children != null) {
  88                 for (File f : children) {
  89                     attemptDelete(f);
  90                 }
  91             }
  92         }
  93         boolean success;
  94         try {
  95             success = !tmpBase.exists() || tmpBase.delete();
  96         } catch (SecurityException se) {
  97             success = false;
  98         }
  99         if (!success) {
 100             System.err.println("Could not clean up " + tmpBase.toString());
 101         }
 102     }
 103     
 104     /**
 105      * See if smoke comes out
 106      */
 107     @Test
 108     public void smokeTest() throws IOException, ConfigException, UnsupportedPlatformException {
 109         Bundler bundler = new WinServiceBundler();
 110 
 111         assertNotNull(bundler.getName());
 112         assertNotNull(bundler.getID());
 113         assertNotNull(bundler.getDescription());
 114         //assertNotNull(bundler.getBundleParameters());
 115 
 116         Map<String, Object> bundleParams = new HashMap<>();
 117 
 118         bundleParams.put(BUILD_ROOT.getID(), tmpBase);
 119         bundleParams.put(APP_NAME.getID(), "Smoke");
 120         bundleParams.put(VERBOSE.getID(), true);
 121 
 122         boolean valid = bundler.validate(bundleParams);
 123         assertTrue(valid);
 124 
 125         File output = bundler.execute(bundleParams, new File(workDir, "smoke"));
 126         assertNotNull(output);
 127         
 128         // make sure that the service launcher is there
 129         File launcher = WinServiceBundler.getLauncherSvc(output, bundleParams);
 130         assertTrue(launcher.exists());
 131     }
 132 
 133     
 134 }