1 19 package org.openide; 20 21 22 import org.netbeans.junit.*; 23 import org.netbeans.junit.NbTestCase; 24 import org.netbeans.junit.NbTestSuite; 25 26 import java.awt.Component ; 27 import java.beans.PropertyChangeListener ; 28 import java.io.IOException ; 29 import java.util.*; 30 import javax.swing.*; 31 import javax.swing.JLabel ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.util.*; 35 import org.openide.util.HelpCtx; 36 37 40 public class InstantiatingIteratorTest extends NbTestCase { 41 42 43 public InstantiatingIteratorTest (String name) { 44 super(name); 45 } 46 47 public static void main(String [] args) { 48 junit.textui.TestRunner.run (new NbTestSuite (InstantiatingIteratorTest.class)); 49 System.exit (0); 50 } 51 52 protected WizardDescriptor wd; 53 protected String exceptedValue; 54 private Iterator iterator; 55 protected int attachedInIterator = 0; 56 protected int attachedInPanel = 0; 57 protected boolean checkOrder = false; 58 protected boolean shouldThrowException = false; 59 protected boolean uninitializeShouldThrowException = false; 60 protected Set changeListenersInIterator = new HashSet (); 61 protected Set changeListenersInPanel = new HashSet (); 62 protected boolean checkIfInAWT; 63 64 protected void setUp () { 65 iterator = new Iterator (); 66 wd = new WizardDescriptor (iterator); 67 wd.addPropertyChangeListener(new Listener ()); 68 java.awt.Dialog d = DialogDisplayer.getDefault ().createDialog (wd); 69 checkOrder = false; 70 shouldThrowException = false; 71 } 73 74 75 protected boolean runInEQ() { 76 return true; 77 } 78 79 public void testCleanChangeListenerAfterFinish () { 80 assertEquals ("One listener is attached.", 1, changeListenersInIterator.size ()); 81 wd.doNextClick (); 82 assertEquals ("Still only one listener is attached after Next.", 1, changeListenersInIterator.size ()); 83 wd.doPreviousClick (); 84 assertEquals ("Still only one listener is attached after Previous.", 1, changeListenersInIterator.size ()); 85 finishWizard (wd); 86 assertEquals ("No one listener is attached after Finish.", 0, changeListenersInIterator.size ()); 87 assertEquals ("No one listener is attached in WD.Panel after Finish.", 0, changeListenersInPanel.size ()); 88 } 89 90 public void testCleanChangeListenerAfterCancel () { 91 assertEquals ("One listener is attached.", 1, changeListenersInIterator.size ()); 92 wd.doCancelClick (); 93 assertEquals ("No one listener is attached after Cancel.", 0, changeListenersInIterator.size ()); 94 assertEquals ("No one listener is attached in WD.Panel after Finish.", 0, changeListenersInPanel.size ()); 95 } 96 97 public void testInitializeIterator () throws Exception { 98 assertTrue ("InstantiatingIterator was initialized.", getInitialized ().booleanValue ()); 99 assertNull ("InstantiatingIterator wasn't instantiated.", getResult ()); 100 } 101 102 public void testUninitializeIterator () throws Exception { 103 assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ()); 104 wd.doCancelClick (); 105 assertFalse ("InstantiatingIterator was uninitialized after cancel.", getInitialized ().booleanValue ()); 106 assertNull ("InstantiatingIterator wasn't instantiated.", getResult ()); 107 } 108 109 public void testFinishAndUninitializeIterator () throws Exception { 110 assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ()); 111 wd.doNextClick (); 112 assertTrue ("InstantiatingIterator wasn't uninitialized after next.", getInitialized ().booleanValue ()); 113 finishWizard (wd); 114 assertFalse ("InstantiatingIterator wasn uninitialized after finish.", getInitialized ().booleanValue ()); 115 assertNotNull ("InstantiatingIterator was instantiated.", getResult ()); 116 } 117 118 public void testUninitializeIteratorAndCalledCurrent () throws Exception { 119 assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ()); 120 wd.doNextClick (); 121 assertTrue ("InstantiatingIterator wasn't uninitialized after next.", getInitialized ().booleanValue ()); 122 finishWizard (wd); 123 assertFalse ("InstantiatingIterator was uninitialized after finish.", getInitialized ().booleanValue ()); 124 assertNotNull ("InstantiatingIterator was instantiated.", getResult ()); 125 } 126 127 public void testOrderStoreSettingAndInstantiate () throws Exception { 128 checkOrder = true; 129 wd.doNextClick (); 130 finishWizard (wd); 131 assertNotNull ("InstantiatingIterator was instantiated.", getResult ()); 132 } 133 134 public void testGetInstantiatedObjects () throws Exception { 135 wd.doNextClick (); 136 finishWizard (wd); 137 assertNotNull ("InstantiatingIterator was instantiated.", getResult ()); 138 Set newObjects = wd.getInstantiatedObjects (); 139 assertEquals ("WD returns same objects as InstantiatingIterator instantiated.", getResult (), newObjects); 140 141 } 142 143 public void testInstantiateInAWTQueueOrNot () { 144 checkIfInAWT = true; 145 146 wd.doNextClick (); 147 finishWizard (wd); 148 try { 149 Set newObjects = wd.getInstantiatedObjects (); 150 } catch (IllegalStateException ise) { 151 fail ("IllegalStateException was caught because WD.instantiate() called outside AWT queue."); 152 } 153 assertNotNull ("InstantiatingIterator was correctly instantiated.", getResult ()); 154 } 155 156 public void testFinishOptionWhenInstantiateFails () throws Exception { 157 shouldThrowException = true; 158 159 wd.doNextClick (); 160 Object state = wd.getValue(); 161 finishWizard (wd); 162 163 assertNull ("InstantiatingIterator was not correctly instantiated.", getResult ()); 164 try { 165 Set newObjects = wd.getInstantiatedObjects (); 166 fail ("No IllegalStateException was caught. Should be thrown when invoked getInstantiatedObjects() on unfinished wizard."); 167 } catch (IllegalStateException ise) { 168 } 170 assertEquals ("The state is same as before instantiate()", state, wd.getValue ()); 171 } 172 173 public void testFinishOptionWhenUninitializeThrowsError () throws Exception { 174 shouldThrowException = false; 175 uninitializeShouldThrowException = true; 176 177 wd.doFinishClick(); 178 Object state = wd.getValue(); 179 finishWizard (wd); 180 181 assertEquals ("The state is same as before instantiate()", state, wd.getValue ()); 182 } 183 184 public class Panel implements 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 changeListenersInPanel.add (l); 200 } 201 202 public HelpCtx getHelp() { 203 return null; 204 } 205 206 public boolean isValid() { 207 return true; 208 } 209 210 public void readSettings(Object settings) { 211 log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 212 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 213 } 214 215 public void removeChangeListener(ChangeListener l) { 216 changeListenersInPanel.remove (l); 217 } 218 219 public void storeSettings(Object settings) { 220 if (checkOrder) { 221 assertNull ("WD.P.storeSettings() called before WD.I.instantiate()", getResult ()); 222 checkOrder = false; 225 } 226 log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 227 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 228 if (exceptedValue != null) { 229 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ())); 230 } 231 } 232 233 public boolean isFinishPanel () { 234 return true; 235 } 236 237 } 238 239 protected Boolean getInitialized () { 240 return iterator.initialized; 241 } 242 243 protected Set getResult () { 244 return iterator.result; 245 } 246 247 public class Iterator implements WizardDescriptor.InstantiatingIterator { 248 int index = 0; 249 WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2]; 250 java.util.Set helpSet; 251 252 private Boolean initialized = null; 253 private Set result = null; 254 255 public WizardDescriptor.Panel current () { 256 assertTrue ("WD.current() called on initialized iterator.", initialized != null && initialized.booleanValue ()); 257 return panels[index]; 258 } 259 public String name () { 260 return "Test iterator"; 261 } 262 public boolean hasNext () { 263 return index < 1; 264 } 265 public boolean hasPrevious () { 266 return index > 0; 267 } 268 public void nextPanel () { 269 if (!hasNext ()) throw new NoSuchElementException (); 270 index ++; 271 } 272 public void previousPanel () { 273 if (!hasPrevious ()) throw new NoSuchElementException (); 274 index --; 275 } 276 public void addChangeListener (ChangeListener l) { 277 changeListenersInIterator.add (l); 278 } 279 public void removeChangeListener (ChangeListener l) { 280 changeListenersInIterator.remove (l); 281 } 282 public java.util.Set instantiate () throws IOException { 283 if (checkIfInAWT) { 284 if (! SwingUtilities.isEventDispatchThread ()) { 285 throw new IOException ("Must run in AWT queue."); 286 } 287 } 288 if (shouldThrowException) { 289 throw new IOException ("Test throw IOException during instantiate()."); 290 } 291 if (initialized.booleanValue ()) { 292 helpSet.add ("member"); 293 result = helpSet; 294 } else { 295 result = null; 296 } 297 return result; 298 } 299 public void initialize (WizardDescriptor wizard) { 300 helpSet = new HashSet (); 301 panels[0] = new Panel("first panel"); 302 panels[1] = new Panel("second panel"); 303 initialized = Boolean.TRUE; 304 } 305 public void uninitialize (WizardDescriptor wizard) { 306 if (uninitializeShouldThrowException) { 307 throw new RuntimeException ("test"); 308 } 309 helpSet.clear (); 310 initialized = Boolean.FALSE; 311 panels = null; 312 } 313 } 314 315 public class Listener implements PropertyChangeListener { 316 317 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 318 if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) { 319 log("propertyChange [time: " + System.currentTimeMillis () + 320 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 321 322 } 323 } 324 325 } 326 327 public String handleValue (Object val) { 328 if (val == null) return "NULL"; 329 if (val instanceof String ) return (String ) val; 330 if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION"; 331 if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION"; 332 if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION"; 333 if (val instanceof JButton) { 334 JButton butt = (JButton) val; 335 ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION"; 337 if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS"; 338 if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION"; 339 if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION"; 340 } 341 return "UNKNOWN OPTION: " + val; 342 } 343 344 public static void finishWizard (WizardDescriptor wd) { 345 wd.doFinishClick (); 346 WizardDescriptor.ASYNCHRONOUS_JOBS_RP.post (new Runnable () { 347 public void run () {} 348 }).waitFinished (); 349 } 350 } 351 | Popular Tags |