jtulach@308
|
1 |
package org.apidesign.exceptions.trycatchredo;
|
jtulach@308
|
2 |
|
jtulach@308
|
3 |
|
jtulach@308
|
4 |
import java.awt.EventQueue;
|
jtulach@308
|
5 |
import java.awt.event.ActionEvent;
|
jtulach@310
|
6 |
import java.io.ByteArrayOutputStream;
|
jtulach@310
|
7 |
import java.io.IOException;
|
jtulach@310
|
8 |
import java.io.OutputStream;
|
jtulach@308
|
9 |
import java.net.URL;
|
jtulach@308
|
10 |
import javax.swing.Action;
|
jtulach@310
|
11 |
import javax.swing.JOptionPane;
|
jtulach@308
|
12 |
import org.junit.After;
|
jtulach@308
|
13 |
import org.junit.Before;
|
jtulach@308
|
14 |
import org.junit.Test;
|
jtulach@308
|
15 |
import static org.junit.Assert.*;
|
jtulach@308
|
16 |
|
jtulach@308
|
17 |
/**
|
jtulach@308
|
18 |
*
|
jtulach@308
|
19 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@308
|
20 |
*/
|
jtulach@308
|
21 |
public class IOManagerTest {
|
jtulach@308
|
22 |
|
jtulach@308
|
23 |
public IOManagerTest() {
|
jtulach@308
|
24 |
}
|
jtulach@308
|
25 |
|
jtulach@308
|
26 |
@Before
|
jtulach@308
|
27 |
public void setUp() {
|
jtulach@308
|
28 |
MemoryURL.initialize();
|
jtulach@308
|
29 |
}
|
jtulach@308
|
30 |
|
jtulach@308
|
31 |
@After
|
jtulach@308
|
32 |
public void tearDown() {
|
jtulach@308
|
33 |
}
|
jtulach@308
|
34 |
|
jtulach@308
|
35 |
@Test
|
jtulach@308
|
36 |
public void simpleWrite() throws Exception {
|
jtulach@308
|
37 |
URL u = new URL("memory://simpleWrite.txt");
|
jtulach@308
|
38 |
MemoryURL.registerURL(u.toExternalForm(), "", null);
|
jtulach@310
|
39 |
final Action a = IOManager.createSaveAction(u, "Hello World!");
|
jtulach@308
|
40 |
EventQueue.invokeAndWait(new Runnable() {
|
jtulach@308
|
41 |
public void run() {
|
jtulach@308
|
42 |
a.actionPerformed(new ActionEvent(this, 0, ""));
|
jtulach@308
|
43 |
}
|
jtulach@308
|
44 |
});
|
jtulach@310
|
45 |
String out = MemoryURL.getOutputForURL(u.toExternalForm());
|
jtulach@310
|
46 |
assertEquals("Hello World!", out);
|
jtulach@308
|
47 |
}
|
jtulach@310
|
48 |
|
jtulach@310
|
49 |
@Test
|
jtulach@310
|
50 |
public void writeWithAQuestion() throws Exception {
|
jtulach@310
|
51 |
URL u = new URL("memory://queryEncoding.txt");
|
jtulach@310
|
52 |
|
jtulach@310
|
53 |
MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
|
jtulach@310
|
54 |
final Action a = IOManager.createSaveAction(u, "Ask a Question");
|
jtulach@310
|
55 |
EventQueue.invokeAndWait(new Runnable() {
|
jtulach@310
|
56 |
public void run() {
|
jtulach@310
|
57 |
a.actionPerformed(new ActionEvent(this, 0, ""));
|
jtulach@310
|
58 |
}
|
jtulach@310
|
59 |
});
|
jtulach@310
|
60 |
String out = MemoryURL.getOutputForURL(u.toExternalForm());
|
jtulach@310
|
61 |
assertEquals("Text is reversed", "noitseuQ a ksA", out);
|
jtulach@310
|
62 |
}
|
jtulach@310
|
63 |
|
jtulach@310
|
64 |
private static class QueryStream extends OutputStream {
|
jtulach@310
|
65 |
Boolean reverse;
|
jtulach@310
|
66 |
ByteArrayOutputStream arr = new ByteArrayOutputStream();
|
jtulach@310
|
67 |
|
jtulach@310
|
68 |
@Override
|
jtulach@310
|
69 |
public synchronized void write(byte[] b, int off, int len) throws IOException {
|
jtulach@310
|
70 |
if (reverse == null) {
|
jtulach@310
|
71 |
throw new QueryException();
|
jtulach@310
|
72 |
}
|
jtulach@310
|
73 |
arr.write(b, off, len);
|
jtulach@310
|
74 |
}
|
jtulach@310
|
75 |
|
jtulach@310
|
76 |
@Override
|
jtulach@310
|
77 |
public synchronized void write(int b) throws IOException {
|
jtulach@310
|
78 |
if (reverse == null) {
|
jtulach@310
|
79 |
throw new QueryException();
|
jtulach@310
|
80 |
}
|
jtulach@310
|
81 |
arr.write(b);
|
jtulach@310
|
82 |
}
|
jtulach@310
|
83 |
|
jtulach@310
|
84 |
@Override
|
jtulach@310
|
85 |
public String toString() {
|
jtulach@310
|
86 |
if (reverse == null) {
|
jtulach@310
|
87 |
return "Reverse question was not answered yet!";
|
jtulach@310
|
88 |
}
|
jtulach@310
|
89 |
if (reverse) {
|
jtulach@310
|
90 |
StringBuilder sb = new StringBuilder();
|
jtulach@310
|
91 |
sb.append(arr.toString());
|
jtulach@310
|
92 |
sb.reverse();
|
jtulach@310
|
93 |
return sb.toString();
|
jtulach@310
|
94 |
}
|
jtulach@310
|
95 |
return arr.toString();
|
jtulach@310
|
96 |
}
|
jtulach@310
|
97 |
|
jtulach@310
|
98 |
private class QueryException extends UserQuestionException {
|
jtulach@310
|
99 |
@Override
|
jtulach@310
|
100 |
public JOptionPane getQuestionPane() {
|
jtulach@310
|
101 |
JOptionPane p = new JOptionPane("Store in reverse way?");
|
jtulach@310
|
102 |
p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
|
jtulach@310
|
103 |
return p;
|
jtulach@310
|
104 |
}
|
jtulach@310
|
105 |
|
jtulach@310
|
106 |
@Override
|
jtulach@310
|
107 |
public void confirm(Object option) {
|
jtulach@310
|
108 |
if (option.equals(JOptionPane.YES_OPTION)) {
|
jtulach@310
|
109 |
reverse = Boolean.TRUE;
|
jtulach@310
|
110 |
return;
|
jtulach@310
|
111 |
}
|
jtulach@310
|
112 |
if (option.equals(JOptionPane.NO_OPTION)) {
|
jtulach@310
|
113 |
reverse = Boolean.FALSE;
|
jtulach@310
|
114 |
return;
|
jtulach@310
|
115 |
}
|
jtulach@310
|
116 |
}
|
jtulach@310
|
117 |
} // end of QueryException
|
jtulach@310
|
118 |
} // end of QueryStream
|
jtulach@310
|
119 |
|
jtulach@310
|
120 |
|
jtulach@308
|
121 |
} |