1 /* 2 * Copyright (c) 2015, 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 import java.io.BufferedReader; 24 import java.io.IOException; 25 import java.io.InputStreamReader; 26 import java.math.BigDecimal; 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.logging.Level; 33 import java.util.logging.Logger; 34 import jdk.testlibrary.JDKToolLauncher; 35 import jdk.testlibrary.Utils; 36 37 public class TmtoolTestScenario { 38 39 private final ArrayList<String> toolOutput = new ArrayList(); 40 private LingeredApp theApp = null; 41 private final String toolName; 42 private final String[] toolArgs; 43 44 /** 45 * @param toolName - name of tool to test 46 * @param toolArgs - tool arguments 47 * @return the object 48 */ 49 public static TmtoolTestScenario create(String toolName, String... toolArgs) { 50 return new TmtoolTestScenario(toolName, toolArgs); 51 } 52 53 /** 54 * @return STDOUT of tool 55 */ 56 public List<String> getToolOutput() { 57 return toolOutput; 58 } 59 60 /** 61 * 62 * @return STDOUT of test app 63 */ 64 public List<String> getAppOutput() { 65 return theApp.getAppOutput(); 66 } 67 68 /** 69 * @return Value of the app output with -XX:+PrintFlagsFinal as a map. 70 */ 71 public Map<String, String> parseFlagsFinal() { 72 List<String> astr = theApp.getAppOutput(); 73 Map<String, String> vmMap = new HashMap(); 74 75 for (String line : astr) { 76 String[] lv = line.trim().split("\\s+"); 77 try { 78 vmMap.put(lv[1], lv[3]); 79 } catch (ArrayIndexOutOfBoundsException ex) { 80 // ignore mailformed lines 81 } 82 } 83 return vmMap; 84 } 85 86 /** 87 * 88 * @param vmArgs - vm and java arguments to launch test app 89 * @return exit code of tool 90 */ 91 public int launch(List<String> vmArgs) { 92 System.out.println("Starting LingeredApp"); 93 try { 94 try { 95 theApp = LingeredApp.startApp(vmArgs); 96 97 System.out.println("Starting " + toolName + " against " + theApp.getPid()); 98 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName); 99 100 for (String cmd : toolArgs) { 101 launcher.addToolArg(cmd); 102 } 103 launcher.addToolArg(Long.toString(theApp.getPid())); 104 105 ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); 106 processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); 107 Process toolProcess = processBuilder.start(); 108 109 // By default child process output stream redirected to pipe, so we are reading it in foreground. 110 BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream())); 111 112 String line; 113 while ((line = reader.readLine()) != null) { 114 toolOutput.add(line.trim()); 115 } 116 117 toolProcess.waitFor(); 118 119 return toolProcess.exitValue(); 120 } finally { 121 theApp.stopApp(); 122 } 123 } catch (IOException | InterruptedException ex) { 124 throw new RuntimeException("Test ERROR " + ex, ex); 125 } 126 } 127 128 public void launch(String... appArgs) throws IOException { 129 launch(Arrays.asList(appArgs)); 130 } 131 132 private TmtoolTestScenario(String toolName, String[] toolArgs) { 133 this.toolName = toolName; 134 this.toolArgs = toolArgs; 135 } 136 137 }