1/*
2 * Copyright (c) 2001, 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 4423074
27 * @summary Need to rebase all the duplicated classes from Merlin.
28 *          This test will check out http POST
29 * @modules java.base/sun.net.www.protocol.https java.base/com.sun.net.ssl.internal.www.protocol.https
30 */
31import java.net.*;
32import java.util.*;
33import java.lang.reflect.*;
34import sun.net.www.protocol.https.HttpsURLConnectionImpl;
35import com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl;
36
37public class CheckMethods {
38    static boolean debug = false;
39    static class MethodSignature {
40        String name;
41        Class[] paramTypes;
42        MethodSignature(String name, Class[] paramTypes) {
43            this.name = name;
44            this.paramTypes = paramTypes;
45        }
46
47        public boolean equals(Object obj) {
48            if (debug)
49                System.out.println("comparing "+this +" against: "+obj);
50            if (!(obj instanceof MethodSignature)) {
51                if (debug)
52                    System.out.println(false);
53                return false;
54            }
55            MethodSignature ms = (MethodSignature) obj;
56            Class[] types = ms.paramTypes;
57            try {
58                for (int i = 0; i < types.length; i++) {
59                    if (!types[i].equals(paramTypes[i])) {
60                        if (debug)
61                            System.out.println(false);
62                        return false;
63                    }
64                }
65            } catch (Exception e) {
66                if (debug)
67                    System.out.println(false);
68                return false;
69            }
70            boolean result = this.name.equals(ms.name);
71            if (debug)
72                System.out.println(result);
73            return result;
74        }
75
76        public String toString() {
77            StringBuffer sb = new StringBuffer(name+"(");
78            for (int i = 0; i < paramTypes.length; i++) {
79                sb.append(paramTypes[i].getName()+",");
80                if (i == (paramTypes.length -1))
81                    sb.deleteCharAt(sb.length()-1);
82            }
83            sb.append(")");
84            return sb.toString();
85        }
86    }
87
88    // check HttpsURLConnectionImpl and HttpsURLConnectionOldImpl
89    // contain all public and protected methods defined in
90    // HttpURLConnection and URLConnection
91    public static void main(String[] args) {
92        ArrayList allMethods = new ArrayList(
93            Arrays.asList(HttpURLConnection.class.getDeclaredMethods()));
94        allMethods.addAll(Arrays.asList(URLConnection.class.getDeclaredMethods()));
95        ArrayList allMethodSignatures = new ArrayList();
96        for (Iterator itr = allMethods.iterator(); itr.hasNext(); ) {
97            Method m = (Method)itr.next();
98            // don't include static and private methods
99            if (!Modifier.isStatic(m.getModifiers()) &&
100                (Modifier.isPublic(m.getModifiers()) ||
101                 Modifier.isProtected(m.getModifiers()))) {
102                allMethodSignatures.add(
103                    new MethodSignature(m.getName(), m.getParameterTypes()));
104            }
105        }
106
107        // testing HttpsURLConnectionImpl
108        List httpsMethods =
109            Arrays.asList(HttpsURLConnectionImpl.class.getDeclaredMethods());
110
111        ArrayList httpsMethodSignatures = new ArrayList();
112        for (Iterator itr = httpsMethods.iterator(); itr.hasNext(); ) {
113            Method m = (Method)itr.next();
114            if (!Modifier.isStatic(m.getModifiers())) {
115                httpsMethodSignatures.add(
116                 new MethodSignature(m.getName(), m.getParameterTypes()));
117            }
118        }
119
120        if (!httpsMethodSignatures.containsAll(allMethodSignatures)) {
121            throw new RuntimeException("Method definition test failed on HttpsURLConnectionImpl");
122        }
123
124        // testing HttpsURLConnectionOldImpl
125        List httpsOldMethods =
126            Arrays.asList(HttpsURLConnectionOldImpl.class.getDeclaredMethods());
127
128        ArrayList httpsOldMethodSignatures = new ArrayList();
129        for (Iterator itr = httpsOldMethods.iterator(); itr.hasNext(); ) {
130            Method m = (Method)itr.next();
131            if (!Modifier.isStatic(m.getModifiers())) {
132                httpsOldMethodSignatures.add(
133                 new MethodSignature(m.getName(), m.getParameterTypes()));
134            }
135        }
136
137        if (!httpsOldMethodSignatures.containsAll(allMethodSignatures)) {
138            throw new RuntimeException("Method definition test failed" +
139                                       " on HttpsURLConnectionOldImpl");
140        }
141
142        // testing for non static public field
143        ArrayList allFields = new ArrayList(
144            Arrays.asList(URLConnection.class.getFields()));
145        allFields.addAll(Arrays.asList(HttpURLConnection.class.getFields()));
146
147        for (Iterator itr = allFields.iterator(); itr.hasNext(); ) {
148            Field f = (Field) itr.next();
149            if (!Modifier.isStatic(f.getModifiers())) {
150                throw new RuntimeException("Non static Public fields" +
151                                           " declared in superclasses");
152            }
153        }
154    }
155}
156