LambdaTranslationCompoundSamTest.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2012, 2013, 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
24import org.testng.annotations.Test;
25
26import static org.testng.Assert.assertEquals;
27
28/**
29 * LambdaTranslationCompoundSamTest
30 *
31 * @author Brian Goetz
32 */
33@Test
34public class LambdaTranslationCompoundSamTest {
35    interface Accepts<T> {
36        void accept(T t);
37    }
38
39    interface AcceptsInt {
40        void accept(int i);
41    }
42
43    interface A<T> extends Accepts<T> {
44
45        default void accept(int value) {
46            throw new IllegalStateException();
47        }
48
49        interface OfInt extends A<Integer>, AcceptsInt {
50            @Override
51            void accept(int value);
52
53            @Override
54            default void accept(Integer i) {
55                accept(i.intValue());
56            }
57        }
58    }
59
60    protected interface Target<T> extends A<T> {
61        public interface OfInt extends Target<Integer>, A.OfInt { }
62    }
63
64    public void testConversion() {
65        int[] result = new int[4];
66
67        Target<Integer> tb = (Integer i) -> { result[0] = i+1; };
68        tb.accept((Integer) 3);
69        assertEquals(4, result[0]);
70
71        Target.OfInt ti = (int i) -> { result[1] = i+1; };
72        ti.accept(7);
73        assertEquals(8, result[1]);
74    }
75}
76