--- /dev/null 2019-11-13 18:34:22.000000000 -0500 +++ new/test/jdk/tools/jpackage/junit/jdk/incubator/jpackage/internal/AppImageFileTest.java 2019-11-13 18:34:19.055214700 -0500 @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.incubator.jpackage.internal; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.LinkedHashMap; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; + +public class AppImageFileTest { + + @Rule + public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testIdentity() throws IOException { + Map params = new LinkedHashMap<>(); + params.put("name", "Foo"); + params.put("app-version", "2.3"); + params.put("description", "Duck is the King"); + AppImageFile aif = create(params); + + Assert.assertEquals("Foo", aif.getLauncherName()); + } + + @Test + public void testInvalidCommandLine() throws IOException { + // Just make sure AppImageFile will tolerate jpackage params that would + // never create app image at both load/save phases. + // People would edit this file just because they can. + // We should be ready to handle curious minds. + Map params = new LinkedHashMap<>(); + params.put("invalidParamName", "randomStringValue"); + create(params); + + params = new LinkedHashMap<>(); + params.put("name", "foo"); + params.put("app-version", ""); + create(params); + } + + @Test + public void testInavlidXml() throws IOException { + assertInvalid(createFromXml("")); + assertInvalid(createFromXml("")); + assertInvalid(createFromXml( + "", + "", + "")); + assertInvalid(createFromXml( + "", + "A", + "B", + "")); + } + + @Test + public void testValidXml() throws IOException { + Assert.assertEquals("Foo", (createFromXml( + "", + "Foo", + "")).getLauncherName()); + + Assert.assertEquals("Boo", (createFromXml( + "", + "Boo", + "Bar", + "")).getLauncherName()); + + var file = createFromXml( + "", + "Foo", + "", + ""); + Assert.assertEquals("Foo", file.getLauncherName()); + Assert.assertArrayEquals(new String[0], + file.getAddLauncherNames().toArray(String[]::new)); + } + + @Test + public void testMainLauncherName() throws IOException { + Map params = new LinkedHashMap<>(); + params.put("name", "Foo"); + params.put("description", "Duck App Description"); + AppImageFile aif = create(params); + + Assert.assertEquals("Foo", aif.getLauncherName()); + } + + @Test + public void testAddLauncherNames() throws IOException { + Map params = new LinkedHashMap<>(); + List> launchersAsMap = new ArrayList<>(); + + Map addLauncher2Params = new LinkedHashMap(); + addLauncher2Params.put("name", "Launcher2Name"); + launchersAsMap.add(addLauncher2Params); + + Map addLauncher3Params = new LinkedHashMap(); + addLauncher3Params.put("name", "Launcher3Name"); + launchersAsMap.add(addLauncher3Params); + + params.put("name", "Duke App"); + params.put("description", "Duke App Description"); + params.put("add-launcher", launchersAsMap); + AppImageFile aif = create(params); + + List addLauncherNames = aif.getAddLauncherNames(); + Assert.assertEquals(2, addLauncherNames.size()); + Assert.assertTrue(addLauncherNames.contains("Launcher2Name")); + Assert.assertTrue(addLauncherNames.contains("Launcher3Name")); + + } + + private AppImageFile create(Map params) throws IOException { + AppImageFile.save(tempFolder.getRoot().toPath(), params); + return AppImageFile.load(tempFolder.getRoot().toPath()); + } + + private void assertInvalid(AppImageFile file) { + Assert.assertNull(file.getLauncherName()); + Assert.assertNull(file.getAddLauncherNames()); + } + + private AppImageFile createFromXml(String... xmlData) throws IOException { + Path directory = tempFolder.getRoot().toPath(); + Path path = AppImageFile.getPathInAppImage(directory); + path.toFile().mkdirs(); + Files.delete(path); + + ArrayList data = new ArrayList(); + data.add(""); + data.addAll(List.of(xmlData)); + + Files.write(path, data, StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING); + + AppImageFile image = AppImageFile.load(directory); + return image; + } + +}