001: package org.netbeans.modules.openfile;
002:
003: import java.awt.Component;
004: import java.awt.datatransfer.Transferable;
005: import java.awt.datatransfer.UnsupportedFlavorException;
006: import java.awt.dnd.DropTarget;
007: import java.io.File;
008: import java.io.IOException;
009: import java.lang.reflect.InvocationTargetException;
010: import java.net.URI;
011: import java.net.URISyntaxException;
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import javax.swing.JComponent;
016: import javax.swing.JPanel;
017: import junit.framework.*;
018: import java.awt.datatransfer.DataFlavor;
019: import java.util.List;
020: import java.util.Set;
021: import org.netbeans.junit.MockServices;
022: import org.netbeans.junit.NbTestCase;
023: import org.openide.filesystems.FileObject;
024: import org.openide.filesystems.FileUtil;
025: import org.openide.util.Lookup;
026:
027: /**
028: *
029: * @author S. Aubrecht
030: */
031: public class DefaultExternalDropHandlerTest extends NbTestCase {
032:
033: public DefaultExternalDropHandlerTest(String testName) {
034: super (testName);
035: }
036:
037: protected void setUp() throws Exception {
038: }
039:
040: protected void tearDown() throws Exception {
041: }
042:
043: public static Test suite() {
044: return new TestSuite(DefaultExternalDropHandlerTest.class);
045: }
046:
047: public void testUriListFlavor() throws ClassNotFoundException {
048: DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
049:
050: DataFlavor uriListFlavor = new DataFlavor(
051: "text/uri-list;class=java.lang.String");
052:
053: assertEquals(uriListFlavor, handler.getUriListDataFlavor());
054: }
055:
056: public void testCanDrop() throws ClassNotFoundException {
057: DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
058:
059: DataFlavor[] flavors = new DataFlavor[] { DataFlavor.imageFlavor };
060: assertFalse(handler.canDrop(flavors));
061:
062: flavors = new DataFlavor[] { DataFlavor.imageFlavor,
063: DataFlavor.stringFlavor };
064: assertFalse(handler.canDrop(flavors));
065:
066: flavors = new DataFlavor[] { DataFlavor.javaFileListFlavor };
067: assertTrue(handler.canDrop(flavors));
068:
069: flavors = new DataFlavor[] { DataFlavor.javaFileListFlavor,
070: DataFlavor.imageFlavor };
071: assertTrue(handler.canDrop(flavors));
072:
073: DataFlavor uriListFlavor = new DataFlavor(
074: "text/uri-list;class=java.lang.String");
075:
076: flavors = new DataFlavor[] { uriListFlavor };
077: assertTrue(handler.canDrop(flavors));
078:
079: flavors = new DataFlavor[] { uriListFlavor,
080: DataFlavor.imageFlavor };
081: assertTrue(handler.canDrop(flavors));
082:
083: flavors = new DataFlavor[] { uriListFlavor,
084: DataFlavor.javaFileListFlavor, DataFlavor.imageFlavor };
085: assertTrue(handler.canDrop(flavors));
086: }
087:
088: public void testGetFileList() throws ClassNotFoundException,
089: URISyntaxException, NoSuchMethodException,
090: InstantiationException, IllegalAccessException,
091: InvocationTargetException {
092: DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
093: DataFlavor uriListFlavor = new DataFlavor(
094: "text/uri-list;class=java.lang.String");
095:
096: Transferable t = new TransferableImpl(new ArrayList(),
097: new DataFlavor[] { DataFlavor.imageFlavor,
098: DataFlavor.stringFlavor });
099: assertNull(handler.getFileList(t));
100:
101: List<File> list = new ArrayList<File>(2);
102: list.add(new File("//somedir/somefile.someext"));
103: list.add(new File("c:\\somedir\\somefile.someext"));
104: t = new TransferableImpl(list,
105: new DataFlavor[] { DataFlavor.javaFileListFlavor,
106: DataFlavor.stringFlavor });
107:
108: List<File> extractedList = handler.getFileList(t);
109: assertNotNull(extractedList);
110: assertEquals(2, extractedList.size());
111: for (Iterator i = list.iterator(); i.hasNext();) {
112: assertTrue(extractedList.contains(i.next()));
113: }
114:
115: String uriList = "file:///somedir/somefile.someext\r\nfile:/c:/somedir/somefile.someext";
116: t = new TransferableImpl(uriList, new DataFlavor[] {
117: uriListFlavor, DataFlavor.stringFlavor });
118: extractedList = handler.getFileList(t);
119: assertNotNull(extractedList);
120: assertEquals(2, extractedList.size());
121: assertTrue(extractedList.contains(new File(new URI(
122: "file:///somedir/somefile.someext"))));
123: assertTrue(extractedList.contains(new File(new URI(
124: "file:/c:/somedir/somefile.someext"))));
125: }
126:
127: public void testOpenFile() throws IOException {
128: DefaultExternalDropHandler handler = new DefaultExternalDropHandler();
129: MockServices.setServices(MockOpenFileImpl.class);
130: MockOpenFileImpl openImpl = Lookup.getDefault().lookup(
131: MockOpenFileImpl.class);
132: assertNotNull("Registered", openImpl);
133:
134: File file = File.createTempFile("somefile", ".someext",
135: getWorkDir());
136: handler.openFile(file);
137: assertTrue(openImpl.wasOpened(file));
138: if (null != file) {
139: file.delete();
140: file.deleteOnExit();
141: }
142: }
143:
144: public static class MockOpenFileImpl implements OpenFileImpl {
145: private Set<File> openedFiles = new HashSet<File>();
146:
147: public boolean open(FileObject fileObject, int line) {
148: if (null != fileObject) {
149: openedFiles.add(FileUtil.toFile(fileObject));
150: return true;
151: }
152: return false;
153: }
154:
155: public boolean wasOpened(File file) {
156: return openedFiles.contains(file);
157: }
158: }
159:
160: private static class TransferableImpl implements Transferable {
161: private Object data;
162: private DataFlavor[] flavors;
163:
164: public TransferableImpl(Object data, DataFlavor[] flavors) {
165: this .data = data;
166: this .flavors = flavors;
167: }
168:
169: public boolean isDataFlavorSupported(DataFlavor flavor) {
170: for (int i = 0; i < flavors.length; i++) {
171: if (flavor.equals(flavors[i]))
172: return true;
173: }
174: return false;
175: }
176:
177: public Object getTransferData(DataFlavor flavor)
178: throws UnsupportedFlavorException, IOException {
179: if (isDataFlavorSupported(flavor)) {
180: return data;
181: }
182: throw new UnsupportedFlavorException(flavor);
183: }
184:
185: public DataFlavor[] getTransferDataFlavors() {
186: return flavors;
187: }
188: }
189:
190: private static class MockDropTarget extends DropTarget {
191: private JComponent component = new JPanel();
192:
193: public MockDropTarget() {
194: }
195:
196: public Component getComponent() {
197: return component;
198: }
199:
200: }
201: }
|