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 import jdk.jpackage.internal.Arguments; 25 import jdk.jpackage.internal.DeployParams; 26 import jdk.jpackage.internal.PackagerException; 27 import java.io.File; 28 29 /* 30 * @test 31 * @bug 8211285 32 * @summary DeployParamsTest 33 * @modules jdk.jpackage 34 * @modules jdk.jpackage/jdk.jpackage.internal 35 * @run main/othervm -Xmx512m DeployParamsTest 36 */ 37 public class DeployParamsTest { 38 39 private static File testRoot = null; 40 41 private static void setUp() { 42 testRoot = new File("deployParamsTest"); 43 System.out.println("DeployParamsTest: " + testRoot.getAbsolutePath()); 44 testRoot.mkdir(); 45 } 46 47 private static void tearDown() { 48 if (testRoot != null) { 49 testRoot.delete(); 50 } 51 } 52 53 private static void testValidateAppName1() throws Exception { 54 DeployParams params = getParamsAppName(); 55 56 setAppName(params, "Test"); 57 params.validate(); 58 59 setAppName(params, "Test Name"); 60 params.validate(); 61 62 setAppName(params, "Test - Name !!!"); 63 params.validate(); 64 } 65 66 private static void testValidateAppName2() throws Exception { 67 DeployParams params = getParamsAppName(); 68 69 setAppName(params, "Test\nName"); 70 appName2TestHelper(params); 71 72 setAppName(params, "Test\rName"); 73 appName2TestHelper(params); 74 75 setAppName(params, "TestName\\"); 76 appName2TestHelper(params); 77 78 setAppName(params, "Test \" Name"); 79 appName2TestHelper(params); 80 } 81 82 private static void appName2TestHelper(DeployParams params) throws Exception { 83 try { 84 params.validate(); 85 } catch (PackagerException pe) { 86 if (!pe.getMessage().startsWith("Error: Invalid Application name")) { 87 throw new Exception("Unexpected PackagerException received: " + pe); 88 } 89 90 return; // Done 91 } 92 93 throw new Exception("Expecting PackagerException"); 94 } 95 96 // Returns deploy params initialized to pass all validation, except for 97 // app name 98 private static DeployParams getParamsAppName() { 99 DeployParams params = new DeployParams(); 100 101 params.setOutput(testRoot); 102 params.addResource(testRoot, new File(testRoot, "test.jar")); 103 params.addBundleArgument(Arguments.CLIOptions.APPCLASS.getId(), "TestClass"); 104 params.addBundleArgument(Arguments.CLIOptions.MAIN_JAR.getId(), "test.jar"); 105 params.addBundleArgument(Arguments.CLIOptions.INPUT.getId(), "input"); 106 107 return params; 108 } 109 110 private static void setAppName(DeployParams params, String appName) { 111 params.addBundleArgument(Arguments.CLIOptions.NAME.getId(), appName); 112 } 113 114 public static void main(String[] args) throws Exception { 115 setUp(); 116 117 try { 118 testValidateAppName1(); 119 testValidateAppName2(); 120 } finally { 121 tearDown(); 122 } 123 } 124 125 } | 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.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 } |