KickJava   Java API By Example, From Geeks To Geeks.

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


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

16
17 package de.schlichtherle.key.passwd.swing;
18
19 import java.awt.*;
20 import java.util.*;
21
22 import javax.swing.*;
23
24 import junit.framework.*;
25
26 import org.netbeans.jemmy.*;
27 import org.netbeans.jemmy.operators.*;
28 import org.netbeans.jemmy.operators.JDialogOperator.JDialogFinder;
29 import org.netbeans.jemmy.util.*;
30
31 /**
32  * @author Christian Schlichtherle
33  * @version @version@
34  * @since TrueZIP 6.1
35  */

36 public class RemoteControl extends Assert implements Runnable JavaDoc {
37     private static int counter;
38
39     private final int index;
40
41     private static final Random rnd = new Random();
42
43     /** The identifier of the protected resource. */
44     public final String JavaDoc id;
45
46     /**
47      * Contains non-<code>null</code> if and only if <code>run()</code> has
48      * terminated because an assertion error happened.
49      */

50     public Throwable JavaDoc throwable;
51
52     public RemoteControl(final String JavaDoc id) {
53         this.index = counter++;
54         this.id = id;
55     }
56
57     public void run() {
58         try {
59             runIt();
60         } catch (Throwable JavaDoc t) {
61             throwable = t;
62         }
63     }
64
65     private void runIt() throws AssertionError JavaDoc {
66         JDialogOperator dialog;
67         JPasswordFieldOperator passwd1, passwd2;
68         String JavaDoc passwd;
69         JCheckBoxOperator newPasswd;
70
71         dialog = waitCreateKeyDialog();
72
73         // Check no error message.
74
assertNull(findErrorMessage(dialog));
75
76         createResourceHook(dialog);
77
78         // Enter mismatching passwords.
79
passwd1 = new JPasswordFieldOperator(dialog, 0);
80         passwd2 = new JPasswordFieldOperator(dialog, 1);
81         passwd1.typeText("foobar1");
82         passwd2.clearText();
83         passwd2.typeText("foobar2");
84
85         pushDefaultButton(dialog);
86
87         dialog = waitCreateKeyDialog();
88
89         // Check error message.
90
assertNotNull(findErrorMessage(dialog));
91         
92         // Enter new password twice.
93
passwd = "secret " + index;
94         passwd1 = new JPasswordFieldOperator(dialog, 0);
95         passwd2 = new JPasswordFieldOperator(dialog, 1);
96         passwd1.typeText(passwd);
97
98         // Check no error message anymore.
99
assertNull(findErrorMessage(dialog));
100
101         passwd2.clearText();
102         passwd2.typeText(passwd);
103
104         pushDefaultButton(dialog);
105
106         dialog = waitOpenKeyDialog();
107
108         // Check no error message.
109
assertNull(findErrorMessage(dialog));
110
111         // Check no new password requested.
112
newPasswd = new JCheckBoxOperator(dialog);
113         assertFalse(newPasswd.isSelected());
114
115         // Request new password.
116
newPasswd.push();
117
118         // Enter wrong password.
119
passwd1 = new JPasswordFieldOperator(dialog, 0);
120         passwd2 = null;
121         passwd1.clearText();
122         passwd1.typeText("wrong");
123
124         pushDefaultButton(dialog);
125
126         dialog = waitOpenKeyDialog();
127
128         // Check error message.
129
assertNotNull(findErrorMessage(dialog));
130
131         // Type correct password.
132
passwd1 = new JPasswordFieldOperator(dialog, 0);
133         passwd2 = null;
134         passwd1.clearText();
135         passwd1.typeText(passwd);
136
137         // Check no error message anymore.
138
assertNull(findErrorMessage(dialog));
139
140         // Check still new password requested.
141
newPasswd = new JCheckBoxOperator(dialog);
142         assertTrue(newPasswd.isSelected());
143
144         pushDefaultButton(dialog);
145
146         dialog = waitCreateKeyDialog();
147
148         // Check no error message.
149
assertNull(findErrorMessage(dialog));
150
151         overwriteResourceHook(dialog);
152
153         // Enter too short passwords.
154
passwd1 = new JPasswordFieldOperator(dialog, 0);
155         passwd2 = new JPasswordFieldOperator(dialog, 1);
156         passwd1.typeText("short"); // 5 chars is too short - min 6!
157
passwd2.clearText();
158         passwd2.typeText("short");
159
160         pushDefaultButton(dialog);
161
162         dialog = waitCreateKeyDialog();
163
164         // Check error message.
165
assertNotNull(findErrorMessage(dialog));
166
167         if (0 == rnd.nextInt(2)) {
168             // Enter new password twice.
169
passwd = "top secret " + index;
170             passwd1 = new JPasswordFieldOperator(dialog, 0);
171             passwd2 = new JPasswordFieldOperator(dialog, 1);
172             passwd1.typeText(passwd);
173
174             // Check no error message anymore.
175
assertNull(findErrorMessage(dialog));
176
177             passwd2.clearText();
178             passwd2.typeText(passwd);
179
180             pushDefaultButton(dialog);
181         } else {
182             // Close dialog in order to reuse the old key.
183
dialog.close();
184         }
185     }
186
187     protected void createResourceHook(JDialogOperator dialog) {
188     }
189
190     protected void overwriteResourceHook(JDialogOperator dialog) {
191     }
192
193     private JDialogOperator waitCreateKeyDialog() {
194         final JDialogOperator dialog = waitDialog();
195         assertNotNull("Expected Create Key Dialog for \"" + id + "\"",
196                 findPasswdField(dialog, 1));
197         return dialog;
198     }
199
200     private JDialogOperator waitOpenKeyDialog() {
201         final JDialogOperator dialog = waitDialog();
202         assertNull("Expected Open Key Dialog for \"" + id + "\"",
203                 findPasswdField(dialog, 1));
204         return dialog;
205     }
206
207     /**
208      * Wait until a dialog pops up which contains our resource ID in
209      * a text component.
210      */

211     private JDialogOperator waitDialog() {
212         // Wait for dialog (index position 0).
213
// Doing this twice seems to help with some timing issues in Jemmy.
214
new QueueTool().waitEmpty(); // waitEmpty(100) doesn't always work
215
JDialogOperator dialog = new JDialogOperator(new ByResourceIDFinder());
216         new QueueTool().waitEmpty(); // waitEmpty(100) doesn't always work
217
/*try {
218             Thread.sleep(100);
219         } catch (InterruptedException ignored) {
220         }*/

221
222         // Ensure that no other dialog is showing, i.e. that there is no
223
// dialog at index position 1.
224
assertNull(JDialogOperator.findJDialog(
225                     new JDialogFinder(),
226                     1));
227
228         return dialog;
229     }
230
231     private static JPasswordFieldOperator findPasswdField(final JDialogOperator dialog, final int i) {
232         final JPasswordField field = JPasswordFieldOperator.findJPasswordField(
233                     (Container) dialog.getSource(),
234                     new JPasswordFieldOperator.JPasswordFieldFinder(),
235                     i);
236         return field != null ? new JPasswordFieldOperator(field) : null;
237     }
238
239     private class ByResourceIDFinder implements ComponentChooser {
240         public boolean checkComponent(Component comp) {
241             return null != JTextComponentOperator.findJTextComponent(
242                         (Container) comp,
243                         new JTextComponentOperator.JTextComponentByTextFinder(
244                             id,
245                             new Operator.DefaultStringComparator(true, true)));
246         }
247
248         public String JavaDoc getDescription() {
249             return "Container with Component with exact text \"" + id + "\".";
250         }
251     }
252
253     private static String JavaDoc findErrorMessage(final ContainerOperator dialog) {
254         final JLabelOperator errorLabel
255                 = new JLabelOperator(dialog, new NameComponentChooser("error"));
256         if (errorLabel == null)
257             return null;
258         final String JavaDoc error = errorLabel.getText();
259         if (error == null || error.trim().length() <= 0)
260             return null;
261         return error;
262     }
263
264     private void pushDefaultButton(JDialogOperator dialog) {
265         final JButtonOperator ok = new JButtonOperator(
266                 dialog.getRootPane().getDefaultButton());
267         while (!ok.isEnabled()) {
268             try {
269                 Thread.sleep(50);
270             } catch (InterruptedException JavaDoc ignored) {
271             }
272         }
273         ok.push();
274     }
275 }
Popular Tags