samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 13:27:04 +0100
changeset 308 7f38f014244c
child 310 fba31e9504a1
permissions -rw-r--r--
Simple write test is OK
     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,MC> outputs = new HashMap<String,MC>();
    43     public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException {
    44         contents.put(u, new ByteArrayInputStream(content.getBytes()));
    45         if (out != null) {
    46             new MC(new URL(u)).out = out;
    47         }
    48     }
    49     
    50     public static byte[] getOutputForURL(String u) {
    51         MC out = outputs.get(u);
    52         Assert.assertNotNull("No output for " + u, out);
    53         return out.out.toByteArray();
    54     }
    55     
    56     protected URLConnection openConnection(URL u) throws IOException {
    57         return new MC(u);
    58     }
    59     
    60     private static final class MC extends URLConnection {
    61         private InputStream values;
    62         private ByteArrayOutputStream out;
    63         
    64         public MC(URL u) {
    65             super(u);
    66             outputs.put(u.toExternalForm(), this);
    67         }
    68 
    69         public void connect() throws IOException {
    70             if (values != null) {
    71                 return;
    72             }
    73             values = contents.remove(url.toExternalForm());
    74             if (values == null) {
    75                 throw new IOException("No such content: " + url);
    76             }
    77         }
    78 
    79         @Override
    80         public InputStream getInputStream() throws IOException {
    81             connect();
    82             return values;
    83         }
    84 
    85         @Override
    86         public OutputStream getOutputStream() throws IOException {
    87             if (out == null) {
    88                 out = new ByteArrayOutputStream();
    89             }
    90             return out;
    91         }
    92     }
    93 }