Rearranged to support test as well as interactive modes
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:27:41 +0100
changeset 311cb8db49f9d1c
parent 310 fba31e9504a1
child 312 0678c9589013
Rearranged to support test as well as interactive modes
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveAction.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/IOManager.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/SaveAction.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/UserQuestionException.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/Main.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/MemoryURL.java
samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/QueryStream.java
samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java
samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java
samples/exceptions/test/org/apidesign/exceptions/trycatchredo/api/IOManagerTest.java
     1.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/IOManager.java	Sun Feb 01 16:03:37 2009 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,29 +0,0 @@
     1.4 -package org.apidesign.exceptions.trycatchredo;
     1.5 -
     1.6 -import java.net.URL;
     1.7 -import javax.swing.Action;
     1.8 -
     1.9 -/**
    1.10 - *
    1.11 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.12 - */
    1.13 -public final class IOManager {
    1.14 -    static boolean old;
    1.15 -
    1.16 -    IOManager() {
    1.17 -    }
    1.18 -
    1.19 -    /** Action that can store a text to given URL.
    1.20 -     *
    1.21 -     * @param where the url to upload the text to
    1.22 -     * @param what the text to upload
    1.23 -     * @return action that can be invoked anytime to save the content
    1.24 -     */
    1.25 -    public static Action createSaveAction(URL where, CharSequence what) {
    1.26 -        if (old) {
    1.27 -            return new SaveAction(where, what);
    1.28 -        } else {
    1.29 -            return new SaveActionWithQuery(where, what);
    1.30 -        }
    1.31 -    }
    1.32 -}
     2.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveAction.java	Sun Feb 01 16:03:37 2009 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,35 +0,0 @@
     2.4 -package org.apidesign.exceptions.trycatchredo;
     2.5 -
     2.6 -import java.awt.EventQueue;
     2.7 -import java.awt.event.ActionEvent;
     2.8 -import java.io.IOException;
     2.9 -import java.io.OutputStream;
    2.10 -import java.net.URL;
    2.11 -import javax.swing.AbstractAction;
    2.12 -import javax.swing.JOptionPane;
    2.13 -
    2.14 -/**
    2.15 - *
    2.16 - * @author Jaroslav Tulach
    2.17 - */
    2.18 -final class SaveAction extends AbstractAction {
    2.19 -    private final URL where;
    2.20 -    private final CharSequence what;
    2.21 -    
    2.22 -    SaveAction(URL where, CharSequence what) {
    2.23 -        this.where = where;
    2.24 -        this.what = what;
    2.25 -    }
    2.26 -    
    2.27 -    
    2.28 -    public void actionPerformed(ActionEvent ev) {
    2.29 -        assert EventQueue.isDispatchThread();
    2.30 -        try {
    2.31 -            OutputStream os = where.openConnection().getOutputStream();
    2.32 -            os.write(what.toString().getBytes());
    2.33 -            os.close();
    2.34 -        } catch (IOException ex) {
    2.35 -            JOptionPane.showMessageDialog(null, ex);
    2.36 -        }
    2.37 -    }
    2.38 -}
     3.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/SaveActionWithQuery.java	Sun Feb 01 16:03:37 2009 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,50 +0,0 @@
     3.4 -package org.apidesign.exceptions.trycatchredo;
     3.5 -
     3.6 -import java.awt.EventQueue;
     3.7 -import java.awt.event.ActionEvent;
     3.8 -import java.io.IOException;
     3.9 -import java.io.OutputStream;
    3.10 -import java.net.URL;
    3.11 -import javax.swing.AbstractAction;
    3.12 -import javax.swing.JDialog;
    3.13 -import javax.swing.JOptionPane;
    3.14 -
    3.15 -/**
    3.16 - *
    3.17 - * @author Jaroslav Tulach
    3.18 - */
    3.19 -final class SaveActionWithQuery extends AbstractAction {
    3.20 -    private final URL where;
    3.21 -    private final CharSequence what;
    3.22 -    
    3.23 -    SaveActionWithQuery(URL where, CharSequence what) {
    3.24 -        this.where = where;
    3.25 -        this.what = what;
    3.26 -    }
    3.27 -    
    3.28 -    
    3.29 -    public void actionPerformed(ActionEvent ev) {
    3.30 -        assert EventQueue.isDispatchThread();
    3.31 -        for (;;) {
    3.32 -            try {
    3.33 -                OutputStream os = where.openConnection().getOutputStream();
    3.34 -                os.write(what.toString().getBytes());
    3.35 -                os.close();
    3.36 -            } catch (UserQuestionException ex) {
    3.37 -                JOptionPane p = ex.getQuestionPane();
    3.38 -                JDialog d = p.createDialog(ex.getLocalizedMessage());
    3.39 -                d.setVisible(true);
    3.40 -                ex.confirm(p.getValue());
    3.41 -                if (
    3.42 -                    !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    3.43 -                    !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    3.44 -                ) {
    3.45 -                    continue;
    3.46 -                }
    3.47 -            } catch (IOException ex) {
    3.48 -                JOptionPane.showMessageDialog(null, ex);
    3.49 -            }
    3.50 -            break;
    3.51 -        }
    3.52 -    }
    3.53 -}
     4.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/UserQuestionException.java	Sun Feb 01 16:03:37 2009 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,29 +0,0 @@
     4.4 -package org.apidesign.exceptions.trycatchredo;
     4.5 -
     4.6 -import java.io.IOException;
     4.7 -import javax.swing.JOptionPane;
     4.8 -
     4.9 -/** Specialized I/O exception to request some kind of user confirmation.
    4.10 - * A code that needs to ask user shall not attempt to open a dialog itself,
    4.11 - * rather it shall emit this exception and let its callers show the dialog
    4.12 - * at appropriate time.
    4.13 - *
    4.14 - * @author Jaroslav Tulach
    4.15 - * @since 2.0
    4.16 - */
    4.17 -public abstract class UserQuestionException extends IOException {
    4.18 -    /** Description of the dialog to show to the user. Whoever catches
    4.19 -     * this exception shall use {@link #getQuestionPane()}.{@link JOptionPane#createDialog(java.lang.String)}
    4.20 -     * to construct and display the dialog.
    4.21 -     * 
    4.22 -     * @return the pane to display to user
    4.23 -     */
    4.24 -    public abstract JOptionPane getQuestionPane();
    4.25 -    /** When the user confirms (or rejects) message presented by the
    4.26 -     * {@link #getQuestionPane()} dialog, the exception shall be notified
    4.27 -     * by calling this method with {@link JOptionPane#getValue()} option.
    4.28 -     *
    4.29 -     * @param option the option selected by the user
    4.30 -     */
    4.31 -    public abstract void confirm(Object option);
    4.32 -}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/IOManager.java	Sun Feb 01 16:27:41 2009 +0100
     5.3 @@ -0,0 +1,44 @@
     5.4 +package org.apidesign.exceptions.trycatchredo.api;
     5.5 +
     5.6 +import java.net.URL;
     5.7 +import javax.swing.Action;
     5.8 +import javax.swing.JDialog;
     5.9 +import javax.swing.JOptionPane;
    5.10 +
    5.11 +/**
    5.12 + *
    5.13 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.14 + */
    5.15 +public final class IOManager {
    5.16 +    IOManager() {
    5.17 +    }
    5.18 +
    5.19 +    /** Action that can store a text to given URL.
    5.20 +     *
    5.21 +     * @param where the url to upload the text to
    5.22 +     * @param what the text to upload
    5.23 +     * @return action that can be invoked anytime to save the content
    5.24 +     */
    5.25 +    public static Action createSaveAction(URL where, CharSequence what) {
    5.26 +        if (old) {
    5.27 +            return new SaveAction(where, what);
    5.28 +        } else {
    5.29 +            return new SaveActionWithQuery(where, what);
    5.30 +        }
    5.31 +    }
    5.32 +
    5.33 +    //
    5.34 +    // Support for executing mock objects in tests
    5.35 +    //
    5.36 +
    5.37 +    static boolean old;
    5.38 +    static Object setVisibleOption;
    5.39 +    static void setVisible(JDialog d, JOptionPane p) {
    5.40 +        if (setVisibleOption == null) {
    5.41 +            d.setVisible(true);
    5.42 +        } else {
    5.43 +            // only in test mode
    5.44 +            p.setValue(setVisibleOption);
    5.45 +        }
    5.46 +    }
    5.47 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/SaveAction.java	Sun Feb 01 16:27:41 2009 +0100
     6.3 @@ -0,0 +1,35 @@
     6.4 +package org.apidesign.exceptions.trycatchredo.api;
     6.5 +
     6.6 +import java.awt.EventQueue;
     6.7 +import java.awt.event.ActionEvent;
     6.8 +import java.io.IOException;
     6.9 +import java.io.OutputStream;
    6.10 +import java.net.URL;
    6.11 +import javax.swing.AbstractAction;
    6.12 +import javax.swing.JOptionPane;
    6.13 +
    6.14 +/**
    6.15 + *
    6.16 + * @author Jaroslav Tulach
    6.17 + */
    6.18 +final class SaveAction extends AbstractAction {
    6.19 +    private final URL where;
    6.20 +    private final CharSequence what;
    6.21 +    
    6.22 +    SaveAction(URL where, CharSequence what) {
    6.23 +        this.where = where;
    6.24 +        this.what = what;
    6.25 +    }
    6.26 +    
    6.27 +    
    6.28 +    public void actionPerformed(ActionEvent ev) {
    6.29 +        assert EventQueue.isDispatchThread();
    6.30 +        try {
    6.31 +            OutputStream os = where.openConnection().getOutputStream();
    6.32 +            os.write(what.toString().getBytes());
    6.33 +            os.close();
    6.34 +        } catch (IOException ex) {
    6.35 +            JOptionPane.showMessageDialog(null, ex);
    6.36 +        }
    6.37 +    }
    6.38 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/SaveActionWithQuery.java	Sun Feb 01 16:27:41 2009 +0100
     7.3 @@ -0,0 +1,54 @@
     7.4 +package org.apidesign.exceptions.trycatchredo.api;
     7.5 +
     7.6 +import java.awt.EventQueue;
     7.7 +import java.awt.event.ActionEvent;
     7.8 +import java.io.IOException;
     7.9 +import java.io.OutputStream;
    7.10 +import java.net.URL;
    7.11 +import javax.swing.AbstractAction;
    7.12 +import javax.swing.JDialog;
    7.13 +import javax.swing.JOptionPane;
    7.14 +
    7.15 +/**
    7.16 + *
    7.17 + * @author Jaroslav Tulach
    7.18 + */
    7.19 +final class SaveActionWithQuery extends AbstractAction {
    7.20 +    private final URL where;
    7.21 +    private final CharSequence what;
    7.22 +    
    7.23 +    SaveActionWithQuery(URL where, CharSequence what) {
    7.24 +        this.where = where;
    7.25 +        this.what = what;
    7.26 +    }
    7.27 +    
    7.28 +    
    7.29 +    public void actionPerformed(ActionEvent ev) {
    7.30 +        assert EventQueue.isDispatchThread();
    7.31 +        for (;;) {
    7.32 +            try {
    7.33 +                OutputStream os = where.openConnection().getOutputStream();
    7.34 +                os.write(what.toString().getBytes());
    7.35 +                os.close();
    7.36 +            } catch (UserQuestionException ex) {
    7.37 +                JOptionPane p = ex.getQuestionPane();
    7.38 +                JDialog d = p.createDialog(ex.getLocalizedMessage());
    7.39 +                setVisible(d, p);
    7.40 +                ex.confirm(p.getValue());
    7.41 +                if (
    7.42 +                    !p.getValue().equals(JOptionPane.CANCEL_OPTION) &&
    7.43 +                    !p.getValue().equals(JOptionPane.CLOSED_OPTION)
    7.44 +                ) {
    7.45 +                    continue;
    7.46 +                }
    7.47 +            } catch (IOException ex) {
    7.48 +                JOptionPane.showMessageDialog(null, ex);
    7.49 +            }
    7.50 +            break;
    7.51 +        }
    7.52 +    }
    7.53 +
    7.54 +    private static void setVisible(JDialog d, JOptionPane p) {
    7.55 +        IOManager.setVisible(d, p);
    7.56 +    }
    7.57 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/api/UserQuestionException.java	Sun Feb 01 16:27:41 2009 +0100
     8.3 @@ -0,0 +1,29 @@
     8.4 +package org.apidesign.exceptions.trycatchredo.api;
     8.5 +
     8.6 +import java.io.IOException;
     8.7 +import javax.swing.JOptionPane;
     8.8 +
     8.9 +/** Specialized I/O exception to request some kind of user confirmation.
    8.10 + * A code that needs to ask user shall not attempt to open a dialog itself,
    8.11 + * rather it shall emit this exception and let its callers show the dialog
    8.12 + * at appropriate time.
    8.13 + *
    8.14 + * @author Jaroslav Tulach
    8.15 + * @since 2.0
    8.16 + */
    8.17 +public abstract class UserQuestionException extends IOException {
    8.18 +    /** Description of the dialog to show to the user. Whoever catches
    8.19 +     * this exception shall use {@link #getQuestionPane()}.{@link JOptionPane#createDialog(java.lang.String)}
    8.20 +     * to construct and display the dialog.
    8.21 +     * 
    8.22 +     * @return the pane to display to user
    8.23 +     */
    8.24 +    public abstract JOptionPane getQuestionPane();
    8.25 +    /** When the user confirms (or rejects) message presented by the
    8.26 +     * {@link #getQuestionPane()} dialog, the exception shall be notified
    8.27 +     * by calling this method with {@link JOptionPane#getValue()} option.
    8.28 +     *
    8.29 +     * @param option the option selected by the user
    8.30 +     */
    8.31 +    public abstract void confirm(Object option);
    8.32 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/Main.java	Sun Feb 01 16:27:41 2009 +0100
     9.3 @@ -0,0 +1,35 @@
     9.4 +package org.apidesign.exceptions.trycatchredo.usage;
     9.5 +
     9.6 +import java.awt.EventQueue;
     9.7 +import java.awt.event.ActionEvent;
     9.8 +import java.net.URL;
     9.9 +import javax.swing.Action;
    9.10 +import org.apidesign.exceptions.trycatchredo.api.IOManager;
    9.11 +import org.apidesign.exceptions.trycatchredo.api.UserQuestionException;
    9.12 +
    9.13 +/** Sample usage showing interactive storage capabilities of the
    9.14 + * {@link UserQuestionException}
    9.15 + *
    9.16 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    9.17 + */
    9.18 +public class Main {
    9.19 +    public static void main(String[] args) throws Exception {
    9.20 +        MemoryURL.initialize();
    9.21 +        
    9.22 +        for (int cnt = 0; cnt < 10; cnt++) {
    9.23 +            URL u = new URL("memory://" + cnt + "/queryEncoding.txt");
    9.24 +            MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
    9.25 +            final Action a = IOManager.createSaveAction(u, "Ask a Question");
    9.26 +            EventQueue.invokeAndWait(new Runnable() {
    9.27 +                public void run() {
    9.28 +                    a.actionPerformed(new ActionEvent(this, 0, ""));
    9.29 +                }
    9.30 +            });
    9.31 +            String out = MemoryURL.getOutputForURL(u.toExternalForm());
    9.32 +
    9.33 +            System.err.println(cnt + " output: " + out);
    9.34 +        }
    9.35 +
    9.36 +        System.exit(0);
    9.37 +    }
    9.38 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/MemoryURL.java	Sun Feb 01 16:27:41 2009 +0100
    10.3 @@ -0,0 +1,94 @@
    10.4 +
    10.5 +package org.apidesign.exceptions.trycatchredo.usage;
    10.6 +
    10.7 +import java.io.ByteArrayInputStream;
    10.8 +import java.io.ByteArrayOutputStream;
    10.9 +import java.io.IOException;
   10.10 +import java.io.InputStream;
   10.11 +import java.io.OutputStream;
   10.12 +import java.net.MalformedURLException;
   10.13 +import java.net.URL;
   10.14 +import java.net.URLConnection;
   10.15 +import java.net.URLStreamHandler;
   10.16 +import java.net.URLStreamHandlerFactory;
   10.17 +import java.util.HashMap;
   10.18 +import java.util.Map;
   10.19 +
   10.20 +/** Support for special "memory://" URLs. Useful when testing network communication.
   10.21 + *
   10.22 + * @author Jaroslav Tulach
   10.23 + */
   10.24 +public final class MemoryURL extends URLStreamHandler {
   10.25 +    private MemoryURL() {
   10.26 +    }
   10.27 +
   10.28 +    public static void initialize() {
   10.29 +    }
   10.30 +    static {
   10.31 +        class F implements URLStreamHandlerFactory {
   10.32 +            public URLStreamHandler createURLStreamHandler(String protocol) {
   10.33 +                if (protocol.startsWith("memory")) {
   10.34 +                    return new MemoryURL();
   10.35 +                }
   10.36 +                return null;
   10.37 +            }
   10.38 +        }
   10.39 +        F f = new F();
   10.40 +        URL.setURLStreamHandlerFactory(f);
   10.41 +    }
   10.42 +    
   10.43 +    private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
   10.44 +    private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
   10.45 +
   10.46 +    public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
   10.47 +        contents.put(u, new ByteArrayInputStream(content.getBytes()));
   10.48 +        outputs.put(u, out);
   10.49 +    }
   10.50 +    
   10.51 +    public static String getOutputForURL(String u) {
   10.52 +        OutputStream out = outputs.get(u);
   10.53 +        return out.toString();
   10.54 +    }
   10.55 +    
   10.56 +    protected URLConnection openConnection(URL u) throws IOException {
   10.57 +        return new MC(u);
   10.58 +    }
   10.59 +    
   10.60 +    private static final class MC extends URLConnection {
   10.61 +        private InputStream values;
   10.62 +        private OutputStream out;
   10.63 +        
   10.64 +        public MC(URL u) {
   10.65 +            super(u);
   10.66 +            out = outputs.get(u.toExternalForm());
   10.67 +            if (out == null) {
   10.68 +                out = new ByteArrayOutputStream();
   10.69 +                outputs.put(u.toExternalForm(), out);
   10.70 +            }
   10.71 +        }
   10.72 +
   10.73 +        public void connect() throws IOException {
   10.74 +            if (values != null) {
   10.75 +                return;
   10.76 +            }
   10.77 +            values = contents.remove(url.toExternalForm());
   10.78 +            if (values == null) {
   10.79 +                throw new IOException("No such content: " + url);
   10.80 +            }
   10.81 +        }
   10.82 +
   10.83 +        @Override
   10.84 +        public InputStream getInputStream() throws IOException {
   10.85 +            connect();
   10.86 +            return values;
   10.87 +        }
   10.88 +
   10.89 +        @Override
   10.90 +        public OutputStream getOutputStream() throws IOException {
   10.91 +            if (out == null) {
   10.92 +                out = new ByteArrayOutputStream();
   10.93 +            }
   10.94 +            return out;
   10.95 +        }
   10.96 +    }
   10.97 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/QueryStream.java	Sun Feb 01 16:27:41 2009 +0100
    11.3 @@ -0,0 +1,65 @@
    11.4 +package org.apidesign.exceptions.trycatchredo.usage;
    11.5 +
    11.6 +import org.apidesign.exceptions.trycatchredo.api.UserQuestionException;
    11.7 +import java.io.ByteArrayOutputStream;
    11.8 +import java.io.IOException;
    11.9 +import java.io.OutputStream;
   11.10 +import javax.swing.JOptionPane;
   11.11 +
   11.12 +public final class QueryStream extends OutputStream {
   11.13 +    private ByteArrayOutputStream arr = new ByteArrayOutputStream();
   11.14 +    /** this field can be manipulated by the QueryException */
   11.15 +    Boolean reverse;
   11.16 +
   11.17 +    @Override
   11.18 +    public synchronized void write(byte[] b, int off, int len) throws IOException {
   11.19 +        if (reverse == null) {
   11.20 +            throw new QueryException();
   11.21 +        }
   11.22 +        arr.write(b, off, len);
   11.23 +    }
   11.24 +
   11.25 +    @Override
   11.26 +    public synchronized void write(int b) throws IOException {
   11.27 +        if (reverse == null) {
   11.28 +            throw new QueryException();
   11.29 +        }
   11.30 +        arr.write(b);
   11.31 +    }
   11.32 +
   11.33 +    @Override
   11.34 +    public String toString() {
   11.35 +        if (reverse == null) {
   11.36 +            return "Reverse question was not answered yet!";
   11.37 +        }
   11.38 +        if (reverse) {
   11.39 +            StringBuilder sb = new StringBuilder();
   11.40 +            sb.append(arr.toString());
   11.41 +            sb.reverse();
   11.42 +            return sb.toString();
   11.43 +        }
   11.44 +        return arr.toString();
   11.45 +    }
   11.46 +
   11.47 +    private class QueryException extends UserQuestionException {
   11.48 +
   11.49 +        @Override
   11.50 +        public JOptionPane getQuestionPane() {
   11.51 +            JOptionPane p = new JOptionPane("Store in reverse way?");
   11.52 +            p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
   11.53 +            return p;
   11.54 +        }
   11.55 +
   11.56 +        @Override
   11.57 +        public void confirm(Object option) {
   11.58 +            if (option.equals(JOptionPane.YES_OPTION)) {
   11.59 +                reverse = Boolean.TRUE;
   11.60 +                return;
   11.61 +            }
   11.62 +            if (option.equals(JOptionPane.NO_OPTION)) {
   11.63 +                reverse = Boolean.FALSE;
   11.64 +                return;
   11.65 +            }
   11.66 +        }
   11.67 +    }
   11.68 +}
    12.1 --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/IOManagerTest.java	Sun Feb 01 16:03:37 2009 +0100
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,121 +0,0 @@
    12.4 -package org.apidesign.exceptions.trycatchredo;
    12.5 -
    12.6 -
    12.7 -import java.awt.EventQueue;
    12.8 -import java.awt.event.ActionEvent;
    12.9 -import java.io.ByteArrayOutputStream;
   12.10 -import java.io.IOException;
   12.11 -import java.io.OutputStream;
   12.12 -import java.net.URL;
   12.13 -import javax.swing.Action;
   12.14 -import javax.swing.JOptionPane;
   12.15 -import org.junit.After;
   12.16 -import org.junit.Before;
   12.17 -import org.junit.Test;
   12.18 -import static org.junit.Assert.*;
   12.19 -
   12.20 -/**
   12.21 - *
   12.22 - * @author Jaroslav Tulach <jtulach@netbeans.org>
   12.23 - */
   12.24 -public class IOManagerTest {
   12.25 -
   12.26 -    public IOManagerTest() {
   12.27 -    }
   12.28 -
   12.29 -    @Before
   12.30 -    public void setUp() {
   12.31 -        MemoryURL.initialize();
   12.32 -    }
   12.33 -
   12.34 -    @After
   12.35 -    public void tearDown() {
   12.36 -    }
   12.37 -
   12.38 -    @Test
   12.39 -    public void simpleWrite() throws Exception {
   12.40 -        URL u = new URL("memory://simpleWrite.txt");
   12.41 -        MemoryURL.registerURL(u.toExternalForm(), "", null);
   12.42 -        final Action a = IOManager.createSaveAction(u, "Hello World!");
   12.43 -        EventQueue.invokeAndWait(new Runnable() {
   12.44 -            public void run() {
   12.45 -                a.actionPerformed(new ActionEvent(this, 0, ""));
   12.46 -            }
   12.47 -        });
   12.48 -        String out = MemoryURL.getOutputForURL(u.toExternalForm());
   12.49 -        assertEquals("Hello World!", out);
   12.50 -    }
   12.51 -
   12.52 -    @Test
   12.53 -    public void writeWithAQuestion() throws Exception {
   12.54 -        URL u = new URL("memory://queryEncoding.txt");
   12.55 -
   12.56 -        MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
   12.57 -        final Action a = IOManager.createSaveAction(u, "Ask a Question");
   12.58 -        EventQueue.invokeAndWait(new Runnable() {
   12.59 -            public void run() {
   12.60 -                a.actionPerformed(new ActionEvent(this, 0, ""));
   12.61 -            }
   12.62 -        });
   12.63 -        String out = MemoryURL.getOutputForURL(u.toExternalForm());
   12.64 -        assertEquals("Text is reversed", "noitseuQ a ksA", out);
   12.65 -    }
   12.66 -
   12.67 -    private static class QueryStream extends OutputStream {
   12.68 -        Boolean reverse;
   12.69 -        ByteArrayOutputStream arr = new ByteArrayOutputStream();
   12.70 -
   12.71 -        @Override
   12.72 -        public synchronized void write(byte[] b, int off, int len) throws IOException {
   12.73 -            if (reverse == null) {
   12.74 -                throw new QueryException();
   12.75 -            }
   12.76 -            arr.write(b, off, len);
   12.77 -        }
   12.78 -
   12.79 -        @Override
   12.80 -        public synchronized void write(int b) throws IOException {
   12.81 -            if (reverse == null) {
   12.82 -                throw new QueryException();
   12.83 -            }
   12.84 -            arr.write(b);
   12.85 -        }
   12.86 -
   12.87 -        @Override
   12.88 -        public String toString() {
   12.89 -            if (reverse == null) {
   12.90 -                return "Reverse question was not answered yet!";
   12.91 -            }
   12.92 -            if (reverse) {
   12.93 -                StringBuilder sb = new StringBuilder();
   12.94 -                sb.append(arr.toString());
   12.95 -                sb.reverse();
   12.96 -                return sb.toString();
   12.97 -            }
   12.98 -            return arr.toString();
   12.99 -        }
  12.100 -
  12.101 -        private class QueryException extends UserQuestionException {
  12.102 -            @Override
  12.103 -            public JOptionPane getQuestionPane() {
  12.104 -                JOptionPane p = new JOptionPane("Store in reverse way?");
  12.105 -                p.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
  12.106 -                return p;
  12.107 -            }
  12.108 -
  12.109 -            @Override
  12.110 -            public void confirm(Object option) {
  12.111 -                if (option.equals(JOptionPane.YES_OPTION)) {
  12.112 -                    reverse = Boolean.TRUE;
  12.113 -                    return;
  12.114 -                }
  12.115 -                if (option.equals(JOptionPane.NO_OPTION)) {
  12.116 -                    reverse = Boolean.FALSE;
  12.117 -                    return;
  12.118 -                }
  12.119 -            }
  12.120 -        } // end of QueryException
  12.121 -    } // end of QueryStream
  12.122 -
  12.123 -
  12.124 -}
  12.125 \ No newline at end of file
    13.1 --- a/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java	Sun Feb 01 16:03:37 2009 +0100
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,95 +0,0 @@
    13.4 -
    13.5 -package org.apidesign.exceptions.trycatchredo;
    13.6 -
    13.7 -import java.io.ByteArrayInputStream;
    13.8 -import java.io.ByteArrayOutputStream;
    13.9 -import java.io.IOException;
   13.10 -import java.io.InputStream;
   13.11 -import java.io.OutputStream;
   13.12 -import java.net.MalformedURLException;
   13.13 -import java.net.URL;
   13.14 -import java.net.URLConnection;
   13.15 -import java.net.URLStreamHandler;
   13.16 -import java.net.URLStreamHandlerFactory;
   13.17 -import java.util.HashMap;
   13.18 -import java.util.Map;
   13.19 -import junit.framework.Assert;
   13.20 -
   13.21 -/**
   13.22 - *
   13.23 - * @author Jaroslav Tulach
   13.24 - */
   13.25 -public final class MemoryURL extends URLStreamHandler {
   13.26 -    private MemoryURL() {
   13.27 -    }
   13.28 -
   13.29 -    static void initialize() {
   13.30 -    }
   13.31 -    static {
   13.32 -        class F implements URLStreamHandlerFactory {
   13.33 -            public URLStreamHandler createURLStreamHandler(String protocol) {
   13.34 -                if (protocol.startsWith("memory")) {
   13.35 -                    return new MemoryURL();
   13.36 -                }
   13.37 -                return null;
   13.38 -            }
   13.39 -        }
   13.40 -        F f = new F();
   13.41 -        URL.setURLStreamHandlerFactory(f);
   13.42 -    }
   13.43 -    
   13.44 -    private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
   13.45 -    private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
   13.46 -    public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
   13.47 -        contents.put(u, new ByteArrayInputStream(content.getBytes()));
   13.48 -        outputs.put(u, out);
   13.49 -    }
   13.50 -    
   13.51 -    public static String getOutputForURL(String u) {
   13.52 -        OutputStream out = outputs.get(u);
   13.53 -        Assert.assertNotNull("No output for " + u, out);
   13.54 -        return out.toString();
   13.55 -    }
   13.56 -    
   13.57 -    protected URLConnection openConnection(URL u) throws IOException {
   13.58 -        return new MC(u);
   13.59 -    }
   13.60 -    
   13.61 -    private static final class MC extends URLConnection {
   13.62 -        private InputStream values;
   13.63 -        private OutputStream out;
   13.64 -        
   13.65 -        public MC(URL u) {
   13.66 -            super(u);
   13.67 -            out = outputs.get(u.toExternalForm());
   13.68 -            if (out == null) {
   13.69 -                out = new ByteArrayOutputStream();
   13.70 -                outputs.put(u.toExternalForm(), out);
   13.71 -            }
   13.72 -        }
   13.73 -
   13.74 -        public void connect() throws IOException {
   13.75 -            if (values != null) {
   13.76 -                return;
   13.77 -            }
   13.78 -            values = contents.remove(url.toExternalForm());
   13.79 -            if (values == null) {
   13.80 -                throw new IOException("No such content: " + url);
   13.81 -            }
   13.82 -        }
   13.83 -
   13.84 -        @Override
   13.85 -        public InputStream getInputStream() throws IOException {
   13.86 -            connect();
   13.87 -            return values;
   13.88 -        }
   13.89 -
   13.90 -        @Override
   13.91 -        public OutputStream getOutputStream() throws IOException {
   13.92 -            if (out == null) {
   13.93 -                out = new ByteArrayOutputStream();
   13.94 -            }
   13.95 -            return out;
   13.96 -        }
   13.97 -    }
   13.98 -}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/samples/exceptions/test/org/apidesign/exceptions/trycatchredo/api/IOManagerTest.java	Sun Feb 01 16:27:41 2009 +0100
    14.3 @@ -0,0 +1,64 @@
    14.4 +package org.apidesign.exceptions.trycatchredo.api;
    14.5 +
    14.6 +
    14.7 +import org.apidesign.exceptions.trycatchredo.usage.MemoryURL;
    14.8 +import java.awt.EventQueue;
    14.9 +import java.awt.event.ActionEvent;
   14.10 +import java.net.URL;
   14.11 +import javax.swing.Action;
   14.12 +import javax.swing.JOptionPane;
   14.13 +import org.apidesign.exceptions.trycatchredo.usage.QueryStream;
   14.14 +import org.junit.After;
   14.15 +import org.junit.Before;
   14.16 +import org.junit.Test;
   14.17 +import static org.junit.Assert.*;
   14.18 +
   14.19 +/**
   14.20 + *
   14.21 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   14.22 + */
   14.23 +public class IOManagerTest {
   14.24 +
   14.25 +    public IOManagerTest() {
   14.26 +    }
   14.27 +
   14.28 +    @Before
   14.29 +    public void setUp() {
   14.30 +        MemoryURL.initialize();
   14.31 +    }
   14.32 +
   14.33 +    @After
   14.34 +    public void tearDown() {
   14.35 +    }
   14.36 +
   14.37 +    @Test
   14.38 +    public void simpleWrite() throws Exception {
   14.39 +        URL u = new URL("memory://simpleWrite.txt");
   14.40 +        MemoryURL.registerURL(u.toExternalForm(), "", null);
   14.41 +        final Action a = IOManager.createSaveAction(u, "Hello World!");
   14.42 +        EventQueue.invokeAndWait(new Runnable() {
   14.43 +            public void run() {
   14.44 +                a.actionPerformed(new ActionEvent(this, 0, ""));
   14.45 +            }
   14.46 +        });
   14.47 +        String out = MemoryURL.getOutputForURL(u.toExternalForm());
   14.48 +        assertEquals("Hello World!", out);
   14.49 +    }
   14.50 +
   14.51 +    @Test
   14.52 +    public void writeWithAQuestion() throws Exception {
   14.53 +        URL u = new URL("memory://queryEncoding.txt");
   14.54 +
   14.55 +        MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
   14.56 +        final Action a = IOManager.createSaveAction(u, "Ask a Question");
   14.57 +        // simulate that the user clicks Yes to the reverse question in the dialog
   14.58 +        IOManager.setVisibleOption = JOptionPane.YES_OPTION;
   14.59 +        EventQueue.invokeAndWait(new Runnable() {
   14.60 +            public void run() {
   14.61 +                a.actionPerformed(new ActionEvent(this, 0, ""));
   14.62 +            }
   14.63 +        });
   14.64 +        String out = MemoryURL.getOutputForURL(u.toExternalForm());
   14.65 +        assertEquals("Text is reversed", "noitseuQ a ksA", out);
   14.66 +    }
   14.67 +}
   14.68 \ No newline at end of file