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 
  24 package compiler.compilercontrol.share;
  25 
  26 import compiler.compilercontrol.share.method.MethodDescriptor;
  27 import compiler.compilercontrol.share.scenario.Command;
  28 import compiler.compilercontrol.share.scenario.CommandGenerator;
  29 import compiler.compilercontrol.share.scenario.CompileCommand;
  30 import compiler.compilercontrol.share.scenario.Scenario;
  31 import jdk.test.lib.Utils;
  32 
  33 import java.lang.reflect.Executable;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 public class MultiCommand extends AbstractTestBase {
  38     private final List<CompileCommand> testCases;
  39 
  40     public MultiCommand(List<CompileCommand> testCases) {
  41         this.testCases = testCases;
  42     }
  43 
  44     /**
  45      * Generates a test containing multiple random commands
  46      *
  47      * @param validOnly shows that all commands should be valid
  48      * @return test instance to run
  49      */
  50     public static AbstractTestBase generateRandomTest(boolean validOnly) {
  51         CommandGenerator cmdGen = new CommandGenerator();
  52         List<Command> commands = cmdGen.generateCommands();
  53         List<CompileCommand> testCases = new ArrayList<>();
  54         for (Command cmd : commands) {
  55             if (validOnly && cmd == Command.NONEXISTENT) {
  56                 // skip invalid command
  57                 continue;
  58             }
  59             Executable exec = Utils.getRandomElement(METHODS).first;
  60             MethodDescriptor md;
  61             if (validOnly) {
  62                 md = AbstractTestBase.getValidMethodDescriptor(exec);
  63             } else {
  64                 md = AbstractTestBase.METHOD_GEN.generateRandomDescriptor(exec);
  65             }
  66             CompileCommand cc = cmdGen.generateCompileCommand(cmd, md, null);
  67             testCases.add(cc);
  68         }
  69         return new MultiCommand(testCases);
  70     }
  71 
  72     @Override
  73     public void test() {
  74         Scenario.Builder builder = Scenario.getBuilder();
  75         for (CompileCommand cc : testCases) {
  76             cc.print();
  77             builder.add(cc);
  78         }
  79         Scenario scenario = builder.build();
  80         scenario.execute();
  81     }
  82 }