1 19 20 package org.netbeans.modules.junit.wizards; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 import java.util.Set ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.modules.junit.GuiUtils; 34 import org.netbeans.modules.junit.JUnitPluginTrampoline; 35 import org.netbeans.modules.junit.JUnitSettings; 36 import org.netbeans.modules.junit.TestCreator; 37 import org.netbeans.modules.junit.TestUtil; 38 import org.netbeans.modules.junit.plugin.JUnitPlugin; 39 import org.netbeans.modules.junit.plugin.JUnitPlugin.CreateTestParam; 40 import org.netbeans.spi.project.ui.templates.support.Templates; 41 import org.openide.WizardDescriptor; 42 import org.openide.filesystems.FileObject; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataObjectNotFoundException; 45 import org.openide.loaders.TemplateWizard; 46 import org.openide.util.NbBundle; 47 48 50 public class SimpleTestCaseWizardIterator 51 implements TemplateWizard.Iterator { 52 53 54 private static SimpleTestCaseWizardIterator instance; 55 56 57 private TemplateWizard wizard; 58 59 60 private static final int INDEX_CHOOSE_CLASS = 2; 61 62 63 private final String nameChooseClass = NbBundle.getMessage( 64 SimpleTestCaseWizardIterator.class, 65 "LBL_panel_ChooseClass"); 67 private int current; 68 69 private List <ChangeListener > changeListeners; 70 71 private Project lastSelectedProject = null; 72 73 private WizardDescriptor.Panel classChooserPanel; 74 75 77 public void addChangeListener(ChangeListener l) { 78 if (changeListeners == null) { 79 changeListeners = new ArrayList <ChangeListener >(2); 80 } 81 changeListeners.add(l); 82 } 83 84 86 public void removeChangeListener(ChangeListener l) { 87 if (changeListeners != null) { 88 changeListeners.remove(l); 89 if (changeListeners.isEmpty()) { 90 changeListeners = null; 91 } 92 } 93 } 94 95 101 private void fireChange() { 102 if (changeListeners != null) { 103 ChangeEvent e = new ChangeEvent (this); 104 Iterator i = changeListeners.iterator(); 105 while (i.hasNext()) { 106 ((ChangeListener ) i.next()).stateChanged(e); 107 } 108 } 109 } 110 111 113 public boolean hasPrevious() { 114 return current > INDEX_CHOOSE_CLASS; 115 } 116 117 119 public boolean hasNext() { 120 return current < INDEX_CHOOSE_CLASS; 121 } 122 123 125 public void previousPanel() { 126 if (!hasPrevious()) { 127 throw new NoSuchElementException (); 128 } 129 current--; 130 } 131 132 134 public void nextPanel() { 135 if (!hasNext()) { 136 throw new NoSuchElementException (); 137 } 138 current++; 139 } 140 141 143 public WizardDescriptor.Panel current() { 144 switch (current) { 145 case INDEX_CHOOSE_CLASS: 146 return getClassChooserPanel(); 147 default: 148 throw new IllegalStateException (); 149 } 150 } 151 152 159 private WizardDescriptor.Panel getClassChooserPanel() { 160 final Project project = Templates.getProject(wizard); 161 if (classChooserPanel == null || project != lastSelectedProject) { 162 final Utils utils = new Utils(project); 163 if (utils.getSourcesToTestsMap(true).isEmpty()) { 164 classChooserPanel = new StepProblemMessage( 165 project, 166 NbBundle.getMessage(EmptyTestCaseWizardIterator.class, 167 "MSG_NoTestSourceGroup")); } else { 169 if (classChooserPanel == null) { 170 classChooserPanel = new SimpleTestStepLocation(); 171 } 172 ((SimpleTestStepLocation) classChooserPanel).setUp(utils); 173 } 174 } 175 lastSelectedProject = project; 176 return classChooserPanel; 177 } 178 179 181 public String name() { 182 switch (current) { 183 case INDEX_CHOOSE_CLASS: 184 return nameChooseClass; 185 default: 186 throw new AssertionError (current); 187 } 188 } 189 190 private void loadSettings(TemplateWizard wizard) { 191 JUnitSettings settings = JUnitSettings.getDefault(); 192 193 wizard.putProperty(GuiUtils.CHK_PUBLIC, 194 Boolean.valueOf(settings.isMembersPublic())); 195 wizard.putProperty(GuiUtils.CHK_PROTECTED, 196 Boolean.valueOf(settings.isMembersProtected())); 197 wizard.putProperty(GuiUtils.CHK_PACKAGE, 198 Boolean.valueOf(settings.isMembersPackage())); 199 wizard.putProperty(GuiUtils.CHK_SETUP, 200 Boolean.valueOf(settings.isGenerateSetUp())); 201 wizard.putProperty(GuiUtils.CHK_TEARDOWN, 202 Boolean.valueOf(settings.isGenerateTearDown())); 203 wizard.putProperty(GuiUtils.CHK_METHOD_BODIES, 204 Boolean.valueOf(settings.isBodyContent())); 205 wizard.putProperty(GuiUtils.CHK_JAVADOC, 206 Boolean.valueOf(settings.isJavaDoc())); 207 wizard.putProperty(GuiUtils.CHK_HINTS, 208 Boolean.valueOf(settings.isBodyComments())); 209 210 wizard.putProperty("NewFileWizard_Title", NbBundle.getMessage(SimpleTestStepLocation.class, "LBL_simpleTestWizard_stepName")); 211 } 212 213 private void saveSettings(TemplateWizard wizard) { 214 JUnitSettings settings = JUnitSettings.getDefault(); 215 216 settings.setMembersPublic( 217 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_PUBLIC))); 218 settings.setMembersProtected( 219 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_PROTECTED))); 220 settings.setMembersPackage( 221 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_PACKAGE))); 222 settings.setGenerateSetUp( 223 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_SETUP))); 224 settings.setGenerateTearDown( 225 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_TEARDOWN))); 226 settings.setBodyContent( 227 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_METHOD_BODIES))); 228 settings.setJavaDoc( 229 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_JAVADOC))); 230 settings.setBodyComments( 231 Boolean.TRUE.equals(wizard.getProperty(GuiUtils.CHK_HINTS))); 232 } 233 234 237 public void initialize(TemplateWizard wiz) { 238 this.wizard = wiz; 239 current = INDEX_CHOOSE_CLASS; 240 loadSettings(wiz); 241 242 String [] panelNames = new String [] { 243 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"), 244 NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_ChooseClass")}; 245 246 ((javax.swing.JComponent )getClassChooserPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames); 247 ((javax.swing.JComponent )getClassChooserPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer (0)); 248 249 } 250 251 254 public void uninitialize(TemplateWizard wiz) { 255 if ((classChooserPanel != null) 256 && !(classChooserPanel instanceof StepProblemMessage)) { 257 258 assert classChooserPanel instanceof SimpleTestStepLocation; 259 260 ((SimpleTestStepLocation) classChooserPanel).cleanUp(); 261 } 262 classChooserPanel = null; 263 changeListeners = null; 264 this.wizard = null; 265 } 266 267 public Set <DataObject> instantiate(TemplateWizard wiz) throws IOException { 268 saveSettings(wiz); 269 270 271 FileObject classToTest = (FileObject) 272 wizard.getProperty(SimpleTestCaseWizard.PROP_CLASS_TO_TEST); 273 FileObject testRootFolder = (FileObject) 274 wizard.getProperty(SimpleTestCaseWizard.PROP_TEST_ROOT_FOLDER); 275 Map <CreateTestParam, Object > params 276 = TestUtil.getSettingsMap(false); 277 278 279 JUnitPlugin plugin = TestUtil.getPluginForProject( 280 Templates.getProject(wizard)); 281 286 final FileObject[] testFileObjects 287 = JUnitPluginTrampoline.DEFAULT.createTests( 288 plugin, 289 new FileObject[] {classToTest}, 290 testRootFolder, 291 params); 292 293 296 if (testFileObjects == null) { 297 throw new IOException (); 298 } 299 300 final Set <DataObject> dataObjects 301 = new HashSet <DataObject>((int) (testFileObjects.length * 1.5f)); 302 for (FileObject testFile : testFileObjects) { 303 try { 304 dataObjects.add(DataObject.find(testFile)); 305 } catch (DataObjectNotFoundException ex) { 306 } 308 } 309 310 if (dataObjects.isEmpty()) { 311 throw new IOException (); 312 } 313 return dataObjects; 314 } 315 316 318 public static SimpleTestCaseWizardIterator singleton() { 319 if (instance == null) { 320 instance = new SimpleTestCaseWizardIterator(); 322 } 323 return instance; 324 } 325 326 } 327 | Popular Tags |