KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AesKeyStrengthPanelTest.java
3  * JUnit based test
4  *
5  * Created on 27. Juni 2006, 21:14
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 de.schlichtherle.key.*;
26
27 import java.awt.*;
28 import java.lang.reflect.*;
29
30 import javax.swing.*;
31
32 import junit.framework.*;
33 import junit.framework.Test;
34
35 import org.netbeans.jemmy.*;
36 import org.netbeans.jemmy.operators.*;
37
38 /**
39  * @author Christian Schlichtherle
40  * @version @version@
41  * @since TrueZIP 6.1
42  */

43 public class AesKeyStrengthPanelTest extends TestCase {
44     static {
45         JemmyProperties.setCurrentOutput(TestOut.getNullOutput()); // shut up!
46
}
47
48     private AesKeyStrengthPanel instance;
49     private JFrame frame;
50     private JFrameOperator frameOp;
51     
52     public AesKeyStrengthPanelTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         EventQueue.invokeLater(new Runnable JavaDoc() {
58             public void run() {
59                 instance = new AesKeyStrengthPanel();
60                 frame = new JFrame();
61                 frame.getContentPane().add(instance);
62                 frame.pack();
63                 frame.setLocationRelativeTo(null);
64                 frame.setVisible(true);
65             }
66         });
67
68         frameOp = new JFrameOperator(); // wait for JFrame
69
}
70
71     protected void tearDown() throws Exception JavaDoc {
72         EventQueue.invokeAndWait(new Runnable JavaDoc() {
73             public void run() {
74                 frame.setVisible(false);
75                 frame.dispose();
76             }
77         });
78     }
79
80     public static Test suite() throws Exception JavaDoc {
81         // THIS DOESN'T WORK RELIABLY!
82
// Increase timeout values to see what's going on.
83
// Otherwise everything happens very fast.
84
/*try {
85             JemmyProperties.getCurrentTimeouts().loadDebugTimeouts();
86         } catch (IOException failure) {
87             throw new UndeclaredThrowableException(failure);
88         }*/

89
90         TestSuite suite = new TestSuite(AesKeyStrengthPanelTest.class);
91         
92         return suite;
93     }
94
95     /**
96      * Test of get/setKeyStrength method, of class de.schlichtherle.key.passwd.swing.AesKeyStrengthPanel.
97      */

98     public void testKeyStrength() throws Exception JavaDoc {
99         int keyStrength, expResult, selection;
100
101         JComboBoxOperator comboBoxOp = new JComboBoxOperator(frameOp);
102
103         //
104
// Check default.
105
//
106

107         expResult = AesKeyProvider.KEY_STRENGTH_256;
108         keyStrength = instance.getKeyStrength();
109         assertEquals(expResult, keyStrength);
110         selection = comboBoxOp.getSelectedIndex();
111         assertEquals(expResult, selection);
112         sleep();
113
114         //
115
// Set key strength via API and check API and GUI.
116
//
117

118         expResult = AesKeyProvider.KEY_STRENGTH_128;
119         instance.setKeyStrength(expResult);
120         keyStrength = instance.getKeyStrength();
121         assertEquals(expResult, keyStrength);
122         selection = comboBoxOp.getSelectedIndex();
123         assertEquals(expResult, selection);
124         sleep();
125
126         expResult = AesKeyProvider.KEY_STRENGTH_192;
127         instance.setKeyStrength(expResult);
128         keyStrength = instance.getKeyStrength();
129         assertEquals(expResult, keyStrength);
130         selection = comboBoxOp.getSelectedIndex();
131         assertEquals(expResult, selection);
132         sleep();
133
134         expResult = AesKeyProvider.KEY_STRENGTH_256;
135         instance.setKeyStrength(expResult);
136         keyStrength = instance.getKeyStrength();
137         assertEquals(expResult, keyStrength);
138         selection = comboBoxOp.getSelectedIndex();
139         assertEquals(expResult, selection);
140         sleep();
141
142         expResult = AesKeyProvider.KEY_STRENGTH_256;
143         try {
144             instance.setKeyStrength(AesKeyProvider.KEY_STRENGTH_128 - 1);
145             fail("Expected IllegalArgumentException");
146         } catch (IllegalArgumentException JavaDoc expected) {
147         }
148         keyStrength = instance.getKeyStrength();
149         assertEquals(expResult, keyStrength);
150         selection = comboBoxOp.getSelectedIndex();
151         assertEquals(expResult, selection);
152         //sleep();
153

154         expResult = AesKeyProvider.KEY_STRENGTH_256;
155         try {
156             instance.setKeyStrength(AesKeyProvider.KEY_STRENGTH_256 + 1);
157             fail("Expected IllegalArgumentException");
158         } catch (IllegalArgumentException JavaDoc expected) {
159         }
160         keyStrength = instance.getKeyStrength();
161         assertEquals(expResult, keyStrength);
162         selection = comboBoxOp.getSelectedIndex();
163         assertEquals(expResult, selection);
164         //sleep();
165

166         //
167
// Set key strength via GUI and check API and GUI.
168
//
169

170         expResult = AesKeyProvider.KEY_STRENGTH_128;
171         comboBoxOp.setSelectedIndex(expResult);
172         keyStrength = instance.getKeyStrength();
173         assertEquals(expResult, keyStrength);
174         selection = comboBoxOp.getSelectedIndex();
175         assertEquals(expResult, selection);
176         sleep();
177
178         expResult = AesKeyProvider.KEY_STRENGTH_192;
179         comboBoxOp.setSelectedIndex(expResult);
180         keyStrength = instance.getKeyStrength();
181         assertEquals(expResult, keyStrength);
182         selection = comboBoxOp.getSelectedIndex();
183         assertEquals(expResult, selection);
184         sleep();
185
186         expResult = AesKeyProvider.KEY_STRENGTH_256;
187         comboBoxOp.setSelectedIndex(expResult);
188         keyStrength = instance.getKeyStrength();
189         assertEquals(expResult, keyStrength);
190         selection = comboBoxOp.getSelectedIndex();
191         assertEquals(expResult, selection);
192         sleep();
193     };
194     
195     private static void sleep() {
196         //new QueueTool().waitEmpty(500); // doesn't always update the screen!
197
try {
198             Thread.sleep(50);
199         } catch (InterruptedException JavaDoc ex) {
200             throw new UndeclaredThrowableException(ex);
201         }
202     }
203 }
204
Popular Tags