1/*
2 * Copyright (c) 2011, 2015, 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 8003280
27 * @summary Add lambda tests
28 *   This test is for identifying SAM types #4, see Helper.java for SAM types
29 * @modules java.sql
30 * @compile LambdaTest2_SAM2.java Helper.java
31 * @run main LambdaTest2_SAM2
32 */
33
34import java.util.Collection;
35import java.util.List;
36import java.util.ArrayList;
37import java.util.concurrent.TimeoutException;
38import java.io.*;
39import java.sql.SQLException;
40import java.sql.SQLTransientException;
41
42public class LambdaTest2_SAM2 {
43    private static List<String> strs = new ArrayList<String>();
44
45    public static void main(String[] args) {
46        strs.add("copy");
47        strs.add("paste");
48        strs.add("delete");
49        strs.add("rename");
50
51        LambdaTest2_SAM2 test = new LambdaTest2_SAM2();
52
53        //type #4 a):
54        test.methodAB((List list) -> 100);
55
56        //type #4 b):
57        test.methodFGHI((String s) -> new Integer(22));
58        //type #4 b):
59        test.methodJK((String s) -> new ArrayList<Number>());
60        test.methodJK((String s) -> new ArrayList());
61        //type #4 b):
62        test.methodJL((String s) -> new ArrayList<Number>());
63        test.methodJL((String s) -> new ArrayList());
64        //type #4 b):
65        test.methodJKL((String s) -> new ArrayList<Number>());
66        test.methodJKL((String s) -> new ArrayList());
67        //type #4 b):
68        test.methodJKLM((String s) -> new ArrayList<Number>());
69        test.methodJKLM((String s) -> new ArrayList());
70
71        // tyep #4 c):
72        test.methodNO((File f) -> {
73                String temp = null;
74                StringBuffer sb = new StringBuffer();
75                try
76                {
77                    BufferedReader br = new BufferedReader(new FileReader(f));
78                    while((temp=br.readLine()) != null)
79                        sb.append(temp).append("\n");
80                }
81                catch(FileNotFoundException fne){throw fne;}
82                catch(IOException e){e.printStackTrace();}
83                return sb.toString();
84        });
85        // tyep #4 c):
86        test.methodNOP((File f) -> {
87                String temp = null;
88                StringBuffer sb = new StringBuffer();
89                try
90                {
91                    BufferedReader br = new BufferedReader(new FileReader(f));
92                    while((temp=br.readLine()) != null)
93                        sb.append(temp).append("\n");
94                }
95                catch(IOException e){e.printStackTrace();}
96                return sb.toString();
97        });
98        // type #4 c):
99        test.methodBooDoo((String s) -> s.length());
100
101        //type #4 d):
102        test.methodQR((Iterable i) -> new ArrayList<String>());
103        test.methodQR((Iterable i) -> new ArrayList());
104        //type #4 d):
105        test.methodUV((List<String> list) -> {
106                test.exceptionMethod1();
107                test.exceptionMethod2();
108                return new ArrayList<String>();
109        });
110        test.methodUV((List<String> list) -> {
111                test.exceptionMethod1();
112                test.exceptionMethod2();
113                return new ArrayList();
114        });
115        //type #4 d):
116        test.methodUVW((List list) -> {
117                test.exceptionMethod1();
118                test.exceptionMethod2();
119                return new ArrayList<String>();
120        });
121        test.methodUVW((List list) -> {
122                test.exceptionMethod1();
123                test.exceptionMethod2();
124                return new ArrayList();
125        });
126    }
127
128    private void exceptionMethod1() throws EOFException{
129    }
130
131    private void exceptionMethod2() throws SQLTransientException{
132    }
133
134    //type #4 a): SAM type ([List], int, {})
135    void methodAB (AB ab) {
136        System.out.println("methodAB(): SAM type interface AB object instantiated: " + ab);
137        System.out.println(ab.getOldest(strs));
138    }
139
140    //type #4 b): SAM type ([String], Integer, {})
141    void methodFGHI(FGHI f) {
142        System.out.println("methodFGHI(): SAM type interface FGHI object instantiated: " + f);
143        System.out.println(f.getValue("str"));
144    }
145
146    //type #4 b): SAM type ([String], List<Number>, {})
147    void methodJK(JK jk) {
148        System.out.println("methodJK(): SAM type interface JK object instantiated: " + jk);
149        for(Number n : jk.getAll("in"))
150            System.out.println(n);
151    }
152
153    //type #4 b): SAM type ([String], List<Number>, {})
154    void methodJL(JL jl) {
155        System.out.println("methodJL(): SAM type interface JL object instantiated: " + jl);
156        for(Number n : ((J)jl).getAll("in")) //cast should be redundant - see 7062745
157            System.out.println(n);
158    }
159
160    //type #4 b): SAM type ([String], List<Number>, {})
161    void methodJKL(JKL jkl) { //commented - see 7062745
162        System.out.println("methodJKL(): SAM type interface JKL object instantiated: " + jkl);
163        for(Number n : ((J)jkl).getAll("in"))
164            System.out.println(n);
165    }
166
167    //type #4 b): SAM type ([String], List<Number>, {})
168    void methodJKLM(JKLM jklm) { //commented - see 7062745
169        System.out.println("methodJKLM(): SAM type interface JKLM object instantiated: " + jklm);
170        for(Number n : ((J)jklm).getAll("in"))
171            System.out.println(n);
172    }
173
174    //type #4 c): SAM type ([File], String, {FileNotFoundException})
175    void methodNO(NO no) {
176        System.out.println("methodNO(): SAM type interface \"NO\" object instantiated: " + no);
177        try {
178            System.out.println("text=" + no.getText(new File("a.txt")));
179            System.out.println("got here, no exception thrown");
180        }
181        catch(FileNotFoundException e){e.printStackTrace();}
182    }
183
184    //type #4 c): SAM type ([File]), String, {})
185    void methodNOP(NOP nop) {
186        System.out.println("methodNOP(): SAM type interface \"NOP\" object instantiated: " + nop);
187        System.out.println("text=" + nop.getText(new File("a.txt")));
188    }
189
190    //type #4 c): SAM type ([String], int, {})
191    void methodBooDoo(BooDoo bd) {
192        System.out.println("methodBooDoo(): SAM type interface BooDoo object instantiated: " + bd);
193        System.out.println("result=" + bd.getAge("lambda"));
194    }
195
196    //type #4 d): SAM type ([Iterable], Iterable<String>, {})
197    void methodQR(QR qr) {
198        System.out.println("methodQR(): SAM type interface QR object instantiated: " + qr);
199        System.out.println("Iterable returned: " + qr.m(new SQLException()));
200    }
201
202    //type #4 d): SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException})
203    void methodUV(UV uv) {
204        System.out.println("methodUV(): SAM type interface UV object instantiated: " + uv);
205        try{
206            System.out.println("result returned: " + uv.foo(strs));
207        }catch(EOFException e){
208            System.out.println(e.getMessage());
209        }catch(SQLTransientException ex){
210            System.out.println(ex.getMessage());
211        }
212    }
213
214    //type #4 d): SAM type ([List], List<String>/List, {EOFException, SQLTransientException})
215    void methodUVW(UVW uvw) {
216        System.out.println("methodUVW(): SAM type interface UVW object instantiated: " + uvw);
217        try{
218            System.out.println("passing List<String>: " + uvw.foo(strs));
219            System.out.println("passing List: " + uvw.foo(new ArrayList()));
220        }catch(EOFException e){
221            System.out.println(e.getMessage());
222        }catch(SQLTransientException ex){
223            System.out.println(ex.getMessage());
224        }
225    }
226}
227