Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/TestLogging.java
38855 views
/*1* Copyright (c) 2006, 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* @summary change httpserver exception printouts27*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.util.logging.*;34import java.io.*;35import java.net.*;36import java.security.*;37import java.security.cert.*;38import javax.net.ssl.*;3940public class TestLogging extends Test {4142public static void main (String[] args) throws Exception {43HttpServer s1 = null;44ExecutorService executor=null;4546try {47System.out.print ("Test9: ");48String root = System.getProperty ("test.src")+ "/docs";49InetSocketAddress addr = new InetSocketAddress (0);50Logger logger = Logger.getLogger ("com.sun.net.httpserver");51logger.setLevel (Level.ALL);52Handler h1 = new ConsoleHandler ();53h1.setLevel (Level.ALL);54logger.addHandler (h1);55s1 = HttpServer.create (addr, 0);56logger.info (root);57HttpHandler h = new FileServerHandler (root);58HttpContext c1 = s1.createContext ("/test1", h);59executor = Executors.newCachedThreadPool();60s1.setExecutor (executor);61s1.start();6263int p1 = s1.getAddress().getPort();6465URL url = new URL ("http://127.0.0.1:"+p1+"/test1/smallfile.txt");66HttpURLConnection urlc = (HttpURLConnection)url.openConnection();67InputStream is = urlc.getInputStream();68while (is.read() != -1) ;69is.close();7071url = new URL ("http://127.0.0.1:"+p1+"/test1/doesntexist.txt");72urlc = (HttpURLConnection)url.openConnection();73try {74is = urlc.getInputStream();75while (is.read() != -1) ;76is.close();77} catch (IOException e) {78System.out.println ("caught expected exception");79}8081Socket s = new Socket ("127.0.0.1", p1);82OutputStream os = s.getOutputStream();83//os.write ("GET xxx HTTP/1.1\r\n".getBytes());84os.write ("HELLO WORLD\r\n".getBytes());85is = s.getInputStream();86while (is.read() != -1) ;87os.close(); is.close(); s.close();88System.out.println ("OK");89} finally {90delay();91if (s1 != null)92s1.stop(2);93if (executor != null)94executor.shutdown();95}96}97}9899100