KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > PositionRefTest


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
21 package org.openide.text;
22
23 import java.util.Date JavaDoc;
24 import java.beans.*;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.swing.text.*;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import javax.swing.text.Position JavaDoc;
33 import javax.swing.text.StyledDocument JavaDoc;
34
35 import junit.textui.TestRunner;
36
37 import org.netbeans.junit.NbTestCase;
38 import org.netbeans.junit.NbTestSuite;
39
40 import org.openide.text.CloneableEditorSupport;
41 import org.openide.text.FilterDocument;
42 import org.openide.text.NbDocument;
43 import org.openide.util.RequestProcessor;
44
45 /**
46  *
47  * @author Petr Nejedly
48  */

49 public class PositionRefTest extends NbTestCase implements CloneableEditorSupport.Env {
50    
51     /** the support to work with */
52     private CES support;
53
54     // Env variables
55
private String JavaDoc content = "Hello";
56     private boolean valid = true;
57     private boolean modified = false;
58     private Date JavaDoc date = new Date JavaDoc ();
59     private transient PropertyChangeSupport prop = new PropertyChangeSupport(this);
60     private transient VetoableChangeSupport veto = new VetoableChangeSupport(this);
61     private Exception JavaDoc exception;
62         
63     public PositionRefTest(String JavaDoc s) {
64         super(s);
65     }
66     
67     public static void main(String JavaDoc[] args) {
68         TestRunner.run(new NbTestSuite(PositionRefTest.class));
69     }
70
71     protected void setUp () {
72         support = new CES (this, org.openide.util.Lookup.EMPTY);
73     }
74
75     
76     /**
77      * Creates a PositionRef biased backwards and verifies it behaves correctly,
78      * then closes and reopens the document and checks again
79      */

80     public void testBiasSurvivesStateChanges() throws Exception JavaDoc {
81     // open the document
82
Document JavaDoc doc = support.openDocument();
83         
84         PositionRef back = support.createPositionRef(3, Position.Bias.Backward);
85         PositionRef forw = support.createPositionRef(3, Position.Bias.Forward);
86         
87         doc.insertString(3, "_", null);
88         assertEquals("Backwards position should not move for insert at its position",
89                 3, back.getOffset());
90         assertEquals("Forwards position should move for insert at its position",
91                 4, forw.getOffset());
92
93         // move positions at the same offset again
94
doc.remove(3, 1);
95
96         support.close();
97         doc = support.openDocument();
98
99         doc.insertString(3, "_", null);
100         assertEquals("Backwards position should not move for insert at its position",
101                 3, back.getOffset());
102         assertEquals("Forwards position should move for insert at its position",
103                 4, forw.getOffset());
104     }
105         
106     //
107
// Implementation of the CloneableEditorSupport.Env
108
//
109

110     public void addPropertyChangeListener(PropertyChangeListener l) {
111         prop.addPropertyChangeListener (l);
112     }
113
114     public void removePropertyChangeListener(PropertyChangeListener l) {
115         prop.removePropertyChangeListener (l);
116     }
117
118     public void addVetoableChangeListener(VetoableChangeListener l) {
119         veto.addVetoableChangeListener (l);
120     }
121
122     public void removeVetoableChangeListener(VetoableChangeListener l) {
123         veto.removeVetoableChangeListener (l);
124     }
125     
126     
127     public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() {
128         return support;
129     }
130     
131     public String JavaDoc getMimeType() {
132         return "text/plain";
133     }
134     
135     public java.util.Date JavaDoc getTime() {
136         return date;
137     }
138     
139     public java.io.InputStream JavaDoc inputStream() throws java.io.IOException JavaDoc {
140     return new ByteArrayInputStream JavaDoc(content.getBytes());
141     }
142     
143     public java.io.OutputStream JavaDoc outputStream() throws java.io.IOException JavaDoc {
144         return new ByteArrayOutputStream JavaDoc();
145     }
146     
147     public boolean isValid() {
148         return valid;
149     }
150     
151     public boolean isModified() {
152         return modified;
153     }
154
155     public void markModified() throws java.io.IOException JavaDoc {
156         modified = true;
157     }
158     
159     public void unmarkModified() {
160         modified = false;
161     }
162     
163     /** Implementation of the CES */
164     private final class CES extends CloneableEditorSupport {
165         
166         public CES (Env env, org.openide.util.Lookup l) {
167             super (env, l);
168         }
169         
170         protected String JavaDoc messageName() {
171             return "Name";
172         }
173         
174         protected String JavaDoc messageOpened() {
175             return "Opened";
176         }
177         
178         protected String JavaDoc messageOpening() {
179             return "Opening";
180         }
181         
182         protected String JavaDoc messageSave() {
183             return "Save";
184         }
185         
186         protected String JavaDoc messageToolTip() {
187             return "ToolTip";
188         }
189
190         protected boolean canClose () {
191             return true;
192         }
193
194     } // end of CES
195

196 }
197
Popular Tags