samples/trycatchredo/test/org/apidesign/exceptions/trycatchredo/api/IOManagerTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 25 Mar 2016 07:34:02 +0100
changeset 411 9eb6379b97f0
parent 312 0678c9589013
permissions -rw-r--r--
Prevent NPE on Aqua LaF by explicitly using Metal
jtulach@312
     1
package org.apidesign.exceptions.trycatchredo.api;
jtulach@312
     2
jtulach@312
     3
jtulach@312
     4
import org.apidesign.exceptions.trycatchredo.usage.MemoryURL;
jtulach@312
     5
import java.awt.EventQueue;
jtulach@312
     6
import java.awt.event.ActionEvent;
jtulach@312
     7
import java.net.URL;
jtulach@312
     8
import javax.swing.Action;
jtulach@312
     9
import javax.swing.JOptionPane;
jaroslav@411
    10
import javax.swing.UIManager;
jaroslav@411
    11
import javax.swing.UnsupportedLookAndFeelException;
jaroslav@411
    12
import javax.swing.plaf.metal.MetalLookAndFeel;
jtulach@312
    13
import org.apidesign.exceptions.trycatchredo.usage.QueryStream;
jtulach@312
    14
import org.junit.After;
jtulach@312
    15
import org.junit.Before;
jtulach@312
    16
import org.junit.Test;
jtulach@312
    17
import static org.junit.Assert.*;
jaroslav@411
    18
import org.junit.BeforeClass;
jtulach@312
    19
jtulach@312
    20
/**
jtulach@312
    21
 *
jtulach@312
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@312
    23
 */
jtulach@312
    24
public class IOManagerTest {
jtulach@312
    25
jtulach@312
    26
    public IOManagerTest() {
jtulach@312
    27
    }
jtulach@312
    28
jaroslav@411
    29
    @BeforeClass
jaroslav@411
    30
    public static void useMetalLaF() throws UnsupportedLookAndFeelException {
jaroslav@411
    31
        UIManager.setLookAndFeel(new MetalLookAndFeel());
jaroslav@411
    32
    }
jaroslav@411
    33
jtulach@312
    34
    @Before
jtulach@312
    35
    public void setUp() {
jtulach@312
    36
        MemoryURL.initialize();
jtulach@312
    37
    }
jtulach@312
    38
jtulach@312
    39
    @After
jtulach@312
    40
    public void tearDown() {
jtulach@312
    41
    }
jtulach@312
    42
jtulach@312
    43
    @Test
jtulach@312
    44
    public void simpleWrite() throws Exception {
jtulach@312
    45
        URL u = new URL("memory://simpleWrite.txt");
jtulach@312
    46
        MemoryURL.registerURL(u.toExternalForm(), "", null);
jtulach@312
    47
        final Action a = IOManager.createSaveAction(u, "Hello World!");
jtulach@312
    48
        EventQueue.invokeAndWait(new Runnable() {
jtulach@312
    49
            public void run() {
jtulach@312
    50
                a.actionPerformed(new ActionEvent(this, 0, ""));
jtulach@312
    51
            }
jtulach@312
    52
        });
jtulach@312
    53
        String out = MemoryURL.getOutputForURL(u.toExternalForm());
jtulach@312
    54
        assertEquals("Hello World!", out);
jtulach@312
    55
    }
jtulach@312
    56
jtulach@312
    57
    @Test
jtulach@312
    58
    public void writeWithAQuestion() throws Exception {
jtulach@312
    59
        URL u = new URL("memory://queryEncoding.txt");
jtulach@312
    60
jtulach@312
    61
        MemoryURL.registerURL(u.toExternalForm(), "", new QueryStream());
jtulach@312
    62
        final Action a = IOManager.createSaveAction(u, "Ask a Question");
jtulach@312
    63
        // simulate that the user clicks Yes to the reverse question in the dialog
jtulach@312
    64
        IOManager.setVisibleOption = JOptionPane.YES_OPTION;
jtulach@312
    65
        EventQueue.invokeAndWait(new Runnable() {
jtulach@312
    66
            public void run() {
jtulach@312
    67
                a.actionPerformed(new ActionEvent(this, 0, ""));
jtulach@312
    68
            }
jtulach@312
    69
        });
jtulach@312
    70
        String out = MemoryURL.getOutputForURL(u.toExternalForm());
jtulach@312
    71
        assertEquals("Text is reversed", "noitseuQ a ksA", out);
jtulach@312
    72
    }
jtulach@312
    73
}