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