1 19 20 package org.netbeans.spi.palette; 21 import java.io.IOException ; 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 38 public abstract class AbstractPaletteTestHid extends TestCase { 39 40 protected FileObject paletteRootFolder; 41 private static final String PALETTE_ROOT_FOLDER_NAME = "test_palette_folder"; 42 private String rootFolderName; 43 44 protected String [] categoryNames; 45 protected String [][] itemNames; 46 47 private static DataLoader myDummyLoader; 48 49 public AbstractPaletteTestHid( String name ) { 50 super( name ); 51 } 52 53 protected void setUp() throws Exception { 54 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 55 rootFolderName = PALETTE_ROOT_FOLDER_NAME+System.currentTimeMillis(); 59 paletteRootFolder = fs.getRoot().createFolder( rootFolderName ); 60 61 63 if( null == myDummyLoader ) 64 myDummyLoader = new DummyItemLoader(); 65 66 createDefaultPaletteContentInFolder( paletteRootFolder ); 67 } 68 69 protected void tearDown() throws Exception { 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 { 85 categoryNames = new String [10]; 86 itemNames = new String [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 getRootFolderName() { 102 return rootFolderName; 103 } 104 105 protected FileObject getCategoryFile( String 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 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 catName, String 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 catName, String 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 |