1 19 20 package org.netbeans.spi.project.ui.support; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.IOException ; 26 import java.lang.ref.Reference ; 27 import java.lang.ref.WeakReference ; 28 import javax.swing.JButton ; 29 import javax.swing.JComponent ; 30 import javax.swing.JDialog ; 31 import javax.swing.JFrame ; 32 import javax.swing.JPanel ; 33 import javax.swing.SwingUtilities ; 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 49 public class ProjectCustomizerTest extends NbTestCase { 50 51 public ProjectCustomizerTest(String testName) { 52 super(testName); 53 } 54 55 private Reference <?>[] runTestCategoriesAreReclaimable() throws Exception { 56 final Reference <?>[] result = new Reference <?>[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 () { 62 public void run() { 63 Dialog d = ProjectCustomizer.createCustomizerDialog(new Category[] {test2, test3}, new CategoryComponentProviderImpl(), null, new ActionListener () { 64 public void actionPerformed(ActionEvent e) { 65 } 67 }, HelpCtx.DEFAULT_HELP); 68 69 d.setVisible(true); 70 71 try { 72 Thread.currentThread().sleep(50); 73 } catch (InterruptedException ex) { 74 ex.printStackTrace(); 75 } 76 77 d.setVisible(false); 78 d.dispose(); 79 80 result[0] = new WeakReference <Object >(d); 81 82 d = null; 83 84 } 85 }); 86 87 Thread.sleep(1000); 89 90 SwingUtilities.invokeAndWait(new Runnable () { 91 public void run() { 92 JFrame f = new JFrame ("test"); 93 94 f.setVisible(true); 95 96 JDialog d = new JDialog (f, false); 97 98 d.setVisible(true); 99 100 try { 101 Thread.currentThread().sleep(50); 102 } catch (InterruptedException 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 <Object >(test1); 117 result[2] = new WeakReference <Object >(test2); 118 result[3] = new WeakReference <Object >(test3); 119 120 return result; 121 } 122 123 public void testCategoriesAreReclaimable() throws Exception { 124 for (Reference <?> ref : runTestCategoriesAreReclaimable()) { 125 assertGC("Is reclaimable", ref); 126 } 127 } 128 129 private static final class CategoryComponentProviderImpl implements CategoryComponentProvider { 130 131 public JComponent create(Category category) { 132 return new JPanel (); 133 } 134 135 } 136 137 public void testReadCategories() { 138 FileSystem sysFS = Repository.getDefault().getDefaultFileSystem(); 139 FileObject projectFO = sysFS.findResource("Projects"); 141 if (projectFO == null) { 142 FileObject rootFO = sysFS.getRoot(); 143 try { 144 projectFO = rootFO.createFolder("Projects"); 145 } catch (IOException 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 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 ex) { 162 fail("Cannot create 'Customizer' folder."); 163 } 164 } 165 try { 166 FileObject testFO = customizerFO.createFolder("TestCategory"); 168 DataFolder testDataFolder = DataFolder.findFolder(testFO); 169 DataObject instance = InstanceDataObject.create(testDataFolder, "Self", 171 "org.netbeans.spi.project.ui.support.ProjectCustomizerTest$TestCompositeCategoryProvider"); 172 } catch (IOException 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 ex) { 180 fail("Reading of categories failed."); 181 } 182 assertNotNull(categories); 183 assertEquals(1, categories.length); 184 assertEquals("TestCategory", categories[0].getDisplayName()); 185 JComponent jc = dcp.create(categories[0]); 186 assertTrue(jc instanceof JButton ); 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 createComponent(ProjectCustomizer.Category category, Lookup context) { 194 return new JButton (); 195 } 196 } 197 198 } 199 | Popular Tags |