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.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 51 public class SQLEditorSupportNormalTest extends NbTestCase { 52 53 private FileObject fileObject; 54 private DataObject dataObject; 55 private MySQLEditorSupport support; 56 57 public SQLEditorSupportNormalTest(String testName) { 58 super(testName); 59 } 60 61 public void setUp() throws Exception { 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 { 73 fileObject.getParent().delete(); 74 } 75 76 public boolean runInEQ() { 77 return true; 78 } 79 80 public void testEditorNameIsNodeDisplayName() throws Exception { 81 support.open(); 82 assertEquals(dataObject.getNodeDelegate().getDisplayName(), support.messageName()); 83 84 Document 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 { 92 support.open(); 93 94 Document 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 102 support.close(); 103 } 104 105 public void testDocumentIsNotSavedOnComponentDeactivatedOrWriteExternal() throws Exception { 106 support.open(); 107 Document 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 (new ByteArrayOutputStream ())); 116 assertFalse(support.saveDocumentCalled); 117 118 support.close(); 119 } 120 121 125 public static final class Pool extends DataLoaderPool { 126 127 public Enumeration loaders() { 128 return Enumerations.singleton(new MySQLDataLoader()); 129 } 130 } 131 132 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 { 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 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 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 187 public boolean canClose() { 188 return true; 189 } 190 191 public void saveDocument() throws IOException { 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 |