1 7 22 23 package de.schlichtherle.key.passwd.swing; 24 25 import java.awt.Window ; 26 import java.util.Random ; 27 28 import javax.swing.JDialog ; 29 import javax.swing.JFrame ; 30 31 import junit.framework.*; 32 33 import org.netbeans.jemmy.JemmyProperties; 34 import org.netbeans.jemmy.TestOut; 35 36 41 public class PromptingKeyManagerTest extends TestCase { 42 static { 43 JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); } 45 46 public static Test suite() throws Exception { 47 TestSuite suite = new TestSuite(PromptingKeyManagerTest.class); 48 50 51 { 53 String 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 testName) { 69 super(testName); 70 } 71 72 protected void setUp() throws Exception { 73 JemmyProperties.setCurrentDispatchingModel(JemmyProperties.getDefaultDispatchingModel()); 74 PromptingKeyManager.setParentWindow(null); 75 } 76 77 protected void tearDown() throws Exception { 78 PromptingKeyManager.resetAndRemoveKeyProviders(); 79 } 80 81 84 public void testParentWindow() { 85 PromptingKeyManager.setParentWindow(null); 86 Window result = PromptingKeyManager.getParentWindow(); 87 assertNotNull(result); 88 assertFalse(result.isVisible()); 89 90 final JFrame frame = new JFrame (); 91 frame.setVisible(true); 92 final JDialog dialog = new JDialog (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 116 public void testMultithreadedKeyMgmtLifeCycle() { 117 JemmyProperties.setCurrentDispatchingModel(JemmyProperties.ROBOT_MODEL_MASK); 119 121 testMultithreadedKeyMgmtLifeCycle(10); 122 } 123 124 private void testMultithreadedKeyMgmtLifeCycle(final int nThreads) { 125 final String RESOURCE_PREFIX = "Resource ID "; 126 final Random rnd = new Random (); 127 128 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 resource = RESOURCE_PREFIX + i; 135 136 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 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 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 165 } 166 167 171 172 for (int i = 0; i < nThreads; i++) { 174 final RemoteControlThread thread = rcThreads[i]; 175 176 thread.start(); 177 181 } 182 183 boolean kmlcThreadStillAlive = false; 186 boolean threadDiedWithException = false; 187 for (int i = 0; i < nThreads; ) { 188 final RemoteControlThread rcThread = rcThreads[i]; 190 try { 191 rcThread.join(); 192 } catch (InterruptedException ignored) { 193 continue; } 195 if (rcThread.getThrowable() != null) 196 threadDiedWithException = true; 197 198 final KeyMgmtLifeCycleThread kmlcThread = kmlcThreads[i]; 200 try { 201 kmlcThread.join(20000); 202 } catch (InterruptedException ignored) { 203 continue; } 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 |