samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:03:37 +0100
changeset 310 fba31e9504a1
parent 308 7f38f014244c
permissions -rw-r--r--
QueryException interactive example
     1 
     2 package org.apidesign.exceptions.trycatchredo;
     3 
     4 import java.io.ByteArrayInputStream;
     5 import java.io.ByteArrayOutputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.OutputStream;
     9 import java.net.MalformedURLException;
    10 import java.net.URL;
    11 import java.net.URLConnection;
    12 import java.net.URLStreamHandler;
    13 import java.net.URLStreamHandlerFactory;
    14 import java.util.HashMap;
    15 import java.util.Map;
    16 import junit.framework.Assert;
    17 
    18 /**
    19  *
    20  * @author Jaroslav Tulach
    21  */
    22 public final class MemoryURL extends URLStreamHandler {
    23     private MemoryURL() {
    24     }
    25 
    26     static void initialize() {
    27     }
    28     static {
    29         class F implements URLStreamHandlerFactory {
    30             public URLStreamHandler createURLStreamHandler(String protocol) {
    31                 if (protocol.startsWith("memory")) {
    32                     return new MemoryURL();
    33                 }
    34                 return null;
    35             }
    36         }
    37         F f = new F();
    38         URL.setURLStreamHandlerFactory(f);
    39     }
    40     
    41     private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
    42     private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
    43     public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
    44         contents.put(u, new ByteArrayInputStream(content.getBytes()));
    45         outputs.put(u, out);
    46     }
    47     
    48     public static String getOutputForURL(String u) {
    49         OutputStream out = outputs.get(u);
    50         Assert.assertNotNull("No output for " + u, out);
    51         return out.toString();
    52     }
    53     
    54     protected URLConnection openConnection(URL u) throws IOException {
    55         return new MC(u);
    56     }
    57     
    58     private static final class MC extends URLConnection {
    59         private InputStream values;
    60         private OutputStream out;
    61         
    62         public MC(URL u) {
    63             super(u);
    64             out = outputs.get(u.toExternalForm());
    65             if (out == null) {
    66                 out = new ByteArrayOutputStream();
    67                 outputs.put(u.toExternalForm(), out);
    68             }
    69         }
    70 
    71         public void connect() throws IOException {
    72             if (values != null) {
    73                 return;
    74             }
    75             values = contents.remove(url.toExternalForm());
    76             if (values == null) {
    77                 throw new IOException("No such content: " + url);
    78             }
    79         }
    80 
    81         @Override
    82         public InputStream getInputStream() throws IOException {
    83             connect();
    84             return values;
    85         }
    86 
    87         @Override
    88         public OutputStream getOutputStream() throws IOException {
    89             if (out == null) {
    90                 out = new ByteArrayOutputStream();
    91             }
    92             return out;
    93         }
    94     }
    95 }