KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > swing > AuthenticationPanelTest


1 /*
2  * AuthenticationPanelTest.java
3  * JUnit based test
4  *
5  * Created on 3. Juli 2006, 23:15
6  */

7 /*
8  * Copyright 2006 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

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 /**
39  * @author Christian Schlichtherle
40  * @version @version@
41  * @since TrueZIP 6.1
42  */

43 public class AuthenticationPanelTest extends TestCase {
44     static {
45         JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); // shut up!
46
}
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 JavaDoc testName) {
63         super(testName);
64     }
65
66     /**
67      * Test of getFileChooser method, of class de.schlichtherle.key.passwd.swing.AuthenticationPanel.
68      */

69     public void testGetFileChooser() throws InterruptedException JavaDoc {
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             // Allocate big fat object in order to cause the internal
77
// cache for the file chooser to be cleared.
78
try {
79                 bfo = new byte[i * 1024 * 1024];
80                 i++; // is not reached on OOME!
81
} catch (OutOfMemoryError JavaDoc oome) {
82                 //oome.printStackTrace();
83
// SoftReferences (which are used by getFileChooser) are
84
// guaranteed to be cleared BEFORE an OOME is thrown.
85
// However, the additional chunk of memory we are requesting
86
// may be too big, in which case the reference is first cleared
87
// and then immediately an OOME is thrown.
88
// When the SoftReference has been cleared, the referent is
89
// made eligible for finalization.
90
// Run the finalization now and assert this:
91
System.runFinalization();
92                 assertNotNull(queue.poll()); // JFileChooser has been finalized
93
break;
94             }
95
96             // Release memory again in order to allow the JVM to operate
97
// normally.
98
bfo = null;
99
100             System.gc();
101         }
102         assert null == queue.poll(); // previous poll() has removed the reference
103
logger.fine("Successfully allocated " + i + " megabytes before JFileChooser was discarded.");
104         
105         // Now ask for a file chooser again.
106
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 JavaDoc text = "Hello world!";
138         EventQueue.invokeLater(new Runnable JavaDoc() {
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 JavaDoc 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); // select tab for key files
165
new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
166
JFileChooserOperator fc = new JFileChooserOperator();
167         assertSame(AuthenticationPanel.getFileChooser(), fc.getSource());
168         fc.cancel();
169         new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_PASSWD); // select tab for passwords
170
new JLabelOperator(frame, text);
171         fc = null;
172
173         new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); // select tab for key files
174
new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
175
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); // select tab for passwords
184
new JLabelOperator(frame, text);
185         
186         //frame.close();
187
}
188 }
189
Popular Tags