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.io.File;
045: import java.io.IOException;
046: import java.util.Arrays;
047: import junit.framework.*;
048: import org.netbeans.junit.NbTestCase;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileSystem;
051: import org.openide.filesystems.LocalFileSystem;
052: import org.openide.filesystems.Repository;
053: import org.openide.loaders.DataLoader;
054: import org.openide.loaders.DataLoaderPool;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.nodes.Node;
058:
059: /**
060: *
061: * @author Stanislav Aubrecht
062: */
063: public abstract class AbstractPaletteTestHid extends NbTestCase {
064:
065: protected FileObject paletteRootFolder;
066: private static final String PALETTE_ROOT_FOLDER_NAME = "test_palette_folder";
067: private String rootFolderName;
068:
069: protected String[] categoryNames;
070: protected String[][] itemNames;
071:
072: private static DataLoader myDummyLoader;
073:
074: public AbstractPaletteTestHid(String name) {
075: super (name);
076: }
077:
078: protected void setUp() throws Exception {
079: System.setProperty("org.openide.util.Lookup",
080: "org.netbeans.spi.palette.AbstractPaletteTestHid$Lkp");
081: // assertEquals ("Our lookup is installed", Lookup.getDefault ().getClass (), Lkp.class);
082:
083: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
084: // paletteRootFolder = fs.findResource( PALETTE_ROOT_FOLDER_NAME );
085: // if( null != paletteRootFolder )
086: // paletteRootFolder.delete();
087: rootFolderName = PALETTE_ROOT_FOLDER_NAME
088: + System.currentTimeMillis();
089: paletteRootFolder = fs.getRoot().createFolder(rootFolderName);
090:
091: // NbPreferences.forModule( DefaultSettings.class ).node( "CommonPaletteSettings" ).removeNode();
092:
093: if (null == myDummyLoader)
094: myDummyLoader = new DummyItemLoader();
095:
096: createDefaultPaletteContentInFolder(paletteRootFolder);
097: }
098:
099: @Override
100: protected boolean runInEQ() {
101: return false;
102: }
103:
104: protected void tearDown() throws Exception {
105: // if( null != paletteRootFolder ) {
106: // FileLock lock = null;
107: // try {
108: // if( paletteRootFolder.isValid() ) {
109: // lock = paletteRootFolder.lock();
110: // paletteRootFolder.delete( lock );
111: // }
112: // } finally {
113: // if( null != lock )
114: // lock.releaseLock();
115: // }
116: // }
117: }
118:
119: protected void createDefaultPaletteContentInFolder(
120: FileObject rootFolder) throws IOException {
121: categoryNames = new String[10];
122: itemNames = new String[categoryNames.length][10];
123: for (int i = 0; i < categoryNames.length; i++) {
124: categoryNames[i] = "Category_" + i;
125:
126: FileObject catFolder = rootFolder
127: .createFolder(categoryNames[i]);
128:
129: for (int j = 0; j < itemNames[i].length; j++) {
130: itemNames[i][j] = categoryNames[i] + "_Item_" + j;
131:
132: FileObject itemFile = catFolder.createData(
133: itemNames[i][j], DummyItemLoader.ITEM_EXT);
134: DataLoaderPool.setPreferredLoader(itemFile,
135: myDummyLoader);
136: }
137: }
138: }
139:
140: protected String getRootFolderName() {
141: return rootFolderName;
142: }
143:
144: protected FileObject getCategoryFile(String catName)
145: throws DataObjectNotFoundException {
146: FileObject fo = paletteRootFolder.getFileObject(catName);
147: if (null == fo) {
148: fail("Category folder '" + catName + "' not found.");
149: }
150: return fo;
151: }
152:
153: protected Node getCategoryNode(String catName)
154: throws DataObjectNotFoundException {
155: FileObject fo = getCategoryFile(catName);
156: DataObject dobj = DataObject.find(fo);
157: if (null == dobj) {
158: fail("Category data object '" + catName + "' not found.");
159: }
160: return dobj.getNodeDelegate();
161: }
162:
163: protected FileObject getItemFile(String catName, String itemName)
164: throws DataObjectNotFoundException {
165: FileObject fo = getCategoryFile(catName);
166: FileObject itemFO = fo.getFileObject(itemName,
167: DummyItemLoader.ITEM_EXT);
168: if (null == itemFO) {
169: fail("Item file '" + itemName + "' not found.");
170: }
171: return itemFO;
172: }
173:
174: protected Node getItemNode(String catName, String itemName)
175: throws DataObjectNotFoundException {
176: FileObject fo = getItemFile(catName, itemName);
177: DataObject dobj = DataObject.find(fo);
178: if (null == dobj) {
179: fail("Item data object '" + itemName + "' not found.");
180: }
181: return dobj.getNodeDelegate();
182: }
183:
184: //
185: // Our fake lookup
186: //
187: public static final class Lkp extends
188: org.openide.util.lookup.AbstractLookup {
189: public Lkp() throws Exception {
190: this (new org.openide.util.lookup.InstanceContent());
191: }
192:
193: private Lkp(org.openide.util.lookup.InstanceContent ic)
194: throws Exception {
195: super (ic);
196:
197: ic.add(new Repository(createLocalFileSystem(Lkp.class
198: .getName()
199: + System.currentTimeMillis(), new String[0])));
200: }
201: }
202:
203: public static FileSystem createLocalFileSystem(String name,
204: String[] resources) throws IOException {
205: File f = File.createTempFile(name, ".tmp");
206: f.delete();
207: f = new File(f.getParent(), name);
208: f.mkdirs();
209: return createLocalFileSystem(f, resources);
210: }
211:
212: public static FileSystem createLocalFileSystem(File mountPoint,
213: String[] resources) throws IOException {
214: mountPoint.mkdir();
215:
216: for (int i = 0; i < resources.length; i++) {
217: File f = new File(mountPoint, resources[i]);
218: if (f.isDirectory() || resources[i].endsWith("/")) {
219: f.mkdirs();
220: } else {
221: f.getParentFile().mkdirs();
222: try {
223: f.createNewFile();
224: } catch (IOException iex) {
225: throw new IOException("While creating "
226: + resources[i] + " in "
227: + mountPoint.getAbsolutePath() + ": "
228: + iex.toString() + ": "
229: + f.getAbsolutePath()
230: + " with resource list: "
231: + Arrays.asList(resources));
232: }
233: }
234: }
235:
236: LocalFileSystem lfs = new StatusFileSystem();
237: try {
238: lfs.setRootDirectory(mountPoint);
239: } catch (Exception ex) {
240: }
241:
242: return lfs;
243: }
244:
245: static class StatusFileSystem extends LocalFileSystem {
246: Status status = new Status() {
247: public String annotateName(String name, java.util.Set files) {
248: return name;
249: }
250:
251: public java.awt.Image annotateIcon(java.awt.Image icon,
252: int iconType, java.util.Set files) {
253: return icon;
254: }
255: };
256:
257: public org.openide.filesystems.FileSystem.Status getStatus() {
258: return status;
259: }
260:
261: }
262: }
|