KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > ui > support > ProjectCustomizerTest


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.ui.support;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JDialog JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.netbeans.junit.NbTestCase;
35 import org.netbeans.spi.project.ui.support.ProjectCustomizer.Category;
36 import org.netbeans.spi.project.ui.support.ProjectCustomizer.CategoryComponentProvider;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.loaders.DataFolder;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.InstanceDataObject;
43 import org.openide.util.HelpCtx;
44 import org.openide.util.Lookup;
45
46 /**
47  * @author Jan Lahoda
48  */

49 public class ProjectCustomizerTest extends NbTestCase {
50     
51     public ProjectCustomizerTest(String JavaDoc testName) {
52         super(testName);
53     }
54
55     private Reference JavaDoc<?>[] runTestCategoriesAreReclaimable() throws Exception JavaDoc {
56         final Reference JavaDoc<?>[] result = new Reference JavaDoc<?>[4];
57               Category test1 = Category.create("test1", "test1", null);
58         final Category test2 = Category.create("test2", "test3", null, test1);
59         final Category test3 = Category.create("test3", "test3", null);
60         
61         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
62             public void run() {
63                 Dialog JavaDoc d = ProjectCustomizer.createCustomizerDialog(new Category[] {test2, test3}, new CategoryComponentProviderImpl(), null, new ActionListener JavaDoc() {
64                     public void actionPerformed(ActionEvent JavaDoc e) {
65                         //ignore
66
}
67                 }, HelpCtx.DEFAULT_HELP);
68                 
69                 d.setVisible(true);
70                 
71                 try {
72                     Thread.currentThread().sleep(50);
73                 } catch (InterruptedException JavaDoc ex) {
74                     ex.printStackTrace();
75                 }
76                 
77                 d.setVisible(false);
78                 d.dispose();
79                 
80                 result[0] = new WeakReference JavaDoc<Object JavaDoc>(d);
81                 
82                 d = null;
83                 
84             }
85         });
86         
87         //the dialog may be still strongly hold by the Swing/AWT, make it disappear:
88
Thread.sleep(1000);
89         
90         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
91             public void run() {
92                 JFrame JavaDoc f = new JFrame JavaDoc("test");
93                 
94                 f.setVisible(true);
95                 
96                 JDialog JavaDoc d = new JDialog JavaDoc(f, false);
97                 
98                 d.setVisible(true);
99                 
100                 try {
101                     Thread.currentThread().sleep(50);
102                 } catch (InterruptedException JavaDoc ex) {
103                     ex.printStackTrace();
104                 }
105                 
106                 d.setVisible(false);
107                 d.dispose();
108                 
109                 f.setVisible(false);
110                 f.dispose();
111                 
112                 d = null;
113             }
114         });
115                 
116         result[1] = new WeakReference JavaDoc<Object JavaDoc>(test1);
117         result[2] = new WeakReference JavaDoc<Object JavaDoc>(test2);
118         result[3] = new WeakReference JavaDoc<Object JavaDoc>(test3);
119         
120         return result;
121     }
122     
123     public void testCategoriesAreReclaimable() throws Exception JavaDoc {
124         for (Reference JavaDoc<?> ref : runTestCategoriesAreReclaimable()) {
125             assertGC("Is reclaimable", ref);
126         }
127     }
128     
129     private static final class CategoryComponentProviderImpl implements CategoryComponentProvider {
130         
131         public JComponent JavaDoc create(Category category) {
132             return new JPanel JavaDoc();
133         }
134         
135     }
136     
137     public void testReadCategories() {
138         FileSystem sysFS = Repository.getDefault().getDefaultFileSystem();
139         // creating direcotry structure on System FileSystem
140
FileObject projectFO = sysFS.findResource("Projects");
141         if (projectFO == null) {
142             FileObject rootFO = sysFS.getRoot();
143             try {
144                 projectFO = rootFO.createFolder("Projects");
145             } catch (IOException JavaDoc ex) {
146                 fail("Cannot create 'Projects' folder.");
147             }
148         }
149         FileObject j2seprojectFO = projectFO.getFileObject("org-netbeans-modules-java-j2seproject");
150         if (j2seprojectFO == null) {
151             try {
152                 j2seprojectFO = projectFO.createFolder("org-netbeans-modules-java-j2seproject");
153             } catch (IOException JavaDoc ex) {
154                 fail("Cannot create 'org-netbeans-modules-java-j2seproject' folder.");
155             }
156         }
157         FileObject customizerFO = j2seprojectFO.getFileObject("Customizer");
158         if (customizerFO == null) {
159             try {
160                 customizerFO = j2seprojectFO.createFolder("Customizer");
161             } catch (IOException JavaDoc ex) {
162                 fail("Cannot create 'Customizer' folder.");
163             }
164         }
165         try {
166             // category folder
167
FileObject testFO = customizerFO.createFolder("TestCategory");
168             DataFolder testDataFolder = DataFolder.findFolder(testFO);
169             // category instance file
170
DataObject instance = InstanceDataObject.create(testDataFolder, "Self",
171                     "org.netbeans.spi.project.ui.support.ProjectCustomizerTest$TestCompositeCategoryProvider");
172         } catch (IOException JavaDoc ex) {
173             fail("Cannot create category folder.");
174         }
175         ProjectCustomizer.DelegateCategoryProvider dcp = new ProjectCustomizer.DelegateCategoryProvider(DataFolder.findFolder(customizerFO), null);
176         ProjectCustomizer.Category categories[] = null;
177         try {
178             categories = dcp.readCategories(DataFolder.findFolder(customizerFO));
179         } catch (Exception JavaDoc ex) {
180             fail("Reading of categories failed.");
181         }
182         assertNotNull(categories);
183         assertEquals(1, categories.length);
184         assertEquals("TestCategory", categories[0].getDisplayName());
185         JComponent JavaDoc jc = dcp.create(categories[0]);
186         assertTrue(jc instanceof JButton JavaDoc);
187     }
188     
189     public static class TestCompositeCategoryProvider implements ProjectCustomizer.CompositeCategoryProvider {
190         public ProjectCustomizer.Category createCategory(Lookup context) {
191             return ProjectCustomizer.Category.create("testCategory", "TestCategory", null, null);
192         }
193         public JComponent JavaDoc createComponent(ProjectCustomizer.Category category, Lookup context) {
194             return new JButton JavaDoc();
195         }
196     }
197     
198 }
199
Popular Tags