KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

117     public void testResetPasswd() {
118         // Enter mismatching passwords.
119
new JPasswordFieldOperator(frame, 0).setText("foofoo");
120         new JPasswordFieldOperator(frame, 1).setText("barbar");
121         
122         // Reset and check state.
123
instance.resetPasswd();
124         assertTrue("".equals(new JPasswordFieldOperator(frame, 0).getText()));
125         assertTrue("".equals(new JPasswordFieldOperator(frame, 1).getText()));
126     }
127
128     /**
129      * Test of getCreateKey method, of class de.schlichtherle.key.passwd.swing.CreateKeyPanel.
130      */

131     public void testPasswd() {
132         String JavaDoc passwd;
133         Object JavaDoc result;
134
135         // Check default.
136
result = instance.getCreateKey();
137         assertNull(result);
138         assertFalse(isBlank(errorLabel.getText()));
139
140         // Enter mismatching passwords.
141
new JPasswordFieldOperator(frame, 0).setText("foofoo");
142         new JPasswordFieldOperator(frame, 1).setText("barbar");
143         result = instance.getCreateKey();
144         assertNull(result);
145         assertFalse(isBlank(errorLabel.getText()));
146
147         // Enter matching passwords, too short.
148
passwd = "secre"; // 5 chars is too short
149
new JPasswordFieldOperator(frame, 0).setText(passwd);
150         new JPasswordFieldOperator(frame, 1).setText(passwd);
151         result = instance.getCreateKey();
152         assertNull(result);
153         assertFalse(isBlank(errorLabel.getText()));
154
155         // Enter matching passwords, long enough.
156
passwd = "secret"; // min 6 chars is OK
157
new JPasswordFieldOperator(frame, 0).setText(passwd);
158         new JPasswordFieldOperator(frame, 1).setText(passwd);
159         result = instance.getCreateKey();
160         assertTrue(result instanceof char[]);
161         assertTrue(Arrays.equals(passwd.toCharArray(), (char[]) result));
162         assertTrue(isBlank(errorLabel.getText()));
163     }
164
165     /**
166      * Test of getCreateKey method, of class de.schlichtherle.key.passwd.swing.CreateKeyPanel.
167      */

168     public void testGetFileAsCreateKey() {
169         new JTabbedPaneOperator(frame).selectPage(AuthenticationPanel.AUTH_KEY_FILE); // select tab for key files
170

171         new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
172
new JFileChooserOperator().chooseFile("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"$%&/()=?");
173         assertTrue(isBlank(errorLabel.getText()));
174         assertNull(instance.getCreateKey());
175         assertFalse(isBlank(errorLabel.getText()));
176
177         new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
178
JFileChooserOperator fc = new JFileChooserOperator();
179         File[] files = fc.getFiles();
180         fc.cancel(); // revert to password panel
181

182         for (int i = 0, l = files.length; i < l; i++) {
183             final File file = files[i];
184             if (!file.isFile())
185                 continue;
186
187             new JButtonOperator(frame, keyFileChooser).push(); // open file chooser
188
fc = new JFileChooserOperator();
189             fc.setSelectedFile(file);
190             fc.approve();
191             final Object JavaDoc key = instance.getCreateKey();
192             if (key != null) {
193                 assertTrue(key instanceof byte[]);
194                 assertTrue(isBlank(errorLabel.getText()));
195             } else {
196                 assertFalse(isBlank(errorLabel.getText()));
197             }
198         }
199     }
200
201     /**
202      * Test of get/setExtraDataUI method, of class de.schlichtherle.key.passwd.swing.CreateKeyPanel.
203      */

204     public void testExtraDataUI() {
205         final JComponent ui = new AesKeyStrengthPanel();
206         instance.setExtraDataUI(ui);
207         frame.pack();
208         assertSame(ui, instance.getExtraDataUI());
209         
210         new JComboBoxOperator(frame); // find combo box
211
}
212 }
213
Popular Tags