1 7 22 23 package de.schlichtherle.key.passwd.swing; 24 25 import java.awt.*; 26 import java.lang.ref.*; 27 import java.io.*; 28 import java.util.logging.*; 29 30 import javax.swing.*; 31 32 import junit.framework.*; 33 34 import org.netbeans.jemmy.*; 35 import org.netbeans.jemmy.operators.*; 36 import org.netbeans.jemmy.util.*; 37 38 43 public class AuthenticationPanelTest extends TestCase { 44 static { 45 JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); } 47 48 private static final Logger logger 49 = Logger.getLogger(AuthenticationPanelTest.class.getName()); 50 51 static final File rootDir; 52 static { 53 if (File.separatorChar == '\\') 54 rootDir = new File("C:\\"); 55 else 56 rootDir = new File("/"); 57 } 58 59 private final ComponentChooser keyFileChooser 60 = new NameComponentChooser("keyFileChooser"); 61 62 public AuthenticationPanelTest(String testName) { 63 super(testName); 64 } 65 66 69 public void testGetFileChooser() throws InterruptedException { 70 final ReferenceQueue queue = new ReferenceQueue(); 71 final PhantomReference ref = initQueue(queue, rootDir); 72 73 byte[] bfo; 74 int i = 1; 75 while (null == queue.poll()) { 76 try { 79 bfo = new byte[i * 1024 * 1024]; 80 i++; } catch (OutOfMemoryError oome) { 82 System.runFinalization(); 92 assertNotNull(queue.poll()); break; 94 } 95 96 bfo = null; 99 100 System.gc(); 101 } 102 assert null == queue.poll(); logger.fine("Successfully allocated " + i + " megabytes before JFileChooser was discarded."); 104 105 JFileChooser fc = AuthenticationPanel.getFileChooser(); 107 assertEquals( 108 "Newly instantiated JFileChooser needs to have same current directory as previous instance!", 109 rootDir, fc.getCurrentDirectory()); 110 } 111 112 private PhantomReference initQueue( 113 final ReferenceQueue queue, 114 final File dir) { 115 JFileChooser fc = AuthenticationPanel.getFileChooser(); 116 fc.setCurrentDirectory(dir); 117 118 final PhantomReference ref = new PhantomReference(fc, queue); 119 120 fc = null; 121 System.gc(); 122 123 fc = AuthenticationPanel.getFileChooser(); 124 fc.setCurrentDirectory(dir); 125 126 fc = null; 127 System.gc(); 128 129 assertNull( 130 "Initial JFileChooser instance should not yet have been thrown away!", 131 queue.poll()); 132 133 return ref; 134 } 135 136 public void testTabbedPane() { 137 final String text = "Hello world!"; 138 EventQueue.invokeLater(new Runnable () { 139 public void run() { 140 final AuthenticationPanel instance = new AuthenticationPanel(); 141 142 JPanel passwdPanel = null; 143 try { 144 instance.setPasswdPanel(passwdPanel); 145 fail("Calling setPasswdPanel(null) should throw an NPE!"); 146 } catch (NullPointerException npe) { 147 } 148 149 passwdPanel = new JPanel(); 150 passwdPanel.add(new JLabel(text)); 151 instance.setPasswdPanel(passwdPanel); 152 153 final JFrame frame = new JFrame(); 154 frame.getContentPane().add(instance); 155 frame.pack(); 156 frame.setLocationRelativeTo(null); 157 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 158 frame.setVisible(true); 159 } 160 }); 161 162 final JFrameOperator frame = new JFrameOperator(); 163 164 new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); new JButtonOperator(frame, keyFileChooser).push(); JFileChooserOperator fc = new JFileChooserOperator(); 167 assertSame(AuthenticationPanel.getFileChooser(), fc.getSource()); 168 fc.cancel(); 169 new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_PASSWD); new JLabelOperator(frame, text); 171 fc = null; 172 173 new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); new JButtonOperator(frame, keyFileChooser).push(); fc = new JFileChooserOperator(); 176 final File file = new File(rootDir, "test"); 177 fc.setSelectedFile(file); 178 fc.approve(); 179 JTextFieldOperator tfOp = new JTextFieldOperator(frame); 180 assertEquals(file.getPath(), tfOp.getText()); 181 fc = null; 182 183 new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_PASSWD); new JLabelOperator(frame, text); 185 186 } 188 } 189 | Popular Tags |