|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8270286 |
| 27 | + * @summary Test for HttpServerProvider::loadProviderFromProperty |
| 28 | + * @run testng/othervm |
| 29 | + * -Dcom.sun.net.httpserver.HttpServerProvider=HttpServerProviderTest$ProviderP |
| 30 | + * HttpServerProviderTest |
| 31 | + * @run testng/othervm |
| 32 | + * -Dcom.sun.net.httpserver.HttpServerProvider=HttpServerProviderTest$ProviderPNPC |
| 33 | + * HttpServerProviderTest |
| 34 | + * @run testng/othervm |
| 35 | + * -Dcom.sun.net.httpserver.HttpServerProvider=HttpServerProviderTest$ProviderNP |
| 36 | + * HttpServerProviderTest |
| 37 | + * @run testng/othervm |
| 38 | + * -Dcom.sun.net.httpserver.HttpServerProvider=HttpServerProviderTest$ProviderT |
| 39 | + * HttpServerProviderTest |
| 40 | + * @run testng/othervm |
| 41 | + * -Dcom.sun.net.httpserver.HttpServerProvider=DoesNotExist |
| 42 | + * HttpServerProviderTest |
| 43 | + */ |
| 44 | + |
| 45 | +import java.lang.reflect.InvocationTargetException; |
| 46 | +import java.net.InetSocketAddress; |
| 47 | +import java.util.ServiceConfigurationError; |
| 48 | +import com.sun.net.httpserver.HttpServer; |
| 49 | +import com.sun.net.httpserver.HttpsServer; |
| 50 | +import com.sun.net.httpserver.spi.HttpServerProvider; |
| 51 | +import org.testng.annotations.Test; |
| 52 | +import static org.testng.Assert.assertEquals; |
| 53 | +import static org.testng.Assert.assertNull; |
| 54 | +import static org.testng.Assert.expectThrows; |
| 55 | + |
| 56 | +public class HttpServerProviderTest { |
| 57 | + public final static String PROPERTY_KEY = "com.sun.net.httpserver.HttpServerProvider"; |
| 58 | + |
| 59 | + @Test |
| 60 | + public void test() throws Exception { |
| 61 | + var provider = System.getProperty(PROPERTY_KEY); |
| 62 | + switch (provider) { |
| 63 | + case "HttpServerProviderTest$ProviderP" -> testPublic(); |
| 64 | + case "HttpServerProviderTest$ProviderPNPC" -> testPublicNonPublicConstructor(); |
| 65 | + case "HttpServerProviderTest$ProviderNP" -> testNonPublic(); |
| 66 | + case "HttpServerProviderTest$ProviderT" -> testThrowingConstructor(); |
| 67 | + default -> testBadData(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void testPublic() throws Exception { |
| 72 | + var n = ProviderP.class.getName(); |
| 73 | + assertEquals(System.getProperty(PROPERTY_KEY), n); |
| 74 | + |
| 75 | + var p = HttpServerProvider.provider(); |
| 76 | + assertNull(p.createHttpServer(null, 0)); |
| 77 | + assertNull(p.createHttpsServer(null, 0)); |
| 78 | + } |
| 79 | + |
| 80 | + private void testPublicNonPublicConstructor() { |
| 81 | + var n = ProviderPNPC.class.getName(); |
| 82 | + assertEquals(System.getProperty(PROPERTY_KEY), n); |
| 83 | + |
| 84 | + var e = expectThrows(ServiceConfigurationError.class, HttpServerProvider::provider); |
| 85 | + assertEquals(e.getClass(), ServiceConfigurationError.class); |
| 86 | + assertEquals(e.getCause().getClass(), IllegalAccessException.class); |
| 87 | + } |
| 88 | + |
| 89 | + private void testNonPublic() { |
| 90 | + var n = ProviderNP.class.getName(); |
| 91 | + assertEquals(System.getProperty(PROPERTY_KEY), n); |
| 92 | + |
| 93 | + var e = expectThrows(ServiceConfigurationError.class, HttpServerProvider::provider); |
| 94 | + assertEquals(e.getClass(), ServiceConfigurationError.class); |
| 95 | + assertEquals(e.getCause().getClass(), IllegalAccessException.class); |
| 96 | + } |
| 97 | + |
| 98 | + private void testThrowingConstructor() { |
| 99 | + var cn = ProviderT.class.getName(); |
| 100 | + assertEquals(System.getProperty(PROPERTY_KEY), cn); |
| 101 | + |
| 102 | + var e = expectThrows(ServiceConfigurationError.class, HttpServerProvider::provider); |
| 103 | + assertEquals(e.getClass(), ServiceConfigurationError.class); |
| 104 | + assertEquals(e.getCause().getClass(), InvocationTargetException.class); |
| 105 | + assertEquals(e.getCause().getCause().getMessage(), "throwing constructor"); |
| 106 | + } |
| 107 | + |
| 108 | + private void testBadData() { |
| 109 | + var cn = "DoesNotExist"; |
| 110 | + assertEquals(System.getProperty(PROPERTY_KEY), cn); |
| 111 | + |
| 112 | + var e = expectThrows(ServiceConfigurationError.class, HttpServerProvider::provider); |
| 113 | + assertEquals(e.getClass(), ServiceConfigurationError.class); |
| 114 | + assertEquals(e.getCause().getClass(), ClassNotFoundException.class); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test provider that is public (P) |
| 119 | + */ |
| 120 | + public static class ProviderP extends HttpServerProvider { |
| 121 | + public ProviderP() { super(); } |
| 122 | + @Override |
| 123 | + public HttpServer createHttpServer(InetSocketAddress addr, int backlog) { return null; } |
| 124 | + @Override |
| 125 | + public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) { return null; } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test provider that is public with a non-public constructor (PNPC) |
| 130 | + */ |
| 131 | + public static class ProviderPNPC extends HttpServerProvider { |
| 132 | + /*package-private*/ ProviderPNPC() { super(); } |
| 133 | + @Override |
| 134 | + public HttpServer createHttpServer(InetSocketAddress addr, int backlog) { return null; } |
| 135 | + @Override |
| 136 | + public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) { return null; } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Test provider that is not public (NP) |
| 141 | + */ |
| 142 | + /*package-private*/ static class ProviderNP extends HttpServerProvider { |
| 143 | + /*package-private*/ ProviderNP() { super(); } |
| 144 | + @Override |
| 145 | + public HttpServer createHttpServer(InetSocketAddress addr, int backlog) { return null; } |
| 146 | + @Override |
| 147 | + public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) { return null; } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test provider with a constructor that throws |
| 152 | + */ |
| 153 | + public static class ProviderT extends HttpServerProvider { |
| 154 | + public ProviderT() { throw new AssertionError("throwing constructor"); } |
| 155 | + @Override |
| 156 | + public HttpServer createHttpServer(InetSocketAddress addr, int backlog) { return null; } |
| 157 | + @Override |
| 158 | + public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) { return null; } |
| 159 | + } |
| 160 | +} |
0 commit comments