Path: blob/master/test/jdk/com/sun/net/httpserver/TestLogging.java
41152 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 642291426* @library /test/lib27* @summary change httpserver exception printouts28* @run main TestLogging29* @run main/othervm -Djava.net.preferIPv6Addresses=true TestLogging30*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.logging.*;37import java.io.*;38import java.net.*;39import java.security.*;40import java.security.cert.*;41import javax.net.ssl.*;42import jdk.test.lib.net.URIBuilder;4344public class TestLogging extends Test {4546public static void main (String[] args) throws Exception {47HttpServer s1 = null;48ExecutorService executor=null;4950try {51System.out.print ("Test9: ");52String root = System.getProperty ("test.src")+ "/docs";53InetAddress loopback = InetAddress.getLoopbackAddress();54InetSocketAddress addr = new InetSocketAddress(loopback, 0);55Logger logger = Logger.getLogger ("com.sun.net.httpserver");56logger.setLevel (Level.ALL);57Handler h1 = new ConsoleHandler ();58h1.setLevel (Level.ALL);59logger.addHandler (h1);60s1 = HttpServer.create (addr, 0);61logger.info (root);62HttpHandler h = new FileServerHandler (root);63HttpContext c1 = s1.createContext ("/test1", h);64executor = Executors.newCachedThreadPool();65s1.setExecutor (executor);66s1.start();6768int p1 = s1.getAddress().getPort();6970URL url = URIBuilder.newBuilder()71.scheme("http")72.loopback()73.port(p1)74.path("/test1/smallfile.txt")75.toURL();76System.out.println("URL: " + url);77HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);78InputStream is = urlc.getInputStream();79while (is.read() != -1) ;80is.close();8182url = URIBuilder.newBuilder()83.scheme("http")84.loopback()85.port(p1)86.path("/test1/doesntexist.txt")87.toURLUnchecked();88System.out.println("URL: " + url);89urlc = (HttpURLConnection)url.openConnection();90try {91is = urlc.getInputStream();92while (is.read() != -1) ;93is.close();94} catch (IOException e) {95System.out.println ("caught expected exception");96}9798Socket s = new Socket (InetAddress.getLoopbackAddress(), p1);99OutputStream os = s.getOutputStream();100//os.write ("GET xxx HTTP/1.1\r\n".getBytes());101os.write ("HELLO WORLD\r\n".getBytes());102is = s.getInputStream();103while (is.read() != -1) ;104os.close(); is.close(); s.close();105System.out.println ("OK");106} finally {107delay();108if (s1 != null)109s1.stop(2);110if (executor != null)111executor.shutdown();112}113}114}115116117