KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > SimpleDESTest


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.text;
21
22 import java.io.PrintStream JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import junit.textui.TestRunner;
25 import org.netbeans.junit.*;
26 import org.openide.DialogDescriptor;
27 import org.openide.cookies.EditorCookie;
28 import org.openide.cookies.OpenCookie;
29 import org.openide.filesystems.*;
30 import org.openide.loaders.DataObject;
31 import org.openide.util.actions.SystemAction;
32
33 /** DefaultDataObject is supposed to have open operation that shows the text
34  * editor or invokes a dialog with questions.
35  *
36  * @author Jaroslav Tulach
37  */

38 public final class SimpleDESTest extends NbTestCase {
39     
40     private FileSystem lfs;
41     private DataObject obj;
42     
43     /** Creates a new instance of DefaultSettingsContextTest */
44     public SimpleDESTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     protected void setUp() throws java.lang.Exception JavaDoc {
49         clearWorkDir ();
50         
51         System.setProperty("org.openide.util.Lookup", "org.openide.text.SimpleDESTest$Lkp");
52         super.setUp();
53         
54         LocalFileSystem l = new LocalFileSystem ();
55         l.setRootDirectory (getWorkDir ());
56         lfs = l;
57         
58         FileObject fo = FileUtil.createData (lfs.getRoot (), "AA/" + getName () + ".test");
59         assertNotNull("file not found", fo);
60         obj = DataObject.find(fo);
61         
62         assertEquals ("The right class", obj.getClass (), SO.class);
63     }
64     
65     public void testHasEditorCookieForResonableContentOfFiles () throws Exception JavaDoc {
66         doCookieCheck (true);
67     }
68     
69     private void doCookieCheck (boolean hasEditCookie) throws Exception JavaDoc {
70         EditorCookie c = tryToOpen (
71             "Ahoj Jardo," +
72             "how are you" +
73             "\t\n\rBye"
74         );
75         assertNotNull (c);
76         
77         assertEquals (
78             "Next questions results in the same cookie",
79             c,
80             obj.getCookie(EditorCookie.class)
81         );
82         assertEquals (
83             "Print cookie is provided",
84             c,
85             obj.getCookie(org.openide.cookies.PrintCookie.class)
86         );
87         assertEquals (
88             "CloseCookie as well",
89             c,
90             obj.getCookie(org.openide.cookies.CloseCookie.class)
91         );
92         
93         if (hasEditCookie) {
94             assertEquals (
95                 "EditCookie as well",
96                 c,
97                 obj.getCookie(org.openide.cookies.EditCookie.class)
98             );
99         } else {
100             assertNull (
101                 "No EditCookie",
102                 obj.getCookie(org.openide.cookies.EditCookie.class)
103             );
104             
105         }
106         
107         OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class);
108         open.open ();
109         
110         javax.swing.text.Document JavaDoc d = c.getDocument();
111         assertNotNull (d);
112         
113         d.insertString(0, "Kuk", null);
114         
115         assertNotNull (
116             "Now there is a save cookie",
117             obj.getCookie (org.openide.cookies.SaveCookie.class)
118         );
119     }
120     
121     public void testItIsPossibleToMaskEditCookie () throws Exception JavaDoc {
122         doCookieCheck (false);
123     }
124     
125     private EditorCookie tryToOpen (String JavaDoc content) throws Exception JavaDoc {
126         FileObject fo = obj.getPrimaryFile();
127         FileLock lock = fo.lock();
128         PrintStream JavaDoc os = new PrintStream JavaDoc (fo.getOutputStream(lock));
129         os.print (content);
130         os.close ();
131         lock.releaseLock();
132         
133         return (EditorCookie)obj.getCookie (EditorCookie.class);
134     }
135     
136     //
137
// Our fake lookup
138
//
139
public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
140         public Lkp () {
141             this (new org.openide.util.lookup.InstanceContent ());
142         }
143         
144         private Lkp (org.openide.util.lookup.InstanceContent ic) {
145             super (ic);
146             ic.add (new DLP ());
147         }
148     }
149     
150     private static final class SL extends org.openide.loaders.UniFileLoader {
151         public SL () {
152             super (SO.class.getName ());
153             getExtensions().addExtension("test");
154         }
155         protected org.openide.loaders.MultiDataObject createMultiObject(FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, java.io.IOException JavaDoc {
156             return new SO (primaryFile);
157         }
158     } // end of SL
159

160     private static final class SO extends org.openide.loaders.MultiDataObject implements org.openide.nodes.CookieSet.Factory {
161         private org.openide.nodes.Node.Cookie cookie = (org.openide.nodes.Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ());
162         
163         
164         public SO (FileObject fo) throws org.openide.loaders.DataObjectExistsException {
165             super (fo, (SL)SL.getLoader(SL.class));
166             
167             if (fo.getNameExt().indexOf ("MaskEdit") == -1) {
168                 getCookieSet ().add (cookie);
169             } else {
170                 getCookieSet ().add (new Class JavaDoc[] {
171                     OpenCookie.class,
172                     org.openide.cookies.CloseCookie.class, EditorCookie.class,
173                     org.openide.cookies.PrintCookie.class
174                 }, this);
175             }
176         }
177         
178         
179         public org.openide.nodes.Node.Cookie createCookie (Class JavaDoc c) {
180             return cookie;
181         }
182     } // end of SO
183

184     private static final class DLP extends org.openide.loaders.DataLoaderPool {
185         protected java.util.Enumeration JavaDoc loaders() {
186             return java.util.Collections.enumeration(
187                 java.util.Collections.singleton(
188                     SL.getLoader (SL.class)
189                 )
190             );
191         }
192     } // end of DataLoaderPool
193
}
194
Popular Tags