1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java Sun Feb 01 13:27:04 2009 +0100
1.3 @@ -0,0 +1,17 @@
1.4 +package org.apidesign.exceptions.trycatchredo;
1.5 +
1.6 +import java.net.URL;
1.7 +import javax.swing.Action;
1.8 +
1.9 +/**
1.10 + *
1.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.12 + */
1.13 +public final class IOManager {
1.14 + IOManager() {
1.15 + }
1.16 +
1.17 + public static Action createSaveAction(URL where, CharSequence what) {
1.18 + return new SaveAction(where, what);
1.19 + }
1.20 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 13:27:04 2009 +0100
2.3 @@ -0,0 +1,44 @@
2.4 +package org.apidesign.exceptions.trycatchredo;
2.5 +
2.6 +
2.7 +import java.awt.EventQueue;
2.8 +import java.awt.event.ActionEvent;
2.9 +import java.net.URL;
2.10 +import javax.swing.Action;
2.11 +import org.junit.After;
2.12 +import org.junit.Before;
2.13 +import org.junit.Test;
2.14 +import static org.junit.Assert.*;
2.15 +
2.16 +/**
2.17 + *
2.18 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.19 + */
2.20 +public class IOManagerTest {
2.21 +
2.22 + public IOManagerTest() {
2.23 + }
2.24 +
2.25 + @Before
2.26 + public void setUp() {
2.27 + MemoryURL.initialize();
2.28 + }
2.29 +
2.30 + @After
2.31 + public void tearDown() {
2.32 + }
2.33 +
2.34 + @Test
2.35 + public void simpleWrite() throws Exception {
2.36 + URL u = new URL("memory://simpleWrite.txt");
2.37 + MemoryURL.registerURL(u.toExternalForm(), "", null);
2.38 + final Action a = IOManager.createSaveAction(u, "Ahoj");
2.39 + EventQueue.invokeAndWait(new Runnable() {
2.40 + public void run() {
2.41 + a.actionPerformed(new ActionEvent(this, 0, ""));
2.42 + }
2.43 + });
2.44 + byte[] out = MemoryURL.getOutputForURL(u.toExternalForm());
2.45 + assertEquals("Four bytes", 4, out.length);
2.46 + }
2.47 +}
2.48 \ No newline at end of file
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java Sun Feb 01 13:27:04 2009 +0100
3.3 @@ -0,0 +1,93 @@
3.4 +
3.5 +package org.apidesign.exceptions.trycatchredo;
3.6 +
3.7 +import java.io.ByteArrayInputStream;
3.8 +import java.io.ByteArrayOutputStream;
3.9 +import java.io.IOException;
3.10 +import java.io.InputStream;
3.11 +import java.io.OutputStream;
3.12 +import java.net.MalformedURLException;
3.13 +import java.net.URL;
3.14 +import java.net.URLConnection;
3.15 +import java.net.URLStreamHandler;
3.16 +import java.net.URLStreamHandlerFactory;
3.17 +import java.util.HashMap;
3.18 +import java.util.Map;
3.19 +import junit.framework.Assert;
3.20 +
3.21 +/**
3.22 + *
3.23 + * @author Jaroslav Tulach
3.24 + */
3.25 +public final class MemoryURL extends URLStreamHandler {
3.26 + private MemoryURL() {
3.27 + }
3.28 +
3.29 + static void initialize() {
3.30 + }
3.31 + static {
3.32 + class F implements URLStreamHandlerFactory {
3.33 + public URLStreamHandler createURLStreamHandler(String protocol) {
3.34 + if (protocol.startsWith("memory")) {
3.35 + return new MemoryURL();
3.36 + }
3.37 + return null;
3.38 + }
3.39 + }
3.40 + F f = new F();
3.41 + URL.setURLStreamHandlerFactory(f);
3.42 + }
3.43 +
3.44 + private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
3.45 + private static Map<String,MC> outputs = new HashMap<String,MC>();
3.46 + public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException {
3.47 + contents.put(u, new ByteArrayInputStream(content.getBytes()));
3.48 + if (out != null) {
3.49 + new MC(new URL(u)).out = out;
3.50 + }
3.51 + }
3.52 +
3.53 + public static byte[] getOutputForURL(String u) {
3.54 + MC out = outputs.get(u);
3.55 + Assert.assertNotNull("No output for " + u, out);
3.56 + return out.out.toByteArray();
3.57 + }
3.58 +
3.59 + protected URLConnection openConnection(URL u) throws IOException {
3.60 + return new MC(u);
3.61 + }
3.62 +
3.63 + private static final class MC extends URLConnection {
3.64 + private InputStream values;
3.65 + private ByteArrayOutputStream out;
3.66 +
3.67 + public MC(URL u) {
3.68 + super(u);
3.69 + outputs.put(u.toExternalForm(), this);
3.70 + }
3.71 +
3.72 + public void connect() throws IOException {
3.73 + if (values != null) {
3.74 + return;
3.75 + }
3.76 + values = contents.remove(url.toExternalForm());
3.77 + if (values == null) {
3.78 + throw new IOException("No such content: " + url);
3.79 + }
3.80 + }
3.81 +
3.82 + @Override
3.83 + public InputStream getInputStream() throws IOException {
3.84 + connect();
3.85 + return values;
3.86 + }
3.87 +
3.88 + @Override
3.89 + public OutputStream getOutputStream() throws IOException {
3.90 + if (out == null) {
3.91 + out = new ByteArrayOutputStream();
3.92 + }
3.93 + return out;
3.94 + }
3.95 + }
3.96 +}