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 abstract class Helper {
  38     public static final WhiteBox WB = WhiteBox.getWhiteBox();
  39     public static final Random R = Utils.getRandomInstance();
  40 
  41     private static final long THRESHOLD = WB.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     public static void startInfiniteLoopThread(Runnable action) {
  52         startInfiniteLoopThread(action, 0L);
  53     }
  54 
  55     public static void startInfiniteLoopThread(Runnable action, long millis) {
  56         Thread t = new Thread(new InfiniteLoop(action, millis));
  57         t.setDaemon(true);
  58         t.start();
  59     }
  60 
  61     public static int callMethod(Callable<Integer> callable) {
  62         int result = 0;
  63         for (int i = 0; i < THRESHOLD; ++i) {
  64             try {
  65                 result = callable.call();
  66             } catch (Exception e) {
  67                 throw new AssertionError(
  68                         "Exception occurred during test method execution", e);
  69             }
  70             Asserts.assertEQ(result, TestCaseImpl.EXPECTED_VALUE,
  71                     "Method returns unexpected value");
  72         }
  73         return result;
  74     }
  75 
  76     private static byte[] loadClassData(String name) throws IOException {
  77         try (BufferedInputStream in = new BufferedInputStream(
  78                 ClassLoader.getSystemResourceAsStream(name.replace(".", "/")
  79                         + ".class"))) {
  80             ByteArrayOutputStream result = new ByteArrayOutputStream();
  81             byte[] buffer = new byte[1024];
  82             int read;
  83             while ((read = in.read(buffer)) != -1) {
  84                 result.write(buffer, 0, read);
  85             }
  86             return result.toByteArray();
  87         }
  88     }
  89 
  90     public interface TestCase {
  91 
  92         public static TestCase get() {
  93             String className = TestCaseImpl.class.getName();
  94             try {
  95                 Class clazz = ByteCodeLoader.load(className, CLASS_DATA);
  96                 return (TestCase) clazz.newInstance();
  97             } catch (ReflectiveOperationException e) {
  98                 throw new Error("TESTBUG: error while creating " + className
  99                         + " instance from reloaded class", e);
 100             }
 101         }
 102 
 103         Callable<Integer> getCallable();
 104         int method();
 105     }
 106 
 107     public static class TestCaseImpl implements TestCase {
 108         private static final int RETURN_VALUE = 42;
 109         private static final int RECURSION_DEPTH = 10;
 110         public static final int EXPECTED_VALUE = RETURN_VALUE * RECURSION_DEPTH;
 111         private volatile int i;
 112         public TestCaseImpl() {
 113         }
 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     }
 134 
 135 }