KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > wizards > SimpleTestCaseWizardIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2004-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
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 /**
49  */

50 public class SimpleTestCaseWizardIterator
51         implements TemplateWizard.Iterator {
52
53     /** */
54     private static SimpleTestCaseWizardIterator instance;
55
56     /** */
57     private TemplateWizard wizard;
58
59     /** index of step "Name & Location" */
60     private static final int INDEX_CHOOSE_CLASS = 2;
61
62     /** name of panel "Name & Location" */
63     private final String JavaDoc nameChooseClass = NbBundle.getMessage(
64             SimpleTestCaseWizardIterator.class,
65             "LBL_panel_ChooseClass"); //NOI18N
66
/** index of the current panel */
67     private int current;
68     /** registered change listeners */
69     private List JavaDoc<ChangeListener JavaDoc> changeListeners;
70     /** */
71     private Project lastSelectedProject = null;
72     /** panel for choosing name and target location of the test class */
73     private WizardDescriptor.Panel classChooserPanel;
74
75     /**
76      */

77     public void addChangeListener(ChangeListener JavaDoc l) {
78         if (changeListeners == null) {
79             changeListeners = new ArrayList JavaDoc<ChangeListener JavaDoc>(2);
80         }
81         changeListeners.add(l);
82     }
83
84     /**
85      */

86     public void removeChangeListener(ChangeListener JavaDoc l) {
87         if (changeListeners != null) {
88             changeListeners.remove(l);
89             if (changeListeners.isEmpty()) {
90                 changeListeners = null;
91             }
92         }
93     }
94
95     /**
96      * Notifies all registered listeners about a change.
97      *
98      * @see #addChangeListener
99      * @see #removeChangeListener
100      */

101     private void fireChange() {
102         if (changeListeners != null) {
103             ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
104             Iterator JavaDoc i = changeListeners.iterator();
105             while (i.hasNext()) {
106                 ((ChangeListener JavaDoc) i.next()).stateChanged(e);
107             }
108         }
109     }
110
111     /**
112      */

113     public boolean hasPrevious() {
114         return current > INDEX_CHOOSE_CLASS;
115     }
116
117     /**
118      */

119     public boolean hasNext() {
120         return current < INDEX_CHOOSE_CLASS;
121     }
122
123     /**
124      */

125     public void previousPanel() {
126         if (!hasPrevious()) {
127             throw new NoSuchElementException JavaDoc();
128         }
129         current--;
130     }
131
132     /**
133      */

134     public void nextPanel() {
135         if (!hasNext()) {
136             throw new NoSuchElementException JavaDoc();
137         }
138         current++;
139     }
140
141     /**
142      */

143     public WizardDescriptor.Panel current() {
144         switch (current) {
145             case INDEX_CHOOSE_CLASS:
146                 return getClassChooserPanel();
147             default:
148                 throw new IllegalStateException JavaDoc();
149         }
150     }
151
152     /**
153      * Returns a panel for choosing name and target location of the test
154      * class. If the panel already exists, returns the existing panel,
155      * otherwise creates a new panel.
156      *
157      * @return existing panel or a newly created panel if it did not exist
158      */

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")); //NOI18N
168
} 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     /**
180      */

181     public String JavaDoc name() {
182         switch (current) {
183             case INDEX_CHOOSE_CLASS:
184                 return nameChooseClass;
185             default:
186                 throw new AssertionError JavaDoc(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     /**
235      * <!-- PENDING -->
236      */

237     public void initialize(TemplateWizard wiz) {
238         this.wizard = wiz;
239         current = INDEX_CHOOSE_CLASS;
240         loadSettings(wiz);
241
242         String JavaDoc [] panelNames = new String JavaDoc [] {
243           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_chooseFileType"),
244           NbBundle.getMessage(EmptyTestCaseWizardIterator.class,"LBL_panel_ChooseClass")};
245
246         ((javax.swing.JComponent JavaDoc)getClassChooserPanel().getComponent()).putClientProperty("WizardPanel_contentData", panelNames);
247         ((javax.swing.JComponent JavaDoc)getClassChooserPanel().getComponent()).putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(0));
248
249     }
250
251     /**
252      * <!-- PENDING -->
253      */

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 JavaDoc<DataObject> instantiate(TemplateWizard wiz) throws IOException JavaDoc {
268         saveSettings(wiz);
269         
270         /* collect and build necessary data: */
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 JavaDoc<CreateTestParam, Object JavaDoc> params
276                 = TestUtil.getSettingsMap(false);
277                 
278         /* create test class(es) for the selected source class: */
279         JUnitPlugin plugin = TestUtil.getPluginForProject(
280                                                 Templates.getProject(wizard));
281         /*
282          * The JUnitPlugin instance must be initialized _before_ field
283          * JUnitPluginTrampoline.DEFAULT gets accessed.
284          * See issue #74744.
285          */

286         final FileObject[] testFileObjects
287                 = JUnitPluginTrampoline.DEFAULT.createTests(
288                      plugin,
289                      new FileObject[] {classToTest},
290                      testRootFolder,
291                      params);
292         
293         //XXX: What if the selected class is not testable?
294
// It should not be skipped!
295

296         if (testFileObjects == null) {
297             throw new IOException JavaDoc();
298         }
299         
300         final Set JavaDoc<DataObject> dataObjects
301                = new HashSet JavaDoc<DataObject>((int) (testFileObjects.length * 1.5f));
302         for (FileObject testFile : testFileObjects) {
303             try {
304                 dataObjects.add(DataObject.find(testFile));
305             } catch (DataObjectNotFoundException ex) {
306                 //XXX - does nothing special - just continues
307
}
308         }
309         
310         if (dataObjects.isEmpty()) {
311             throw new IOException JavaDoc();
312         }
313         return dataObjects;
314     }
315
316     /**
317      */

318     public static SimpleTestCaseWizardIterator singleton() {
319         if (instance == null) {
320             // PENDING - it should not be kept forever
321
instance = new SimpleTestCaseWizardIterator();
322         }
323         return instance;
324     }
325
326 }
327
Popular Tags