1 /*
   2  * Copyright (c) 2018, 2019, 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 package jdk.incubator.jpackage.internal;
  24 
  25 import java.io.File;
  26 import java.io.IOException;
  27 import org.hamcrest.BaseMatcher;
  28 import org.hamcrest.Description;
  29 import org.junit.Rule;
  30 import org.junit.Before;
  31 import org.junit.Test;
  32 import org.junit.rules.ExpectedException;
  33 import org.junit.rules.TemporaryFolder;
  34 
  35 /**
  36  * Test for JDK-8211285
  37  */
  38 public class DeployParamsTest {
  39 
  40     @Rule
  41     public final TemporaryFolder tempFolder = new TemporaryFolder();
  42 
  43     @Rule
  44     public final ExpectedException thrown = ExpectedException.none();
  45 
  46     @Before
  47     public void setUp() throws IOException {
  48         testRoot = tempFolder.newFolder();
  49     }
  50 
  51     @Test
  52     public void testValidAppName() throws PackagerException {
  53         initParamsAppName();
  54 
  55         setAppNameAndValidate("Test");
  56 
  57         setAppNameAndValidate("Test Name");
  58 
  59         setAppNameAndValidate("Test - Name !!!");
  60     }
  61 
  62     @Test
  63     public void testInvalidAppName() throws PackagerException {
  64         initForInvalidAppNamePackagerException();
  65         initParamsAppName();
  66         setAppNameAndValidate("Test\nName");
  67     }
  68 
  69     @Test
  70     public void testInvalidAppName2() throws PackagerException {
  71         initForInvalidAppNamePackagerException();
  72         initParamsAppName();
  73         setAppNameAndValidate("Test\rName");
  74     }
  75 
  76     @Test
  77     public void testInvalidAppName3() throws PackagerException {
  78         initForInvalidAppNamePackagerException();
  79         initParamsAppName();
  80         setAppNameAndValidate("TestName\\");
  81     }
  82 
  83     @Test
  84     public void testInvalidAppName4() throws PackagerException {
  85         initForInvalidAppNamePackagerException();
  86         initParamsAppName();
  87         setAppNameAndValidate("Test \" Name");
  88     }
  89 
  90     private void initForInvalidAppNamePackagerException() {
  91         thrown.expect(PackagerException.class);
  92 
  93         String msg = "Error: Invalid Application name";
  94 
  95         // Unfortunately org.hamcrest.core.StringStartsWith is not available
  96         // with older junit, DIY
  97 
  98         // thrown.expectMessage(startsWith("Error: Invalid Application name"));
  99         thrown.expectMessage(new BaseMatcher() {
 100             @Override
 101             @SuppressWarnings("unchecked")
 102             public boolean matches(Object o) {
 103                 if (o instanceof String) {
 104                     return ((String) o).startsWith(msg);
 105                 }
 106                 return false;
 107             }
 108 
 109             @Override
 110             public void describeTo(Description d) {
 111                 d.appendText(msg);
 112             }
 113         });
 114     }
 115 
 116     // Returns deploy params initialized to pass all validation, except for
 117     // app name
 118     private void initParamsAppName() {
 119         params = new DeployParams();
 120 
 121         params.setOutput(testRoot);
 122         params.addResource(testRoot, new File(testRoot, "test.jar"));
 123         params.addBundleArgument(Arguments.CLIOptions.APPCLASS.getId(),
 124                 "TestClass");
 125         params.addBundleArgument(Arguments.CLIOptions.MAIN_JAR.getId(),
 126                 "test.jar");
 127         params.addBundleArgument(Arguments.CLIOptions.INPUT.getId(), "input");
 128     }
 129 
 130     private void setAppNameAndValidate(String appName) throws PackagerException {
 131         params.addBundleArgument(Arguments.CLIOptions.NAME.getId(), appName);
 132         params.validate();
 133     }
 134 
 135     private File testRoot = null;
 136     private DeployParams params;
 137 }