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 java.io.File; 25 import java.io.PrintWriter; 26 import java.io.StringWriter; 27 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import java.util.spi.ToolProvider; 34 35 public class JPackageHelper { 36 37 private static final boolean VERBOSE = false; 38 private static final String OS = System.getProperty("os.name").toLowerCase(); 39 private static final String JAVA_HOME = System.getProperty("java.home"); 40 public static final String TEST_SRC_ROOT; 41 public static final String TEST_SRC; 42 private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin"); 43 private static final Path JPACKAGE; 44 private static final Path JAVAC; 45 private static final Path JAR; 46 private static final Path JLINK; 47 48 static { 49 if (OS.startsWith("win")) { 130 131 private static String[] getCommand(String... args) { 132 String[] command; 133 if (args == null) { 134 command = new String[1]; 135 } else { 136 command = new String[args.length + 1]; 137 } 138 139 int index = 0; 140 command[index] = JPACKAGE.toString(); 141 142 if (args != null) { 143 for (String arg : args) { 144 index++; 145 command[index] = arg; 146 } 147 } 148 149 return command; 150 } 151 152 public static String executeCLI(boolean retValZero, String... args) throws Exception { 153 int retVal; 154 File outfile = new File("output.log"); 155 try { 156 String[] command = getCommand(args); 157 retVal = execute(outfile, command); 158 } catch (Exception ex) { 159 if (outfile.exists()) { 160 System.err.println(Files.readString(outfile.toPath())); 161 } 162 throw ex; 163 } 164 165 String output = Files.readString(outfile.toPath()); 166 if (retValZero) { 167 if (retVal != 0) { 168 System.err.println(output); 169 throw new AssertionError("jpackage exited with error: " + retVal); | 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 java.io.File; 25 import java.io.IOException; 26 import java.io.PrintWriter; 27 import java.io.StringWriter; 28 import java.nio.file.FileVisitResult; 29 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.SimpleFileVisitor; 33 import java.nio.file.attribute.BasicFileAttributes; 34 import java.util.ArrayList; 35 import java.util.List; 36 37 import java.util.spi.ToolProvider; 38 39 public class JPackageHelper { 40 41 private static final boolean VERBOSE = false; 42 private static final String OS = System.getProperty("os.name").toLowerCase(); 43 private static final String JAVA_HOME = System.getProperty("java.home"); 44 public static final String TEST_SRC_ROOT; 45 public static final String TEST_SRC; 46 private static final Path BIN_DIR = Path.of(JAVA_HOME, "bin"); 47 private static final Path JPACKAGE; 48 private static final Path JAVAC; 49 private static final Path JAR; 50 private static final Path JLINK; 51 52 static { 53 if (OS.startsWith("win")) { 134 135 private static String[] getCommand(String... args) { 136 String[] command; 137 if (args == null) { 138 command = new String[1]; 139 } else { 140 command = new String[args.length + 1]; 141 } 142 143 int index = 0; 144 command[index] = JPACKAGE.toString(); 145 146 if (args != null) { 147 for (String arg : args) { 148 index++; 149 command[index] = arg; 150 } 151 } 152 153 return command; 154 } 155 156 private static void deleteRecursive(File path) throws IOException { 157 if (!path.exists()) { 158 return; 159 } 160 161 Path directory = path.toPath(); 162 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { 163 @Override 164 public FileVisitResult visitFile(Path file, 165 BasicFileAttributes attr) throws IOException { 166 if (OS.startsWith("win")) { 167 Files.setAttribute(file, "dos:readonly", false); 168 } 169 Files.delete(file); 170 return FileVisitResult.CONTINUE; 171 } 172 173 @Override 174 public FileVisitResult preVisitDirectory(Path dir, 175 BasicFileAttributes attr) throws IOException { 176 if (OS.startsWith("win")) { 177 Files.setAttribute(dir, "dos:readonly", false); 178 } 179 return FileVisitResult.CONTINUE; 180 } 181 182 @Override 183 public FileVisitResult postVisitDirectory(Path dir, IOException e) 184 throws IOException { 185 Files.delete(dir); 186 return FileVisitResult.CONTINUE; 187 } 188 }); 189 } 190 191 public static void deleteOutputFolder(String output) throws IOException { 192 File outputFolder = new File(output); 193 System.out.println("AMDEBUG output: " + outputFolder.getAbsolutePath()); 194 deleteRecursive(outputFolder); 195 } 196 197 public static String executeCLI(boolean retValZero, String... args) throws Exception { 198 int retVal; 199 File outfile = new File("output.log"); 200 try { 201 String[] command = getCommand(args); 202 retVal = execute(outfile, command); 203 } catch (Exception ex) { 204 if (outfile.exists()) { 205 System.err.println(Files.readString(outfile.toPath())); 206 } 207 throw ex; 208 } 209 210 String output = Files.readString(outfile.toPath()); 211 if (retValZero) { 212 if (retVal != 0) { 213 System.err.println(output); 214 throw new AssertionError("jpackage exited with error: " + retVal); |