changeset 7517:1dd294588d0c

8005968: Write tests for static methods in interfaces Reviewed-by: mcimadamore Contributed-by: sonali.goel@oracle.com
author mcimadamore
date Fri, 22 Feb 2013 16:45:27 +0000
parents ecc9a27d4b83
children 9df184b39d16
files combo-tests/tests/tools/javac/lambda/StaticMethodTest.java
diffstat 1 files changed, 155 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/combo-tests/tests/tools/javac/lambda/StaticMethodTest.java	Fri Feb 22 16:45:27 2013 +0000
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package tools.javac.lambda;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.testng.annotations.Factory;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+import tools.javac.combo.*;
+
+/**
+ * StaticMethodTest: Test static methods in interfaces by running bytecode 
+ *                   compiled at different times; where a static method is added/declared
+ *                   in one interface and only that interface is recompiled.
+ *
+ *                   Static methods in interface are not inherited and 
+ *                   are not visible to any implementing (extending) classes (interfaces).
+ * @bug    8005968
+ * @author sogoel
+ * 
+ */
+public class StaticMethodTest extends ComboTestBase<StaticMethodTest> {
+    @Factory
+    public static Object[] testStaticMethod() throws Exception {
+        return factory(StaticMethodTest.class);
+    }
+    
+    @DimensionVar("SHAPE") CShapes shapeType;
+    
+    @DimensionVar("ADD") AddType addType;
+        
+    @SourceFile(value="B.java", group="A")
+    String interfaceBModified = "interface B #{SHAPE.B_DECL} { #{ADD} }";
+    
+    @SourceFile(value="B.java", group="B")
+    String interfaceB =         "interface B #{SHAPE.B_DECL} {}";
+    
+    @SourceFile(value="A.java", group="B")
+    String interfaceA =         "interface A { static String m() { return \"A\"; } }";
+    
+    @SourceFile(value="C.java", group="B")
+    String classC = "#{SHAPE.C}";
+    
+    @SourceFile(value="Main.java", group="B")
+    String classMain = "public class Main {\n" +
+                       "    public String main() {\n" +
+                       "        return new C().m();\n" +
+                       "    }\n" +
+                       "}";
+
+    @Override
+    protected boolean shouldRun() { return true; }
+
+    @Override
+    protected void run(Class<?> clazz) throws ReflectiveOperationException {
+        Method m = clazz.getMethod("main");
+        Object obj = clazz.newInstance();
+        String result = null, output = null;
+        try {
+            result = (String) m.invoke(obj);
+        } catch (InvocationTargetException ex) {
+            output = ex.getCause().toString();
+        }
+        assertEquals(output, null);
+        assertEquals(result, "C");
+    }
+
+    /*
+     * In order for class Main to compile in group B, object of class C should have
+     * access to a method m(). Therefore, a method m() is needed in class C.
+     * static m() in interfaces A, B is not visible to C.
+     */
+    static String methodSig = "public String m () { return \"C\"; }"; 
+    enum CShapes implements Template { //shapes of class hierarchy
+        //class C implements interface A, B
+        C_AB("",
+           "class C implements A, B { " + methodSig + " }"), 
+        //class C implements interface B, B extends interface A
+        C_B_A("extends A",
+           "class C implements B { " + methodSig + " }"),
+        //class C implements interface AB, AB extends interface A, B
+        C_I_AB("",
+           "interface AB extends A, B {  }\n" +
+           "class C implements AB { " + methodSig + " }"),
+        //class C implements interface AB, AB extends interface A, B and defines its own static method 
+        C_I_AB2("",
+        "interface AB extends A, B { static String m() { return \"AB\"; } }\n" +
+        "class C implements AB { " + methodSig + " }"),
+        //class C extends Class D implements Interface B
+        C_CI("",
+            "class D { public String m() { return \"D\"; } }\n" +
+            "class C extends D implements B { " + methodSig + " }");
+
+        private final String sB_DECL;
+        private final String sC;
+
+        CShapes(String sB_DECL, String sC) {
+            this.sB_DECL = sB_DECL;
+            this.sC = sC;
+        }
+
+        public String expand(String selector) {
+            switch(selector) {
+                case "B_DECL": return sB_DECL;
+                case "C": return sC;
+                default: return toString();
+            }
+        }
+    }
+
+    enum AddType implements Template { // add by adding static method or abstract interface method
+        ADD("static String m() { return \"B\"; }"),
+        REDECLARE("String m();");
+
+        final String newCode;
+
+        AddType(String str) {
+            newCode = str;
+        }
+
+        public String expand(String selector) {
+            return newCode;
+        }
+    }
+}
+