test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java

changeset 1415
01c9d4161882
child 1510
7873d37f5b37
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/lambdaExpression/SamConversionComboTest.java	Sat Nov 17 19:01:03 2012 +0000
     1.3 @@ -0,0 +1,277 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *   Test SAM conversion of lambda expressions in combinations of different contexts,
    1.32 + *           lambda body types(statement/expression), explict/implicit target type etc, to verify
    1.33 + *           SAM conversion being conducted successfully as expected.
    1.34 + */
    1.35 +
    1.36 +import com.sun.source.util.JavacTask;
    1.37 +import java.net.URI;
    1.38 +import java.util.Arrays;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.JavaCompiler;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.SimpleJavaFileObject;
    1.43 +import javax.tools.ToolProvider;
    1.44 +
    1.45 +public class SamConversionComboTest {
    1.46 +
    1.47 +    enum FInterface {
    1.48 +        A("A", "interface A { Integer m(int i); }"),
    1.49 +        B("B", "interface B { int m(Integer i); }"),
    1.50 +        C("C", "interface C { int m(Integer i) throws Exception; }");
    1.51 +
    1.52 +        String interfaceType;
    1.53 +        String interfaceDef;
    1.54 +
    1.55 +        FInterface(String interfaceType, String interfaceDef) {
    1.56 +            this.interfaceType = interfaceType;
    1.57 +            this.interfaceDef = interfaceDef;
    1.58 +        }
    1.59 +
    1.60 +        String getParameterType() {
    1.61 +            switch(this) {
    1.62 +            case A:
    1.63 +                return "int";
    1.64 +            case B:
    1.65 +            case C:
    1.66 +                return "Integer";
    1.67 +            default:
    1.68 +                return null;
    1.69 +            }
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    enum Context {
    1.74 +        ASSIGNMENT("#FType f = #LBody;"),
    1.75 +        METHOD_CALL("void method1(#FType f) { }\n" +
    1.76 +                    "    void method2() {\n" +
    1.77 +                    "        method1(#LBody);\n" +
    1.78 +                    "    }"),
    1.79 +        CONSTRUCTOR("X x = new X(#LBody);"),
    1.80 +        RETURN_OF_METHOD("#FType method1() {\n" +
    1.81 +                         "    return #LBody;\n" +
    1.82 +                         "}"),
    1.83 +        ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (#FType)#LBody};"),
    1.84 +        LAMBDA_BODY("#FType f = n -> ((#FType)#LBody).m(n);"),
    1.85 +        CAST("void test() throws Exception { int n = ((#FType)#LBody).m(1); }"),
    1.86 +        CONDITIONAL_EXPRESSION("#FType f = 2 > 1 ? #LBody : null;");
    1.87 +
    1.88 +        String context;
    1.89 +
    1.90 +        Context(String context) {
    1.91 +            this.context = context;
    1.92 +        }
    1.93 +
    1.94 +        String getContext(FInterface f, LambdaKind lk, LambdaBody lb, ReturnValue rv) {
    1.95 +            return context.replace("#FType", f.interfaceType).replace("#LBody", lb.getLambdaBody(f, lk, rv));
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    enum LambdaKind {
   1.100 +        EXPRESSION("#VAL"),
   1.101 +        STATEMENT("{return #VAL;}"),
   1.102 +        EXCEPTION_STMT("{throw new Exception();}");
   1.103 +
   1.104 +        String stmt;
   1.105 +
   1.106 +        LambdaKind(String stmt) {
   1.107 +            this.stmt = stmt;
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    enum ReturnValue {
   1.112 +        INT("i + 1"),
   1.113 +        INTEGER("new Integer(i+1)"),
   1.114 +        INT2("i.intValue() + 1"),
   1.115 +        STRING("i + \"\""),
   1.116 +        DOUBLE("i * 1.0");
   1.117 +
   1.118 +        String rValue;
   1.119 +
   1.120 +        ReturnValue(String rValue) {
   1.121 +            this.rValue = rValue;
   1.122 +        }
   1.123 +    }
   1.124 +
   1.125 +    enum LambdaBody {
   1.126 +        IMPLICIT("i -> #RET"),//type inferred
   1.127 +        EXPLICIT("(#Type i) -> #RET");//explicit type
   1.128 +
   1.129 +        String bodyStr;
   1.130 +
   1.131 +        LambdaBody(String bodyStr) {
   1.132 +            this.bodyStr = bodyStr;
   1.133 +        }
   1.134 +
   1.135 +        String getLambdaBody(FInterface fi, LambdaKind lk, ReturnValue rv) {
   1.136 +            return bodyStr.replace("#Type", fi.getParameterType()).replace("#RET", lk.stmt.replace("#VAL", rv.rValue));
   1.137 +        }
   1.138 +    }
   1.139 +
   1.140 +    boolean checkSamConversion() {
   1.141 +        if(lambdaKind != LambdaKind.EXCEPTION_STMT && (returnValue == ReturnValue.DOUBLE || returnValue == ReturnValue.STRING)) //return type mismatch
   1.142 +            return false;
   1.143 +        if(context != Context.CONSTRUCTOR) {//context other than construcotr argument
   1.144 +            if(fInterface != FInterface.C && lambdaKind == LambdaKind.EXCEPTION_STMT)
   1.145 +                return false;
   1.146 +            if(fInterface == FInterface.A && returnValue == ReturnValue.INT2)
   1.147 +                return false;
   1.148 +        }
   1.149 +        else { //constructor argument context
   1.150 +            //match X(A a) or X(B b) or X(C c)
   1.151 +            if (lambdaKind == LambdaKind.EXCEPTION_STMT) {
   1.152 +                return false; //ambiguous target type
   1.153 +            }
   1.154 +            else if(lambdaBody == LambdaBody.IMPLICIT) {
   1.155 +                if(returnValue != ReturnValue.INTEGER) //ambiguous target type
   1.156 +                    return false;
   1.157 +            }
   1.158 +            else { //explicit parameter type
   1.159 +                if(fInterface.getParameterType().equals("Integer")) //ambiguous target type
   1.160 +                //e.g. X x = new X((Integer i) -> i + 1);
   1.161 +                    return false;
   1.162 +                if(returnValue == ReturnValue.INT2)
   1.163 +                //e.g. X x = new X(int i -> i.intValue() + 1);
   1.164 +                    return false;
   1.165 +            }
   1.166 +        }
   1.167 +        return true;
   1.168 +    }
   1.169 +
   1.170 +    SourceFile samSourceFile = new SourceFile("FInterface.java", "#C") {
   1.171 +        public String toString() {
   1.172 +            String interfaces = "";
   1.173 +            for(FInterface fi : FInterface.values())
   1.174 +                interfaces += fi.interfaceDef + "\n";
   1.175 +            return template.replace("#C", interfaces);
   1.176 +        }
   1.177 +    };
   1.178 +
   1.179 +    String clientTemplate = "class Client {\n" +
   1.180 +                            "    #Context\n" +
   1.181 +                            "}\n\n" +
   1.182 +
   1.183 +                            "class X {\n" +
   1.184 +                            "    int value = 0;\n\n" +
   1.185 +
   1.186 +                            "    X(A a) {\n" +
   1.187 +                            "        value = a.m(6);\n" +
   1.188 +                            "    }\n\n" +
   1.189 +
   1.190 +                            "    X(B b) {\n" +
   1.191 +                            "        value = b.m(7);\n" +
   1.192 +                            "    }\n\n" +
   1.193 +
   1.194 +                            "    X(C c) {\n" +
   1.195 +                            "        try {\n" +
   1.196 +                            "            value = c.m(8);\n" +
   1.197 +                            "        } catch (Exception e){}\n" +
   1.198 +                            "    }\n" +
   1.199 +                            "}";
   1.200 +    SourceFile clientSourceFile = new SourceFile("Client.java", clientTemplate) {
   1.201 +        public String toString() {
   1.202 +            return template.replace("#Context", context.getContext(fInterface, lambdaKind, lambdaBody, returnValue));
   1.203 +        }
   1.204 +    };
   1.205 +
   1.206 +    void test() throws Exception {
   1.207 +        System.out.println("\n====================================");
   1.208 +        System.out.println(fInterface + ", " +  context + ", " + lambdaKind + ", " + lambdaBody + ", " + returnValue);
   1.209 +        System.out.println(samSourceFile + "\n");
   1.210 +        String clientFileStr = clientSourceFile.toString();
   1.211 +        System.out.println(clientFileStr.substring(0, clientFileStr.indexOf("\n\n")));
   1.212 +
   1.213 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.214 +        DiagnosticChecker dc = new DiagnosticChecker();
   1.215 +        JavacTask ct = (JavacTask)tool.getTask(null, null, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
   1.216 +        ct.analyze();
   1.217 +        if (dc.errorFound == checkSamConversion()) {
   1.218 +            throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile);
   1.219 +        }
   1.220 +        count++;
   1.221 +    }
   1.222 +
   1.223 +    abstract class SourceFile extends SimpleJavaFileObject {
   1.224 +
   1.225 +        protected String template;
   1.226 +
   1.227 +        public SourceFile(String filename, String template) {
   1.228 +            super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
   1.229 +            this.template = template;
   1.230 +        }
   1.231 +
   1.232 +        @Override
   1.233 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.234 +            return toString();
   1.235 +        }
   1.236 +
   1.237 +        public abstract String toString();
   1.238 +    }
   1.239 +
   1.240 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.241 +
   1.242 +        boolean errorFound = false;
   1.243 +
   1.244 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.245 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.246 +                errorFound = true;
   1.247 +            }
   1.248 +        }
   1.249 +    }
   1.250 +
   1.251 +    FInterface fInterface;
   1.252 +    Context context;
   1.253 +    LambdaBody lambdaBody;
   1.254 +    LambdaKind lambdaKind;
   1.255 +    ReturnValue returnValue;
   1.256 +    static int count = 0;
   1.257 +
   1.258 +    SamConversionComboTest(FInterface f, Context c, LambdaBody lb, LambdaKind lk, ReturnValue rv) {
   1.259 +        fInterface = f;
   1.260 +        context = c;
   1.261 +        lambdaKind = lk;
   1.262 +        lambdaBody = lb;
   1.263 +        returnValue = rv;
   1.264 +    }
   1.265 +
   1.266 +    public static void main(String[] args) throws Exception {
   1.267 +        for(Context ct : Context.values()) {
   1.268 +            for (FInterface fi : FInterface.values()) {
   1.269 +                for (LambdaKind lk: LambdaKind.values()) {
   1.270 +                    for (LambdaBody lb : LambdaBody.values()) {
   1.271 +                        for(ReturnValue rv : ReturnValue.values()) {
   1.272 +                            new SamConversionComboTest(fi, ct, lb, lk, rv).test();
   1.273 +                        }
   1.274 +                    }
   1.275 +                }
   1.276 +            }
   1.277 +        }
   1.278 +        System.out.println("total tests: " + count);
   1.279 +    }
   1.280 +}

mercurial