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.palette;
043:
044: import java.beans.BeanInfo;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.IOException;
048: import java.io.OutputStreamWriter;
049: import org.netbeans.junit.MockServices;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.modules.palette.Category;
052: import org.netbeans.modules.palette.Item;
053: import org.openide.filesystems.FileLock;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.filesystems.Repository;
057: import org.openide.loaders.DataObject;
058: import org.openide.nodes.Node;
059: import org.openide.text.ActiveEditorDrop;
060: import org.openide.util.Lookup;
061: import org.openide.util.NbBundle;
062:
063: /**
064: *
065: * @author Libor Kotouc
066: */
067: public class PaletteItemTest extends NbTestCase {
068:
069: private static final String PALETTE_ROOT = "FooPalette";
070: private static final String ITEMS_FOLDER = PALETTE_ROOT
071: + "/FooCategory";
072: private static final String ITEM_FILE = "FooItem.xml";
073: private static final String CLASS_NAME = "org.netbeans.spi.palette.FooItem";
074: private static final String BODY = "<fooTag att=''></fooTag>";
075: private static final String ICON16 = "org/netbeans/spi/palette/FooItem16.gif";
076: private static final String ICON32 = "org/netbeans/spi/palette/FooItem32.gif";
077: private static final String BUNDLE_NAME = "org.netbeans.spi.palette.Bundle";
078: private static final String NAME_KEY = "NAME_foo-FooItem";
079: private static final String TOOLTIP_KEY = "HINT_foo-FooItem";
080:
081: private FileObject itemsFolder;
082:
083: private Lookup selectedNode;
084:
085: public PaletteItemTest(String testName) {
086: super (testName);
087: }
088:
089: protected void setUp() throws Exception {
090: MockServices.setServices(new Class[] { RepositoryImpl.class });
091: itemsFolder = createItemsFolder();
092: assertNotNull(itemsFolder);
093: }
094:
095: protected void tearDown() throws Exception {
096: FileObject[] ch = itemsFolder.getChildren();
097: for (int i = 0; i < ch.length; i++)
098: ch[i].delete();
099: FileObject paletteRoot = itemsFolder.getParent();
100: itemsFolder.delete();
101: paletteRoot.delete();
102: }
103:
104: public void testReadItemWithActiveEditorDrop() throws Exception {
105: FileObject itemFile = createItemFileWithActiveEditorDrop();
106: verifyPaletteItem(itemFile);
107: }
108:
109: public void testReadItemWithBody() throws Exception {
110: FileObject itemFile = createItemFileWithBody();
111: verifyPaletteItem(itemFile);
112: }
113:
114: public void testReadItemWithInlineDescription() throws Exception {
115: FileObject itemFile = createItemFileWithInLineDescription();
116: verifyPaletteItem(itemFile);
117: }
118:
119: private void verifyPaletteItem(FileObject itemFile)
120: throws Exception {
121: assertNotNull(itemFile);
122:
123: Node itemNode = DataObject.find(itemFile).getNodeDelegate();
124:
125: assertEquals("Item loaded with a wrong display name.", NbBundle
126: .getBundle(BUNDLE_NAME).getString(NAME_KEY), itemNode
127: .getDisplayName());
128: assertEquals("Item loaded with a wrong description.", NbBundle
129: .getBundle(BUNDLE_NAME).getString(TOOLTIP_KEY),
130: itemNode.getShortDescription());
131: assertNotNull("Item loaded with no small icon.", itemNode
132: .getIcon(BeanInfo.ICON_COLOR_16x16));
133: assertNotNull("Item loaded with no big icon.", itemNode
134: .getIcon(BeanInfo.ICON_COLOR_32x32));
135:
136: Object o = itemNode.getLookup().lookup(ActiveEditorDrop.class);
137: assertNotNull(
138: "Item does not contain ActiveEditorDrop implementation in its lookup.",
139: o);
140: }
141:
142: public void testFindItem() throws Exception {
143: FileObject itemFile = createItemFileWithActiveEditorDrop();
144: assertNotNull(itemFile);
145:
146: //create palette
147: PaletteController pc = PaletteFactory.createPalette(
148: PALETTE_ROOT, new DummyActions());
149:
150: //find node with ActiveEditorDrop impl in its lookup
151: Node root = (Node) pc.getRoot().lookup(Node.class);
152: Node[] cats = root.getChildren().getNodes(true);
153: Node foundNode = null;
154: Node foundCat = null;
155: assertEquals("Too many categories", 1, cats.length);
156: Node[] items = cats[0].getChildren().getNodes(true);
157: assertEquals("Too many items", 1, items.length);
158:
159: assertTrue("Item not found.", DataObject.find(itemFile)
160: .getNodeDelegate().getLookup().lookup(
161: ActiveEditorDrop.class) == items[0].getLookup()
162: .lookup(ActiveEditorDrop.class));
163:
164: }
165:
166: public void testSelectItem() throws Exception {
167: FileObject itemFile = createItemFileWithActiveEditorDrop();
168: assertNotNull(itemFile);
169:
170: //create palette
171: PaletteController pc = PaletteFactory.createPalette(
172: PALETTE_ROOT, new DummyActions());
173: pc.addPropertyChangeListener(new PaletteListener(pc));
174:
175: //simulate node selection
176: Category modelCat = pc.getModel().getCategories()[0];
177: Item modelItem = modelCat.getItems()[0];
178: pc.getModel().setSelectedItem(modelCat.getLookup(),
179: modelItem.getLookup());
180:
181: assertNotNull(
182: "Selected item does not contain ActiveEditorDrop implementation",
183: selectedNode.lookup(ActiveEditorDrop.class));
184: }
185:
186: //---------------------------------- helpers ------------------------------------------------------------------
187:
188: private class PaletteListener implements PropertyChangeListener {
189: PaletteController pc;
190:
191: PaletteListener(PaletteController pc) {
192: this .pc = pc;
193: }
194:
195: public void propertyChange(PropertyChangeEvent evt) {
196: assertEquals("Property "
197: + PaletteController.PROP_SELECTED_ITEM
198: + " was expected.",
199: PaletteController.PROP_SELECTED_ITEM, evt
200: .getPropertyName());
201:
202: selectedNode = pc.getSelectedItem();
203: }
204: };
205:
206: private FileObject createItemsFolder() throws IOException {
207: FileObject root = Repository.getDefault()
208: .getDefaultFileSystem().getRoot();
209: FileObject fooCategory = FileUtil.createFolder(root,
210: ITEMS_FOLDER);
211: return fooCategory;
212: }
213:
214: private FileObject createItemFileWithActiveEditorDrop()
215: throws Exception {
216: FileObject fo = itemsFolder.createData(ITEM_FILE);
217: FileLock lock = fo.lock();
218: try {
219: OutputStreamWriter writer = new OutputStreamWriter(fo
220: .getOutputStream(lock), "UTF-8");
221: try {
222: writer.write("<?xml version='1.0' encoding='UTF-8'?>");
223: writer
224: .write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>");
225: writer.write("<editor_palette_item version='1.0'>");
226: writer.write("<class name='" + CLASS_NAME + "' />");
227: writer.write("<icon16 urlvalue='" + ICON16 + "' />");
228: writer.write("<icon32 urlvalue='" + ICON32 + "' />");
229: writer.write("<description localizing-bundle='"
230: + BUNDLE_NAME + "' display-name-key='"
231: + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY
232: + "' />");
233: writer.write("</editor_palette_item>");
234: } finally {
235: writer.close();
236: }
237: } finally {
238: lock.releaseLock();
239: }
240: return fo;
241: }
242:
243: private FileObject createItemFileWithBody() throws Exception {
244: FileObject fo = itemsFolder.createData(ITEM_FILE);
245: FileLock lock = fo.lock();
246: try {
247: OutputStreamWriter writer = new OutputStreamWriter(fo
248: .getOutputStream(lock), "UTF-8");
249: try {
250: writer.write("<?xml version='1.0' encoding='UTF-8'?>");
251: writer
252: .write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.0//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd'>");
253: writer.write("<editor_palette_item version='1.0'>");
254: writer.write("<body><![CDATA[" + BODY + "]]></body>");
255: writer.write("<icon16 urlvalue='" + ICON16 + "' />");
256: writer.write("<icon32 urlvalue='" + ICON32 + "' />");
257: writer.write("<description localizing-bundle='"
258: + BUNDLE_NAME + "' display-name-key='"
259: + NAME_KEY + "' tooltip-key='" + TOOLTIP_KEY
260: + "' />");
261: writer.write("</editor_palette_item>");
262: } finally {
263: writer.close();
264: }
265: } finally {
266: lock.releaseLock();
267: }
268: return fo;
269: }
270:
271: private FileObject createItemFileWithInLineDescription()
272: throws Exception {
273: FileObject fo = itemsFolder.createData(ITEM_FILE);
274: FileLock lock = fo.lock();
275: try {
276: OutputStreamWriter writer = new OutputStreamWriter(fo
277: .getOutputStream(lock), "UTF-8");
278: try {
279: writer.write("<?xml version='1.0' encoding='UTF-8'?>");
280: writer
281: .write("<!DOCTYPE editor_palette_item PUBLIC '-//NetBeans//Editor Palette Item 1.1//EN' 'http://www.netbeans.org/dtds/editor-palette-item-1_1.dtd'>");
282: writer.write("<editor_palette_item version='1.1'>");
283: writer.write("<body><![CDATA[" + BODY + "]]></body>");
284: writer.write("<icon16 urlvalue='" + ICON16 + "' />");
285: writer.write("<icon32 urlvalue='" + ICON32 + "' />");
286: writer.write("<inline-description> <display-name>"
287: + NbBundle.getBundle(BUNDLE_NAME).getString(
288: NAME_KEY)
289: + "</display-name> <tooltip>"
290: + NbBundle.getBundle(BUNDLE_NAME).getString(
291: TOOLTIP_KEY)
292: + "</tooltip> </inline-description>");
293: writer.write("</editor_palette_item>");
294: } finally {
295: writer.close();
296: }
297: } finally {
298: lock.releaseLock();
299: }
300: return fo;
301: }
302:
303: }
|