1 /*
2  * Copyright (c) 2019, 2020, 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.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
testIdentity()46     public void testIdentity() throws IOException {
47         Map<String, ? super Object> params = new LinkedHashMap<>();
48         params.put(Arguments.CLIOptions.NAME.getId(), "Foo");
49         params.put(Arguments.CLIOptions.VERSION.getId(), "2.3");
50         params.put(Arguments.CLIOptions.DESCRIPTION.getId(), "Duck is the King");
51         AppImageFile aif = create(params);
52 
53         Assert.assertEquals("Foo", aif.getLauncherName());
54     }
55 
56     @Test
testInvalidCommandLine()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         params.put(Arguments.CLIOptions.APPCLASS.getId(), "TestClass");
65         params.put(Arguments.CLIOptions.MAIN_JAR.getId(), "test.jar");
66         create(params);
67 
68         params = new LinkedHashMap<>();
69         params.put(Arguments.CLIOptions.NAME.getId(), "foo");
70         params.put(Arguments.CLIOptions.VERSION.getId(), "");
71         create(params);
72     }
73 
74     @Test
testInavlidXml()75     public void testInavlidXml() throws IOException {
76         assertInvalid(createFromXml("<foo/>"));
77         assertInvalid(createFromXml("<jpackage-state/>"));
78         assertInvalid(createFromXml(
79                 "<jpackage-state>",
80                     "<main-launcher></main-launcher>",
81                 "</jpackage-state>"));
82         assertInvalid(createFromXml(
83                 "<jpackage-state>",
84                     "<launcher>A</launcher>",
85                     "<launcher>B</launcher>",
86                 "</jpackage-state>"));
87     }
88 
89     @Test
testValidXml()90     public void testValidXml() throws IOException {
91         Assert.assertEquals("Foo", (createFromXml(
92                 "<jpackage-state>",
93                     "<main-launcher>Foo</main-launcher>",
94                 "</jpackage-state>")).getLauncherName());
95 
96         Assert.assertEquals("Boo", (createFromXml(
97                 "<jpackage-state>",
98                     "<main-launcher>Boo</main-launcher>",
99                     "<main-launcher>Bar</main-launcher>",
100                 "</jpackage-state>")).getLauncherName());
101 
102         var file = createFromXml(
103                 "<jpackage-state>",
104                     "<main-launcher>Foo</main-launcher>",
105                     "<launcher></launcher>",
106                 "</jpackage-state>");
107         Assert.assertEquals("Foo", file.getLauncherName());
108         Assert.assertArrayEquals(new String[0],
109                 file.getAddLauncherNames().toArray(String[]::new));
110     }
111 
112     @Test
testMainLauncherName()113     public void testMainLauncherName() throws IOException {
114         Map<String, ? super Object> params = new LinkedHashMap<>();
115         params.put("name", "Foo");
116         params.put("description", "Duck App Description");
117         AppImageFile aif = create(params);
118 
119         Assert.assertEquals("Foo", aif.getLauncherName());
120     }
121 
122     @Test
testAddLauncherNames()123     public void testAddLauncherNames() throws IOException {
124         Map<String, ? super Object> params = new LinkedHashMap<>();
125         List<Map<String, ? super Object>> launchersAsMap = new ArrayList<>();
126 
127         Map<String, ? super Object> addLauncher2Params = new LinkedHashMap();
128         addLauncher2Params.put("name", "Launcher2Name");
129         launchersAsMap.add(addLauncher2Params);
130 
131         Map<String, ? super Object> addLauncher3Params = new LinkedHashMap();
132         addLauncher3Params.put("name", "Launcher3Name");
133         launchersAsMap.add(addLauncher3Params);
134 
135         params.put("name", "Duke App");
136         params.put("description", "Duke App Description");
137         params.put("add-launcher", launchersAsMap);
138         AppImageFile aif = create(params);
139 
140         List<String> addLauncherNames = aif.getAddLauncherNames();
141         Assert.assertEquals(2, addLauncherNames.size());
142         Assert.assertTrue(addLauncherNames.contains("Launcher2Name"));
143         Assert.assertTrue(addLauncherNames.contains("Launcher3Name"));
144 
145     }
146 
create(Map<String, Object> params)147     private AppImageFile create(Map<String, Object> params) throws IOException {
148         AppImageFile.save(tempFolder.getRoot().toPath(), params);
149         return AppImageFile.load(tempFolder.getRoot().toPath());
150     }
151 
assertInvalid(AppImageFile file)152     private void assertInvalid(AppImageFile file) {
153         Assert.assertNull(file.getLauncherName());
154         Assert.assertNull(file.getAddLauncherNames());
155     }
156 
createFromXml(String... xmlData)157     private AppImageFile createFromXml(String... xmlData) throws IOException {
158         Path directory = tempFolder.getRoot().toPath();
159         Path path = AppImageFile.getPathInAppImage(directory);
160         path.toFile().mkdirs();
161         Files.delete(path);
162 
163         ArrayList<String> data = new ArrayList();
164         data.add("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>");
165         data.addAll(List.of(xmlData));
166 
167         Files.write(path, data, StandardOpenOption.CREATE,
168                     StandardOpenOption.TRUNCATE_EXISTING);
169 
170         AppImageFile image = AppImageFile.load(directory);
171         return image;
172     }
173 
174 }
175