KickJava   Java API By Example, From Geeks To Geeks.

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


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.Repository;
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 as an SQL console.
48  *
49  * Whenever a test is added here asserting some functionality that should present
50  * ONLY serving as a SQL console, a test asserting that functionality is NOT present
51  * when serving for a normal DataObject should be added to SQLEditorSupportPlainTest.
52  *
53  * @author Andrei Badea
54  */

55 public class SQLEditorSupportConsoleTest extends NbTestCase {
56     
57     private FileObject fileObject;
58     private DataObject dataObject;
59     private MySQLEditorSupport support;
60     
61     public SQLEditorSupportConsoleTest(String JavaDoc testName) {
62         super(testName);
63     }
64     
65     public void setUp() throws Exception JavaDoc {
66         FileObject folder = Repository.getDefault().getDefaultFileSystem().getRoot().createFolder("folder");
67         fileObject = folder.createData("SQL Command", "sql");
68         assertEquals("nbfs", fileObject.getURL().getProtocol());
69         
70         MockServices.setServices(Pool.class);
71         assertEquals(Pool.class, Lookup.getDefault().lookup(DataLoaderPool.class).getClass());
72         
73         dataObject = DataObject.find(fileObject);
74         support = (MySQLEditorSupport)dataObject.getCookie(OpenCookie.class);
75     }
76     
77     public void tearDown() throws Exception JavaDoc {
78         fileObject.getParent().delete();
79     }
80     
81     public boolean runInEQ() {
82         return true;
83     }
84     
85     public void testEditorNameIsDataObjectName() throws Exception JavaDoc {
86         // assert data object name is file object's name
87
assertEquals(fileObject.getName(), dataObject.getName());
88         
89         support.open();
90         assertEquals(dataObject.getName(), support.messageName());
91         
92         Document JavaDoc doc = support.openDocument();
93         doc.insertString(0, "test", null);
94         
95         assertEquals(dataObject.getName(), support.messageName());
96         support.close();
97     }
98     
99     public void testDataObjectNotModifiedWhenDocumentChanged() throws Exception JavaDoc {
100         support.open();
101         Document JavaDoc doc = support.openDocument();
102         doc.insertString(0, "test", null);
103         
104         assertTrue(support.isModified());
105         assertFalse(dataObject.isModified());
106         assertNull(dataObject.getCookie(SaveCookie.class));
107         assertTrue(support.canClose());
108         
109         support.close();
110     }
111     
112     public void testDocumentIsSaved() throws Exception JavaDoc {
113         support.open();
114         Document JavaDoc doc = support.openDocument();
115         doc.insertString(0, "test", null);
116
117         MySQLEditorSupport.MySQLCloneableEditor editor = (MySQLEditorSupport.MySQLCloneableEditor)support.getAllEditors().getComponents().nextElement();
118         editor.callComponentDeactivated();
119         assertTrue(support.saveDocumentCalled);
120
121         support.saveDocumentCalled = false;
122         doc.insertString(0, "test", null);
123         editor.writeExternal(new ObjectOutputStream JavaDoc(new ByteArrayOutputStream JavaDoc()));
124         assertTrue(support.saveDocumentCalled);
125
126         support.close();
127     }
128     
129     /**
130      * DataLoaderPool which is registered in the default lookup and loads
131      * MySQLDataLoader.
132      */

133     public static final class Pool extends DataLoaderPool {
134         
135         public Enumeration JavaDoc loaders() {
136             return Enumerations.singleton(new MySQLDataLoader());
137         }
138     }
139     
140     /**
141      * DataLoader for SQL files. Not using SQLDataLoader because we want
142      * the loader to return our special MySQLDataObject's.
143      */

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

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

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