KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22 import junit.framework.*;
23 import org.openide.filesystems.FileLock;
24 import org.openide.filesystems.FileObject;
25 import org.openide.filesystems.FileSystem;
26 import org.openide.filesystems.Repository;
27 import org.openide.loaders.DataLoader;
28 import org.openide.loaders.DataLoaderPool;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.nodes.Node;
32
33
34 /**
35  *
36  * @author Stanislav Aubrecht
37  */

38 public abstract class AbstractPaletteTestHid extends TestCase {
39
40     protected FileObject paletteRootFolder;
41     private static final String JavaDoc PALETTE_ROOT_FOLDER_NAME = "test_palette_folder";
42     private String JavaDoc rootFolderName;
43     
44     protected String JavaDoc[] categoryNames;
45     protected String JavaDoc[][] itemNames;
46     
47     private static DataLoader myDummyLoader;
48
49     public AbstractPaletteTestHid( String JavaDoc name ) {
50         super( name );
51     }
52     
53     protected void setUp() throws Exception JavaDoc {
54         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
55 // paletteRootFolder = fs.findResource( PALETTE_ROOT_FOLDER_NAME );
56
// if( null != paletteRootFolder )
57
// paletteRootFolder.delete();
58
rootFolderName = PALETTE_ROOT_FOLDER_NAME+System.currentTimeMillis();
59         paletteRootFolder = fs.getRoot().createFolder( rootFolderName );
60         
61 // NbPreferences.forModule( DefaultSettings.class ).node( "CommonPaletteSettings" ).removeNode();
62

63         if( null == myDummyLoader )
64             myDummyLoader = new DummyItemLoader();
65         
66         createDefaultPaletteContentInFolder( paletteRootFolder );
67             }
68
69     protected void tearDown() throws Exception JavaDoc {
70         if( null != paletteRootFolder ) {
71             FileLock lock = null;
72             try {
73                 if( paletteRootFolder.isValid() ) {
74                     lock = paletteRootFolder.lock();
75                     paletteRootFolder.delete( lock );
76                 }
77             } finally {
78                 if( null != lock )
79                     lock.releaseLock();
80             }
81         }
82     }
83     
84     protected void createDefaultPaletteContentInFolder( FileObject rootFolder ) throws IOException JavaDoc {
85         categoryNames = new String JavaDoc[10];
86         itemNames = new String JavaDoc[categoryNames.length][10];
87         for( int i=0; i<categoryNames.length; i++ ) {
88             categoryNames[i] = "Category_" + i;
89             
90             FileObject catFolder = rootFolder.createFolder( categoryNames[i] );
91             
92             for( int j=0; j<itemNames[i].length; j++ ) {
93                 itemNames[i][j] = categoryNames[i] + "_Item_" + j;
94                 
95                 FileObject itemFile = catFolder.createData( itemNames[i][j], DummyItemLoader.ITEM_EXT );
96                 DataLoaderPool.setPreferredLoader( itemFile, myDummyLoader );
97             }
98         }
99     }
100     
101     protected String JavaDoc getRootFolderName() {
102         return rootFolderName;
103     }
104     
105     protected FileObject getCategoryFile( String JavaDoc catName ) throws DataObjectNotFoundException {
106         FileObject fo = paletteRootFolder.getFileObject( catName );
107         if( null == fo ) {
108             fail( "Category folder '" + catName + "' not found." );
109         }
110         return fo;
111     }
112     
113     protected Node getCategoryNode( String JavaDoc catName ) throws DataObjectNotFoundException {
114         FileObject fo = getCategoryFile( catName );
115         DataObject dobj = DataObject.find( fo );
116         if( null == dobj ) {
117             fail( "Category data object '" + catName + "' not found." );
118         }
119         return dobj.getNodeDelegate();
120     }
121     
122     protected FileObject getItemFile( String JavaDoc catName, String JavaDoc itemName ) throws DataObjectNotFoundException {
123         FileObject fo = getCategoryFile( catName );
124         FileObject itemFO = fo.getFileObject( itemName, DummyItemLoader.ITEM_EXT );
125         if( null == itemFO ) {
126             fail( "Item file '" + itemName + "' not found." );
127         }
128         return itemFO;
129     }
130     
131     protected Node getItemNode( String JavaDoc catName, String JavaDoc itemName ) throws DataObjectNotFoundException {
132         FileObject fo = getItemFile( catName, itemName );
133         DataObject dobj = DataObject.find( fo );
134         if( null == dobj ) {
135             fail( "Item data object '" + itemName + "' not found." );
136         }
137         return dobj.getNodeDelegate();
138     }
139 }
140
Popular Tags