KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > undo > QuietUndoManagerTest


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.schema.core.undo;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.text.AbstractDocument JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import junit.framework.TestCase;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.editor.BaseDocument;
28 import org.netbeans.modules.xml.retriever.catalog.Utilities;
29 import org.netbeans.modules.xml.schema.core.SchemaDataObject;
30 import org.netbeans.modules.xml.schema.core.SchemaEditorSupport;
31 import org.netbeans.modules.xml.schema.core.Util;
32 import org.netbeans.modules.xml.schema.model.GlobalElement;
33 import org.netbeans.modules.xml.schema.model.Schema;
34 import org.netbeans.modules.xml.schema.model.SchemaModel;
35 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
36 import org.netbeans.modules.xml.xam.ModelSource;
37 import org.netbeans.modules.xml.xam.ui.undo.QuietUndoManager;
38 import org.openide.cookies.SaveCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41
42 /**
43  * Tests the QuietUndoManager with the full data object/loader framework,
44  * the purpose of which is to test using the CloneableEditorSupport to
45  * catch errors that occur only when the editor support is in place.
46  *
47  * @author Nathan Fiedler
48  */

49 public class QuietUndoManagerTest extends TestCase {
50     public static final String JavaDoc XSD_PO = "PurchaseOrder.xsd";
51     private Project project;
52     private SchemaModel model;
53     private FileObject purchaseOrderFO;
54     private AbstractDocument JavaDoc document;
55     private QuietUndoManager manager;
56
57     public QuietUndoManagerTest(String JavaDoc testName) {
58         super(testName);
59     }
60
61     @Override JavaDoc
62     protected void setUp() throws Exception JavaDoc {
63         project = Util.createJavaTestProject();
64         purchaseOrderFO = Util.populateProject(
65                 project, "resources/" + XSD_PO, "src/com/acme/schemas");
66         ModelSource ms = Utilities.getModelSource(purchaseOrderFO, true);
67         model = SchemaModelFactory.getDefault().getModel(ms);
68     }
69
70     @Override JavaDoc
71     protected void tearDown() throws Exception JavaDoc {
72         deleteModelFile(purchaseOrderFO);
73     }
74     
75     private static void deleteModelFile(FileObject fo) throws Exception JavaDoc {
76         if (fo == null) {
77             return;
78         }
79         SchemaDataObject sdo = (SchemaDataObject) DataObject.find(fo);
80         if (sdo != null) {
81             SaveCookie save = (SaveCookie) sdo.getCookie(SaveCookie.class);
82             if (save != null) {
83                 save.save();
84             }
85         }
86         fo.delete();
87     }
88
89     /**
90      * Ends the compound mode of the undo manager and discards all edits.
91      */

92     private void endTestCase() {
93         manager.endCompound();
94         document.removeUndoableEditListener(manager);
95         if (model != null) {
96             model.removeUndoableEditListener(manager);
97         }
98         manager.discardAllEdits();
99     }
100
101     /**
102      * Simulate switching to the model view.
103      */

104     private void viewModel() {
105         manager.endCompound();
106         document.removeUndoableEditListener(manager);
107         if (model != null) {
108             try {
109                 // Sync any changes made to the document.
110
model.sync();
111             } catch (IOException JavaDoc ioe) {
112                 fail(ioe.toString());
113             }
114             // Ensure manager is not registered twice.
115
model.removeUndoableEditListener(manager);
116             model.addUndoableEditListener(manager);
117         }
118     }
119
120     /**
121      * Simulate switching to the source view.
122      */

123     private void viewSource() {
124         if (model != null) {
125             model.removeUndoableEditListener(manager);
126         }
127         // Ensure manager is not registered twice.
128
document.removeUndoableEditListener(manager);
129         document.addUndoableEditListener(manager);
130         manager.beginCompound();
131     }
132
133     /**
134      * Try to reproduce issue 83963.
135      */

136     public void testIssue83963() {
137         try {
138             SchemaDataObject sdo = (SchemaDataObject) DataObject.find(
139                     purchaseOrderFO);
140             SchemaEditorSupport ses = sdo.getSchemaEditorSupport();
141             model = ses.getModel();
142             manager = ses.getUndoManager();
143         } catch (IOException JavaDoc ioe) {
144             fail(ioe.toString());
145         }
146
147         assertNotNull("failed to acquire undo manager", manager);
148         assertNotNull("failed to load SchemaModel", model);
149         document = (AbstractDocument JavaDoc) model.getModelSource().getLookup().
150                 lookup(AbstractDocument JavaDoc.class);
151         assertNotNull("ModelSource did not contain AbstractDocument", document);
152         // We are most interested in the issues that BaseDocument creates.
153
assertTrue(document instanceof BaseDocument);
154
155         // For some reason, SchemaEditorSupport.prepareDocument() is not run,
156
// so we must do here what it would have done.
157
document.removeUndoableEditListener(manager);
158         manager.setDocument(document);
159
160         String JavaDoc original = null;
161         try {
162             original = document.getText(0, document.getLength());
163         } catch (BadLocationException JavaDoc ble) {
164             fail(ble.toString());
165         }
166         viewModel();
167         assertFalse(manager.canUndo());
168         assertFalse(manager.canRedo());
169         model.startTransaction();
170         GlobalElement elem = model.getFactory().createGlobalElement();
171         elem.setName("element83963");
172         model.getSchema().addElement(elem);
173         model.endTransaction();
174         Schema schema = model.getSchema();
175         boolean found = false;
176         for (GlobalElement ge : schema.getElements()) {
177             if (ge.getName().equals("element83963")) {
178                 found = true;
179                 break;
180             }
181         }
182         assertTrue("failed to add global element", found);
183         assertTrue(manager.canUndo());
184         assertFalse(manager.canRedo());
185         viewSource();
186         assertTrue(manager.canUndo());
187         assertFalse(manager.canRedo());
188         try {
189             int length = document.getLength();
190             String JavaDoc text = document.getText(0, length);
191             assertTrue(text.contains("element83963"));
192             int mark = text.lastIndexOf("element83963");
193             assertTrue(mark > 0 && mark < length);
194             mark += "element83963\"/>".length();
195             // Must create a document edit that overlaps the model edit,
196
// and it must introduce a new DOM element (e.g. comment).
197
// For example, the following two lines will not cause the error.
198
//document.insertString(mark, "\n \n", null);
199
//assertEquals(length + 4, document.getLength());
200
document.insertString(mark, "\n<!-- -->\n", null);
201             assertEquals(length + 10, document.getLength());
202         } catch (BadLocationException JavaDoc ble) {
203             fail(ble.toString());
204         }
205         assertTrue(manager.canUndo());
206         assertFalse(manager.canRedo());
207         manager.undo();
208         assertTrue(manager.canUndo());
209         assertTrue(manager.canRedo());
210         manager.undo();
211         assertFalse(manager.canUndo());
212         assertTrue(manager.canRedo());
213         String JavaDoc actual = null;
214         try {
215             actual = document.getText(0, document.getLength());
216         } catch (BadLocationException JavaDoc ble) {
217             fail(ble.toString());
218         }
219         assertTrue(actual.equals(original));
220         manager.redo();
221         assertTrue(manager.canUndo());
222         assertTrue(manager.canRedo());
223 // XXX: uncomment the redo() to get the error seen in issue 83963
224
// manager.redo();
225
assertTrue(manager.canUndo());
226 // XXX: Once the above redo() is working again, uncomment this code.
227
// assertFalse(manager.canRedo());
228
// Note that the 'xsd:' namespace is not used in this schema.
229
String JavaDoc expected =
230                 " <!-- etc. -->\n" +
231                 " <element name=\"element83963\"/>\n" +
232 // XXX: Once the above redo() is working again, uncomment this code.
233
// "<!-- -->\n" +
234
// "\n" +
235
"</schema>";
236         try {
237             actual = document.getText(0, document.getLength());
238             assertTrue(actual.endsWith(expected));
239         } catch (BadLocationException JavaDoc ble) {
240             fail(ble.toString());
241         }
242         endTestCase();
243     }
244 }
245
Popular Tags