KickJava   Java API By Example, From Geeks To Geeks.

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


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

41 public class PromptingKeyManagerTest extends TestCase {
42     static {
43         JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); // shut up!
44
}
45
46     public static Test suite() throws Exception JavaDoc {
47         TestSuite suite = new TestSuite(PromptingKeyManagerTest.class);
48         /*TestSuite suite = new TestSuite();
49         suite.addTest(new PromptingKeyManagerTest("testParentWindow"));*/

50
51         // Who says you can't have fun with automated GUI testing? :-)
52
{
53             String JavaDoc feedback;
54             feedback = "de.schlichtherle.key.passwd.swing.InvalidOpenKeyFeedback";
55             System.setProperty(feedback,
56                     System.getProperty(feedback,
57                         "de.schlichtherle.key.passwd.swing.HurlingWindowFeedback"));
58
59             feedback = "de.schlichtherle.key.passwd.swing.InvalidCreateKeyFeedback";
60             System.setProperty(feedback,
61                     System.getProperty(feedback,
62                         "de.schlichtherle.key.passwd.swing.HurlingWindowFeedback"));
63         }
64         
65         return suite;
66     }
67     
68     public PromptingKeyManagerTest(String JavaDoc testName) {
69         super(testName);
70     }
71
72     protected void setUp() throws Exception JavaDoc {
73         JemmyProperties.setCurrentDispatchingModel(JemmyProperties.getDefaultDispatchingModel());
74         PromptingKeyManager.setParentWindow(null);
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78         PromptingKeyManager.resetAndRemoveKeyProviders();
79     }
80
81     /**
82      * Test of getParentWindow method, of class de.schlichtherle.key.passwd.swing.PromptingKeyManager.
83      */

84     public void testParentWindow() {
85         PromptingKeyManager.setParentWindow(null);
86         Window JavaDoc result = PromptingKeyManager.getParentWindow();
87         assertNotNull(result);
88         assertFalse(result.isVisible());
89
90         final JFrame JavaDoc frame = new JFrame JavaDoc();
91         frame.setVisible(true);
92         final JDialog JavaDoc dialog = new JDialog JavaDoc(frame);
93         PromptingKeyManager.setParentWindow(dialog);
94
95         assertFalse(dialog.isVisible());
96         result = PromptingKeyManager.getParentWindow();
97         assertSame(frame, result);
98
99         dialog.setVisible(true);
100         result = PromptingKeyManager.getParentWindow();
101         assertSame(dialog, result);
102
103         dialog.setVisible(false);
104         frame.setVisible(false);
105     }
106
107     /**
108      * Concurrently start some threads which open some dialogs to prompt the
109      * user for the keys during the typical life cycle of a protected resource
110      * and its associated key.
111      * Ensure that only one window is showing at any time.
112      * <p>
113      * This test works with Jemmy's Robot dispatching model only.
114      * So beware not to touch the GUI windows concurrently.
115      */

116     public void testMultithreadedKeyMgmtLifeCycle() {
117         // This test only works with the Robot Dispatching Model.
118
JemmyProperties.setCurrentDispatchingModel(JemmyProperties.ROBOT_MODEL_MASK);
119         //JemmyProperties.setCurrentTimeout("WindowWaiter.WaitWindowTimeout", 180000);
120

121         testMultithreadedKeyMgmtLifeCycle(10);
122     }
123
124     private void testMultithreadedKeyMgmtLifeCycle(final int nThreads) {
125         final String JavaDoc RESOURCE_PREFIX = "Resource ID ";
126         final Random JavaDoc rnd = new Random JavaDoc();
127
128         // Init required threads for each resource.
129
final RemoteControlThread[] rcThreads
130                 = new RemoteControlThread[nThreads];
131         final KeyMgmtLifeCycleThread[] kmlcThreads
132                 = new KeyMgmtLifeCycleThread[nThreads];
133         for (int i = 0; i < nThreads; i++) {
134             final String JavaDoc resource = RESOURCE_PREFIX + i;
135
136             // Init, but don't yet start Remote Control Thread.
137
final RemoteControl rc = (i % 2 == 0)
138                     ? new AesRemoteControl(resource)
139                     : new RemoteControl(resource);
140             final RemoteControlThread rcThread
141                     = new RemoteControlThread(rc);
142             rcThreads[i] = rcThread;
143
144             // Init, but don't yet start Key Management Life Cycle Thread.
145
final KeyMgmtLifeCycle kmlc = (i % 2 == 0)
146                     ? new AesKeyMgmtLifeCycle(resource)
147                     : new KeyMgmtLifeCycle(resource);
148             final KeyMgmtLifeCycleThread rlcThread
149                     = new KeyMgmtLifeCycleThread(kmlc);
150             kmlcThreads[i] = rlcThread;
151         }
152
153         // Start Key Management Life Cycle Threads in arbitrary order.
154
for (int i = 0; i < nThreads; i++) {
155             final int j = i + rnd.nextInt(nThreads - i);
156             final KeyMgmtLifeCycleThread thread = kmlcThreads[j];
157             kmlcThreads[j] = kmlcThreads[i];
158             kmlcThreads[i] = thread;
159
160             thread.start();
161             /*try {
162                 Thread.sleep(100);
163             } catch (InterruptedException ignored) {
164             }*/

165         }
166
167         /*try {
168             Thread.sleep(5000);
169         } catch (InterruptedException ignored) {
170         }*/

171
172         // Start Remote Control Threads in order.
173
for (int i = 0; i < nThreads; i++) {
174             final RemoteControlThread thread = rcThreads[i];
175
176             thread.start();
177             /*try {
178                 Thread.sleep(100);
179             } catch (InterruptedException ignored) {
180             }*/

181         }
182
183         // Wait for each thread to finish and check that key prompting hasn't
184
// been cancelled by the user and that no exceptions happened.
185
boolean kmlcThreadStillAlive = false;
186         boolean threadDiedWithException = false;
187         for (int i = 0; i < nThreads; ) {
188             // Wait for Remote Control Thread to be finished..
189
final RemoteControlThread rcThread = rcThreads[i];
190             try {
191                 rcThread.join();
192             } catch (InterruptedException JavaDoc ignored) {
193                 continue; // repeat
194
}
195             if (rcThread.getThrowable() != null)
196                 threadDiedWithException = true;
197
198             // Wait for Key Management Life Cycle Thread to be finished.
199
final KeyMgmtLifeCycleThread kmlcThread = kmlcThreads[i];
200             try {
201                 kmlcThread.join(20000);
202             } catch (InterruptedException JavaDoc ignored) {
203                 continue; // repeat
204
}
205             if (kmlcThread.isAlive())
206                 kmlcThreadStillAlive = true;
207             else if (kmlcThread.getThrowable() != null)
208                 threadDiedWithException = true;
209             
210             i++;
211         }
212         
213         if (threadDiedWithException)
214             fail("Some threads terminated with an exception!");
215         if (kmlcThreadStillAlive)
216             fail("The life cycles of some resources and their associated keys haven't finished!");
217     }
218 }
219
Popular Tags