001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.project.ui.support;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.io.IOException;
048: import java.lang.ref.Reference;
049: import java.lang.ref.WeakReference;
050: import javax.swing.JButton;
051: import javax.swing.JComponent;
052: import javax.swing.JDialog;
053: import javax.swing.JFrame;
054: import javax.swing.JPanel;
055: import javax.swing.SwingUtilities;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category;
058: import org.netbeans.spi.project.ui.support.ProjectCustomizer.CategoryComponentProvider;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileSystem;
061: import org.openide.filesystems.Repository;
062: import org.openide.loaders.DataFolder;
063: import org.openide.loaders.DataObject;
064: import org.openide.loaders.InstanceDataObject;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.Lookup;
067:
068: /**
069: * @author Jan Lahoda
070: */
071: public class ProjectCustomizerTest extends NbTestCase {
072:
073: public ProjectCustomizerTest(String testName) {
074: super (testName);
075: }
076:
077: private Reference<?>[] runTestCategoriesAreReclaimable()
078: throws Exception {
079: final Reference<?>[] result = new Reference<?>[4];
080: Category test1 = Category.create("test1", "test1", null);
081: final Category test2 = Category.create("test2", "test3", null,
082: test1);
083: final Category test3 = Category.create("test3", "test3", null);
084:
085: SwingUtilities.invokeAndWait(new Runnable() {
086: public void run() {
087: Dialog d = ProjectCustomizer.createCustomizerDialog(
088: new Category[] { test2, test3 },
089: new CategoryComponentProviderImpl(), null,
090: new ActionListener() {
091: public void actionPerformed(ActionEvent e) {
092: //ignore
093: }
094: }, HelpCtx.DEFAULT_HELP);
095:
096: d.setVisible(true);
097:
098: try {
099: Thread.currentThread().sleep(50);
100: } catch (InterruptedException ex) {
101: ex.printStackTrace();
102: }
103:
104: d.setVisible(false);
105: d.dispose();
106:
107: result[0] = new WeakReference<Object>(d);
108:
109: d = null;
110:
111: }
112: });
113:
114: //the dialog may be still strongly hold by the Swing/AWT, make it disappear:
115: Thread.sleep(1000);
116:
117: SwingUtilities.invokeAndWait(new Runnable() {
118: public void run() {
119: JFrame f = new JFrame("test");
120:
121: f.setVisible(true);
122:
123: JDialog d = new JDialog(f, false);
124:
125: d.setVisible(true);
126:
127: try {
128: Thread.currentThread().sleep(50);
129: } catch (InterruptedException ex) {
130: ex.printStackTrace();
131: }
132:
133: d.setVisible(false);
134: d.dispose();
135:
136: f.setVisible(false);
137: f.dispose();
138:
139: d = null;
140: }
141: });
142:
143: result[1] = new WeakReference<Object>(test1);
144: result[2] = new WeakReference<Object>(test2);
145: result[3] = new WeakReference<Object>(test3);
146:
147: return result;
148: }
149:
150: public void testCategoriesAreReclaimable() throws Exception {
151: for (Reference<?> ref : runTestCategoriesAreReclaimable()) {
152: assertGC("Is reclaimable", ref);
153: }
154: }
155:
156: private static final class CategoryComponentProviderImpl implements
157: CategoryComponentProvider {
158:
159: public JComponent create(Category category) {
160: return new JPanel();
161: }
162:
163: }
164:
165: public void testReadCategories() {
166: FileSystem sysFS = Repository.getDefault()
167: .getDefaultFileSystem();
168: // creating direcotry structure on System FileSystem
169: FileObject projectFO = sysFS.findResource("Projects");
170: if (projectFO == null) {
171: FileObject rootFO = sysFS.getRoot();
172: try {
173: projectFO = rootFO.createFolder("Projects");
174: } catch (IOException ex) {
175: fail("Cannot create 'Projects' folder.");
176: }
177: }
178: FileObject j2seprojectFO = projectFO
179: .getFileObject("org-netbeans-modules-java-j2seproject");
180: if (j2seprojectFO == null) {
181: try {
182: j2seprojectFO = projectFO
183: .createFolder("org-netbeans-modules-java-j2seproject");
184: } catch (IOException ex) {
185: fail("Cannot create 'org-netbeans-modules-java-j2seproject' folder.");
186: }
187: }
188: FileObject customizerFO = j2seprojectFO
189: .getFileObject("Customizer");
190: if (customizerFO == null) {
191: try {
192: customizerFO = j2seprojectFO.createFolder("Customizer");
193: } catch (IOException ex) {
194: fail("Cannot create 'Customizer' folder.");
195: }
196: }
197: try {
198: // category folder
199: FileObject testFO = customizerFO
200: .createFolder("TestCategory");
201: DataFolder testDataFolder = DataFolder.findFolder(testFO);
202: // category instance file
203: DataObject instance = InstanceDataObject
204: .create(
205: testDataFolder,
206: "Self",
207: "org.netbeans.spi.project.ui.support.ProjectCustomizerTest$TestCompositeCategoryProvider");
208: } catch (IOException ex) {
209: fail("Cannot create category folder.");
210: }
211: ProjectCustomizer.DelegateCategoryProvider dcp = new ProjectCustomizer.DelegateCategoryProvider(
212: DataFolder.findFolder(customizerFO), null);
213: ProjectCustomizer.Category categories[] = null;
214: try {
215: categories = dcp.readCategories(DataFolder
216: .findFolder(customizerFO));
217: } catch (Exception ex) {
218: fail("Reading of categories failed.");
219: }
220: assertNotNull(categories);
221: assertEquals(1, categories.length);
222: assertEquals("TestCategory", categories[0].getDisplayName());
223: JComponent jc = dcp.create(categories[0]);
224: assertTrue(jc instanceof JButton);
225: }
226:
227: public static class TestCompositeCategoryProvider implements
228: ProjectCustomizer.CompositeCategoryProvider {
229: public ProjectCustomizer.Category createCategory(Lookup context) {
230: return ProjectCustomizer.Category.create("testCategory",
231: "TestCategory", null, null);
232: }
233:
234: public JComponent createComponent(
235: ProjectCustomizer.Category category, Lookup context) {
236: return new JButton();
237: }
238: }
239:
240: }
|