1 19 20 package org.netbeans.modules.db.sql.loader; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.ObjectOutputStream ; 25 import java.util.Enumeration ; 26 import javax.swing.text.Document ; 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 55 public class SQLEditorSupportConsoleTest extends NbTestCase { 56 57 private FileObject fileObject; 58 private DataObject dataObject; 59 private MySQLEditorSupport support; 60 61 public SQLEditorSupportConsoleTest(String testName) { 62 super(testName); 63 } 64 65 public void setUp() throws Exception { 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 { 78 fileObject.getParent().delete(); 79 } 80 81 public boolean runInEQ() { 82 return true; 83 } 84 85 public void testEditorNameIsDataObjectName() throws Exception { 86 assertEquals(fileObject.getName(), dataObject.getName()); 88 89 support.open(); 90 assertEquals(dataObject.getName(), support.messageName()); 91 92 Document 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 { 100 support.open(); 101 Document 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 { 113 support.open(); 114 Document 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 (new ByteArrayOutputStream ())); 124 assertTrue(support.saveDocumentCalled); 125 126 support.close(); 127 } 128 129 133 public static final class Pool extends DataLoaderPool { 134 135 public Enumeration loaders() { 136 return Enumerations.singleton(new MySQLDataLoader()); 137 } 138 } 139 140 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 { 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 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 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 { 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 |