KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > palette > PaletteItemTest


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.palette;
21
22 import java.beans.BeanInfo JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import org.netbeans.junit.MockServices;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.palette.Category;
30 import org.netbeans.modules.palette.Item;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35 import org.openide.loaders.DataObject;
36 import org.openide.nodes.Node;
37 import org.openide.text.ActiveEditorDrop;
38 import org.openide.util.Lookup;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author Libor Kotouc
44  */

45 public class PaletteItemTest extends NbTestCase {
46   
47     private static final String JavaDoc PALETTE_ROOT = "FooPalette";
48     private static final String JavaDoc ITEMS_FOLDER = PALETTE_ROOT + "/FooCategory";
49     private static final String JavaDoc ITEM_FILE = "FooItem.xml";
50     private static final String JavaDoc CLASS_NAME = "org.netbeans.spi.palette.FooItem";
51     private static final String JavaDoc BODY = "<fooTag att=''></fooTag>";
52     private static final String JavaDoc ICON16 = "org/netbeans/spi/palette/FooItem16.gif";
53     private static final String JavaDoc ICON32 = "org/netbeans/spi/palette/FooItem32.gif";
54     private static final String JavaDoc BUNDLE_NAME = "org.netbeans.spi.palette.Bundle";
55     private static final String JavaDoc NAME_KEY = "NAME_foo-FooItem";
56     private static final String JavaDoc TOOLTIP_KEY = "HINT_foo-FooItem";
57     
58     
59     private FileObject itemsFolder;
60     
61     private Lookup selectedNode;
62     
63     public PaletteItemTest(String JavaDoc testName) {
64         super(testName);
65     }
66     
67     protected void setUp() throws Exception JavaDoc {
68         MockServices.setServices(new Class JavaDoc[] {RepositoryImpl.class});
69         itemsFolder = createItemsFolder();
70         assertNotNull(itemsFolder);
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         FileObject[] ch = itemsFolder.getChildren();
75         for (int i = 0; i < ch.length; i++)
76             ch[i].delete();
77         FileObject paletteRoot = itemsFolder.getParent();
78         itemsFolder.delete();
79         paletteRoot.delete();
80     }
81     
82     public void testReadItemWithActiveEditorDrop() throws Exception JavaDoc {
83         FileObject itemFile = createItemFileWithActiveEditorDrop();
84         verifyPaletteItem(itemFile);
85     }
86
87     public void testReadItemWithBody() throws Exception JavaDoc {
88         FileObject itemFile = createItemFileWithBody();
89         verifyPaletteItem(itemFile);
90     }
91     
92     public void testReadItemWithInlineDescription() throws Exception JavaDoc {
93         FileObject itemFile = createItemFileWithInLineDescription();
94         verifyPaletteItem(itemFile);
95     }
96     
97     private void verifyPaletteItem(FileObject itemFile) throws Exception JavaDoc {
98         assertNotNull(itemFile);
99         
100         Node itemNode = DataObject.find(itemFile).getNodeDelegate();
101         
102         assertEquals("Item loaded with a wrong display name.", NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY), itemNode.getDisplayName());
103         assertEquals("Item loaded with a wrong description.", NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY), itemNode.getShortDescription());
104         assertNotNull("Item loaded with no small icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_16x16));
105         assertNotNull("Item loaded with no big icon.", itemNode.getIcon(BeanInfo.ICON_COLOR_32x32));
106         
107         Object JavaDoc o = itemNode.getLookup().lookup(ActiveEditorDrop.class);
108         assertNotNull("Item does not contain ActiveEditorDrop implementation in its lookup.", o);
109     }
110
111     public void testFindItem() throws Exception JavaDoc {
112         FileObject itemFile = createItemFileWithActiveEditorDrop();
113         assertNotNull(itemFile);
114
115         //create palette
116
PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions());
117         
118         //find node with ActiveEditorDrop impl in its lookup
119
Node root = (Node)pc.getRoot().lookup(Node.class);
120         Node[] cats = root.getChildren().getNodes(true);
121         Node foundNode = null;
122         Node foundCat = null;
123         assertEquals("Too many categories", 1, cats.length);
124         Node[] items = cats[0].getChildren().getNodes(true);
125         assertEquals("Too many items", 1, items.length);
126         
127         assertTrue("Item not found.",
128                     DataObject.find(itemFile).getNodeDelegate().getLookup().lookup(ActiveEditorDrop.class) ==
129                                                        items[0].getLookup().lookup(ActiveEditorDrop.class)
130         );
131
132     }
133     
134     public void testSelectItem() throws Exception JavaDoc {
135         FileObject itemFile = createItemFileWithActiveEditorDrop();
136         assertNotNull(itemFile);
137
138         //create palette
139
PaletteController pc = PaletteFactory.createPalette(PALETTE_ROOT, new DummyActions());
140         pc.addPropertyChangeListener(new PaletteListener(pc));
141
142         //simulate node selection
143
Category modelCat = pc.getModel().getCategories()[0];
144         Item modelItem = modelCat.getItems()[0];
145         pc.getModel().setSelectedItem(modelCat.getLookup(), modelItem.getLookup());
146         
147         assertNotNull("Selected item does not contain ActiveEditorDrop implementation",
148                 selectedNode.lookup(ActiveEditorDrop.class));
149     }
150
151     
152     //---------------------------------- helpers ------------------------------------------------------------------
153

154     private class PaletteListener implements PropertyChangeListener JavaDoc {
155         PaletteController pc;
156         PaletteListener(PaletteController pc) { this.pc = pc; }
157         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
158             assertEquals("Property " + PaletteController.PROP_SELECTED_ITEM + " was expected.",
159                     PaletteController.PROP_SELECTED_ITEM, evt.getPropertyName());
160             
161             selectedNode = pc.getSelectedItem();
162         }
163     };
164         
165     
166     private FileObject createItemsFolder() throws IOException JavaDoc {
167         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
168         FileObject fooCategory = FileUtil.createFolder(root, ITEMS_FOLDER);
169         return fooCategory;
170     }
171     
172     private FileObject createItemFileWithActiveEditorDrop() throws Exception JavaDoc {
173         FileObject fo = itemsFolder.createData(ITEM_FILE);
174         FileLock lock = fo.lock();
175         try {
176             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(fo.getOutputStream(lock), "UTF-8");
177             try {
178                 writer.write("<?xml version='1.0' encoding='UTF-8'?>");
179                 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>");
180                 writer.write("<editor_palette_item version='1.0'>");
181                 writer.write("<class name='" + CLASS_NAME + "' />");
182                 writer.write("<icon16 urlvalue='" + ICON16 + "' />");
183                 writer.write("<icon32 urlvalue='" + ICON32 + "' />");
184                 writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />");
185                 writer.write("</editor_palette_item>");
186             } finally {
187                 writer.close();
188             }
189         } finally {
190             lock.releaseLock();
191         }
192         return fo;
193     }
194     
195     private FileObject createItemFileWithBody() throws Exception JavaDoc {
196         FileObject fo = itemsFolder.createData(ITEM_FILE);
197         FileLock lock = fo.lock();
198         try {
199             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(fo.getOutputStream(lock), "UTF-8");
200             try {
201                 writer.write("<?xml version='1.0' encoding='UTF-8'?>");
202                 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>");
203                 writer.write("<editor_palette_item version='1.0'>");
204                 writer.write("<body><![CDATA[" + BODY + "]]></body>");
205                 writer.write("<icon16 urlvalue='" + ICON16 + "' />");
206                 writer.write("<icon32 urlvalue='" + ICON32 + "' />");
207                 writer.write("<description localizing-bundle='" + BUNDLE_NAME + "' display-name-key='" + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY + "' />");
208                 writer.write("</editor_palette_item>");
209             } finally {
210                 writer.close();
211             }
212         } finally {
213             lock.releaseLock();
214         }
215         return fo;
216     }
217     
218     private FileObject createItemFileWithInLineDescription() throws Exception JavaDoc {
219         FileObject fo = itemsFolder.createData(ITEM_FILE);
220         FileLock lock = fo.lock();
221         try {
222             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(fo.getOutputStream(lock), "UTF-8");
223             try {
224                 writer.write("<?xml version='1.0' encoding='UTF-8'?>");
225                 writer.write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.1//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_1.dtd'>");
226                 writer.write("<editor_palette_item version='1.1'>");
227                 writer.write("<body><![CDATA[" + BODY + "]]></body>");
228                 writer.write("<icon16 urlvalue='" + ICON16 + "' />");
229                 writer.write("<icon32 urlvalue='" + ICON32 + "' />");
230                 writer.write("<inline-description> <display-name>"
231                         +NbBundle.getBundle(BUNDLE_NAME).getString(NAME_KEY)+"</display-name> <tooltip>"
232                         +NbBundle.getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY)+"</tooltip> </inline-description>");
233                 writer.write("</editor_palette_item>");
234             } finally {
235                 writer.close();
236             }
237         } finally {
238             lock.releaseLock();
239         }
240         return fo;
241     }
242     
243 }
244
Popular Tags