KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Sun Public License Notice
3  *
4  * The contents of this file are subject to the Sun Public License
5  * Version 1.0 (the "License"). You may not use this file except in
6  * compliance with the License. A copy of the License is available at
7  * http://www.sun.com/
8  *
9  * The Original Code is NetBeans. The Initial Developer of the Original
10  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
11  * Microsystems, Inc. All Rights Reserved.
12  */

13
14 package org.openide.loaders;
15
16 import java.io.IOException JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.logging.Level JavaDoc;
19 import java.util.logging.Logger JavaDoc;
20 import org.netbeans.junit.MockServices;
21 import org.netbeans.junit.NbTestCase;
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileSystem;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.filesystems.Repository;
26 import org.openide.nodes.Children;
27 import org.openide.nodes.Node;
28 import org.openide.util.Enumerations;
29 import org.openide.util.lookup.Lookups;
30
31 /** Test things about shadows and broken shadows, etc.
32  * @author Jaroslav Tulach
33  */

34 public class DataShadowLookupTest extends NbTestCase
35 implements java.net.URLStreamHandlerFactory JavaDoc {
36     /** original object */
37     private DataObject original;
38     /** folder to work with */
39     private DataFolder folder;
40     /** fs we work on */
41     private FileSystem lfs;
42
43     private Logger JavaDoc err;
44     
45     static {
46         // to handle nbfs urls...
47
// java.net.URL.setURLStreamHandlerFactory (new DataShadowLookupTest(null));
48
MockServices.setServices(new Class JavaDoc[] { Pool.class });
49     }
50     
51     public DataShadowLookupTest (String JavaDoc name) {
52         super(name);
53     }
54
55     protected Level JavaDoc logLevel() {
56         return Level.INFO;
57     }
58     
59     protected void setUp() throws Exception JavaDoc {
60         
61         lfs = Repository.getDefault ().getDefaultFileSystem ();
62         
63         FileObject[] delete = lfs.getRoot().getChildren();
64         for (int i = 0; i < delete.length; i++) {
65             delete[i].delete();
66         }
67
68         
69         FileObject fo = FileUtil.createData (lfs.getRoot (), getName () + "/folder/original.string");
70         assertNotNull(fo);
71         original = DataObject.find (fo);
72         assertFalse ("Just to be sure that this is not shadow", original instanceof DataShadow);
73         assertEquals ("It is the right class", StringObject.class, original.getClass ());
74         fo = FileUtil.createFolder (lfs.getRoot (), getName () + "/modify");
75         assertNotNull(fo);
76         assertTrue (fo.isFolder ());
77         folder = DataFolder.findFolder (fo);
78         
79         Repository.getDefault ().addFileSystem (lfs);
80         
81         err = Logger.getLogger(getName());
82     }
83     
84     public java.net.URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
85         if (protocol.equals ("nbfs")) {
86             return FileUtil.nbfsURLStreamHandler ();
87         }
88         return null;
89     }
90     
91     public void testStringIsInLookupOfDataShadow() throws Exception JavaDoc {
92         DataShadow shade = original.createShadow(folder);
93
94         {
95             String JavaDoc s = (String JavaDoc)original.getNodeDelegate().getLookup().lookup(String JavaDoc.class);
96             assertNotNull("String is in the original's lookup", s);
97         }
98         
99         assertSame(shade.getOriginal(), original);
100         String JavaDoc s = (String JavaDoc)shade.getNodeDelegate().getLookup().lookup(String JavaDoc.class);
101         assertNotNull("String is in the lookup", s);
102         assertEquals("It is the name of the original", original.getName(), s);
103     }
104
105     public static final class Pool extends DataLoaderPool {
106         protected Enumeration JavaDoc loaders() {
107             return Enumerations.singleton(StringLoader.findObject(StringLoader.class, true));
108         }
109         
110     }
111     
112     private static final class StringLoader extends UniFileLoader {
113         public StringLoader() {
114             super("org.openide.loaders.StringObject");
115             getExtensions().addExtension("string");
116         }
117         
118         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
119             return new StringObject(this, primaryFile);
120         }
121         
122     } // end of StringLoader
123

124     private static final class StringObject extends MultiDataObject {
125         public StringObject(StringLoader l, FileObject fo) throws DataObjectExistsException {
126             super(fo, l);
127         }
128
129         protected Node createNodeDelegate() {
130             return new DataNode(this, Children.LEAF, Lookups.singleton(getName()));
131         }
132     } // end of StringObject
133

134     
135 }
136
Popular Tags