1 /*
   2  * Copyright (c) 2014, 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 import java.io.BufferedInputStream;
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 import java.util.concurrent.Callable;
  29 import java.util.Random;
  30 
  31 import com.oracle.java.testlibrary.Asserts;
  32 import com.oracle.java.testlibrary.ByteCodeLoader;
  33 import com.oracle.java.testlibrary.InfiniteLoop;
  34 import com.oracle.java.testlibrary.Utils;
  35 import sun.hotspot.WhiteBox;
  36 
  37 public final class Helper {
  38     public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  39     public static final Random RNG = Utils.getRandomInstance();
  40 
  41     private static final long THRESHOLD = WHITE_BOX.getIntxVMFlag("CompileThreshold");
  42     private static byte[] CLASS_DATA;
  43     static {
  44         try {
  45             CLASS_DATA = loadClassData(TestCaseImpl.class.getName());
  46         } catch (IOException e) {
  47             throw new Error("TESTBUG: cannot load class byte code", e);
  48         }
  49     }
  50 
  51     private Helper() {
  52     }
  53 
  54     public static void startInfiniteLoopThread(Runnable action) {
  55         startInfiniteLoopThread(action, 0L);
  56     }
  57 
  58     public static void startInfiniteLoopThread(Runnable action, long millis) {
  59         Thread t = new Thread(new InfiniteLoop(action, millis));
  60         t.setDaemon(true);
  61         t.start();
  62     }
  63 
  64     public static int callMethod(Callable<Integer> callable, int expected) {
  65         int result = 0;
  66         for (int i = 0; i < THRESHOLD; ++i) {
  67             try {
  68                 result = callable.call();
  69             } catch (Exception e) {
  70                 throw new AssertionError(
  71                         "Exception occurred during test method execution", e);
  72             }
  73             Asserts.assertEQ(result, expected, "Method returns unexpected value");
  74         }
  75         return result;
  76     }
  77 
  78     private static byte[] loadClassData(String name) throws IOException {
  79         try (BufferedInputStream in = new BufferedInputStream(
  80                 ClassLoader.getSystemResourceAsStream(name.replace(".", "/")
  81                         + ".class"))) {
  82             ByteArrayOutputStream result = new ByteArrayOutputStream();
  83             byte[] buffer = new byte[1024];
  84             int read;
  85             while ((read = in.read(buffer)) != -1) {
  86                 result.write(buffer, 0, read);
  87             }
  88             return result.toByteArray();
  89         }
  90     }
  91 
  92     public interface TestCase {
  93 
  94         public static TestCase get() {
  95             String className = TestCaseImpl.class.getName();
  96             try {
  97                 Class clazz = ByteCodeLoader.load(className, CLASS_DATA);
  98                 return (TestCase) clazz.newInstance();
  99             } catch (ReflectiveOperationException e) {
 100                 throw new Error("TESTBUG: error while creating " + className
 101                         + " instance from reloaded class", e);
 102             }
 103         }
 104 
 105         Callable<Integer> getCallable();
 106         int method();
 107         int expectedValue();
 108     }
 109 
 110     public static class TestCaseImpl implements TestCase {
 111         private static final int RETURN_VALUE = 42;
 112         private static final int RECURSION_DEPTH = 10;
 113         private volatile int i;
 114 
 115         @Override
 116         public Callable<Integer> getCallable() {
 117             return () -> {
 118                 i = 0;
 119                 return method();
 120             };
 121         }
 122 
 123         @Override
 124         public int method() {
 125             ++i;
 126             int result = RETURN_VALUE;
 127             if (i < RECURSION_DEPTH) {
 128                 return result + method();
 129             }
 130             return result;
 131         }
 132 
 133         @Override
 134         public int expectedValue() {
 135             return RETURN_VALUE * RECURSION_DEPTH;
 136         }
 137     }
 138 
 139 }