/* * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Test invoking static method.
*/
@Test publicvoid testInvokeStatic1() throws Exception {
VThreadRunner.run(() -> { int result = (int) divideMethod().invoke(null, 20, 2);
assertTrue(result == 10);
});
}
/** * Test that InvocationTargetException is thrown when a static method throws * exception.
*/
@Test publicvoid testInvokeStatic2() throws Exception {
VThreadRunner.run(() -> { try {
divideMethod().invoke(null, 20, 0);
fail();
} catch (InvocationTargetException e) {
assertTrue(e.getCause() instanceof ArithmeticException);
}
});
}
/** * Test that IllegalArgumentException is thrown when trying to invoke a static * method with bad parameters.
*/
@Test publicvoid testInvokeStatic3() throws Exception {
VThreadRunner.run(() -> {
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(null));
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(null, 1));
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(null, 1, 2, 3));
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(new Object()));
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(new Object(), 1));
assertThrows(IllegalArgumentException.class,
() -> divideMethod().invoke(new Object(), 1, 2, 3));
});
}
/** * Test that ExceptionInInitializerError is thrown when invoking a static * method triggers its class to be initialized and it fails with exception.
*/
@Test publicvoid testInvokeStatic4() throws Exception {
VThreadRunner.run(() -> {
Method foo = BadClass1.class.getDeclaredMethod("foo"); try {
foo.invoke(null);
fail();
} catch (ExceptionInInitializerError e) {
assertTrue(e.getCause() instanceof ArithmeticException);
}
});
}
/** * Test that an error is thrown when invoking a static method triggers its * class to be initialized and it fails with an error.
*/
@Test publicvoid testInvokeStatic5() throws Exception {
VThreadRunner.run(() -> {
Method foo = BadClass2.class.getDeclaredMethod("foo");
assertThrows(AbstractMethodError.class, () -> foo.invoke(null));
});
}
/** * Test that invoking a static method does not pin the carrier thread.
*/
@Test publicvoid testInvokeStatic6() throws Exception { if (!ThreadBuilders.supportsCustomScheduler()) thrownew SkipException("No support for custom schedulers");
Method parkMethod = Parker.class.getDeclaredMethod("park"); try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) { Thread.Builder builder = ThreadBuilders.virtualThreadBuilder(scheduler);
ThreadFactory factory = builder.factory(); Thread vthread = factory.newThread(() -> { try {
parkMethod.invoke(null); // blocks
} catch (Exception e) { }
});
vthread.start(); try { // give thread time to be scheduled Thread.sleep(100);
// unpark with another virtual thread, runs on same carrier thread Thread unparker = factory.newThread(() -> LockSupport.unpark(vthread));
unparker.start();
unparker.join();
} finally {
LockSupport.unpark(vthread); // in case test fails
}
}
}
/** * Test invoking instance method.
*/
@Test publicvoid testInvokeInstance1() throws Exception {
VThreadRunner.run(() -> { var adder = new Adder();
Adder.addMethod().invoke(adder, 5);
assertTrue(adder.sum == 5);
});
}
/** * Test that InvocationTargetException is thrown when an instance method throws * exception.
*/
@Test publicvoid testInvokeInstance2() throws Exception {
VThreadRunner.run(() -> { var adder = new Adder(); try {
Adder.addMethod().invoke(adder, -5);
fail();
} catch (InvocationTargetException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
}
});
}
/** * Test that NullPointerException and IllegalArgumentException are thrown when * trying to invoke an instance method with null or bad parameters.
*/
@Test publicvoid testInvokeInstance3() throws Exception {
VThreadRunner.run(() -> { var adder = new Adder();
Method addMethod = Adder.addMethod();
assertThrows(NullPointerException.class,
() -> addMethod.invoke(null));
assertThrows(IllegalArgumentException.class,
() -> addMethod.invoke(adder));
assertThrows(IllegalArgumentException.class,
() -> addMethod.invoke(adder, 1, 2));
assertThrows(IllegalArgumentException.class,
() -> addMethod.invoke(adder, 1, "hi"));
assertThrows(IllegalArgumentException.class,
() -> addMethod.invoke(adder, "hi"));
});
}
/** * Test that InvocationTargetException is thrown when a constructor throws * exception.
*/
@Test publicvoid testNewInstance2() throws Exception {
VThreadRunner.run(() -> {
Constructor<?> ctor = Adder.class.getDeclaredConstructor(long.class); try {
ctor.newInstance(-10);
fail();
} catch (InvocationTargetException e) {
assertTrue(e.getCause() instanceof IllegalArgumentException);
}
});
}
/** * Test that IllegalArgumentException is thrown when newInstacne is called * with bad parameters.
*/
@Test publicvoid testNewInstance3() throws Exception {
VThreadRunner.run(() -> { var adder = new Adder();
Constructor<?> ctor = Adder.class.getDeclaredConstructor(long.class);
assertThrows(IllegalArgumentException.class,
() -> ctor.newInstance((Object[])null));
assertThrows(IllegalArgumentException.class,
() -> ctor.newInstance(adder));
assertThrows(IllegalArgumentException.class,
() -> ctor.newInstance(adder, null));
assertThrows(IllegalArgumentException.class,
() -> ctor.newInstance(adder, "foo"));
assertThrows(IllegalArgumentException.class,
() -> ctor.newInstance(adder, 1, 2));
});
}
/** * Test that ExceptionInInitializerError is thrown when invoking newInstance * triggers the class to be initialized and it fails with exception.
*/
@Test publicvoid testNewInstance4() throws Exception {
VThreadRunner.run(() -> {
Constructor<?> ctor = BadClass3.class.getDeclaredConstructor(); try {
ctor.newInstance((Object[])null);
fail();
} catch (ExceptionInInitializerError e) {
assertTrue(e.getCause() instanceof ArithmeticException);
}
});
}
/** * Test that error is thrown when invoking newInstance triggers the class * to be initialized and it fails with an error.
*/
@Test publicvoid testNewInstance5() throws Exception {
VThreadRunner.run(() -> {
Constructor<?> ctor = BadClass4.class.getDeclaredConstructor();
assertThrows(AbstractMethodError.class, () -> ctor.newInstance((Object[])null));
});
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.