KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > ModificationResultTest


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.api.java.source;
21
22 import java.io.OutputStream JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.text.Document JavaDoc;
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 /**
40  *
41  * @author Jan Lahoda
42  */

43 public class ModificationResultTest extends NbTestCase {
44     
45     /** Creates a new instance of ModificationResultTest */
46     public ModificationResultTest(String JavaDoc name) {
47         super(name);
48     }
49     
50     private void writeIntoFile(FileObject file, String JavaDoc what) throws Exception JavaDoc {
51         FileLock lock = file.lock();
52         OutputStream JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc<FileObject, List JavaDoc<ModificationResult.Difference>>();
86         result.diffs.put(testFile, Arrays.asList(diff1, diff2));
87         
88         return result;
89     }
90     
91     private void performTestToFile(String JavaDoc creator) throws Exception JavaDoc {
92         prepareTest();
93         
94         Method JavaDoc m = ModificationResultTest.class.getDeclaredMethod(creator, new Class JavaDoc[0]);
95         
96         ModificationResult result = (ModificationResult) m.invoke(this, new Object JavaDoc[0]);
97         
98         result.commit();
99         
100         Document JavaDoc doc = ces.openDocument();
101         
102         ref(doc.getText(0, doc.getLength()));
103         
104         compareReferenceFiles();
105     }
106     
107     private void performTestToDocument(String JavaDoc creator) throws Exception JavaDoc {
108         prepareTest();
109         
110         Document JavaDoc doc = ces.openDocument();
111         
112         Method JavaDoc m = ModificationResultTest.class.getDeclaredMethod(creator, new Class JavaDoc[0]);
113         
114         ModificationResult result = (ModificationResult) m.invoke(this, new Object JavaDoc[0]);
115         
116         result.commit();
117         
118         ref(doc.getText(0, doc.getLength()));
119         
120         compareReferenceFiles();
121     }
122     
123     private ModificationResult prepareRemoveResult() throws Exception JavaDoc {
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 JavaDoc<FileObject, List JavaDoc<ModificationResult.Difference>>();
134         result.diffs.put(testFile, Arrays.asList(diff1, diff2));
135         
136         return result;
137     }
138     
139     private ModificationResult prepareModificationResult1() throws Exception JavaDoc {
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 JavaDoc<FileObject, List JavaDoc<ModificationResult.Difference>>();
150         result.diffs.put(testFile, Arrays.asList(diff1, diff2));
151         
152         return result;
153     }
154     
155     private ModificationResult prepareModificationResult2() throws Exception JavaDoc {
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 JavaDoc<FileObject, List JavaDoc<ModificationResult.Difference>>();
166         result.diffs.put(testFile, Arrays.asList(diff1, diff2));
167         
168         return result;
169     }
170     
171     public void testInsertToFile() throws Exception JavaDoc {
172         performTestToFile("prepareInsertResult");
173     }
174     
175     public void testInsertToDocument() throws Exception JavaDoc {
176         performTestToDocument("prepareInsertResult");
177     }
178     
179     public void testRemoveFromFile() throws Exception JavaDoc {
180         performTestToFile("prepareRemoveResult");
181     }
182     
183     public void testRemoveFromDocument() throws Exception JavaDoc {
184         performTestToDocument("prepareRemoveResult");
185     }
186     
187     public void testModification1ToFile() throws Exception JavaDoc {
188         performTestToFile("prepareModificationResult1");
189     }
190     
191     public void testModification1ToDocument() throws Exception JavaDoc {
192         performTestToDocument("prepareModificationResult1");
193     }
194     
195     public void testModification2ToFile() throws Exception JavaDoc {
196         performTestToFile("prepareModificationResult2");
197     }
198     
199     public void testModification2ToDocument() throws Exception JavaDoc {
200         performTestToDocument("prepareModificationResult2");
201     }
202     
203 }
204
Popular Tags