1 19 20 package org.openide.loaders; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.util.ArrayList ; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.LocalFileSystem; 32 import org.openide.filesystems.Repository; 33 import org.openide.nodes.Node; 34 import org.openide.util.datatransfer.PasteType; 35 36 public class DataFolderPasteTypesTest extends NbTestCase { 37 38 private File dir; 39 private Node folderNode; 40 private LocalFileSystem testFileSystem; 41 42 43 public DataFolderPasteTypesTest (String name) { 44 super (name); 45 } 46 47 protected void setUp() throws Exception { 48 clearWorkDir(); 49 50 testFileSystem = new LocalFileSystem(); 51 testFileSystem.setRootDirectory( getWorkDir() ); 52 Repository.getDefault().addFileSystem( testFileSystem ); 53 54 FileObject fo = FileUtil.createFolder( testFileSystem.getRoot(), "testDir"); 55 DataObject dob = DataObject.find( fo ); 56 folderNode = dob.getNodeDelegate(); 57 } 58 59 protected void tearDown() throws Exception { 60 61 clearWorkDir(); 62 Repository.getDefault().removeFileSystem( testFileSystem ); 63 } 64 65 public void testNoPasteTypes() throws ClassNotFoundException { 66 DataFlavor flavor = new DataFlavor ( "unsupported/flavor;class=java.lang.Object" ); 67 68 DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode; 69 ArrayList list = new ArrayList (); 70 node.createPasteTypes( new MockTransferable( new DataFlavor [] {flavor}, null ), list ); 71 assertEquals( 0, list.size() ); 72 } 73 74 public void testJavaFileListPasteTypes() throws ClassNotFoundException , IOException { 75 FileObject testFO = FileUtil.createData( testFileSystem.getRoot(), "testFile.txt" ); 76 File testFile = FileUtil.toFile( testFO ); 77 ArrayList fileList = new ArrayList (1); 78 fileList.add( testFile ); 79 Transferable t = new MockTransferable( new DataFlavor [] {DataFlavor.javaFileListFlavor}, fileList ); 80 81 DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode; 82 ArrayList list = new ArrayList (); 83 node.createPasteTypes( t, list ); 84 assertFalse( list.isEmpty() ); 85 PasteType paste = (PasteType)list.get( 0 ); 86 paste.paste(); 87 88 FileObject[] children = testFileSystem.getRoot().getFileObject( "testDir" ).getChildren(); 89 assertEquals( 1, children.length ); 90 assertEquals( children[0].getNameExt(), "testFile.txt" ); 91 } 92 93 public void testUriFileListPasteTypes() throws ClassNotFoundException , IOException { 94 DataFlavor flavor = new DataFlavor ( "unsupported/flavor;class=java.lang.Object" ); 95 FileObject testFO = FileUtil.createData( testFileSystem.getRoot(), "testFile.txt" ); 96 File testFile = FileUtil.toFile( testFO ); 97 String uriList = testFile.toURI() + "\r\n"; 98 Transferable t = new MockTransferable( new DataFlavor [] {new DataFlavor ("text/uri-list;class=java.lang.String")}, uriList ); 99 100 DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode; 101 ArrayList list = new ArrayList (); 102 node.createPasteTypes( t, list ); 103 assertFalse( list.isEmpty() ); 104 PasteType paste = (PasteType)list.get( 0 ); 105 paste.paste(); 106 107 FileObject[] children = testFileSystem.getRoot().getFileObject( "testDir" ).getChildren(); 108 assertEquals( 1, children.length ); 109 assertEquals( children[0].getNameExt(), "testFile.txt" ); 110 } 111 112 private static class MockTransferable implements Transferable { 113 private DataFlavor [] flavors; 114 private Object data; 115 public MockTransferable( DataFlavor [] flavors, Object data ) { 116 this.flavors = flavors; 117 this.data = data; 118 } 119 120 public DataFlavor [] getTransferDataFlavors() { 121 return flavors; 122 } 123 124 public boolean isDataFlavorSupported(DataFlavor flavor) { 125 for( int i=0; i<flavors.length; i++ ) { 126 if( flavors[i].equals( flavor ) ) { 127 return true; 128 } 129 } 130 return false; 131 } 132 133 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException , IOException { 134 if( !isDataFlavorSupported( flavor ) ) { 135 throw new UnsupportedFlavorException ( flavor ); 136 } 137 return data; 138 } 139 140 } 141 } 142 | Popular Tags |