KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DataFolderPasteTypesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
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 JavaDoc dir;
39     private Node folderNode;
40     private LocalFileSystem testFileSystem;
41
42     
43     public DataFolderPasteTypesTest (String JavaDoc name) {
44         super (name);
45     }
46
47     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
60
61         clearWorkDir();
62         Repository.getDefault().removeFileSystem( testFileSystem );
63     }
64
65     public void testNoPasteTypes() throws ClassNotFoundException JavaDoc {
66         DataFlavor JavaDoc flavor = new DataFlavor JavaDoc( "unsupported/flavor;class=java.lang.Object" );
67
68         DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode;
69         ArrayList JavaDoc list = new ArrayList JavaDoc();
70         node.createPasteTypes( new MockTransferable( new DataFlavor JavaDoc[] {flavor}, null ), list );
71         assertEquals( 0, list.size() );
72     }
73
74     public void testJavaFileListPasteTypes() throws ClassNotFoundException JavaDoc, IOException JavaDoc {
75         FileObject testFO = FileUtil.createData( testFileSystem.getRoot(), "testFile.txt" );
76         File JavaDoc testFile = FileUtil.toFile( testFO );
77         ArrayList JavaDoc fileList = new ArrayList JavaDoc(1);
78         fileList.add( testFile );
79         Transferable JavaDoc t = new MockTransferable( new DataFlavor JavaDoc[] {DataFlavor.javaFileListFlavor}, fileList );
80
81         DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode;
82         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc, IOException JavaDoc {
94         DataFlavor JavaDoc flavor = new DataFlavor JavaDoc( "unsupported/flavor;class=java.lang.Object" );
95         FileObject testFO = FileUtil.createData( testFileSystem.getRoot(), "testFile.txt" );
96         File JavaDoc testFile = FileUtil.toFile( testFO );
97         String JavaDoc uriList = testFile.toURI() + "\r\n";
98         Transferable JavaDoc t = new MockTransferable( new DataFlavor JavaDoc[] {new DataFlavor JavaDoc("text/uri-list;class=java.lang.String")}, uriList );
99
100         DataFolder.FolderNode node = (DataFolder.FolderNode)folderNode;
101         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc {
113         private DataFlavor JavaDoc[] flavors;
114         private Object JavaDoc data;
115         public MockTransferable( DataFlavor JavaDoc[] flavors, Object JavaDoc data ) {
116             this.flavors = flavors;
117             this.data = data;
118         }
119
120         public DataFlavor JavaDoc[] getTransferDataFlavors() {
121             return flavors;
122         }
123
124         public boolean isDataFlavorSupported(DataFlavor JavaDoc 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 JavaDoc getTransferData(DataFlavor JavaDoc flavor) throws UnsupportedFlavorException JavaDoc, IOException JavaDoc {
134             if( !isDataFlavorSupported( flavor ) ) {
135                 throw new UnsupportedFlavorException JavaDoc( flavor );
136             }
137             return data;
138         }
139
140     }
141 }
142
Popular Tags