jtulach@308: package org.apidesign.exceptions.trycatchredo; jtulach@308: jtulach@308: jtulach@308: import java.awt.EventQueue; jtulach@308: import java.awt.event.ActionEvent; jtulach@310: import java.io.ByteArrayOutputStream; jtulach@310: import java.io.IOException; jtulach@310: import java.io.OutputStream; jtulach@308: import java.net.URL; jtulach@308: import javax.swing.Action; jtulach@310: import javax.swing.JOptionPane; jtulach@308: import org.junit.After; jtulach@308: import org.junit.Before; jtulach@308: import org.junit.Test; jtulach@308: import static org.junit.Assert.*; jtulach@308: jtulach@308: /** jtulach@308: * jtulach@308: * @author Jaroslav Tulach jtulach@308: */ jtulach@308: public class IOManagerTest { jtulach@308: jtulach@308: public IOManagerTest() { jtulach@308: } jtulach@308: jtulach@308: @Before jtulach@308: public void setUp() { jtulach@308: MemoryURL.initialize(); jtulach@308: } jtulach@308: jtulach@308: @After jtulach@308: public void tearDown() { jtulach@308: } jtulach@308: jtulach@308: @Test jtulach@308: public void simpleWrite() throws Exception { jtulach@308: URL u = new URL("memory://simpleWrite.txt"); jtulach@308: MemoryURL.registerURL(u.toExternalForm(), "", null); jtulach@310: final Action a = IOManager.createSaveAction(u, "Hello World!"); jtulach@308: EventQueue.invokeAndWait(new Runnable() { jtulach@308: public void run() { jtulach@308: a.actionPerformed(new ActionEvent(this, 0, "")); jtulach@308: } jtulach@308: }); jtulach@310: String out = MemoryURL.getOutputForURL(u.toExternalForm()); jtulach@310: assertEquals("Hello World!", out); jtulach@308: } jtulach@310: jtulach@310: @Test jtulach@310: public void writeWithAQuestion() throws Exception { jtulach@310: URL u = new URL("memory://queryEncoding.txt"); jtulach@310: jtulach@310: MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream()); jtulach@310: final Action a = IOManager.createSaveAction(u, "Ask a Question"); jtulach@310: EventQueue.invokeAndWait(new Runnable() { jtulach@310: public void run() { jtulach@310: a.actionPerformed(new ActionEvent(this, 0, "")); jtulach@310: } jtulach@310: }); jtulach@310: String out = MemoryURL.getOutputForURL(u.toExternalForm()); jtulach@310: assertEquals("Text is reversed", "noitseuQ a ksA", out); jtulach@310: } jtulach@310: jtulach@310: private static class QueryStream extends OutputStream { jtulach@310: Boolean reverse; jtulach@310: ByteArrayOutputStream arr = new ByteArrayOutputStream(); jtulach@310: jtulach@310: @Override jtulach@310: public synchronized void write(byte[] b, int off, int len) throws IOException { jtulach@310: if (reverse == null) { jtulach@310: throw new QueryException(); jtulach@310: } jtulach@310: arr.write(b, off, len); jtulach@310: } jtulach@310: jtulach@310: @Override jtulach@310: public synchronized void write(int b) throws IOException { jtulach@310: if (reverse == null) { jtulach@310: throw new QueryException(); jtulach@310: } jtulach@310: arr.write(b); jtulach@310: } jtulach@310: jtulach@310: @Override jtulach@310: public String toString() { jtulach@310: if (reverse == null) { jtulach@310: return "Reverse question was not answered yet!"; jtulach@310: } jtulach@310: if (reverse) { jtulach@310: StringBuilder sb = new StringBuilder(); jtulach@310: sb.append(arr.toString()); jtulach@310: sb.reverse(); jtulach@310: return sb.toString(); jtulach@310: } jtulach@310: return arr.toString(); jtulach@310: } jtulach@310: jtulach@310: private class QueryException extends UserQuestionException { jtulach@310: @Override jtulach@310: public JOptionPane getQuestionPane() { jtulach@310: JOptionPane p = new JOptionPane("Store in reverse way?"); jtulach@310: p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION); jtulach@310: return p; jtulach@310: } jtulach@310: jtulach@310: @Override jtulach@310: public void confirm(Object option) { jtulach@310: if (option.equals(JOptionPane.YES_OPTION)) { jtulach@310: reverse = Boolean.TRUE; jtulach@310: return; jtulach@310: } jtulach@310: if (option.equals(JOptionPane.NO_OPTION)) { jtulach@310: reverse = Boolean.FALSE; jtulach@310: return; jtulach@310: } jtulach@310: } jtulach@310: } // end of QueryException jtulach@310: } // end of QueryStream jtulach@310: jtulach@310: jtulach@308: }