KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > test > XmlMultiViewEditorTest


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.xml.multiview.test;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
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 JavaDoc;
46 import javax.swing.text.BadLocationException JavaDoc;
47
48 /**
49  *
50  * @author Milan Kuchtiak
51  */

52 public class XmlMultiViewEditorTest extends NbTestCase {
53     private DataLoader loader;
54     private BookDataObject bookDO;
55
56     public XmlMultiViewEditorTest(String JavaDoc testName) {
57         super(testName);
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61         DataLoaderPool pool = DataLoaderPool.getDefault();
62         assertNotNull (pool);
63         loader = DataLoader.getLoader(BookDataLoader.class);
64     }
65
66
67     /** Tet if sample.book was correctly recognized by BookDataLoader and
68      * if sample.book was open in editor (XML view)
69      */

70     public void testBookDataObject() throws IOException JavaDoc {
71         initDataObject();
72     }
73
74     public void testChangeModel() throws IOException JavaDoc {
75         initDataObject();
76         try {
77             Book book = bookDO.getBook();
78             book.setAttributeValue("chapter", 0, "length", "110");
79             bookDO.modelUpdatedFromUI();
80         } catch (Exception JavaDoc ex) {
81             throw new AssertionFailedErrorException("Failed to change book model",ex);
82         }
83         // test if data object was modified
84
SaveCookie cookie = Helper.getSaveCookie(bookDO);
85         assertNotNull("Data Object Not Modified", cookie);
86         cookie.save();
87
88         // test to golden file
89
File JavaDoc 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 JavaDoc {
95         initDataObject();
96         try {
97             bookDO.showElement(bookDO.getBook().getChapter()[1]);
98         } catch (Exception JavaDoc 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 JavaDoc doc = titleTF.getDocument();
107             doc.remove(0, doc.getLength());
108             doc.insertString(0, "The garden full of beans", null);
109         } catch (Exception JavaDoc ex) {
110             throw new AssertionFailedErrorException("Failed to set the title for Chapter: ", ex);
111         }
112         // open XML View
113
((EditCookie) bookDO.getCookie(EditCookie.class)).edit();
114         // handle consequent calls of SwingUtilities.invokeLater();
115
Helper.waitForDispatchThread();
116
117         // test if data object was modified
118
SaveCookie cookie = Helper.getSaveCookie(bookDO);
119         assertNotNull("Data Object Not Modified", cookie);
120         cookie.save();
121
122         // test to golden file
123
File JavaDoc 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 JavaDoc {
129         initDataObject();
130         String JavaDoc golden = "ChangedChapterTitle.pass";
131         FileObject fo = bookDO.getPrimaryFile();
132         InputStream JavaDoc is = new FileInputStream JavaDoc(getGoldenFile(golden));
133         try {
134             org.openide.filesystems.FileLock lock = fo.lock();
135             OutputStream JavaDoc 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); // wait for external change update
157

158         XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) bookDO.getCookie(EditorCookie.class);
159         Document JavaDoc 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 JavaDoc ex) {
164             throw new AssertionFailedErrorException(ex);
165         }
166     }
167
168     private void doSetPreferredLoader (FileObject fo, DataLoader loader) throws IOException JavaDoc {
169         DataLoaderPool.setPreferredLoader (fo, loader);
170     }
171
172     private void initDataObject() throws IOException JavaDoc {
173         if (bookDO == null) {
174             File JavaDoc 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             // wait to open the document
187
Helper.waitForDispatchThread();
188
189             XmlMultiViewEditorSupport editor = (XmlMultiViewEditorSupport) bookDO.getCookie(EditorCookie.class);
190             Document JavaDoc doc = Helper.getDocument(editor);
191             assertTrue("The document is empty :", doc == null || doc.getLength() > 0);
192         }
193     }
194
195     /**
196      * Used for running test from inside the IDE by internal execution.
197      *
198      * @param args the command line arguments
199      */

200     public static void main(String JavaDoc[] args) {
201         TestRunner.run(new NbTestSuite(XmlMultiViewEditorTest.class));
202     }
203 }
Popular Tags