KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * OpenKeyPanelTest.java
3  * JUnit based test
4  *
5  * Created on 4. Juli 2006, 04:11
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.io.*;
26 import java.util.*;
27
28 import javax.swing.*;
29
30 import junit.framework.*;
31
32 import org.netbeans.jemmy.*;
33 import org.netbeans.jemmy.operators.*;
34 import org.netbeans.jemmy.util.*;
35
36 /**
37  * @author Christian Schlichtherle
38  * @version @version@
39  * @since TrueZIP 6.1
40  */

41 public class OpenKeyPanelTest extends TestCase {
42     static {
43         JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); // shut up!
44
}
45
46     private OpenKeyPanel instance;
47     private JFrameOperator frame;
48     private JLabelOperator errorLabel;
49     private final ComponentChooser keyFileChooser
50                 = new NameComponentChooser("keyFileChooser");
51     
52     public OpenKeyPanelTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         instance = new OpenKeyPanel();
58         frame = showInstanceInFrame();
59         errorLabel = findErrorLabel(frame);
60     }
61
62     protected void tearDown() throws Exception JavaDoc {
63         frame.setVisible(false);
64     }
65
66     private JFrameOperator showInstanceInFrame() {
67         final JFrame frame = new JFrame();
68         frame.getContentPane().add(instance);
69         frame.pack();
70         frame.setLocationRelativeTo(null);
71         frame.setVisible(true);
72         return new JFrameOperator();
73     }
74
75     public void testResourceID() {
76         final String JavaDoc resourceID = "Hello world!";
77         instance.setResourceID(resourceID);
78         assertEquals(resourceID, instance.getResourceID());
79         
80         new JTextComponentOperator(frame, resourceID);
81     }
82
83     /**
84      * Test of setError method, of class de.schlichtherle.key.passwd.swing.OpenKeyPanel.
85      */

86     public void testSetError() {
87         instance.setError("This is a test error message!");
88         assertFalse(isBlank(errorLabel.getText()));
89         new JTextFieldOperator(frame).typeText("secret");
90         assertTrue(isBlank(errorLabel.getText()));
91
92         instance.setError("This is a test error message!");
93         assertFalse(isBlank(errorLabel.getText()));
94         new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); // select tab for key files
95
new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
96
new JFileChooserOperator().chooseFile("file");
97         assertTrue(isBlank(errorLabel.getText()));
98     }
99
100     private JLabelOperator findErrorLabel(final JFrameOperator frame) {
101         final String JavaDoc error = "error";
102         instance.setError(error);
103         final JLabelOperator errorLabel = new JLabelOperator(frame, error);
104         ((JFrame) frame.getSource()).pack();
105         instance.setError(null);
106         return errorLabel;
107     }
108
109     private static boolean isBlank(String JavaDoc s) {
110         return s == null || s.trim().length() <= 0;
111     }
112
113     /**
114      * Test of getOpenKey method, of class de.schlichtherle.key.passwd.swing.OpenKeyPanel.
115      */

116     public void testPasswd() {
117         String JavaDoc passwd;
118         Object JavaDoc result;
119         
120         final JLabelOperator errorLabel = findErrorLabel(frame);
121
122         // Check default.
123
result = instance.getOpenKey();
124         assertTrue(result instanceof char[]);
125         assertTrue(Arrays.equals("".toCharArray(), (char[]) result));
126         assertTrue(isBlank(errorLabel.getText()));
127
128         passwd = "secret";
129         new JPasswordFieldOperator(frame).setText(passwd);
130         result = instance.getOpenKey();
131         assertTrue(result instanceof char[]);
132         assertTrue(Arrays.equals(passwd.toCharArray(), (char[]) result));
133         assertTrue(isBlank(errorLabel.getText()));
134     }
135
136     /**
137      * Test of getOpenKey method, of class de.schlichtherle.key.passwd.swing.OpenKeyPanel.
138      */

139     public void testKeyFile() {
140         new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); // select tab for key files
141

142         new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
143
new JFileChooserOperator().chooseFile("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"$%&/()=?");
144         assertTrue(isBlank(errorLabel.getText()));
145         assertNull(instance.getOpenKey());
146         assertNotNull(errorLabel.getText());
147
148         new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
149
JFileChooserOperator fc = new JFileChooserOperator();
150         File[] files = fc.getFiles();
151         fc.cancel(); // close file chooser
152

153         for (int i = 0, l = files.length; i < l; i++) {
154             final File file = files[i];
155             if (!file.isFile())
156                 continue;
157
158             new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
159
fc = new JFileChooserOperator();
160             fc.setSelectedFile(file);
161             fc.approve(); // close file chooser
162
final Object JavaDoc key = instance.getOpenKey();
163             if (key != null) {
164                 assertTrue(key instanceof byte[]);
165                 assertTrue(isBlank(errorLabel.getText()));
166             } else {
167                 assertFalse(isBlank(errorLabel.getText()));
168             }
169         }
170     }
171
172     /**
173      * Test of is/setKeyChangeRequested method, of class de.schlichtherle.key.passwd.swing.OpenKeyPanel.
174      */

175     public void testKeyChangeRequested() {
176         assertFalse(instance.isKeyChangeRequested());
177         assertFalse(new JCheckBoxOperator(frame).isSelected());
178
179         instance.setKeyChangeRequested(true);
180         assertTrue(instance.isKeyChangeRequested());
181         assertTrue(new JCheckBoxOperator(frame).isSelected());
182
183         instance.setKeyChangeRequested(false);
184         assertFalse(instance.isKeyChangeRequested());
185         assertFalse(new JCheckBoxOperator(frame).isSelected());
186
187         new JCheckBoxOperator(frame).setSelected(true);
188         assertTrue(instance.isKeyChangeRequested());
189
190         new JCheckBoxOperator(frame).setSelected(false);
191         assertFalse(instance.isKeyChangeRequested());
192     }
193
194     /**
195      * Test of get/setExtraDataUI method, of class de.schlichtherle.key.passwd.swing.OpenKeyPanel.
196      */

197     public void testExtraDataUI() {
198         final JComponent ui = new AesKeyStrengthPanel();
199         instance.setExtraDataUI(ui);
200         frame.pack();
201         assertSame(ui, instance.getExtraDataUI());
202         
203         new JComboBoxOperator(frame); // find combo box
204
}
205 }
206
Popular Tags