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. 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 javax.xml.stream.XMLInputFactory; 25 import javax.xml.stream.XMLStreamConstants; 26 import javax.xml.stream.XMLStreamReader; 27 import java.io.BufferedReader; 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.FileReader; 31 32 public class OptionsBase { 33 34 static final String TEST_BUNDLE_NAME = "TestBundleName"; 35 static final String TEST_BUNDLE_IDENTIFIER = "net.java.openjdk.packagerTest"; 36 static final String TEST_CATECORY = "public.app-category.test"; 37 private static String TEST_NAME; 38 private static String EXT; 39 private static String OUTPUT; 40 private static String[] CMD; 41 42 private static void testCreateInstaller() throws Exception { 43 JPackageHelper.executeCLI(true, CMD); 44 45 if (EXT.equals("dmg")) { 46 String disk = null; 47 try { 48 var log = new File("hdiutil.log"); 49 JPackageHelper.execute(log, "/usr/bin/hdiutil", 50 "attach", OUTPUT); 51 try(var br = new BufferedReader(new FileReader(log))) { 52 var line = br.lines().reduce((a, b) -> b).orElse(null) 53 .split("\t"); 54 disk = line[0].trim(); 55 testPkg(line[2].trim() + File.separator + TEST_NAME + 56 "-1.0.pkg"); 57 } 58 } finally { 59 if (disk != null) { 60 JPackageHelper.execute(null, 61 "/usr/bin/hdiutil", "detach", disk); 62 } 63 } 64 } else { 65 testPkg(OUTPUT); 66 } 67 } 68 69 private static void testPkg(String path) throws Exception { 70 JPackageHelper.execute(null, "/usr/sbin/pkgutil", 71 "--expand-full", path, "expand"); 72 var info = new File("expand/" + TEST_NAME + "-app.pkg/Payload/" 73 + TEST_NAME + ".app/Contents/Info.plist"); 74 if (!info.exists()) { 75 throw new AssertionError("Info.plist not found"); 76 } 77 78 String bundleName = null; 79 String bundleIdentifier = null; 80 String categoryType = null; 81 try (FileInputStream fis = new FileInputStream(info)) { 82 var xmlInFact = XMLInputFactory.newInstance(); 83 xmlInFact.setProperty(XMLInputFactory.SUPPORT_DTD, false); 84 xmlInFact.setProperty( 85 XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); 86 var reader = xmlInFact.createXMLStreamReader(fis); 87 while (reader.hasNext()) { 88 if (reader.next() == XMLStreamConstants.CHARACTERS) { 89 switch (reader.getText()) { 90 case "CFBundleName": { 91 bundleName = readValue(reader); 92 break; 93 } 94 case "CFBundleIdentifier" : { 95 bundleIdentifier = readValue(reader); 96 break; 97 } 98 case "LSApplicationCategoryType" : { 99 categoryType = readValue(reader); 100 break; 101 } 102 } 103 } 104 } 105 } 106 boolean passed = true; 107 if (!TEST_BUNDLE_NAME.equals(bundleName)) { 108 passed = false; 109 System.err.println("Wrong bundle name [" + bundleName + 110 "] expected [" + TEST_BUNDLE_NAME + "]" ); 111 } 112 if (!TEST_BUNDLE_IDENTIFIER.equals(bundleIdentifier)) { 113 passed = false; 114 System.err.println("Wrong bundle identifier [" + 115 bundleIdentifier + "] expected [" + TEST_BUNDLE_IDENTIFIER 116 + "]" ); 117 } 118 if (!TEST_CATECORY.equals(categoryType)) { 119 passed = false; 120 System.err.println("Wrong appstore category [" + categoryType + 121 "] expected [" + TEST_CATECORY + "]" ); 122 } 123 124 if (!passed) { 125 throw new AssertionError("Test failed"); 126 } 127 } 128 129 static private String readValue(XMLStreamReader reader) throws Exception { 130 while (reader.hasNext() && reader.next() != XMLStreamConstants.START_ELEMENT); 131 return reader.hasNext() ? reader.getElementText() : null; 132 } 133 134 private static void verifyInstall() throws Exception { 135 String app = JPackagePath.getOSXInstalledApp("jpackage", TEST_NAME); 136 JPackageInstallerHelper.validateApp(app); 137 } 138 139 private static void verifyUnInstall() throws Exception { 140 // Not needed on OS X, since we just deleting installed application 141 // without using generated installer. We keeping this for consistnency 142 // between platforms. 143 } 144 145 private static void init(String name, String ext) { 146 TEST_NAME = name; 147 EXT = ext; 148 OUTPUT = "output" + File.separator + TEST_NAME + "-1.0." + EXT; 149 CMD = new String[] { 150 "--package-type", EXT, 151 "--input", "input", 152 "--output", "output", 153 "--name", TEST_NAME, 154 "--main-jar", "hello.jar", 155 "--main-class", "Hello", 156 "--mac-bundle-name", TEST_BUNDLE_NAME, 157 "--mac-bundle-identifier", TEST_BUNDLE_IDENTIFIER, 158 "--mac-app-store-category", TEST_CATECORY 159 }; 160 } 161 162 public static void run(String name, String ext) throws Exception { 163 init(name, ext); 164 165 if (JPackageInstallerHelper.isVerifyInstall()) { 166 verifyInstall(); 167 } else if (JPackageInstallerHelper.isVerifyUnInstall()) { 168 verifyUnInstall(); 169 } else { 170 JPackageHelper.createHelloInstallerJar(); 171 testCreateInstaller(); 172 } 173 } 174 }