1 package org.apidesign.exceptions.trycatchredo;
4 import java.awt.EventQueue;
5 import java.awt.event.ActionEvent;
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.io.OutputStream;
10 import javax.swing.Action;
11 import javax.swing.JOptionPane;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import static org.junit.Assert.*;
19 * @author Jaroslav Tulach <jtulach@netbeans.org>
21 public class IOManagerTest {
23 public IOManagerTest() {
28 MemoryURL.initialize();
32 public void tearDown() {
36 public void simpleWrite() throws Exception {
37 URL u = new URL("memory://simpleWrite.txt");
38 MemoryURL.registerURL(u.toExternalForm(), "", null);
39 final Action a = IOManager.createSaveAction(u, "Hello World!");
40 EventQueue.invokeAndWait(new Runnable() {
42 a.actionPerformed(new ActionEvent(this, 0, ""));
45 String out = MemoryURL.getOutputForURL(u.toExternalForm());
46 assertEquals("Hello World!", out);
50 public void writeWithAQuestion() throws Exception {
51 URL u = new URL("memory://queryEncoding.txt");
53 MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
54 final Action a = IOManager.createSaveAction(u, "Ask a Question");
55 EventQueue.invokeAndWait(new Runnable() {
57 a.actionPerformed(new ActionEvent(this, 0, ""));
60 String out = MemoryURL.getOutputForURL(u.toExternalForm());
61 assertEquals("Text is reversed", "noitseuQ a ksA", out);
64 private static class QueryStream extends OutputStream {
66 ByteArrayOutputStream arr = new ByteArrayOutputStream();
69 public synchronized void write(byte[] b, int off, int len) throws IOException {
70 if (reverse == null) {
71 throw new QueryException();
73 arr.write(b, off, len);
77 public synchronized void write(int b) throws IOException {
78 if (reverse == null) {
79 throw new QueryException();
85 public String toString() {
86 if (reverse == null) {
87 return "Reverse question was not answered yet!";
90 StringBuilder sb = new StringBuilder();
91 sb.append(arr.toString());
95 return arr.toString();
98 private class QueryException extends UserQuestionException {
100 public JOptionPane getQuestionPane() {
101 JOptionPane p = new JOptionPane("Store in reverse way?");
102 p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
107 public void confirm(Object option) {
108 if (option.equals(JOptionPane.YES_OPTION)) {
109 reverse = Boolean.TRUE;
112 if (option.equals(JOptionPane.NO_OPTION)) {
113 reverse = Boolean.FALSE;
117 } // end of QueryException
118 } // end of QueryStream