CheckMethods.java revision 13545:eee1ced1d8e7
1/*
2 * Copyright (c) 2003, 2007, 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 4791676
27 * @summary various pass through methods missing in SSLSocketImpl
28 */
29import java.net.*;
30import java.util.*;
31import java.lang.reflect.*;
32
33public class CheckMethods {
34    static boolean debug = false;
35    static class MethodSignature {
36        String name;
37        Class[] paramTypes;
38        MethodSignature(String name, Class[] paramTypes) {
39            this.name = name;
40            this.paramTypes = paramTypes;
41        }
42
43        public boolean equals(Object obj) {
44            if (debug) {
45                System.out.println("comparing " + this + " against: " + obj);
46            }
47            if (!(obj instanceof MethodSignature)) {
48                if (debug)
49                    System.out.println(false);
50                return false;
51            }
52            MethodSignature ms = (MethodSignature) obj;
53            Class[] types = ms.paramTypes;
54            try {
55                for (int i = 0; i < types.length; i++) {
56                    if (!types[i].equals(paramTypes[i])) {
57                        if (debug)
58                            System.out.println(false);
59                        return false;
60                    }
61                }
62            } catch (Exception e) {
63                if (debug)
64                    System.out.println(false);
65                return false;
66            }
67            boolean result = this.name.equals(ms.name);
68            if (debug)
69                System.out.println(result);
70            return result;
71        }
72
73        public String toString() {
74            StringBuffer sb = new StringBuffer(name + "(");
75            for (int i = 0; i < paramTypes.length; i++) {
76                sb.append(paramTypes[i].getName() + ",");
77                if (i == (paramTypes.length - 1))
78                    sb.deleteCharAt(sb.length() - 1);
79            }
80            sb.append(")");
81            return sb.toString();
82        }
83    }
84
85    // check that SSLSocket contains all public and protected
86    // methods defined in Socket
87    public static void main(String[] args) throws Exception {
88        ArrayList allMethods = new ArrayList(
89                Arrays.asList(Socket.class.getDeclaredMethods()));
90
91        ArrayList allMethodSignatures = new ArrayList();
92        for (Iterator itr = allMethods.iterator(); itr.hasNext();) {
93            Method m = (Method) itr.next();
94            // don't include static and private methods
95            if (!Modifier.isStatic(m.getModifiers()) &&
96                    (Modifier.isPublic(m.getModifiers()) ||
97                    Modifier.isProtected(m.getModifiers()))) {
98                allMethodSignatures.add( new MethodSignature(m.getName(),
99                        m.getParameterTypes()));
100            }
101        }
102
103        // testing Socket
104        Class sslSI = Class.forName(
105            "sun.security.ssl.SSLSocketImpl");
106        Class baseSSLSI = Class.forName(
107            "sun.security.ssl.BaseSSLSocketImpl");
108
109        ArrayList sslSocketMethods =
110                new ArrayList(Arrays.asList(sslSI.getDeclaredMethods()));
111
112        sslSocketMethods.addAll( new ArrayList(
113                Arrays.asList(baseSSLSI.getDeclaredMethods())));
114
115        ArrayList sslSocketMethodSignatures = new ArrayList();
116        for (Iterator itr = sslSocketMethods.iterator(); itr.hasNext();) {
117            Method m = (Method) itr.next();
118            if (!Modifier.isStatic(m.getModifiers())) {
119                sslSocketMethodSignatures.add(
120                        new MethodSignature(m.getName(),
121                        m.getParameterTypes()));
122            }
123        }
124
125        if (!sslSocketMethodSignatures.containsAll(allMethodSignatures)) {
126            throw new RuntimeException(
127                "Method definition test failed on SSLSocketImpl");
128        }
129
130        // testing for non static public field
131        ArrayList allFields =
132                new ArrayList(Arrays.asList(Socket.class.getFields()));
133
134        for (Iterator itr = allFields.iterator(); itr.hasNext();) {
135            Field f = (Field) itr.next();
136            if (!Modifier.isStatic(f.getModifiers())) {
137                throw new RuntimeException("Non static Public fields" +
138                        " declared in superclasses");
139            }
140        }
141    }
142}
143