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