1/* 2 * Copyright (c) 2012, 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 * @bug 8003639 27 * @summary convert lambda testng tests to jtreg and add them 28 * @run testng LambdaTranslationTest1 29 */ 30 31import org.testng.annotations.Test; 32 33import static org.testng.Assert.assertEquals; 34 35@Test 36public class LambdaTranslationTest1 extends LT1Sub { 37 38 String cntxt = "blah"; 39 40 private static final ThreadLocal<Object> result = new ThreadLocal<>(); 41 42 private static void setResult(Object s) { result.set(s); } 43 private static void appendResult(Object s) { result.set(result.get().toString() + s); } 44 45 private static void assertResult(String expected) { 46 assertEquals(result.get().toString(), expected); 47 } 48 49 static Integer count(String s) { 50 return s.length(); 51 } 52 53 static int icount(String s) { 54 return s.length(); 55 } 56 57 static void eye(Integer i) { 58 setResult(String.format("I:%d", i)); 59 } 60 61 static void ieye(int i) { 62 setResult(String.format("i:%d", i)); 63 } 64 65 static void deye(double d) { 66 setResult(String.format("d:%f", d)); 67 } 68 69 public void testLambdas() { 70 TBlock<Object> b = t -> {setResult("Sink0::" + t);}; 71 b.apply("Howdy"); 72 assertResult("Sink0::Howdy"); 73 74 TBlock<String> b1 = t -> {setResult("Sink1::" + t);}; 75 b1.apply("Rowdy"); 76 assertResult("Sink1::Rowdy"); 77 78 for (int i = 5; i < 10; ++i) { 79 TBlock<Integer> b2 = t -> {setResult("Sink2::" + t);}; 80 b2.apply(i); 81 assertResult("Sink2::" + i); 82 } 83 84 TBlock<Integer> b3 = t -> {setResult("Sink3::" + t);}; 85 for (int i = 900; i > 0; i -= 100) { 86 b3.apply(i); 87 assertResult("Sink3::" + i); 88 } 89 90 cntxt = "blah"; 91 TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; 92 b4.apply("Yor"); 93 assertResult("b4: blah .. Yor"); 94 95 String flaw = "flaw"; 96 TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; 97 b5.apply("BB"); 98 assertResult("b5: flaw .. BB"); 99 100 cntxt = "flew"; 101 TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; 102 b6.apply("flee"); 103 assertResult("b6: flee .. flew .. flaw"); 104 105 TBlock<String> b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));}; 106 b7.apply("this:"); 107 assertResult("b7: this: instance:flew"); 108 109 TBlock<String> b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));}; 110 b8.apply("super:"); 111 assertResult("b8: super: I'm the sub"); 112 113 TBlock<String> b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));}; 114 b7b.apply("implicit this:"); 115 assertResult("b9: implicit this: instance:flew"); 116 117 TBlock<Object> b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));}; 118 b10.apply("thing"); 119 assertResult("b10: new LT1Thing: thing"); 120 121 TBlock<Object> b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) { 122 String get() { 123 return "*" + str.toString() + "*"; 124 } 125 }).get()));}; 126 b11.apply(999); 127 assertResult("b11: *999*"); 128 } 129 130 public void testMethodRefs() { 131 LT1IA ia = LambdaTranslationTest1::eye; 132 ia.doit(1234); 133 assertResult("I:1234"); 134 135 LT1IIA iia = LambdaTranslationTest1::ieye; 136 iia.doit(1234); 137 assertResult("i:1234"); 138 139 LT1IA da = LambdaTranslationTest1::deye; 140 da.doit(1234); 141 assertResult("d:1234.000000"); 142 143 LT1SA a = LambdaTranslationTest1::count; 144 assertEquals((Integer) 5, a.doit("howdy")); 145 146 a = LambdaTranslationTest1::icount; 147 assertEquals((Integer) 6, a.doit("shower")); 148 } 149 150 public void testInner() throws Exception { 151 (new In()).doInner(); 152 } 153 154 protected String protectedSuperclassMethod() { 155 return "instance:" + cntxt; 156 } 157 158 private class In { 159 160 private int that = 1234; 161 162 void doInner() { 163 TBlock<String> i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));}; 164 i4.apply("=1234"); 165 assertResult("i4: 1234 .. =1234"); 166 167 TBlock<String> i5 = t -> {setResult(""); appendResult(t); appendResult(t);}; 168 i5.apply("fruit"); 169 assertResult("fruitfruit"); 170 171 cntxt = "human"; 172 TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; 173 b4.apply("bin"); 174 assertResult("b4: human .. bin"); 175 176 final String flaw = "flaw"; 177 178/** 179 Callable<String> c5 = () -> "["+flaw+"]" ; 180 System.out.printf("c5: %s\n", c5.call() ); 181 **/ 182 183 TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; 184 b5.apply("BB"); 185 assertResult("b5: flaw .. BB"); 186 187 cntxt = "borg"; 188 TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; 189 b6.apply("flee"); 190 assertResult("b6: flee .. borg .. flaw"); 191 192 TBlock<String> b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));}; 193 b7b.apply("implicit outer this"); 194 assertResult("b7b: implicit outer this instance:borg"); 195 196 /** 197 TBlock<Object> b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); }; 198 b9.apply("thing"); 199 200 TBlock<Object> ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); }; 201 ba.apply(999); 202 203 */ 204 } 205 } 206} 207 208class LT1Sub { 209 protected String protectedSuperclassMethod() { 210 return "I'm the sub"; 211 } 212} 213 214class LT1Thing { 215 final Object str; 216 217 LT1Thing(Object s) { 218 str = s; 219 } 220} 221 222interface LT1SA { 223 Integer doit(String s); 224} 225 226interface LT1IA { 227 void doit(int i); 228} 229 230interface LT1IIA { 231 void doit(Integer i); 232} 233