KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > DefaultExternalDropHandlerTest


1 package org.netbeans.modules.openfile;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.datatransfer.Transferable JavaDoc;
5 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
6 import java.awt.dnd.DropTarget JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.net.URI JavaDoc;
11 import java.net.URISyntaxException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.swing.JComponent JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17 import junit.framework.*;
18 import java.awt.datatransfer.DataFlavor JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
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 /**
28  *
29  * @author S. Aubrecht
30  */

31 public class DefaultExternalDropHandlerTest extends NbTestCase {
32     
33     public DefaultExternalDropHandlerTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     protected void setUp() throws Exception JavaDoc {
38     }
39
40     protected void tearDown() throws Exception JavaDoc {
41     }
42
43     public static Test suite() {
44         return new TestSuite(DefaultExternalDropHandlerTest.class);
45     }
46
47     public void testUriListFlavor() throws ClassNotFoundException JavaDoc {
48         DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
49
50         DataFlavor JavaDoc uriListFlavor = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
51
52         assertEquals( uriListFlavor, handler.getUriListDataFlavor() );
53     }
54
55     public void testCanDrop() throws ClassNotFoundException JavaDoc {
56         DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
57
58         DataFlavor JavaDoc[] flavors = new DataFlavor JavaDoc[] { DataFlavor.imageFlavor };
59         assertFalse( handler.canDrop( flavors ) );
60
61         flavors = new DataFlavor JavaDoc[] { DataFlavor.imageFlavor, DataFlavor.stringFlavor };
62         assertFalse( handler.canDrop( flavors ) );
63
64         flavors = new DataFlavor JavaDoc[] { DataFlavor.javaFileListFlavor };
65         assertTrue( handler.canDrop( flavors ) );
66
67         flavors = new DataFlavor JavaDoc[] { DataFlavor.javaFileListFlavor, DataFlavor.imageFlavor };
68         assertTrue( handler.canDrop( flavors ) );
69
70         DataFlavor JavaDoc uriListFlavor = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
71
72         flavors = new DataFlavor JavaDoc[] { uriListFlavor };
73         assertTrue( handler.canDrop( flavors ) );
74
75         flavors = new DataFlavor JavaDoc[] { uriListFlavor, DataFlavor.imageFlavor };
76         assertTrue( handler.canDrop( flavors ) );
77
78         flavors = new DataFlavor JavaDoc[] { uriListFlavor, DataFlavor.javaFileListFlavor, DataFlavor.imageFlavor };
79         assertTrue( handler.canDrop( flavors ) );
80     }
81
82     public void testGetFileList() throws ClassNotFoundException JavaDoc, URISyntaxException JavaDoc, NoSuchMethodException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
83         DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
84         DataFlavor JavaDoc uriListFlavor = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
85
86         Transferable JavaDoc t = new TransferableImpl( new ArrayList JavaDoc(), new DataFlavor JavaDoc[] { DataFlavor.imageFlavor, DataFlavor.stringFlavor } );
87         assertNull( handler.getFileList( t ) );
88
89         ArrayList JavaDoc list = new ArrayList JavaDoc( 2 );
90         list.add( new File JavaDoc("//somedir/somefile.someext") );
91         list.add( new File JavaDoc("c:\\somedir\\somefile.someext") );
92         t = new TransferableImpl( list, new DataFlavor JavaDoc[] { DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor } );
93
94         List JavaDoc extractedList = handler.getFileList( t );
95         assertNotNull( extractedList );
96         assertEquals( 2, extractedList.size() );
97         for( Iterator JavaDoc i=list.iterator(); i.hasNext(); ) {
98             assertTrue( extractedList.contains( i.next() ) );
99         }
100
101         String JavaDoc uriList = "file:///somedir/somefile.someext\r\nfile:/c:/somedir/somefile.someext";
102         t = new TransferableImpl( uriList, new DataFlavor JavaDoc[] { uriListFlavor, DataFlavor.stringFlavor } );
103         extractedList = handler.getFileList( t );
104         assertNotNull( extractedList );
105         assertEquals( 2, extractedList.size() );
106         assertTrue( extractedList.contains( new File JavaDoc( new URI JavaDoc("file:///somedir/somefile.someext") ) ) );
107         assertTrue( extractedList.contains( new File JavaDoc( new URI JavaDoc("file:/c:/somedir/somefile.someext") ) ) );
108     }
109
110     public void testOpenFile() throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc/*<FileObject>*/ openedFiles = new HashSet JavaDoc();
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 JavaDoc file ) {
138             return openedFiles.contains( file );
139         }
140     }
141
142     private static class TransferableImpl implements Transferable JavaDoc {
143         private Object JavaDoc data;
144         private DataFlavor JavaDoc[] flavors;
145
146         public TransferableImpl( Object JavaDoc data, DataFlavor JavaDoc[] flavors ) {
147             this.data = data;
148             this.flavors = flavors;
149         }
150
151         public boolean isDataFlavorSupported(DataFlavor JavaDoc 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 JavaDoc getTransferData(DataFlavor JavaDoc flavor) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc {
160             if( isDataFlavorSupported( flavor ) ) {
161                 return data;
162             }
163             throw new UnsupportedFlavorException JavaDoc( flavor );
164         }
165
166         public DataFlavor JavaDoc[] getTransferDataFlavors() {
167             return flavors;
168         }
169     }
170
171     private static class MockDropTarget extends DropTarget JavaDoc {
172         private JComponent JavaDoc component = new JPanel JavaDoc();
173
174         public MockDropTarget() {
175         }
176
177         public Component JavaDoc getComponent() {
178             return component;
179         }
180
181     }
182 }
183
Popular Tags