KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > convertors > InstanceDataObjectCopyTest


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.netbeans.modules.settings.convertors;
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 InstanceDataObjectCopyTest extends org.netbeans.junit.NbTestCase {
47     private FileObject fo;
48     
49     
50     public InstanceDataObjectCopyTest (String JavaDoc testName) {
51         super (testName);
52     }
53     
54     protected void setUp () throws java.lang.Exception JavaDoc {
55         clearWorkDir ();
56         Lookup.getDefault().lookup(ModuleInfo.class);
57         
58     }
59
60     public void testSettingsFileOnNonSFSAfterCopyShouldHaveEditor () throws Exception JavaDoc {
61         doSettingsFileOnNonSFSAfterCopyShouldHaveEditor (true);
62     }
63     public void testSettingsFileOnNonSFSAfterCreateFTShouldHaveEditor () throws Exception JavaDoc {
64         doSettingsFileOnNonSFSAfterCopyShouldHaveEditor (false);
65     }
66         
67     private void doSettingsFileOnNonSFSAfterCopyShouldHaveEditor (boolean copy) throws Exception JavaDoc {
68         clearWorkDir ();
69         LocalFileSystem lfs = new LocalFileSystem ();
70         lfs.setRootDirectory (getWorkDir ());
71         
72         FileObject set = createSettings (lfs.getRoot (), "x.settings");
73         DataObject old = DataObject.find (set);
74         Date JavaDoc d = set.lastModified();
75         
76         InstanceCookie ic = (InstanceCookie)old.getCookie(InstanceCookie.class);
77         assertNotNull ("The cookie is there", ic);
78         Object JavaDoc instance = ic.instanceCreate();
79         assertNotNull ("It produces a result", instance);
80         assertEquals ("It is Button", Button JavaDoc.class, instance.getClass ());
81         
82         FileObject tgt = FileUtil.createFolder(lfs.getRoot (), "moved");
83         DataFolder fld = DataFolder.findFolder (tgt);
84         
85         DataObject obj = copy ? old.copy (fld) : old.createFromTemplate(fld);
86         
87         assertEquals ("No change in modifications", d, set.lastModified());
88         assertEquals ("The same name", obj.getPrimaryFile().getNameExt (), set.getNameExt());
89         
90         assertEquals (InstanceDataObject.class, obj.getClass ());
91         assertNotNull ("It has edit cookie", obj.getCookie (EditCookie.class));
92         assertNotNull ("It has open cookie", obj.getCookie (OpenCookie.class));
93         assertNotNull ("It has editor cookie", obj.getCookie (EditorCookie.class));
94
95         Object JavaDoc o = obj.getNodeDelegate ().getPreferredAction ();
96         Class JavaDoc c = o == null ? Object JavaDoc.class : o.getClass ();
97         
98         assertEquals ("Default actions should be open on non-SFS", org.openide.actions.OpenAction.class, c);
99     }
100
101     private FileObject createSettings (FileObject root, String JavaDoc name) throws IOException {
102         FileObject set = FileUtil.createData (root, name);
103
104         FileLock lock = set.lock ();
105         PrintStream os = new PrintStream (set.getOutputStream (lock));
106         
107         os.println ("<?xml version=\"1.0\"?>");
108         os.println ("<!DOCTYPE settings PUBLIC \"-//NetBeans//DTD Session settings 1.0//EN\" \"http://www.netbeans.org/dtds/sessionsettings-1_0.dtd\">");
109         os.println ("<settings version=\"1.0\">");
110 // os.println ("<module name=\"org.apache.tools.ant.module/3\" spec=\"3.15\"/>");
111
os.println ("<instanceof class=\"java.io.Serializable\"/>");
112         os.println ("<instanceof class=\"java.lang.Object\"/>");
113         os.println ("<instanceof class=\"java.awt.Component\"/>");
114         os.println ("<instance class=\"java.awt.Button\"/>");
115         os.println ("</settings>");
116         
117         os.close ();
118         lock.releaseLock();
119         return set;
120     }
121 }
122
Popular Tags