J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6422914 |
| 27 | * @summary change httpserver exception printouts |
| 28 | */ |
| 29 | |
| 30 | import com.sun.net.httpserver.*; |
| 31 | |
| 32 | import java.util.*; |
| 33 | import java.util.concurrent.*; |
| 34 | import java.util.logging.*; |
| 35 | import java.io.*; |
| 36 | import java.net.*; |
| 37 | import java.security.*; |
| 38 | import java.security.cert.*; |
| 39 | import javax.net.ssl.*; |
| 40 | |
| 41 | public class TestLogging extends Test { |
| 42 | |
| 43 | public static void main (String[] args) throws Exception { |
| 44 | HttpServer s1 = null; |
| 45 | ExecutorService executor=null; |
| 46 | |
| 47 | try { |
| 48 | System.out.print ("Test9: "); |
| 49 | String root = System.getProperty ("test.src")+ "/docs"; |
| 50 | InetSocketAddress addr = new InetSocketAddress (0); |
| 51 | Logger logger = Logger.getLogger ("com.sun.net.httpserver"); |
| 52 | logger.setLevel (Level.ALL); |
| 53 | Handler h1 = new ConsoleHandler (); |
| 54 | h1.setLevel (Level.ALL); |
| 55 | logger.addHandler (h1); |
| 56 | s1 = HttpServer.create (addr, 0); |
| 57 | logger.info (root); |
| 58 | HttpHandler h = new FileServerHandler (root); |
| 59 | HttpContext c1 = s1.createContext ("/test1", h); |
| 60 | executor = Executors.newCachedThreadPool(); |
| 61 | s1.setExecutor (executor); |
| 62 | s1.start(); |
| 63 | |
| 64 | int p1 = s1.getAddress().getPort(); |
| 65 | |
| 66 | URL url = new URL ("http://127.0.0.1:"+p1+"/test1/smallfile.txt"); |
| 67 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); |
| 68 | InputStream is = urlc.getInputStream(); |
| 69 | while (is.read() != -1) ; |
| 70 | is.close(); |
| 71 | |
| 72 | url = new URL ("http://127.0.0.1:"+p1+"/test1/doesntexist.txt"); |
| 73 | urlc = (HttpURLConnection)url.openConnection(); |
| 74 | try { |
| 75 | is = urlc.getInputStream(); |
| 76 | while (is.read() != -1) ; |
| 77 | is.close(); |
| 78 | } catch (IOException e) { |
| 79 | System.out.println ("caught expected exception"); |
| 80 | } |
| 81 | |
| 82 | Socket s = new Socket ("127.0.0.1", p1); |
| 83 | OutputStream os = s.getOutputStream(); |
| 84 | //os.write ("GET xxx HTTP/1.1\r\n".getBytes()); |
| 85 | os.write ("HELLO WORLD\r\n".getBytes()); |
| 86 | is = s.getInputStream(); |
| 87 | while (is.read() != -1) ; |
| 88 | os.close(); is.close(); s.close(); |
| 89 | System.out.println ("OK"); |
| 90 | } finally { |
| 91 | delay(); |
| 92 | s1.stop(2); |
| 93 | executor.shutdown (); |
| 94 | } |
| 95 | } |
| 96 | } |