1.1 --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 13:27:04 2009 +0100
1.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java Sun Feb 01 16:03:37 2009 +0100
1.3 @@ -3,8 +3,12 @@
1.4
1.5 import java.awt.EventQueue;
1.6 import java.awt.event.ActionEvent;
1.7 +import java.io.ByteArrayOutputStream;
1.8 +import java.io.IOException;
1.9 +import java.io.OutputStream;
1.10 import java.net.URL;
1.11 import javax.swing.Action;
1.12 +import javax.swing.JOptionPane;
1.13 import org.junit.After;
1.14 import org.junit.Before;
1.15 import org.junit.Test;
1.16 @@ -32,13 +36,86 @@
1.17 public void simpleWrite() throws Exception {
1.18 URL u = new URL("memory://simpleWrite.txt");
1.19 MemoryURL.registerURL(u.toExternalForm(), "", null);
1.20 - final Action a = IOManager.createSaveAction(u, "Ahoj");
1.21 + final Action a = IOManager.createSaveAction(u, "Hello World!");
1.22 EventQueue.invokeAndWait(new Runnable() {
1.23 public void run() {
1.24 a.actionPerformed(new ActionEvent(this, 0, ""));
1.25 }
1.26 });
1.27 - byte[] out = MemoryURL.getOutputForURL(u.toExternalForm());
1.28 - assertEquals("Four bytes", 4, out.length);
1.29 + String out = MemoryURL.getOutputForURL(u.toExternalForm());
1.30 + assertEquals("Hello World!", out);
1.31 }
1.32 +
1.33 + @Test
1.34 + public void writeWithAQuestion() throws Exception {
1.35 + URL u = new URL("memory://queryEncoding.txt");
1.36 +
1.37 + MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
1.38 + final Action a = IOManager.createSaveAction(u, "Ask a Question");
1.39 + EventQueue.invokeAndWait(new Runnable() {
1.40 + public void run() {
1.41 + a.actionPerformed(new ActionEvent(this, 0, ""));
1.42 + }
1.43 + });
1.44 + String out = MemoryURL.getOutputForURL(u.toExternalForm());
1.45 + assertEquals("Text is reversed", "noitseuQ a ksA", out);
1.46 + }
1.47 +
1.48 + private static class QueryStream extends OutputStream {
1.49 + Boolean reverse;
1.50 + ByteArrayOutputStream arr = new ByteArrayOutputStream();
1.51 +
1.52 + @Override
1.53 + public synchronized void write(byte[] b, int off, int len) throws IOException {
1.54 + if (reverse == null) {
1.55 + throw new QueryException();
1.56 + }
1.57 + arr.write(b, off, len);
1.58 + }
1.59 +
1.60 + @Override
1.61 + public synchronized void write(int b) throws IOException {
1.62 + if (reverse == null) {
1.63 + throw new QueryException();
1.64 + }
1.65 + arr.write(b);
1.66 + }
1.67 +
1.68 + @Override
1.69 + public String toString() {
1.70 + if (reverse == null) {
1.71 + return "Reverse question was not answered yet!";
1.72 + }
1.73 + if (reverse) {
1.74 + StringBuilder sb = new StringBuilder();
1.75 + sb.append(arr.toString());
1.76 + sb.reverse();
1.77 + return sb.toString();
1.78 + }
1.79 + return arr.toString();
1.80 + }
1.81 +
1.82 + private class QueryException extends UserQuestionException {
1.83 + @Override
1.84 + public JOptionPane getQuestionPane() {
1.85 + JOptionPane p = new JOptionPane("Store in reverse way?");
1.86 + p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
1.87 + return p;
1.88 + }
1.89 +
1.90 + @Override
1.91 + public void confirm(Object option) {
1.92 + if (option.equals(JOptionPane.YES_OPTION)) {
1.93 + reverse = Boolean.TRUE;
1.94 + return;
1.95 + }
1.96 + if (option.equals(JOptionPane.NO_OPTION)) {
1.97 + reverse = Boolean.FALSE;
1.98 + return;
1.99 + }
1.100 + }
1.101 + } // end of QueryException
1.102 + } // end of QueryStream
1.103 +
1.104 +
1.105 }
1.106 \ No newline at end of file