1 19 package org.openide; 20 21 22 import org.netbeans.junit.NbTestSuite; 23 24 import java.awt.Component ; 25 import java.io.IOException ; 26 import java.util.*; 27 import javax.swing.*; 28 import javax.swing.JLabel ; 29 import javax.swing.event.ChangeListener ; 30 import org.openide.InstantiatingIteratorTest.Listener; 31 import org.openide.util.HelpCtx; 32 33 37 public class AsynchronousInstantiatingIteratorTest extends InstantiatingIteratorTest { 38 39 40 public AsynchronousInstantiatingIteratorTest (String name) { 41 super(name); 42 } 43 44 public static void main(String [] args) { 45 junit.textui.TestRunner.run (new NbTestSuite (AsynchronousInstantiatingIteratorTest.class)); 46 System.exit (0); 47 } 48 49 private Iterator iterator; 50 51 protected void setUp () { 52 iterator = new Iterator (); 53 wd = new WizardDescriptor (iterator); 54 wd.addPropertyChangeListener(new Listener ()); 55 java.awt.Dialog d = DialogDisplayer.getDefault ().createDialog (wd); 56 checkOrder = false; 57 shouldThrowException = false; 58 } 60 61 62 protected boolean runInEQ() { 63 return true; 64 } 65 66 public void testInstantiateInAWTQueueOrNot () { 67 checkIfInAWT = true; 68 69 wd.doNextClick (); 70 finishWizard (wd); 71 try { 72 Set newObjects = wd.getInstantiatedObjects (); 73 } catch (IllegalStateException ise) { 74 fail ("IllegalStateException was caught because WD.instantiate() called in AWT queue."); 75 } 76 assertNotNull ("InstantiatingIterator was correctly instantiated.", getResult ()); 77 } 78 79 public class Panel implements WizardDescriptor.FinishablePanel { 80 private JLabel component; 81 private String text; 82 public Panel(String text) { 83 this.text = text; 84 } 85 86 public Component getComponent() { 87 if (component == null) { 88 component = new JLabel (text); 89 } 90 return component; 91 } 92 93 public void addChangeListener(ChangeListener l) { 94 changeListenersInPanel.add (l); 95 } 96 97 public HelpCtx getHelp() { 98 return null; 99 } 100 101 public boolean isValid() { 102 return true; 103 } 104 105 public void readSettings(Object settings) { 106 log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 107 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 108 } 109 110 public void removeChangeListener(ChangeListener l) { 111 changeListenersInPanel.remove (l); 112 } 113 114 public void storeSettings(Object settings) { 115 if (checkOrder) { 116 assertNull ("WD.P.storeSettings() called before WD.I.instantiate()", iterator.result); 117 checkOrder = false; 120 } 121 log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () + 122 "] with PROP_VALUE: " + handleValue (wd.getValue ())); 123 if (exceptedValue != null) { 124 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ())); 125 } 126 } 127 128 public boolean isFinishPanel () { 129 return true; 130 } 131 132 } 133 134 protected Boolean getInitialized () { 135 return iterator.initialized; 136 } 137 138 protected Set getResult () { 139 return iterator.result; 140 } 141 142 public class Iterator implements WizardDescriptor.AsynchronousInstantiatingIterator { 143 int index = 0; 144 WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2]; 145 java.util.Set helpSet; 146 147 private Boolean initialized = null; 148 private Set result = null; 149 150 public WizardDescriptor.Panel current () { 151 assertTrue ("WD.current() called on initialized iterator.", initialized != null && initialized.booleanValue ()); 152 return panels[index]; 153 } 154 public String name () { 155 return "Test iterator"; 156 } 157 public boolean hasNext () { 158 return index < 1; 159 } 160 public boolean hasPrevious () { 161 return index > 0; 162 } 163 public void nextPanel () { 164 if (!hasNext ()) throw new NoSuchElementException (); 165 index ++; 166 } 167 public void previousPanel () { 168 if (!hasPrevious ()) throw new NoSuchElementException (); 169 index --; 170 } 171 public void addChangeListener (ChangeListener l) { 172 changeListenersInIterator.add (l); 173 } 174 public void removeChangeListener (ChangeListener l) { 175 changeListenersInIterator.remove (l); 176 } 177 public java.util.Set instantiate () throws IOException { 178 if (checkIfInAWT) { 179 if (SwingUtilities.isEventDispatchThread ()) { 180 throw new IOException ("Cannot run in AWT queue."); 181 } 182 } 183 if (shouldThrowException) { 184 throw new IOException ("Test throw IOException during instantiate()."); 185 } 186 if (initialized.booleanValue ()) { 187 helpSet.add ("member"); 188 result = helpSet; 189 } else { 190 result = null; 191 } 192 return result; 193 } 194 public void initialize (WizardDescriptor wizard) { 195 helpSet = new HashSet (); 196 panels[0] = new Panel("first panel"); 197 panels[1] = new Panel("second panel"); 198 initialized = Boolean.TRUE; 199 } 200 public void uninitialize (WizardDescriptor wizard) { 201 helpSet.clear (); 202 initialized = Boolean.FALSE; 203 panels = null; 204 } 205 } 206 207 } 208 | Popular Tags |