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