test/hotspot/jtreg/runtime/InvocationTests/shared/AbstractGenerator.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
       
     1 /*
       
     2  * Copyright (c) 2009, 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 
       
    25 package shared;
       
    26 
       
    27 import java.io.File;
       
    28 import java.io.FileOutputStream;
       
    29 import java.util.Arrays;
       
    30 import java.util.Map;
       
    31 import java.util.List;
       
    32 import java.util.ArrayList;
       
    33 
       
    34 public abstract class AbstractGenerator {
       
    35     protected final boolean dumpClasses;
       
    36     protected final boolean executeTests;
       
    37     private static int testNum = 0;
       
    38 
       
    39     protected AbstractGenerator(String[] args) {
       
    40         List<String> params = new ArrayList<String>(Arrays.asList(args));
       
    41 
       
    42         if (params.contains("--help")) {
       
    43             Utils.printHelp();
       
    44             System.exit(0);
       
    45         }
       
    46 
       
    47         dumpClasses = params.contains("--dump");
       
    48         executeTests = !params.contains("--noexecute");
       
    49 
       
    50         params.remove("--dump");
       
    51         params.remove("--noexecute");
       
    52 
       
    53         Utils.init(params);
       
    54     }
       
    55 
       
    56     /*******************************************************************/
       
    57     public static void writeToFile(File dir, Map<String, byte[]> classes) {
       
    58         for (String name : classes.keySet()) {
       
    59             try {
       
    60                 writeToFile(dir, name, classes.get(name));
       
    61             } catch (Exception e) {
       
    62                 throw new RuntimeException(e);
       
    63             }
       
    64         }
       
    65     }
       
    66 
       
    67     /*******************************************************************/
       
    68     public static void writeToFile(File dir, String fullName, byte[] classBytecode) {
       
    69         if (!dir.isDirectory()) {
       
    70             throw new RuntimeException("Invalid parameter: dir doesn't point to an existing directory");
       
    71         }
       
    72 
       
    73         File classFile =
       
    74             new File(
       
    75                     dir.getPath() + File.separator
       
    76                     + fullName.replaceAll("\\.", File.separator)
       
    77                     + ".class"
       
    78                     );
       
    79 
       
    80         classFile.getParentFile().mkdirs();
       
    81 
       
    82         try {
       
    83             FileOutputStream fos = new FileOutputStream(classFile);
       
    84             try {
       
    85                 fos.write(classBytecode);
       
    86             } finally {
       
    87                 fos.close();
       
    88             }
       
    89         } catch (Exception e) {
       
    90             throw new RuntimeException(e);
       
    91         }
       
    92     }
       
    93 
       
    94     protected boolean exec(Map<String, byte[]> classes, String description, String calleeClassName, String classNameC, String[] callSites) throws ClassNotFoundException {
       
    95         boolean isPassed = true;
       
    96 
       
    97         testNum++;
       
    98 
       
    99         String caseDescription = String.format("%4d| %s", testNum, description);
       
   100 
       
   101         // Create test executor for a single case
       
   102         classes.put(
       
   103                 ExecutorGenerator.className
       
   104                 , new ExecutorGenerator(
       
   105                         caseDescription
       
   106                         , calleeClassName
       
   107                         , classNameC
       
   108                     ).generateExecutor(callSites)
       
   109         );
       
   110 
       
   111         // Dump generated set to disk, if needed
       
   112         if (dumpClasses) {
       
   113             File dir = new File("classes" + File.separator + String.format("%04d", testNum));
       
   114             dir.mkdirs();
       
   115             writeToFile(dir, classes);
       
   116         }
       
   117 
       
   118         ByteArrayClassLoader loader = new ByteArrayClassLoader(classes);
       
   119 
       
   120         Class paramClass;
       
   121         Class targetClass;
       
   122         Checker checker;
       
   123 
       
   124         try {
       
   125             paramClass = loader.loadClass(calleeClassName);
       
   126             targetClass = loader.loadClass(classNameC);
       
   127 
       
   128             checker = getChecker(paramClass, targetClass);
       
   129         } catch (Throwable e) {
       
   130             String result = Checker.abbreviateResult(e.getClass().getName());
       
   131 
       
   132             System.out.printf(caseDescription);
       
   133 
       
   134             for (String site : callSites) {
       
   135                 System.out.printf(" %7s", result);
       
   136             }
       
   137 
       
   138             System.out.println("");
       
   139 
       
   140             return true;
       
   141         }
       
   142 
       
   143         if (executeTests) {
       
   144             // Check runtime behavior
       
   145             Caller caller = new Caller(loader, checker, paramClass, targetClass);
       
   146             boolean printedCaseDes = false;
       
   147             for (String site : callSites) {
       
   148                 String callResult = caller.call(site);
       
   149 
       
   150                 if (!caller.isPassed()) {
       
   151                     isPassed = false;
       
   152                     if (!printedCaseDes) {
       
   153                         System.out.printf(caseDescription);
       
   154                         printedCaseDes = true;
       
   155                     }
       
   156                     System.out.printf(" %7s", callResult);
       
   157                 }
       
   158             }
       
   159             if (!caller.isPassed()) {
       
   160                 System.out.println(" |   FAILED");
       
   161             }
       
   162         } else {
       
   163             for (String site : callSites) {
       
   164                 String result = checker.check(loader.loadClass(site));
       
   165                 System.out.printf(" %7s", Checker.abbreviateResult(result));
       
   166             }
       
   167         }
       
   168 
       
   169         return isPassed;
       
   170     }
       
   171 
       
   172     protected abstract Checker getChecker(Class paramClass, Class targetClass);
       
   173 }