1 /* 2 * Copyright (c) 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. 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 package jdk.incubator.jpackage.internal; 26 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.StandardOpenOption; 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.LinkedHashMap; 35 import org.junit.Assert; 36 import org.junit.Test; 37 import org.junit.Rule; 38 import org.junit.rules.TemporaryFolder; 39 40 public class AppImageFileTest { 41 42 @Rule 43 public final TemporaryFolder tempFolder = new TemporaryFolder(); 44 45 @Test 46 public void testIdentity() throws IOException { 47 Map<String, ? super Object> params = new LinkedHashMap<>(); 48 params.put("name", "Foo"); 49 params.put("app-version", "2.3"); 50 params.put("description", "Duck is the King"); 51 AppImageFile aif = create(params); 52 53 Assert.assertEquals("Foo", aif.getLauncherName()); 54 } 55 56 @Test 57 public void testInvalidCommandLine() throws IOException { 58 // Just make sure AppImageFile will tolerate jpackage params that would 59 // never create app image at both load/save phases. 60 // People would edit this file just because they can. 61 // We should be ready to handle curious minds. 62 Map<String, ? super Object> params = new LinkedHashMap<>(); 63 params.put("invalidParamName", "randomStringValue"); 64 create(params); 65 66 params = new LinkedHashMap<>(); 67 params.put("name", "foo"); 68 params.put("app-version", ""); 69 create(params); 70 } 71 72 @Test 73 public void testInavlidXml() throws IOException { 74 assertInvalid(createFromXml("<foo/>")); 75 assertInvalid(createFromXml("<jpackage-state/>")); 76 assertInvalid(createFromXml( 77 "<jpackage-state>", 78 "<main-launcher></main-launcher>", 79 "</jpackage-state>")); 80 assertInvalid(createFromXml( 81 "<jpackage-state>", 82 "<launcher>A</launcher>", 83 "<launcher>B</launcher>", 84 "</jpackage-state>")); 85 } 86 87 @Test 88 public void testValidXml() throws IOException { 89 Assert.assertEquals("Foo", (createFromXml( 90 "<jpackage-state>", 91 "<main-launcher>Foo</main-launcher>", 92 "</jpackage-state>")).getLauncherName()); 93 94 Assert.assertEquals("Boo", (createFromXml( 95 "<jpackage-state>", 96 "<main-launcher>Boo</main-launcher>", 97 "<main-launcher>Bar</main-launcher>", 98 "</jpackage-state>")).getLauncherName()); 99 100 var file = createFromXml( 101 "<jpackage-state>", 102 "<main-launcher>Foo</main-launcher>", 103 "<launcher></launcher>", 104 "</jpackage-state>"); 105 Assert.assertEquals("Foo", file.getLauncherName()); 106 Assert.assertArrayEquals(new String[0], 107 file.getAddLauncherNames().toArray(String[]::new)); 108 } 109 110 @Test 111 public void testMainLauncherName() throws IOException { 112 Map<String, ? super Object> params = new LinkedHashMap<>(); 113 params.put("name", "Foo"); 114 params.put("description", "Duck App Description"); 115 AppImageFile aif = create(params); 116 117 Assert.assertEquals("Foo", aif.getLauncherName()); 118 } 119 120 @Test 121 public void testAddLauncherNames() throws IOException { 122 Map<String, ? super Object> params = new LinkedHashMap<>(); 123 List<Map<String, ? super Object>> launchersAsMap = new ArrayList<>(); 124 125 Map<String, ? super Object> addLauncher2Params = new LinkedHashMap(); 126 addLauncher2Params.put("name", "Launcher2Name"); 127 launchersAsMap.add(addLauncher2Params); 128 129 Map<String, ? super Object> addLauncher3Params = new LinkedHashMap(); 130 addLauncher3Params.put("name", "Launcher3Name"); 131 launchersAsMap.add(addLauncher3Params); 132 133 params.put("name", "Duke App"); 134 params.put("description", "Duke App Description"); 135 params.put("add-launcher", launchersAsMap); 136 AppImageFile aif = create(params); 137 138 List<String> addLauncherNames = aif.getAddLauncherNames(); 139 Assert.assertEquals(2, addLauncherNames.size()); 140 Assert.assertTrue(addLauncherNames.contains("Launcher2Name")); 141 Assert.assertTrue(addLauncherNames.contains("Launcher3Name")); 142 143 } 144 145 private AppImageFile create(Map<String, Object> params) throws IOException { 146 AppImageFile.save(tempFolder.getRoot().toPath(), params); 147 return AppImageFile.load(tempFolder.getRoot().toPath()); 148 } 149 150 private void assertInvalid(AppImageFile file) { 151 Assert.assertNull(file.getLauncherName()); 152 Assert.assertNull(file.getAddLauncherNames()); 153 } 154 155 private AppImageFile createFromXml(String... xmlData) throws IOException { 156 Path directory = tempFolder.getRoot().toPath(); 157 Path path = AppImageFile.getPathInAppImage(directory); 158 path.toFile().mkdirs(); 159 Files.delete(path); 160 161 ArrayList<String> data = new ArrayList(); 162 data.add("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"); 163 data.addAll(List.of(xmlData)); 164 165 Files.write(path, data, StandardOpenOption.CREATE, 166 StandardOpenOption.TRUNCATE_EXISTING); 167 168 AppImageFile image = AppImageFile.load(directory); 169 return image; 170 } 171 172 }