1 19 20 package org.netbeans.modules.xml.multiview.test; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.FileInputStream ; 26 import java.io.OutputStream ; 27 28 import junit.textui.TestRunner; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.junit.AssertionFailedErrorException; 32 33 import org.openide.cookies.EditorCookie; 34 import org.openide.cookies.EditCookie; 35 import org.openide.cookies.SaveCookie; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.loaders.*; 39 40 import org.netbeans.modules.xml.multiview.test.util.Helper; 41 import org.netbeans.modules.xml.multiview.test.bookmodel.*; 42 import org.netbeans.modules.xml.multiview.XmlMultiViewEditorSupport; 43 44 import javax.swing.*; 45 import javax.swing.text.Document ; 46 import javax.swing.text.BadLocationException ; 47 48 52 public class XmlMultiViewEditorTest extends NbTestCase { 53 private DataLoader loader; 54 private BookDataObject bookDO; 55 56 public XmlMultiViewEditorTest(String testName) { 57 super(testName); 58 } 59 60 protected void setUp() throws Exception { 61 DataLoaderPool pool = DataLoaderPool.getDefault(); 62 assertNotNull (pool); 63 loader = DataLoader.getLoader(BookDataLoader.class); 64 } 65 66 67 70 public void testBookDataObject() throws IOException { 71 initDataObject(); 72 } 73 74 public void testChangeModel() throws IOException { 75 initDataObject(); 76 try { 77 Book book = bookDO.getBook(); 78 book.setAttributeValue("chapter", 0, "length", "110"); 79 bookDO.modelUpdatedFromUI(); 80 } catch (Exception ex) { 81 throw new AssertionFailedErrorException("Failed to change book model",ex); 82 } 83 SaveCookie cookie = Helper.getSaveCookie(bookDO); 85 assertNotNull("Data Object Not Modified", cookie); 86 cookie.save(); 87 88 File original = Helper.getBookFile(getDataDir()); 90 assertTrue("File doesn't contain the text : <chapter length=\"110\">", 91 Helper.isTextInFile("<chapter length=\"110\">",original)); 92 } 93 94 public void testChangeModelInDesignView() throws IOException { 95 initDataObject(); 96 try { 97 bookDO.showElement(bookDO.getBook().getChapter()[1]); 98 } catch (Exception ex) { 99 throw new AssertionFailedErrorException("Failed to open Chapter section", ex); 100 } 101 Helper.waitForDispatchThread(); 102 try { 103 JTextField titleTF = Helper.getChapterTitleTF(bookDO, bookDO.getBook().getChapter()[1]); 104 titleTF.requestFocus(); 105 Helper.waitForDispatchThread(); 106 javax.swing.text.Document doc = titleTF.getDocument(); 107 doc.remove(0, doc.getLength()); 108 doc.insertString(0, "The garden full of beans", null); 109 } catch (Exception ex) { 110 throw new AssertionFailedErrorException("Failed to set the title for Chapter: ", ex); 111 } 112 ((EditCookie) bookDO.getCookie(EditCookie.class)).edit(); 114 Helper.waitForDispatchThread(); 116 117 SaveCookie cookie = Helper.getSaveCookie(bookDO); 119 assertNotNull("Data Object Not Modified", cookie); 120 cookie.save(); 121 122 File original = Helper.getBookFile(getDataDir()); 124 assertTrue("File doesn't contain the text : <title lang=\"en\">The garden full of beans</title>", 125 Helper.isTextInFile("<title lang=\"en\">The garden full of beans</title>", original)); 126 } 127 128 public void testExternalChange() throws IOException { 129 initDataObject(); 130 String golden = "ChangedChapterTitle.pass"; 131 FileObject fo = bookDO.getPrimaryFile(); 132 InputStream is = new FileInputStream (getGoldenFile(golden)); 133 try { 134 org.openide.filesystems.FileLock lock = fo.lock(); 135 OutputStream os = fo.getOutputStream(lock); 136 try { 137 138 int b; 139 while ((b = is.read()) != -1) { 140 char ch = (char) b; 141 if (ch == '2') { 142 os.write(b); 143 } 144 os.write(b); 145 } 146 } 147 finally { 148 os.close(); 149 is.close(); 150 lock.releaseLock(); 151 } 152 } catch (org.openide.filesystems.FileAlreadyLockedException ex) { 153 throw new AssertionFailedErrorException("Lock problem : ", ex); 154 } 155 156 Helper.sleep(2000); 158 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) bookDO.getCookie(EditorCookie.class); 159 Document doc = editor.getDocument(); 160 try { 161 assertTrue("XML document doesn't contain the external changes: ", 162 doc.getText(0, doc.getLength()).indexOf("<chapter length=\"122\">") > 0); 163 } catch (BadLocationException ex) { 164 throw new AssertionFailedErrorException(ex); 165 } 166 } 167 168 private void doSetPreferredLoader (FileObject fo, DataLoader loader) throws IOException { 169 DataLoaderPool.setPreferredLoader (fo, loader); 170 } 171 172 private void initDataObject() throws IOException { 173 if (bookDO == null) { 174 File f = Helper.getBookFile(getDataDir()); 175 FileObject fo = FileUtil.toFileObject(f); 176 assertNotNull(fo); 177 178 doSetPreferredLoader(fo, loader); 179 DataObject dObj = DataObject.find(fo); 180 assertNotNull("Book DataObject not found", dObj); 181 assertEquals(BookDataObject.class, dObj.getClass()); 182 183 bookDO = (BookDataObject) dObj; 184 ((EditCookie) bookDO.getCookie(EditCookie.class)).edit(); 185 186 Helper.waitForDispatchThread(); 188 189 XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) bookDO.getCookie(EditorCookie.class); 190 Document doc = Helper.getDocument(editor); 191 assertTrue("The document is empty :", doc == null || doc.getLength() > 0); 192 } 193 } 194 195 200 public static void main(String [] args) { 201 TestRunner.run(new NbTestSuite(XmlMultiViewEditorTest.class)); 202 } 203 } | Popular Tags |