1 19 20 21 package org.openide.text; 22 23 import java.util.Date ; 24 import java.beans.*; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import javax.swing.text.*; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Document ; 32 import javax.swing.text.Position ; 33 import javax.swing.text.StyledDocument ; 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 49 public class PositionRefTest extends NbTestCase implements CloneableEditorSupport.Env { 50 51 52 private CES support; 53 54 private String content = "Hello"; 56 private boolean valid = true; 57 private boolean modified = false; 58 private Date date = new Date (); 59 private transient PropertyChangeSupport prop = new PropertyChangeSupport(this); 60 private transient VetoableChangeSupport veto = new VetoableChangeSupport(this); 61 private Exception exception; 62 63 public PositionRefTest(String s) { 64 super(s); 65 } 66 67 public static void main(String [] 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 80 public void testBiasSurvivesStateChanges() throws Exception { 81 Document 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 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 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 getMimeType() { 132 return "text/plain"; 133 } 134 135 public java.util.Date getTime() { 136 return date; 137 } 138 139 public java.io.InputStream inputStream() throws java.io.IOException { 140 return new ByteArrayInputStream (content.getBytes()); 141 } 142 143 public java.io.OutputStream outputStream() throws java.io.IOException { 144 return new ByteArrayOutputStream (); 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 { 156 modified = true; 157 } 158 159 public void unmarkModified() { 160 modified = false; 161 } 162 163 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 messageName() { 171 return "Name"; 172 } 173 174 protected String messageOpened() { 175 return "Opened"; 176 } 177 178 protected String messageOpening() { 179 return "Opening"; 180 } 181 182 protected String messageSave() { 183 return "Save"; 184 } 185 186 protected String messageToolTip() { 187 return "ToolTip"; 188 } 189 190 protected boolean canClose () { 191 return true; 192 } 193 194 } 196 } 197 | Popular Tags |