1 package org.netbeans.modules.openfile; 2 3 import java.awt.Component ; 4 import java.awt.datatransfer.Transferable ; 5 import java.awt.datatransfer.UnsupportedFlavorException ; 6 import java.awt.dnd.DropTarget ; 7 import java.io.File ; 8 import java.io.IOException ; 9 import java.lang.reflect.InvocationTargetException ; 10 import java.net.URI ; 11 import java.net.URISyntaxException ; 12 import java.util.ArrayList ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import javax.swing.JComponent ; 16 import javax.swing.JPanel ; 17 import junit.framework.*; 18 import java.awt.datatransfer.DataFlavor ; 19 import java.util.List ; 20 import java.util.Set ; 21 import org.netbeans.junit.MockServices; 22 import org.netbeans.junit.NbTestCase; 23 import org.openide.filesystems.FileObject; 24 import org.openide.filesystems.FileUtil; 25 import org.openide.util.Lookup; 26 27 31 public class DefaultExternalDropHandlerTest extends NbTestCase { 32 33 public DefaultExternalDropHandlerTest(String testName) { 34 super(testName); 35 } 36 37 protected void setUp() throws Exception { 38 } 39 40 protected void tearDown() throws Exception { 41 } 42 43 public static Test suite() { 44 return new TestSuite(DefaultExternalDropHandlerTest.class); 45 } 46 47 public void testUriListFlavor() throws ClassNotFoundException { 48 DefaultExternalDropHandler handler = new DefaultExternalDropHandler(); 49 50 DataFlavor uriListFlavor = new DataFlavor ("text/uri-list;class=java.lang.String"); 51 52 assertEquals( uriListFlavor, handler.getUriListDataFlavor() ); 53 } 54 55 public void testCanDrop() throws ClassNotFoundException { 56 DefaultExternalDropHandler handler = new DefaultExternalDropHandler(); 57 58 DataFlavor [] flavors = new DataFlavor [] { DataFlavor.imageFlavor }; 59 assertFalse( handler.canDrop( flavors ) ); 60 61 flavors = new DataFlavor [] { DataFlavor.imageFlavor, DataFlavor.stringFlavor }; 62 assertFalse( handler.canDrop( flavors ) ); 63 64 flavors = new DataFlavor [] { DataFlavor.javaFileListFlavor }; 65 assertTrue( handler.canDrop( flavors ) ); 66 67 flavors = new DataFlavor [] { DataFlavor.javaFileListFlavor, DataFlavor.imageFlavor }; 68 assertTrue( handler.canDrop( flavors ) ); 69 70 DataFlavor uriListFlavor = new DataFlavor ("text/uri-list;class=java.lang.String"); 71 72 flavors = new DataFlavor [] { uriListFlavor }; 73 assertTrue( handler.canDrop( flavors ) ); 74 75 flavors = new DataFlavor [] { uriListFlavor, DataFlavor.imageFlavor }; 76 assertTrue( handler.canDrop( flavors ) ); 77 78 flavors = new DataFlavor [] { uriListFlavor, DataFlavor.javaFileListFlavor, DataFlavor.imageFlavor }; 79 assertTrue( handler.canDrop( flavors ) ); 80 } 81 82 public void testGetFileList() throws ClassNotFoundException , URISyntaxException , NoSuchMethodException , InstantiationException , IllegalAccessException , InvocationTargetException { 83 DefaultExternalDropHandler handler = new DefaultExternalDropHandler(); 84 DataFlavor uriListFlavor = new DataFlavor ("text/uri-list;class=java.lang.String"); 85 86 Transferable t = new TransferableImpl( new ArrayList (), new DataFlavor [] { DataFlavor.imageFlavor, DataFlavor.stringFlavor } ); 87 assertNull( handler.getFileList( t ) ); 88 89 ArrayList list = new ArrayList ( 2 ); 90 list.add( new File ("//somedir/somefile.someext") ); 91 list.add( new File ("c:\\somedir\\somefile.someext") ); 92 t = new TransferableImpl( list, new DataFlavor [] { DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor } ); 93 94 List extractedList = handler.getFileList( t ); 95 assertNotNull( extractedList ); 96 assertEquals( 2, extractedList.size() ); 97 for( Iterator i=list.iterator(); i.hasNext(); ) { 98 assertTrue( extractedList.contains( i.next() ) ); 99 } 100 101 String uriList = "file:///somedir/somefile.someext\r\nfile:/c:/somedir/somefile.someext"; 102 t = new TransferableImpl( uriList, new DataFlavor [] { uriListFlavor, DataFlavor.stringFlavor } ); 103 extractedList = handler.getFileList( t ); 104 assertNotNull( extractedList ); 105 assertEquals( 2, extractedList.size() ); 106 assertTrue( extractedList.contains( new File ( new URI ("file:///somedir/somefile.someext") ) ) ); 107 assertTrue( extractedList.contains( new File ( new URI ("file:/c:/somedir/somefile.someext") ) ) ); 108 } 109 110 public void testOpenFile() throws IOException { 111 DefaultExternalDropHandler handler = new DefaultExternalDropHandler(); 112 MockServices.setServices(MockOpenFileImpl.class); 113 MockOpenFileImpl openImpl = Lookup.getDefault().lookup(MockOpenFileImpl.class); 114 assertNotNull("Registered", openImpl); 115 116 117 File file = File.createTempFile( "somefile", ".someext", getWorkDir() ); 118 handler.openFile( file ); 119 assertTrue( openImpl.wasOpened( file ) ); 120 if( null != file ) { 121 file.delete(); 122 file.deleteOnExit(); 123 } 124 } 125 126 public static class MockOpenFileImpl implements OpenFileImpl { 127 private Set openedFiles = new HashSet (); 128 129 public boolean open(FileObject fileObject, int line) { 130 if( null != fileObject ) { 131 openedFiles.add( FileUtil.toFile(fileObject) ); 132 return true; 133 } 134 return false; 135 } 136 137 public boolean wasOpened( File file ) { 138 return openedFiles.contains( file ); 139 } 140 } 141 142 private static class TransferableImpl implements Transferable { 143 private Object data; 144 private DataFlavor [] flavors; 145 146 public TransferableImpl( Object data, DataFlavor [] flavors ) { 147 this.data = data; 148 this.flavors = flavors; 149 } 150 151 public boolean isDataFlavorSupported(DataFlavor flavor) { 152 for( int i=0; i<flavors.length; i++ ) { 153 if( flavor.equals( flavors[i] ) ) 154 return true; 155 } 156 return false; 157 } 158 159 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException , IOException { 160 if( isDataFlavorSupported( flavor ) ) { 161 return data; 162 } 163 throw new UnsupportedFlavorException ( flavor ); 164 } 165 166 public DataFlavor [] getTransferDataFlavors() { 167 return flavors; 168 } 169 } 170 171 private static class MockDropTarget extends DropTarget { 172 private JComponent component = new JPanel (); 173 174 public MockDropTarget() { 175 } 176 177 public Component getComponent() { 178 return component; 179 } 180 181 } 182 } 183 | Popular Tags |