KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > SeparatorTest


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 package org.netbeans.api.java.source.gen;
20
21 import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Stack JavaDoc;
26 import org.netbeans.api.java.lexer.JavaTokenId;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.netbeans.modules.java.source.save.ListMatcher;
30 import static org.netbeans.modules.java.source.save.ListMatcher.*;
31
32 /**
33  * Test adding/removing separators.
34  *
35  * @author Pavel Flaska
36  */

37 public class SeparatorTest extends NbTestCase {
38
39     /** Creates a new instance of SeparatorTest */
40     public SeparatorTest(String JavaDoc testName) {
41         super(testName);
42     }
43     
44     public static NbTestSuite suite() {
45         NbTestSuite suite = new NbTestSuite();
46         suite.addTestSuite(SeparatorTest.class);
47         return suite;
48     }
49
50     public void testAddToEmpty() {
51         String JavaDoc[] oldL = { };
52         String JavaDoc[] newL = { "A", "B", "C" };
53         String JavaDoc golden =
54                 "head {insert} A next \n" +
55                 "{insert} B next \n" +
56                 "{insert} C tail \n";
57         ListMatcher matcher = ListMatcher.instance(oldL, newL);
58         if (matcher.match()) {
59             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
60             s.compute();
61             System.err.println("testAddToEmpty: ");
62             System.err.println(p(oldL, newL));
63             System.err.println(s.print());
64             assertEquals(golden, s.print());
65         } else {
66             assertTrue("No match!", false);
67         }
68     }
69     
70     public void testModify1() {
71         String JavaDoc[] oldL = { "A", "X", "C" };
72         String JavaDoc[] newL = { "A", "B", "C" };
73         String JavaDoc golden = "";
74         ListMatcher matcher = ListMatcher.instance(oldL, newL);
75         if (matcher.match()) {
76             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
77             s.compute();
78             System.err.println("testModify1: ");
79             System.err.println(p(oldL, newL));
80             System.err.println(s.print());
81             assertEquals(golden, s.print());
82         } else {
83             assertTrue("No match!", false);
84         }
85     }
86
87     public void testRemoveAll() {
88         String JavaDoc[] oldL= { "A", "B", "C" };
89         String JavaDoc[] newL = { };
90         String JavaDoc golden =
91                 "head {delete} A next \n" +
92                 "{delete} B next \n" +
93                 "{delete} C tail \n";
94         ListMatcher matcher = ListMatcher.instance(oldL, newL);
95         if (matcher.match()) {
96             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
97             s.compute();
98             System.err.println("testRemoveAll: ");
99             System.err.println(p(oldL, newL));
100             System.err.println(s.print());
101             assertEquals(golden, s.print());
102         } else {
103             assertTrue("No match!", false);
104         }
105     }
106     
107     public void testAddToIndex0() {
108         String JavaDoc[] oldL= { "B" };
109         String JavaDoc[] newL = { "A", "B" };
110         String JavaDoc golden =
111                 "{insert} A next \n";
112         ListMatcher matcher = ListMatcher.instance(oldL, newL);
113         if (matcher.match()) {
114             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
115             s.compute();
116             System.err.println("testAddToIndex0:");
117             System.err.println(p(oldL, newL));
118             System.err.println(s.print());
119             assertEquals(golden, s.print());
120         } else {
121             assertTrue("No match!", false);
122         }
123     }
124     
125     public void testRemoveAtIndex0() {
126         String JavaDoc[] oldL = { "A", "B" };
127         String JavaDoc[] newL = { "B" };
128         String JavaDoc golden =
129                 "{delete} A next \n";
130         ListMatcher matcher = ListMatcher.instance(oldL, newL);
131         if (matcher.match()) {
132             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
133             s.compute();
134             System.err.println("testRemoveAtIndex0:");
135             System.err.println(p(oldL, newL));
136             System.err.println(s.print());
137             assertEquals(golden, s.print());
138         } else {
139             assertTrue("No match!", false);
140         }
141     }
142     
143     public void testRemoveLastTwo() {
144         String JavaDoc[] oldL = { "A", "B", "C" };
145         String JavaDoc[] newL = { "A" };
146         String JavaDoc golden =
147                 "previous {delete} B next \n" +
148                 "{delete} C \n";
149         ListMatcher matcher = ListMatcher.instance(oldL, newL);
150         if (matcher.match()) {
151             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
152             s.compute();
153             System.err.println("testRemoveLastTwo:");
154             System.err.println(p(oldL, newL));
155             System.err.println(s.print());
156             assertEquals(golden, s.print());
157         } else {
158             assertTrue("No match!", false);
159         }
160     }
161     
162     public void testRemoveLastThree() {
163         String JavaDoc[] oldL = { "A", "B", "C", "D" };
164         String JavaDoc[] newL = { "A" };
165         String JavaDoc golden =
166                 "previous {delete} B next \n" +
167                 "{delete} C next \n" +
168                 "{delete} D \n";
169         ListMatcher matcher = ListMatcher.instance(oldL, newL);
170         if (matcher.match()) {
171             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
172             s.compute();
173             System.err.println("testRemoveLastThree:");
174             System.err.println(p(oldL, newL));
175             System.err.println(s.print());
176             assertEquals(golden, s.print());
177         } else {
178             assertTrue("No match!", false);
179         }
180     }
181     
182     public void testRemoveLast() {
183         String JavaDoc[] oldL = { "A", "B" };
184         String JavaDoc[] newL = { "A" };
185         String JavaDoc golden =
186                 "previous {delete} B \n";
187         ListMatcher matcher = ListMatcher.instance(oldL, newL);
188         if (matcher.match()) {
189             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
190             s.compute();
191             System.err.println("testRemoveLast:");
192             System.err.println(p(oldL, newL));
193             System.err.println(s.print());
194             assertEquals(golden, s.print());
195         } else {
196             assertTrue("No match!", false);
197         }
198     }
199     
200     public void testRemoveLast2() {
201         String JavaDoc[] oldL = { "A" };
202         String JavaDoc[] newL = { };
203         String JavaDoc golden =
204                 "head {delete} A tail \n";
205         ListMatcher matcher = ListMatcher.instance(oldL, newL);
206         if (matcher.match()) {
207             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
208             s.compute();
209             System.err.println("testRemoveLast2:");
210             System.err.println(p(oldL, newL));
211             System.err.println(s.print());
212             assertEquals(golden, s.print());
213         } else {
214             assertTrue("No match!", false);
215         }
216     }
217     
218     public void testComplex() {
219         String JavaDoc[] oldL = { "A", "B", "C", "D", "E", "F", "G" };
220         String JavaDoc[] newL = { "B", "C", "C1", "D", "E", "G", "H" };
221         String JavaDoc golden =
222                 "{delete} A next \n" +
223                 "{insert} C1 next \n" +
224                 "{delete} F next \n" +
225                 "previous {insert} H \n";
226         ListMatcher matcher = ListMatcher.instance(oldL, newL);
227         if (matcher.match()) {
228             Separator s = new Separator(matcher.getTransformedResult(), JavaTokenId.COMMA);
229             s.compute();
230             System.err.println("testComplex: ");
231             System.err.println(p(oldL, newL));
232             System.err.println(s.print());
233             assertEquals(golden, s.print());
234         } else {
235             assertTrue("No match!", false);
236         }
237     }
238     
239     private String JavaDoc p(String JavaDoc[] o1, String JavaDoc o2[]) {
240         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
241         for (int i = 0; i < o1.length; i++) {
242             if (i > 0) sb.append(", ");
243             sb.append(o1[i]);
244         }
245         sb.append("\n");
246         for (int i = 0; i < o2.length; i++) {
247             if (i > 0) sb.append(", ");
248             sb.append(o2[i]);
249         }
250         sb.append("\n");
251         return sb.toString();
252     }
253 }
254
Popular Tags