--- /dev/null 2015-08-04 16:55:50.726524033 +0300 +++ new/test/compiler/compilercontrol/share/scenario/Scenario.java 2015-09-28 15:03:27.976697217 +0300 @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package compiler.compilercontrol.share.scenario; + +import compiler.compilercontrol.share.actions.BaseAction; +import compiler.compilercontrol.share.processors.CommandProcessor; +import compiler.compilercontrol.share.processors.LogProcessor; +import compiler.compilercontrol.share.processors.PrintProcessor; +import compiler.compilercontrol.share.processors.QuietProcessor; +import jdk.test.lib.Asserts; +import jdk.test.lib.OutputAnalyzer; +import jdk.test.lib.Pair; +import jdk.test.lib.ProcessTools; +import pool.PoolHelper; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.lang.reflect.Executable; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.function.Consumer; + +/** + * Test scenario + */ +public final class Scenario { + private final List vmopts; + private final Map states; + private final List> processors; + + private Scenario(List vmopts, + Map states, + List commands) { + this.vmopts = vmopts; + this.states = states; + processors = new ArrayList<>(); + processors.add(new LogProcessor(states)); + processors.add(new PrintProcessor(states)); + List nonQuieted = new ArrayList<>(); + List quieted = new ArrayList<>(); + boolean metQuiet = false; + for (CompileCommand cc : commands) { + metQuiet |= cc.command == Command.QUIET; + if (metQuiet) { + quieted.add(cc); + } else { + nonQuieted.add(cc); + } + } + processors.add(new CommandProcessor(nonQuieted)); + processors.add(new QuietProcessor(quieted)); + } + + /** + * Executes scenario + */ + public void execute() { + // Construct execution command with CompileCommand and class + List argsList = new ArrayList<>(); + // Add VM options + argsList.addAll(vmopts); + // Add class name that would be executed as a main in a separate VM + String classCmd = BaseAction.class.getName(); + // Add class name + Collections.addAll(argsList, classCmd); + int port = connectTestVM(); + argsList.add(String.valueOf(port)); + OutputAnalyzer output; + try { + output = ProcessTools.executeTestJvmAllArgs( + argsList.toArray(new String[argsList.size()])); + } catch (Throwable thr) { + throw new Error("Execution failed", thr); + } + output.shouldHaveExitValue(0); + for (Consumer processor : processors) { + processor.accept(output); + } + } + + /* + * Performs connection with a test VM, sends method states and performs + * JCMD operations on a test VM. + */ + private int connectTestVM() { + int port; + try { + ServerSocket serverSocket = new ServerSocket(0); + port = serverSocket.getLocalPort(); + Asserts.assertNotEquals(port, -1, "Socket is not bound: " + port); + new Thread(() -> { + try ( + Socket socket = serverSocket.accept(); + PrintWriter pw = new PrintWriter(socket.getOutputStream(), + true); + BufferedReader in = new BufferedReader( + new InputStreamReader(socket.getInputStream()))) { + // Get pid of the executed process + int pid = Integer.parseInt(in.readLine()); + Asserts.assertNE(pid, 0, "Got incorrect pid"); + // serialize and send state map + for (Executable x : states.keySet()) { + pw.println("{"); + pw.println(x.toGenericString()); + pw.println(states.get(x).toString()); + pw.println("}"); + } + } catch (IOException e) { + throw new Error("Failed to write data", e); + } + }).start(); + } catch (IOException e) { + throw new Error("IO error", e); + } + return port; + } + + /** + * Gets states of methods for this scenario + * + * @return pairs of executable and its state + */ + public Map getStates() { + return states; + } + + /** + * Type of the command + */ + public static enum Type { + OPTION, + FILE, + DIRECTIVE + } + + public static Builder getBuilder() { + return new Builder(); + } + + public static class Builder { + private final Set vmopts = new LinkedHashSet<>(); + private final Map builders = new HashMap<>(); + + public Builder() { + builders.put(Type.FILE, new CommandFileBuilder()); + builders.put(Type.OPTION, new CommandOptionsBuilder()); + builders.put(Type.DIRECTIVE, new DirectiveBuilder()); + } + + public void add(CompileCommand compileCommand, Type type) { + String[] vmOptions = compileCommand.command.vmOpts; + Collections.addAll(vmopts, vmOptions); + StateBuilder builder = builders.get(type); + Asserts.assertNotNull(builder, "Missing builder for the type: " + + type); + builder.add(compileCommand); + } + + public Scenario build() { + // Get all VM options + List options = new ArrayList<>(); + options.addAll(vmopts); + for (StateBuilder builder : builders.values()) { + options.addAll(builder.getOptions()); + } + // Get states from each of the state builders + Map commandFileStates = + builders.get(Type.FILE).getStates(); + Map commandOptionStates = + builders.get(Type.OPTION).getStates(); + Map directiveFileStates = + builders.get(Type.DIRECTIVE).getStates(); + // Merge states + List>> methods = new PoolHelper() + .getAllMethods(); + Map finalStates = new HashMap<>(); + for (Pair> pair : methods) { + Executable x = pair.first; + State commandOptionState = commandOptionStates.get(x); + State commandFileState = commandFileStates.get(x); + State st = State.merge(commandOptionState, commandFileState); + State directiveState = directiveFileStates.get(x); + st = State.merge(st, directiveState); + finalStates.put(x, st); + } + /* Create a list of commands from options and file + to handle quiet command */ + List ccList = new ArrayList<>(); + ccList.addAll(builders.get(Type.OPTION).getCompileCommands()); + ccList.addAll(builders.get(Type.FILE).getCompileCommands()); + return new Scenario(options, finalStates, ccList); + } + } +}