Tue, 21 May 2013 13:50:56 +0100
8005207: test has 2 @bug tags
Reviewed-by: mcimadamore
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | /** |
aoqi@0 | 27 | * @test |
aoqi@0 | 28 | * @bug 8003639 |
aoqi@0 | 29 | * @summary convert lambda testng tests to jtreg and add them |
aoqi@0 | 30 | * @run testng LambdaTranslationTest1 |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | import org.testng.annotations.Test; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import static org.testng.Assert.assertEquals; |
aoqi@0 | 36 | |
aoqi@0 | 37 | @Test |
aoqi@0 | 38 | public class LambdaTranslationTest1 extends LT1Sub { |
aoqi@0 | 39 | |
aoqi@0 | 40 | String cntxt = "blah"; |
aoqi@0 | 41 | |
aoqi@0 | 42 | private static final ThreadLocal<Object> result = new ThreadLocal<>(); |
aoqi@0 | 43 | |
aoqi@0 | 44 | private static void setResult(Object s) { result.set(s); } |
aoqi@0 | 45 | private static void appendResult(Object s) { result.set(result.get().toString() + s); } |
aoqi@0 | 46 | |
aoqi@0 | 47 | private static void assertResult(String expected) { |
aoqi@0 | 48 | assertEquals(result.get().toString(), expected); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | |
aoqi@0 | 51 | static Integer count(String s) { |
aoqi@0 | 52 | return s.length(); |
aoqi@0 | 53 | } |
aoqi@0 | 54 | |
aoqi@0 | 55 | static int icount(String s) { |
aoqi@0 | 56 | return s.length(); |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | static void eye(Integer i) { |
aoqi@0 | 60 | setResult(String.format("I:%d", i)); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | static void ieye(int i) { |
aoqi@0 | 64 | setResult(String.format("i:%d", i)); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | static void deye(double d) { |
aoqi@0 | 68 | setResult(String.format("d:%f", d)); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public void testLambdas() { |
aoqi@0 | 72 | TBlock<Object> b = t -> {setResult("Sink0::" + t);}; |
aoqi@0 | 73 | b.apply("Howdy"); |
aoqi@0 | 74 | assertResult("Sink0::Howdy"); |
aoqi@0 | 75 | |
aoqi@0 | 76 | TBlock<String> b1 = t -> {setResult("Sink1::" + t);}; |
aoqi@0 | 77 | b1.apply("Rowdy"); |
aoqi@0 | 78 | assertResult("Sink1::Rowdy"); |
aoqi@0 | 79 | |
aoqi@0 | 80 | for (int i = 5; i < 10; ++i) { |
aoqi@0 | 81 | TBlock<Integer> b2 = t -> {setResult("Sink2::" + t);}; |
aoqi@0 | 82 | b2.apply(i); |
aoqi@0 | 83 | assertResult("Sink2::" + i); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | TBlock<Integer> b3 = t -> {setResult("Sink3::" + t);}; |
aoqi@0 | 87 | for (int i = 900; i > 0; i -= 100) { |
aoqi@0 | 88 | b3.apply(i); |
aoqi@0 | 89 | assertResult("Sink3::" + i); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | cntxt = "blah"; |
aoqi@0 | 93 | TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; |
aoqi@0 | 94 | b4.apply("Yor"); |
aoqi@0 | 95 | assertResult("b4: blah .. Yor"); |
aoqi@0 | 96 | |
aoqi@0 | 97 | String flaw = "flaw"; |
aoqi@0 | 98 | TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; |
aoqi@0 | 99 | b5.apply("BB"); |
aoqi@0 | 100 | assertResult("b5: flaw .. BB"); |
aoqi@0 | 101 | |
aoqi@0 | 102 | cntxt = "flew"; |
aoqi@0 | 103 | TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; |
aoqi@0 | 104 | b6.apply("flee"); |
aoqi@0 | 105 | assertResult("b6: flee .. flew .. flaw"); |
aoqi@0 | 106 | |
aoqi@0 | 107 | TBlock<String> b7 = t -> {setResult(String.format("b7: %s %s", t, this.protectedSuperclassMethod()));}; |
aoqi@0 | 108 | b7.apply("this:"); |
aoqi@0 | 109 | assertResult("b7: this: instance:flew"); |
aoqi@0 | 110 | |
aoqi@0 | 111 | TBlock<String> b8 = t -> {setResult(String.format("b8: %s %s", t, super.protectedSuperclassMethod()));}; |
aoqi@0 | 112 | b8.apply("super:"); |
aoqi@0 | 113 | assertResult("b8: super: I'm the sub"); |
aoqi@0 | 114 | |
aoqi@0 | 115 | TBlock<String> b7b = t -> {setResult(String.format("b9: %s %s", t, protectedSuperclassMethod()));}; |
aoqi@0 | 116 | b7b.apply("implicit this:"); |
aoqi@0 | 117 | assertResult("b9: implicit this: instance:flew"); |
aoqi@0 | 118 | |
aoqi@0 | 119 | TBlock<Object> b10 = t -> {setResult(String.format("b10: new LT1Thing: %s", (new LT1Thing(t)).str));}; |
aoqi@0 | 120 | b10.apply("thing"); |
aoqi@0 | 121 | assertResult("b10: new LT1Thing: thing"); |
aoqi@0 | 122 | |
aoqi@0 | 123 | TBlock<Object> b11 = t -> {setResult(String.format("b11: %s", (new LT1Thing(t) { |
aoqi@0 | 124 | String get() { |
aoqi@0 | 125 | return "*" + str.toString() + "*"; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | }).get()));}; |
aoqi@0 | 128 | b11.apply(999); |
aoqi@0 | 129 | assertResult("b11: *999*"); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | public void testMethodRefs() { |
aoqi@0 | 133 | LT1IA ia = LambdaTranslationTest1::eye; |
aoqi@0 | 134 | ia.doit(1234); |
aoqi@0 | 135 | assertResult("I:1234"); |
aoqi@0 | 136 | |
aoqi@0 | 137 | LT1IIA iia = LambdaTranslationTest1::ieye; |
aoqi@0 | 138 | iia.doit(1234); |
aoqi@0 | 139 | assertResult("i:1234"); |
aoqi@0 | 140 | |
aoqi@0 | 141 | LT1IA da = LambdaTranslationTest1::deye; |
aoqi@0 | 142 | da.doit(1234); |
aoqi@0 | 143 | assertResult("d:1234.000000"); |
aoqi@0 | 144 | |
aoqi@0 | 145 | LT1SA a = LambdaTranslationTest1::count; |
aoqi@0 | 146 | assertEquals((Integer) 5, a.doit("howdy")); |
aoqi@0 | 147 | |
aoqi@0 | 148 | a = LambdaTranslationTest1::icount; |
aoqi@0 | 149 | assertEquals((Integer) 6, a.doit("shower")); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | public void testInner() throws Exception { |
aoqi@0 | 153 | (new In()).doInner(); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | protected String protectedSuperclassMethod() { |
aoqi@0 | 157 | return "instance:" + cntxt; |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | private class In { |
aoqi@0 | 161 | |
aoqi@0 | 162 | private int that = 1234; |
aoqi@0 | 163 | |
aoqi@0 | 164 | void doInner() { |
aoqi@0 | 165 | TBlock<String> i4 = t -> {setResult(String.format("i4: %d .. %s", that, t));}; |
aoqi@0 | 166 | i4.apply("=1234"); |
aoqi@0 | 167 | assertResult("i4: 1234 .. =1234"); |
aoqi@0 | 168 | |
aoqi@0 | 169 | TBlock<String> i5 = t -> {setResult(""); appendResult(t); appendResult(t);}; |
aoqi@0 | 170 | i5.apply("fruit"); |
aoqi@0 | 171 | assertResult("fruitfruit"); |
aoqi@0 | 172 | |
aoqi@0 | 173 | cntxt = "human"; |
aoqi@0 | 174 | TBlock<String> b4 = t -> {setResult(String.format("b4: %s .. %s", cntxt, t));}; |
aoqi@0 | 175 | b4.apply("bin"); |
aoqi@0 | 176 | assertResult("b4: human .. bin"); |
aoqi@0 | 177 | |
aoqi@0 | 178 | final String flaw = "flaw"; |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** |
aoqi@0 | 181 | Callable<String> c5 = () -> "["+flaw+"]" ; |
aoqi@0 | 182 | System.out.printf("c5: %s\n", c5.call() ); |
aoqi@0 | 183 | **/ |
aoqi@0 | 184 | |
aoqi@0 | 185 | TBlock<String> b5 = t -> {setResult(String.format("b5: %s .. %s", flaw, t));}; |
aoqi@0 | 186 | b5.apply("BB"); |
aoqi@0 | 187 | assertResult("b5: flaw .. BB"); |
aoqi@0 | 188 | |
aoqi@0 | 189 | cntxt = "borg"; |
aoqi@0 | 190 | TBlock<String> b6 = t -> {setResult(String.format("b6: %s .. %s .. %s", t, cntxt, flaw));}; |
aoqi@0 | 191 | b6.apply("flee"); |
aoqi@0 | 192 | assertResult("b6: flee .. borg .. flaw"); |
aoqi@0 | 193 | |
aoqi@0 | 194 | TBlock<String> b7b = t -> {setResult(String.format("b7b: %s %s", t, protectedSuperclassMethod()));}; |
aoqi@0 | 195 | b7b.apply("implicit outer this"); |
aoqi@0 | 196 | assertResult("b7b: implicit outer this instance:borg"); |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** |
aoqi@0 | 199 | TBlock<Object> b9 = t -> { System.out.printf("New: %s\n", (new LT1Thing(t)).str); }; |
aoqi@0 | 200 | b9.apply("thing"); |
aoqi@0 | 201 | |
aoqi@0 | 202 | TBlock<Object> ba = t -> { System.out.printf("Def: %s\n", (new LT1Thing(t) { String get() { return "*" + str.toString() +"*";}}).get() ); }; |
aoqi@0 | 203 | ba.apply(999); |
aoqi@0 | 204 | |
aoqi@0 | 205 | */ |
aoqi@0 | 206 | } |
aoqi@0 | 207 | } |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | class LT1Sub { |
aoqi@0 | 211 | protected String protectedSuperclassMethod() { |
aoqi@0 | 212 | return "I'm the sub"; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | class LT1Thing { |
aoqi@0 | 217 | final Object str; |
aoqi@0 | 218 | |
aoqi@0 | 219 | LT1Thing(Object s) { |
aoqi@0 | 220 | str = s; |
aoqi@0 | 221 | } |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | interface LT1SA { |
aoqi@0 | 225 | Integer doit(String s); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | interface LT1IA { |
aoqi@0 | 229 | void doit(int i); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | |
aoqi@0 | 232 | interface LT1IIA { |
aoqi@0 | 233 | void doit(Integer i); |
aoqi@0 | 234 | } |