1 19 20 package org.netbeans.api.java.source; 21 22 import java.io.OutputStream ; 23 import java.lang.reflect.Method ; 24 import java.util.Arrays ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.Position.Bias; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.cookies.EditorCookie; 31 import org.openide.filesystems.FileLock; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileSystem; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.loaders.DataObject; 36 import org.openide.text.CloneableEditorSupport; 37 import org.openide.text.PositionRef; 38 39 43 public class ModificationResultTest extends NbTestCase { 44 45 46 public ModificationResultTest(String name) { 47 super(name); 48 } 49 50 private void writeIntoFile(FileObject file, String what) throws Exception { 51 FileLock lock = file.lock(); 52 OutputStream out = file.getOutputStream(lock); 53 54 try { 55 out.write(what.getBytes()); 56 } finally { 57 out.close(); 58 lock.releaseLock(); 59 } 60 } 61 62 private FileObject testFile; 63 private CloneableEditorSupport ces; 64 65 private void prepareTest() throws Exception { 66 FileSystem fs = FileUtil.createMemoryFileSystem(); 67 FileObject root = fs.getRoot(); 68 testFile = FileUtil.createData(root, "test/test.java"); 69 70 writeIntoFile(testFile, "test\ntest\ntest\n"); 71 72 DataObject od = DataObject.find(testFile); 73 74 ces = (CloneableEditorSupport) od.getCookie(EditorCookie.class); 75 } 76 77 private ModificationResult prepareInsertResult() throws Exception { 78 PositionRef start1 = ces.createPositionRef(5, Bias.Forward); 79 ModificationResult.Difference diff1 = new ModificationResult.Difference(ModificationResult.Difference.Kind.INSERT, start1, start1, "", "new-test1\n"); 80 PositionRef start2 = ces.createPositionRef(10, Bias.Forward); 81 ModificationResult.Difference diff2 = new ModificationResult.Difference(ModificationResult.Difference.Kind.INSERT, start2, start2, "", "new-test2\n"); 82 83 ModificationResult result = new ModificationResult(null); 84 85 result.diffs = new HashMap <FileObject, List <ModificationResult.Difference>>(); 86 result.diffs.put(testFile, Arrays.asList(diff1, diff2)); 87 88 return result; 89 } 90 91 private void performTestToFile(String creator) throws Exception { 92 prepareTest(); 93 94 Method m = ModificationResultTest.class.getDeclaredMethod(creator, new Class [0]); 95 96 ModificationResult result = (ModificationResult) m.invoke(this, new Object [0]); 97 98 result.commit(); 99 100 Document doc = ces.openDocument(); 101 102 ref(doc.getText(0, doc.getLength())); 103 104 compareReferenceFiles(); 105 } 106 107 private void performTestToDocument(String creator) throws Exception { 108 prepareTest(); 109 110 Document doc = ces.openDocument(); 111 112 Method m = ModificationResultTest.class.getDeclaredMethod(creator, new Class [0]); 113 114 ModificationResult result = (ModificationResult) m.invoke(this, new Object [0]); 115 116 result.commit(); 117 118 ref(doc.getText(0, doc.getLength())); 119 120 compareReferenceFiles(); 121 } 122 123 private ModificationResult prepareRemoveResult() throws Exception { 124 PositionRef start1 = ces.createPositionRef(5, Bias.Forward); 125 PositionRef end1 = ces.createPositionRef(9, Bias.Forward); 126 ModificationResult.Difference diff1 = new ModificationResult.Difference(ModificationResult.Difference.Kind.REMOVE, start1, end1, "test", ""); 127 PositionRef start2 = ces.createPositionRef(11, Bias.Forward); 128 PositionRef end2 = ces.createPositionRef(12, Bias.Forward); 129 ModificationResult.Difference diff2 = new ModificationResult.Difference(ModificationResult.Difference.Kind.REMOVE, start2, end2, "e", ""); 130 131 ModificationResult result = new ModificationResult(null); 132 133 result.diffs = new HashMap <FileObject, List <ModificationResult.Difference>>(); 134 result.diffs.put(testFile, Arrays.asList(diff1, diff2)); 135 136 return result; 137 } 138 139 private ModificationResult prepareModificationResult1() throws Exception { 140 PositionRef start1 = ces.createPositionRef(5, Bias.Forward); 141 PositionRef end1 = ces.createPositionRef(9, Bias.Forward); 142 ModificationResult.Difference diff1 = new ModificationResult.Difference(ModificationResult.Difference.Kind.CHANGE, start1, end1, "test", "ab"); 143 PositionRef start2 = ces.createPositionRef(11, Bias.Forward); 144 PositionRef end2 = ces.createPositionRef(13, Bias.Forward); 145 ModificationResult.Difference diff2 = new ModificationResult.Difference(ModificationResult.Difference.Kind.CHANGE, start2, end2, "es", "a"); 146 147 ModificationResult result = new ModificationResult(null); 148 149 result.diffs = new HashMap <FileObject, List <ModificationResult.Difference>>(); 150 result.diffs.put(testFile, Arrays.asList(diff1, diff2)); 151 152 return result; 153 } 154 155 private ModificationResult prepareModificationResult2() throws Exception { 156 PositionRef start1 = ces.createPositionRef(5, Bias.Forward); 157 PositionRef end1 = ces.createPositionRef(9, Bias.Forward); 158 ModificationResult.Difference diff1 = new ModificationResult.Difference(ModificationResult.Difference.Kind.CHANGE, start1, end1, "test", "abcde"); 159 PositionRef start2 = ces.createPositionRef(11, Bias.Forward); 160 PositionRef end2 = ces.createPositionRef(13, Bias.Forward); 161 ModificationResult.Difference diff2 = new ModificationResult.Difference(ModificationResult.Difference.Kind.CHANGE, start2, end2, "es", "a"); 162 163 ModificationResult result = new ModificationResult(null); 164 165 result.diffs = new HashMap <FileObject, List <ModificationResult.Difference>>(); 166 result.diffs.put(testFile, Arrays.asList(diff1, diff2)); 167 168 return result; 169 } 170 171 public void testInsertToFile() throws Exception { 172 performTestToFile("prepareInsertResult"); 173 } 174 175 public void testInsertToDocument() throws Exception { 176 performTestToDocument("prepareInsertResult"); 177 } 178 179 public void testRemoveFromFile() throws Exception { 180 performTestToFile("prepareRemoveResult"); 181 } 182 183 public void testRemoveFromDocument() throws Exception { 184 performTestToDocument("prepareRemoveResult"); 185 } 186 187 public void testModification1ToFile() throws Exception { 188 performTestToFile("prepareModificationResult1"); 189 } 190 191 public void testModification1ToDocument() throws Exception { 192 performTestToDocument("prepareModificationResult1"); 193 } 194 195 public void testModification2ToFile() throws Exception { 196 performTestToFile("prepareModificationResult2"); 197 } 198 199 public void testModification2ToDocument() throws Exception { 200 performTestToDocument("prepareModificationResult2"); 201 } 202 203 } 204 | Popular Tags |