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