1 19 package org.openide; 20 import java.lang.reflect.InvocationTargetException ; 21 import org.netbeans.junit.NbTestSuite; 22 23 import java.awt.Component ; 24 import java.util.*; 25 import javax.swing.*; 26 import javax.swing.JLabel ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.util.*; 30 import org.openide.util.HelpCtx; 31 32 35 public class AsynchronousValidatingPanelTest extends LoggingTestCaseHid { 36 37 public AsynchronousValidatingPanelTest (String name) { 38 super(name); 39 } 40 41 public static void main(String [] args) { 42 junit.textui.TestRunner.run (new NbTestSuite (AsynchronousValidatingPanelTest.class)); 43 System.exit (0); 44 } 45 46 WizardDescriptor wd; 47 String exceptedValue; 48 49 private ErrorManager err; 50 51 protected final void setUp () { 52 WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2]; 53 panels[0] = new Panel("first panel"); 54 panels[1] = new Panel("second panel"); 55 wd = new WizardDescriptor(panels); 56 wd.addPropertyChangeListener(new Listener ()); 57 java.awt.Dialog d = DialogDisplayer.getDefault().createDialog (wd); 58 err = ErrorManager.getDefault ().getInstance ("test-" + getName ()); 60 } 61 62 public void testAsynchronousLazyValidation () throws Exception { 63 Panel panels[] = new Panel[3]; 64 65 class MyPanel extends Panel implements WizardDescriptor.AsynchronousValidatingPanel { 66 public String validateMsg; 67 public String failedMsg; 68 public boolean running; 69 public boolean wasEnabled; 70 71 public MyPanel () { 72 super ("enhanced panel"); 73 } 74 75 public void prepareValidation () { 76 running = true; 77 } 78 79 public synchronized void validate () throws WizardValidationException { 80 err.log ("validate() entry."); 81 wasEnabled = wd.isNextEnabled () || wd.isFinishEnabled (); 82 running = false; 83 notifyAll (); 84 if (validateMsg != null) { 85 failedMsg = validateMsg; 86 err.log ("Throw WizardValidationException."); 87 throw new WizardValidationException (null, "MyPanel.validate() failed.", validateMsg); 88 } 89 err.log ("validate() exit."); 90 return; 91 } 92 } 93 94 class MyFinishPanel extends MyPanel implements WizardDescriptor.FinishablePanel { 95 public boolean isFinishPanel () { 96 return true; 97 } 98 } 99 100 MyPanel mp = new MyPanel (); 101 MyFinishPanel mfp = new MyFinishPanel (); 102 panels[0] = mp; 103 panels[1] = mfp; 104 panels[2] = new Panel ("Last one"); 105 wd = new WizardDescriptor(panels); 106 107 assertNull ("Component has not been yet initialized", panels[1].component); 108 mp.failedMsg = null; 109 mp.validateMsg = "xtest-fail-without-msg"; 110 assertTrue ("Next button must be enabled.", wd.isNextEnabled ()); 111 err.log ("Do Next. Validation will run with: validateMsg=" + mp.validateMsg + ", failedMsg=" + mp.failedMsg); 112 synchronized (mp) { 113 wd.doNextClick (); 114 assertTrue ("Validation runs.", mp.running); 115 assertFalse ("Wizard is not valid when validation runs.", wd.isForwardEnabled ()); 116 while (mp.running) { 118 assertFalse ("Wizard is not valid during validation.", wd.isForwardEnabled ()); 119 mp.wait (); 120 } 121 } 122 waitWhileSetValidDone (); 123 assertFalse ("Finish is disabled during validation.", mp.wasEnabled); 124 assertTrue ("Wizard is ready to next validation however previous failed.", wd.isForwardEnabled ()); 125 assertEquals ("The lazy validation failed on Next.", mp.validateMsg, mp.failedMsg); 126 assertNull ("The lazy validation failed, still no initialiaation", panels[1].component); 127 assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); 128 mp.failedMsg = null; 129 mp.validateMsg = null; 130 synchronized (mp) { 131 err.log ("Do Next. Validation will run with: validateMsg=" + mp.validateMsg + ", failedMsg=" + mp.failedMsg); 132 assertTrue ("Wizard is valid before validation.", wd.isForwardEnabled ()); 133 wd.doNextClick (); 134 assertTrue ("Validation runs.", mp.running); 135 while (mp.running) { 136 assertFalse ("Wizard is not valid during validation.", wd.isForwardEnabled ()); 137 mp.wait (); 138 } 139 } 140 waitWhileSetValidDone (); 141 assertFalse ("Finish is disabled during validation.", mp.wasEnabled); 142 assertTrue ("Wizard is valid when validation passes.", wd.isForwardEnabled ()); 143 assertNull ("Validation on Next passes", mp.failedMsg); 144 assertNotNull ("Now we switched to another panel", panels[1].component); 145 assertNull ("The lazy validation failed, still no initialiaation", panels[2].component); 146 147 Object state = wd.getValue(); 149 mfp.validateMsg = "xtest-fail-without-msg"; 150 mfp.failedMsg = null; 151 synchronized (mfp) { 152 err.log ("Do Finish. Validation will run with: validateMsg=" + mfp.validateMsg + ", failedMsg=" + mfp.failedMsg); 153 wd.doFinishClick(); 154 while (mfp.running) { 155 assertFalse ("Wizard is not valid during validation.", wd.isForwardEnabled ()); 156 mfp.wait (); 157 } 158 } 159 waitWhileSetValidDone (); 160 assertFalse ("Finish is disabled during validation.", mp.wasEnabled); 161 assertTrue ("Wizard is ready to next validation.", wd.isForwardEnabled ()); 162 assertEquals ("The lazy validation failed on Finish.", mfp.validateMsg, mfp.failedMsg); 163 assertNull ("The validation failed, still no initialiaation", panels[2].component); 164 assertEquals ("State has not changed", state, wd.getValue ()); 165 166 mfp.validateMsg = null; 167 mfp.failedMsg = null; 168 synchronized (mfp) { 169 err.log ("Do Finish. Validation will run with: validateMsg=" + mfp.validateMsg + ", failedMsg=" + mfp.failedMsg); 170 wd.doFinishClick (); 171 while (mfp.running) { 172 assertFalse ("Wizard is not valid during validation.", wd.isForwardEnabled ()); 173 mfp.wait (); 174 } 175 } 176 waitWhileSetValidDone (); 177 assertFalse ("Finish is disabled during validation.", mp.wasEnabled); 178 assertTrue ("Wizard is valid when validation passes.", wd.isForwardEnabled ()); 179 assertNull ("Validation on Finish passes", mfp.failedMsg); 180 assertNull ("Finish was clicked, no initialization either", panels[2].component); 181 assertEquals ("The state is finish", WizardDescriptor.FINISH_OPTION, wd.getValue ()); 182 } 183 184 public class Panel implements WizardDescriptor.Panel, WizardDescriptor.FinishablePanel { 185 private JLabel component; 186 private String text; 187 public Panel(String text) { 188 this.text = text; 189 } 190 191 public Component getComponent() { 192 if (component == null) { 193 component = new JLabel (text); 194 } 195 return component; 196 } 197 198 public void addChangeListener(ChangeListener l) { 199 } 200 201 public HelpCtx getHelp() { 202 return null; 203 } 204 205 public boolean isValid() { 206 return true; 207 } 208 209 public boolean isFinishPanel () { 210 return true; 211 } 212 213 public void readSettings(Object settings) { 214 log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 215 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 216 } 217 218 public void removeChangeListener(ChangeListener l) { 219 } 220 221 public void storeSettings(Object settings) { 222 log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 223 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 224 if (exceptedValue != null) { 225 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ())); 226 } 227 } 228 229 } 230 231 public class Listener implements java.beans.PropertyChangeListener { 232 233 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 234 if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) { 235 log("propertyChange [time: " + System.currentTimeMillis () + 236 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 237 238 } 239 } 240 241 } 242 243 public String handleValue (Object val) { 244 if (val == null) return "NULL"; 245 if (val instanceof String ) return (String ) val; 246 if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION"; 247 if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION"; 248 if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION"; 249 if (val instanceof JButton) { 250 JButton butt = (JButton) val; 251 ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION"; 253 if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS"; 254 if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION"; 255 if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION"; 256 } 257 return "UNKNOWN OPTION: " + val; 258 } 259 260 private void waitWhileSetValidDone () { 261 err.log ("Start waitWhileSetValidDone."); 262 try { 263 WizardDescriptor.ASYNCHRONOUS_JOBS_RP.post (new Runnable () { 264 public void run () { 265 } 266 }).waitFinished (); 267 SwingUtilities.invokeAndWait (new Runnable () { 268 public void run () { 269 } 270 }); 271 } catch (InterruptedException ex) { 272 fail (ex.getMessage ()); 273 } catch (InvocationTargetException ex) { 274 fail (ex.getMessage ()); 275 } 276 err.log ("End of waitWhileSetValidDone."); 277 } 278 279 } 280 | Popular Tags |