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 LambdaTranslationTest1 aoqi@0: */ aoqi@0: aoqi@0: import org.testng.annotations.Test; aoqi@0: aoqi@0: import static org.testng.Assert.assertEquals; aoqi@0: aoqi@0: @Test aoqi@0: public class LambdaTranslationTest1 extends LT1Sub { aoqi@0: aoqi@0: String cntxt = "blah"; aoqi@0: aoqi@0: private static final ThreadLocal result = new ThreadLocal<>(); aoqi@0: aoqi@0: private static void setResult(Object s) { result.set(s); } aoqi@0: private static void appendResult(Object s) { result.set(result.get().toString() + s); } aoqi@0: aoqi@0: private static void assertResult(String expected) { aoqi@0: assertEquals(result.get().toString(), expected); aoqi@0: } aoqi@0: aoqi@0: static Integer count(String s) { aoqi@0: return s.length(); aoqi@0: } aoqi@0: aoqi@0: static int icount(String s) { aoqi@0: return s.length(); aoqi@0: } aoqi@0: aoqi@0: static void eye(Integer i) { aoqi@0: setResult(String.format("I:%d", i)); aoqi@0: } aoqi@0: aoqi@0: static void ieye(int i) { aoqi@0: setResult(String.format("i:%d", i)); aoqi@0: } aoqi@0: aoqi@0: static void deye(double d) { aoqi@0: setResult(String.format("d:%f", d)); aoqi@0: } aoqi@0: aoqi@0: public void testLambdas() { aoqi@0: TBlock b = t -> {setResult("Sink0::" + t);}; aoqi@0: b.apply("Howdy"); aoqi@0: assertResult("Sink0::Howdy"); aoqi@0: aoqi@0: TBlock b1 = t -> {setResult("Sink1::" + t);}; aoqi@0: b1.apply("Rowdy"); aoqi@0: assertResult("Sink1::Rowdy"); aoqi@0: aoqi@0: for (int i = 5; i < 10; ++i) { aoqi@0: TBlock b2 = t -> {setResult("Sink2::" + t);}; aoqi@0: b2.apply(i); aoqi@0: assertResult("Sink2::" + i); aoqi@0: } aoqi@0: aoqi@0: TBlock b3 = t -> {setResult("Sink3::" + t);}; aoqi@0: for (int i = 900; i > 0; i -= 100) { aoqi@0: b3.apply(i); aoqi@0: assertResult("Sink3::" + i); aoqi@0: } aoqi@0: aoqi@0: cntxt = "blah"; aoqi@0: TBlock b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; aoqi@0: b4.apply("Yor"); aoqi@0: assertResult("b4: blah .. Yor"); aoqi@0: aoqi@0: String flaw = "flaw"; aoqi@0: TBlock b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; aoqi@0: b5.apply("BB"); aoqi@0: assertResult("b5: flaw .. BB"); aoqi@0: aoqi@0: cntxt = "flew"; aoqi@0: TBlock b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; aoqi@0: b6.apply("flee"); aoqi@0: assertResult("b6: flee .. flew .. flaw"); aoqi@0: aoqi@0: TBlock b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));}; aoqi@0: b7.apply("this:"); aoqi@0: assertResult("b7: this: instance:flew"); aoqi@0: aoqi@0: TBlock b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));}; aoqi@0: b8.apply("super:"); aoqi@0: assertResult("b8: super: I'm the sub"); aoqi@0: aoqi@0: TBlock b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));}; aoqi@0: b7b.apply("implicit this:"); aoqi@0: assertResult("b9: implicit this: instance:flew"); aoqi@0: aoqi@0: TBlock b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));}; aoqi@0: b10.apply("thing"); aoqi@0: assertResult("b10: new LT1Thing: thing"); aoqi@0: aoqi@0: TBlock b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) { aoqi@0: String get() { aoqi@0: return "*" + str.toString() + "*"; aoqi@0: } aoqi@0: }).get()));}; aoqi@0: b11.apply(999); aoqi@0: assertResult("b11: *999*"); aoqi@0: } aoqi@0: aoqi@0: public void testMethodRefs() { aoqi@0: LT1IA ia = LambdaTranslationTest1::eye; aoqi@0: ia.doit(1234); aoqi@0: assertResult("I:1234"); aoqi@0: aoqi@0: LT1IIA iia = LambdaTranslationTest1::ieye; aoqi@0: iia.doit(1234); aoqi@0: assertResult("i:1234"); aoqi@0: aoqi@0: LT1IA da = LambdaTranslationTest1::deye; aoqi@0: da.doit(1234); aoqi@0: assertResult("d:1234.000000"); aoqi@0: aoqi@0: LT1SA a = LambdaTranslationTest1::count; aoqi@0: assertEquals((Integer) 5, a.doit("howdy")); aoqi@0: aoqi@0: a = LambdaTranslationTest1::icount; aoqi@0: assertEquals((Integer) 6, a.doit("shower")); aoqi@0: } aoqi@0: aoqi@0: public void testInner() throws Exception { aoqi@0: (new In()).doInner(); aoqi@0: } aoqi@0: aoqi@0: protected String protectedSuperclassMethod() { aoqi@0: return "instance:" + cntxt; aoqi@0: } aoqi@0: aoqi@0: private class In { aoqi@0: aoqi@0: private int that = 1234; aoqi@0: aoqi@0: void doInner() { aoqi@0: TBlock i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));}; aoqi@0: i4.apply("=1234"); aoqi@0: assertResult("i4: 1234 .. =1234"); aoqi@0: aoqi@0: TBlock i5 = t -> {setResult(""); appendResult(t); appendResult(t);}; aoqi@0: i5.apply("fruit"); aoqi@0: assertResult("fruitfruit"); aoqi@0: aoqi@0: cntxt = "human"; aoqi@0: TBlock b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; aoqi@0: b4.apply("bin"); aoqi@0: assertResult("b4: human .. bin"); aoqi@0: aoqi@0: final String flaw = "flaw"; aoqi@0: aoqi@0: /** aoqi@0: Callable c5 = () -> "["+flaw+"]" ; aoqi@0: System.out.printf("c5: %s\n", c5.call() ); aoqi@0: **/ aoqi@0: aoqi@0: TBlock b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; aoqi@0: b5.apply("BB"); aoqi@0: assertResult("b5: flaw .. BB"); aoqi@0: aoqi@0: cntxt = "borg"; aoqi@0: TBlock b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; aoqi@0: b6.apply("flee"); aoqi@0: assertResult("b6: flee .. borg .. flaw"); aoqi@0: aoqi@0: TBlock b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));}; aoqi@0: b7b.apply("implicit outer this"); aoqi@0: assertResult("b7b: implicit outer this instance:borg"); aoqi@0: aoqi@0: /** aoqi@0: TBlock b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); }; aoqi@0: b9.apply("thing"); aoqi@0: aoqi@0: TBlock ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); }; aoqi@0: ba.apply(999); aoqi@0: aoqi@0: */ aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class LT1Sub { aoqi@0: protected String protectedSuperclassMethod() { aoqi@0: return "I'm the sub"; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class LT1Thing { aoqi@0: final Object str; aoqi@0: aoqi@0: LT1Thing(Object s) { aoqi@0: str = s; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: interface LT1SA { aoqi@0: Integer doit(String s); aoqi@0: } aoqi@0: aoqi@0: interface LT1IA { aoqi@0: void doit(int i); aoqi@0: } aoqi@0: aoqi@0: interface LT1IIA { aoqi@0: void doit(Integer i); aoqi@0: }