# HG changeset patch # User Jaroslav Tulach # Date 1233491224 -3600 # Node ID 7f38f014244cc34404e2bfff98123090a35f082a # Parent 52f941f090cdb73ea893adc6d2a057a821269bda Simple write test is OK diff -r 52f941f090cd -r 7f38f014244c samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java Sun Feb 01 13:27:04 2009 +0100 @@ -0,0 +1,17 @@ +package org.apidesign.exceptions.trycatchredo; + +import java.net.URL; +import javax.swing.Action; + +/** + * + * @author Jaroslav Tulach + */ +public final class IOManager { + IOManager() { + } + + public static Action createSaveAction(URL where, CharSequence what) { + return new SaveAction(where, what); + } +} diff -r 52f941f090cd -r 7f38f014244c samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 13:27:04 2009 +0100 @@ -0,0 +1,44 @@ +package org.apidesign.exceptions.trycatchredo; + + +import java.awt.EventQueue; +import java.awt.event.ActionEvent; +import java.net.URL; +import javax.swing.Action; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class IOManagerTest { + + public IOManagerTest() { + } + + @Before + public void setUp() { + MemoryURL.initialize(); + } + + @After + public void tearDown() { + } + + @Test + public void simpleWrite() throws Exception { + URL u = new URL("memory://simpleWrite.txt"); + MemoryURL.registerURL(u.toExternalForm(), "", null); + final Action a = IOManager.createSaveAction(u, "Ahoj"); + EventQueue.invokeAndWait(new Runnable() { + public void run() { + a.actionPerformed(new ActionEvent(this, 0, "")); + } + }); + byte[] out = MemoryURL.getOutputForURL(u.toExternalForm()); + assertEquals("Four bytes", 4, out.length); + } +} \ No newline at end of file diff -r 52f941f090cd -r 7f38f014244c samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java Sun Feb 01 13:27:04 2009 +0100 @@ -0,0 +1,93 @@ + +package org.apidesign.exceptions.trycatchredo; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; +import java.util.HashMap; +import java.util.Map; +import junit.framework.Assert; + +/** + * + * @author Jaroslav Tulach + */ +public final class MemoryURL extends URLStreamHandler { + private MemoryURL() { + } + + static void initialize() { + } + static { + class F implements URLStreamHandlerFactory { + public URLStreamHandler createURLStreamHandler(String protocol) { + if (protocol.startsWith("memory")) { + return new MemoryURL(); + } + return null; + } + } + F f = new F(); + URL.setURLStreamHandlerFactory(f); + } + + private static Map contents = new HashMap(); + private static Map outputs = new HashMap(); + public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException { + contents.put(u, new ByteArrayInputStream(content.getBytes())); + if (out != null) { + new MC(new URL(u)).out = out; + } + } + + public static byte[] getOutputForURL(String u) { + MC out = outputs.get(u); + Assert.assertNotNull("No output for " + u, out); + return out.out.toByteArray(); + } + + protected URLConnection openConnection(URL u) throws IOException { + return new MC(u); + } + + private static final class MC extends URLConnection { + private InputStream values; + private ByteArrayOutputStream out; + + public MC(URL u) { + super(u); + outputs.put(u.toExternalForm(), this); + } + + public void connect() throws IOException { + if (values != null) { + return; + } + values = contents.remove(url.toExternalForm()); + if (values == null) { + throw new IOException("No such content: " + url); + } + } + + @Override + public InputStream getInputStream() throws IOException { + connect(); + return values; + } + + @Override + public OutputStream getOutputStream() throws IOException { + if (out == null) { + out = new ByteArrayOutputStream(); + } + return out; + } + } +}