aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 8003639 aoqi@0: * @summary convert lambda testng tests to jtreg and add them aoqi@0: * @run testng MethodReferenceTestFDCCE aoqi@0: */ aoqi@0: aoqi@0: import org.testng.annotations.Test; aoqi@0: import java.lang.reflect.Array; aoqi@0: aoqi@0: import static org.testng.Assert.assertEquals; aoqi@0: import static org.testng.Assert.assertTrue; aoqi@0: import static org.testng.Assert.fail; aoqi@0: aoqi@0: /** aoqi@0: * Method references and raw types. aoqi@0: * @author Robert Field aoqi@0: */ aoqi@0: aoqi@0: @Test aoqi@0: @SuppressWarnings({"rawtypes", "unchecked"}) aoqi@0: public class MethodReferenceTestFDCCE { aoqi@0: aoqi@0: static void assertCCE(Throwable t) { aoqi@0: assertEquals(t.getClass().getName(), "java.lang.ClassCastException"); aoqi@0: } aoqi@0: aoqi@0: interface Pred { boolean accept(T x); } aoqi@0: aoqi@0: interface Ps { boolean accept(short x); } aoqi@0: aoqi@0: interface Oo { Object too(int x); } aoqi@0: aoqi@0: interface Reto { T m(); } aoqi@0: aoqi@0: class A {} aoqi@0: class B extends A {} aoqi@0: aoqi@0: static boolean isMinor(int x) { aoqi@0: return x < 18; aoqi@0: } aoqi@0: aoqi@0: static boolean tst(A x) { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: static Object otst(Object x) { aoqi@0: return x; aoqi@0: } aoqi@0: aoqi@0: static boolean stst(Short x) { aoqi@0: return x < 18; aoqi@0: } aoqi@0: aoqi@0: static short ritst() { aoqi@0: return 123; aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrim1() { aoqi@0: Pred p = MethodReferenceTestFDCCE::isMinor; aoqi@0: Pred p2 = p; aoqi@0: assertTrue(p2.accept((Byte)(byte)15)); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrim2() { aoqi@0: Pred p = MethodReferenceTestFDCCE::isMinor; aoqi@0: Pred p2 = p; aoqi@0: assertTrue(p2.accept((byte)15)); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrimICCE() { aoqi@0: Pred p = MethodReferenceTestFDCCE::isMinor; aoqi@0: Pred p2 = p; aoqi@0: try { aoqi@0: p2.accept(15); // should throw CCE aoqi@0: fail("Exception should have been thrown"); aoqi@0: } catch (Throwable t) { aoqi@0: assertCCE(t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrimOCCE() { aoqi@0: Pred p = MethodReferenceTestFDCCE::isMinor; aoqi@0: Pred p2 = p; aoqi@0: try { aoqi@0: p2.accept(new Object()); // should throw CCE aoqi@0: fail("Exception should have been thrown"); aoqi@0: } catch (Throwable t) { aoqi@0: assertCCE(t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDRef() { aoqi@0: Pred p = MethodReferenceTestFDCCE::tst; aoqi@0: Pred p2 = p; aoqi@0: assertTrue(p2.accept(new B())); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDRefCCE() { aoqi@0: Pred p = MethodReferenceTestFDCCE::tst; aoqi@0: Pred p2 = p; aoqi@0: try { aoqi@0: p2.accept(new A()); // should throw CCE aoqi@0: fail("Exception should have been thrown"); aoqi@0: } catch (Throwable t) { aoqi@0: assertCCE(t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrimPrim() { aoqi@0: Ps p = MethodReferenceTestFDCCE::isMinor; aoqi@0: assertTrue(p.accept((byte)15)); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrimBoxed() { aoqi@0: Ps p = MethodReferenceTestFDCCE::stst; aoqi@0: assertTrue(p.accept((byte)15)); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDPrimRef() { aoqi@0: Oo p = MethodReferenceTestFDCCE::otst; aoqi@0: assertEquals(p.too(15).getClass().getName(), "java.lang.Integer"); aoqi@0: } aoqi@0: aoqi@0: public void testMethodReferenceFDRet1() { aoqi@0: Reto p = MethodReferenceTestFDCCE::ritst; aoqi@0: assertEquals(p.m(), (Short)(short)123); aoqi@0: } aoqi@0: }