1 /*
  2  * Copyright (c) 2022, 2023, 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  * @test
 26  * @summary Test that preview APIs throws exception when preview features not enabled
 27  * @run junit/othervm PreviewFeaturesNotEnabled
 28  */
 29 
 30 import java.lang.reflect.Method;
 31 import java.lang.reflect.InvocationTargetException;
 32 import java.util.concurrent.Executors;
 33 
 34 import org.junit.jupiter.api.Test;
 35 import static org.junit.jupiter.api.Assertions.*;
 36 
 37 class PreviewFeaturesNotEnabled {
 38 
 39     /**
 40      * Thread.ofVirtual should fail with UOE.
 41      */
 42     @Test
 43     void testOfVirtual() throws Exception {
 44         Method ofVirtual = Thread.class.getDeclaredMethod("ofVirtual");
 45         var exc = assertThrows(InvocationTargetException.class, () -> ofVirtual.invoke(null));
 46         assertTrue(exc.getCause() instanceof UnsupportedOperationException);
 47     }
 48 
 49     /**
 50      * Thread.startVirtualThread should fail with UOE.
 51      */
 52     @Test
 53     void testStartVirutalThread() throws Exception {
 54         Method startVirtualThread = Thread.class.getMethod("startVirtualThread", Runnable.class);
 55         Runnable task = () -> { };
 56         var exc = assertThrows(InvocationTargetException.class,
 57                 () -> startVirtualThread.invoke(null, task));
 58         assertTrue(exc.getCause() instanceof UnsupportedOperationException);
 59     }
 60 
 61     /**
 62      * Executors.newVirtualThreadPerTaskExecutor should fail with UOE.
 63      */
 64     @Test
 65     void testNewVirtualThreadPerTaskExecutor() throws Exception {
 66         Method newVirtualThreadPerTaskExecutor = Executors.class.getMethod("newVirtualThreadPerTaskExecutor");
 67         var exc = assertThrows(InvocationTargetException.class,
 68                 () -> newVirtualThreadPerTaskExecutor.invoke(null));
 69         assertTrue(exc.getCause() instanceof UnsupportedOperationException);
 70     }
 71 
 72     /**
 73      * Directly accessing internal Continuation class should fail with UOE.
 74      */
 75     @Test
 76     void testContinuationInitializer() throws Exception {
 77         var exc = assertThrows(ExceptionInInitializerError.class,
 78                 () -> Class.forName("jdk.internal.vm.Continuation"));
 79         assertTrue(exc.getCause() instanceof UnsupportedOperationException);
 80     }
 81 
 82     /**
 83      * Thread.isVirtual should not fail.
 84      */
 85     @Test
 86     void testIsVirtual() throws Exception {
 87         boolean isVirtual = isVirtual(Thread.currentThread());
 88         assertFalse(isVirtual);
 89     }
 90 
 91     /**
 92      * Thread.ofPlatform should not fail.
 93      */
 94     @Test
 95     void testOfPlatform() throws Exception {
 96         Method ofPlatform = Thread.class.getDeclaredMethod("ofPlatform");
 97         Object builder = ofPlatform.invoke(null);
 98         Method startMethod = Class.forName("java.lang.Thread$Builder")
 99                 .getMethod("start", Runnable.class);
100         Runnable task = () -> { };
101         Thread thread = (Thread) startMethod.invoke(builder, task);
102     }
103 
104     /**
105      * Invokes Thread::isVirtual reflectively to test if the given thread is a
106      * virtual thread.
107      */
108     private static boolean isVirtual(Thread thread) throws Exception {
109         Method isVirtualMethod = Thread.class.getDeclaredMethod("isVirtual");
110         return (boolean) isVirtualMethod.invoke(thread);
111     }
112 }