KickJava   Java API By Example, From Geeks To Geeks.

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


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.Button JavaDoc;
23 import java.util.Date JavaDoc;
24 import junit.framework.*;
25 import java.io.*;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import org.openide.*;
31 import org.openide.cookies.*;
32 import org.openide.filesystems.*;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.loaders.*;
35 import org.openide.modules.ModuleInfo;
36 import org.openide.util.*;
37 import org.openide.util.Utilities;
38 import org.openide.util.actions.*;
39 import org.openide.nodes.*;
40 import java.util.List JavaDoc;
41
42 /**
43  *
44  * @author Jaroslav Tulach
45  */

46 public class InstanceDataObjectHasEditorTest extends org.netbeans.junit.NbTestCase {
47     private FileObject fo;
48     
49     
50     public InstanceDataObjectHasEditorTest (String JavaDoc testName) {
51         super (testName);
52     }
53     
54     protected void setUp () throws java.lang.Exception JavaDoc {
55         clearWorkDir ();
56
57         // initialize modules
58
Lookup.getDefault().lookup(ModuleInfo.class);
59     }
60
61     public void testSettingsFileOnSFSShouldHaveEditor () throws Exception JavaDoc {
62         FileObject set = createSettings (Repository.getDefault ().getDefaultFileSystem ().getRoot (), "x.settings");
63         DataObject obj = DataObject.find (set);
64         assertEquals (InstanceDataObject.class, obj.getClass ());
65         assertNull ("It does not have edit cookie", obj.getCookie (EditCookie.class));
66         assertNull ("It does not have open cookie", obj.getCookie (OpenCookie.class));
67         assertNull ("It does not have editor cookie", obj.getCookie (EditorCookie.class));
68         
69         Object JavaDoc o = obj.getNodeDelegate ().getPreferredAction ();
70         Class JavaDoc c = o == null ? Object JavaDoc.class : o.getClass ();
71         
72         if (c == org.openide.actions.OpenAction.class) {
73             fail ("Default actions should not be open on SFS: " + o);
74         }
75     }
76     
77     public void testSettingsFileOnNonSFSShouldHaveEditor () throws Exception JavaDoc {
78         clearWorkDir ();
79         LocalFileSystem lfs = new LocalFileSystem ();
80         lfs.setRootDirectory (getWorkDir ());
81         
82         FileObject set = createSettings (lfs.getRoot (), "x.settings");
83         DataObject obj = DataObject.find (set);
84         assertEquals (InstanceDataObject.class, obj.getClass ());
85         assertNotNull ("It has edit cookie", obj.getCookie (EditCookie.class));
86         assertNotNull ("It has open cookie", obj.getCookie (OpenCookie.class));
87         assertNotNull ("It has editor cookie", obj.getCookie (EditorCookie.class));
88
89         Object JavaDoc o = obj.getNodeDelegate ().getPreferredAction ();
90         Class JavaDoc c = o == null ? Object JavaDoc.class : o.getClass ();
91         
92         assertEquals ("Default actions should be open on non-SFS", org.openide.actions.OpenAction.class, c);
93     }
94     
95     public void testSettingsFileOnNonSFSAfterCopyShouldHaveEditor () throws Exception JavaDoc {
96         clearWorkDir ();
97         LocalFileSystem lfs = new LocalFileSystem ();
98         lfs.setRootDirectory (getWorkDir ());
99         
100         FileObject set = createSettings (lfs.getRoot (), "x.settings");
101         DataObject old = DataObject.find (set);
102         Date JavaDoc d = set.lastModified();
103         
104         /* This code would work only with core/settings, so moving the test there
105         InstanceCookie ic = (InstanceCookie)old.getCookie(InstanceCookie.class);
106         assertNotNull ("The cookie is there", ic);
107         Object instance = ic.instanceCreate();
108         assertNotNull ("It produces a result", instance);
109         assertEquals ("It is Button", Button.class, instance.getClass ());
110          */

111         
112         FileObject tgt = FileUtil.createFolder(lfs.getRoot (), "moved");
113         DataFolder fld = DataFolder.findFolder (tgt);
114         
115         DataObject obj = old.copy (fld);
116         
117         assertEquals ("No change in modifications", d, set.lastModified());
118         assertEquals ("The same name", obj.getPrimaryFile().getNameExt (), set.getNameExt());
119         
120         assertEquals (InstanceDataObject.class, obj.getClass ());
121         assertNotNull ("It has edit cookie", obj.getCookie (EditCookie.class));
122         assertNotNull ("It has open cookie", obj.getCookie (OpenCookie.class));
123         assertNotNull ("It has editor cookie", obj.getCookie (EditorCookie.class));
124
125         Object JavaDoc o = obj.getNodeDelegate ().getPreferredAction ();
126         Class JavaDoc c = o == null ? Object JavaDoc.class : o.getClass ();
127         
128         assertEquals ("Default actions should be open on non-SFS", org.openide.actions.OpenAction.class, c);
129     }
130
131     private FileObject createSettings (FileObject root, String JavaDoc name) throws IOException {
132         FileObject set = FileUtil.createData (root, name);
133
134         FileLock lock = set.lock ();
135         PrintStream os = new PrintStream (set.getOutputStream (lock));
136         
137         os.println ("<?xml version=\"1.0\"?>");
138         os.println ("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">");
139         os.println ("<settings version=\"1.0\">");
140 // os.println ("<module name=\"org.apache.tools.ant.module/3\" spec=\"3.15\"/>");
141
os.println ("<instanceof class=\"java.io.Serializable\"/>");
142         os.println ("<instanceof class=\"java.lang.Object\"/>");
143         os.println ("<instanceof class=\"java.awt.Component\"/>");
144         os.println ("<instance class=\"java.awt.Button\"/>");
145         os.println ("</settings>");
146         
147         os.close ();
148         lock.releaseLock();
149         return set;
150     }
151 }
152
Popular Tags