1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.net;
18
19import java.lang.reflect.Field;
20import java.net.URL;
21import java.net.URLConnection;
22import java.net.URLStreamHandler;
23import java.net.URLStreamHandlerFactory;
24import junit.framework.TestCase;
25import libcore.java.net.customstreamhandler.http.Handler;
26
27public final class URLStreamHandlerFactoryTest extends TestCase {
28    private URLStreamHandlerFactory oldFactory;
29    private Field factoryField;
30    private boolean isCreateURLStreamHandlerCalled;
31
32    public void setUp() throws IllegalAccessException {
33        for (Field field : URL.class.getDeclaredFields()) {
34            if (URLStreamHandlerFactory.class.equals(field.getType())) {
35                assertNull("URL declares multiple URLStreamHandlerFactory fields", factoryField);
36                factoryField = field;
37                factoryField.setAccessible(true);
38                oldFactory = (URLStreamHandlerFactory) factoryField.get(null);
39                return;
40            }
41        }
42
43        fail("URL does not declare a URLStreamHandlerFactory field");
44    }
45
46    public void tearDown() throws IllegalAccessException {
47        factoryField.set(null, null);
48        URL.setURLStreamHandlerFactory(oldFactory);
49    }
50
51    public void testCreateURLStreamHandler() throws Exception {
52        TestURLStreamHandlerFactory shf = new TestURLStreamHandlerFactory();
53        assertFalse(isCreateURLStreamHandlerCalled);
54
55        URL.setURLStreamHandlerFactory(shf);
56        URL url = new URL("http://android.com/");
57
58        URLConnection connection = url.openConnection();
59        assertTrue(connection instanceof Handler.HandlerURLConnection);
60
61        try {
62            URL.setURLStreamHandlerFactory(shf);
63            fail();
64        } catch (Error expected) {
65        }
66
67        try {
68            URL.setURLStreamHandlerFactory(null);
69            fail();
70        } catch (Error expected) {
71        }
72    }
73
74    public void testInstallCustomProtocolHandler() throws Exception {
75        // clear cached protocol handlers if they exist
76        factoryField.set(null, null);
77        URL.setURLStreamHandlerFactory(oldFactory);
78
79        try {
80            System.setProperty("java.protocol.handler.pkgs", getHandlerPackageName());
81            URLConnection connection = new URL("http://android.com/").openConnection();
82            assertTrue(connection instanceof Handler.HandlerURLConnection);
83        } finally {
84            System.clearProperty("java.protocol.handler.pkgs");
85        }
86    }
87
88    public void testFirstUseIsCached() throws Exception {
89        // clear cached protocol handlers if they exist
90        factoryField.set(null, null);
91        URL.setURLStreamHandlerFactory(oldFactory);
92
93        // creating a connection should use the platform's default stream handler
94        URLConnection connection1 = new URL("http://android.com/").openConnection();
95        assertFalse(connection1 instanceof Handler.HandlerURLConnection);
96
97        try {
98            // set the property and get another connection. The property should not be honored
99            System.setProperty("java.protocol.handler.pkgs", getHandlerPackageName());
100            URLConnection connection2 = new URL("http://android.com/").openConnection();
101            assertFalse(connection2 instanceof Handler.HandlerURLConnection);
102        } finally {
103            System.clearProperty("java.protocol.handler.pkgs");
104        }
105    }
106
107    private String getHandlerPackageName() {
108        String className = Handler.class.getName();
109        return className.substring(0, className.indexOf(".http.Handler"));
110    }
111
112    class TestURLStreamHandlerFactory implements URLStreamHandlerFactory {
113        @Override public URLStreamHandler createURLStreamHandler(String protocol) {
114            isCreateURLStreamHandlerCalled = true;
115            return new Handler();
116        }
117    }
118}
119