KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > loader > SQLEditorSupportNormalTest


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.db.sql.loader;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.junit.MockServices;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.cookies.OpenCookie;
30 import org.openide.cookies.SaveCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.loaders.DataLoaderPool;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataObjectExistsException;
36 import org.openide.loaders.ExtensionList;
37 import org.openide.loaders.MultiDataObject;
38 import org.openide.loaders.UniFileLoader;
39 import org.openide.nodes.CookieSet;
40 import org.openide.nodes.Node;
41 import org.openide.text.CloneableEditor;
42 import org.openide.util.Enumerations;
43 import org.openide.util.Lookup;
44 import org.openide.windows.CloneableTopComponent;
45
46 /**
47  * Tests the functionality of SQLEditorSupport when serving for a normal DataObject.
48  *
49  * @author Andrei Badea
50  */

51 public class SQLEditorSupportNormalTest extends NbTestCase {
52     
53     private FileObject fileObject;
54     private DataObject dataObject;
55     private MySQLEditorSupport support;
56     
57     public SQLEditorSupportNormalTest(String JavaDoc testName) {
58         super(testName);
59     }
60     
61     public void setUp() throws Exception JavaDoc {
62         FileObject folder = FileUtil.toFileObject(getWorkDir()).createFolder("folder");
63         fileObject = folder.createData("SQLFile", "sql");
64         
65         MockServices.setServices(Pool.class);
66         assertEquals(Pool.class, Lookup.getDefault().lookup(DataLoaderPool.class).getClass());
67         
68         dataObject = DataObject.find(fileObject);
69         support = (MySQLEditorSupport)dataObject.getCookie(OpenCookie.class);
70     }
71     
72     public void tearDown() throws Exception JavaDoc {
73         fileObject.getParent().delete();
74     }
75     
76     public boolean runInEQ() {
77         return true;
78     }
79     
80     public void testEditorNameIsNodeDisplayName() throws Exception JavaDoc {
81         support.open();
82         assertEquals(dataObject.getNodeDelegate().getDisplayName(), support.messageName());
83         
84         Document JavaDoc doc = support.openDocument();
85         doc.insertString(0, "test", null);
86         
87         assertTrue(support.messageName().indexOf(dataObject.getNodeDelegate().getDisplayName()) >= 0);
88         support.close();
89     }
90     
91     public void testDataObjectIsModifiedWhenDocumentChanged() throws Exception JavaDoc {
92         support.open();
93         
94         Document JavaDoc doc = support.openDocument();
95         doc.insertString(0, "test", null);
96         
97         assertTrue(support.isModified());
98         assertTrue(dataObject.isModified());
99         assertNotNull(dataObject.getCookie(SaveCookie.class));
100         // XXX should test canClose() here, but how, since canClose() displays a dialog box?
101

102         support.close();
103     }
104     
105     public void testDocumentIsNotSavedOnComponentDeactivatedOrWriteExternal() throws Exception JavaDoc {
106         support.open();
107         Document JavaDoc doc = support.openDocument();
108         doc.insertString(0, "test", null);
109
110         MySQLEditorSupport.MySQLCloneableEditor editor = (MySQLEditorSupport.MySQLCloneableEditor)support.getAllEditors().getComponents().nextElement();
111         editor.callComponentDeactivated();
112         assertFalse(support.saveDocumentCalled);
113
114         doc.insertString(0, "test", null);
115         editor.writeExternal(new ObjectOutputStream JavaDoc(new ByteArrayOutputStream JavaDoc()));
116         assertFalse(support.saveDocumentCalled);
117         
118         support.close();
119     }
120     
121     /**
122      * DataLoaderPool which is registered in the default lookup and loads
123      * MySQLDataLoader.
124      */

125     public static final class Pool extends DataLoaderPool {
126         
127         public Enumeration JavaDoc loaders() {
128             return Enumerations.singleton(new MySQLDataLoader());
129         }
130     }
131     
132     /**
133      * DataLoader for SQL files. Not using SQLDataLoader because we want
134      * the loader to return our special MySQLDataObject's.
135      */

136     private static final class MySQLDataLoader extends UniFileLoader {
137     
138         public MySQLDataLoader() {
139             super("org.netbeans.modules.db.sql.loader.MySQLDataLoader");
140         }
141     
142         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
143             return new MySQLDataObject(primaryFile, this);
144         }
145
146         protected void initialize() {
147             super.initialize();
148             ExtensionList extensions = new ExtensionList();
149             extensions.addExtension("sql");
150             setExtensions(extensions);
151         }
152     }
153     
154     /**
155      * SQLDataObject which has MySQLEditorSupport in its cookie set instead
156      * of the cookie added by SQLDataObject.
157      */

158     private static final class MySQLDataObject extends SQLDataObject {
159         
160         public MySQLDataObject(FileObject primaryFile, UniFileLoader loader) throws DataObjectExistsException {
161             super(primaryFile, loader);
162             CookieSet cookies = getCookieSet();
163             cookies.remove(cookies.getCookie(OpenCookie.class));
164             cookies.add(new MySQLEditorSupport(this));
165         }
166
167         protected Node createNodeDelegate() {
168             return new SQLNode(this);
169         }
170     }
171     
172     /**
173      * SQLEditorSupport which allows finding out whether the saveDocument() method was called
174      * and calling the componentDeactivated() method.
175      */

176     private static final class MySQLEditorSupport extends SQLEditorSupport {
177         
178         boolean saveDocumentCalled = false;
179         
180         public MySQLEditorSupport(SQLDataObject obj) {
181             super(obj);
182         }
183         
184         /**
185          * In order to silently close a modified support.
186          */

187         public boolean canClose() {
188             return true;
189         }
190         
191         public void saveDocument() throws IOException JavaDoc {
192             super.saveDocument();
193             saveDocumentCalled = true;
194         }
195         
196         public CloneableTopComponent.Ref getAllEditors() {
197             return allEditors;
198         }
199         
200         public CloneableEditor createCloneableEditor() {
201             return new MySQLCloneableEditor(this);
202         }
203         
204         private static final class MySQLCloneableEditor extends SQLCloneableEditor {
205             
206             public MySQLCloneableEditor(MySQLEditorSupport support) {
207                 super(support);
208             }
209             
210             public void callComponentDeactivated() {
211                 componentDeactivated();
212             }
213         }
214     }
215 }
216
Popular Tags