1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.util.Stack ; 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 37 public class SeparatorTest extends NbTestCase { 38 39 40 public SeparatorTest(String 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 [] oldL = { }; 52 String [] newL = { "A", "B", "C" }; 53 String 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 [] oldL = { "A", "X", "C" }; 72 String [] newL = { "A", "B", "C" }; 73 String 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 [] oldL= { "A", "B", "C" }; 89 String [] newL = { }; 90 String 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 [] oldL= { "B" }; 109 String [] newL = { "A", "B" }; 110 String 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 [] oldL = { "A", "B" }; 127 String [] newL = { "B" }; 128 String 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 [] oldL = { "A", "B", "C" }; 145 String [] newL = { "A" }; 146 String 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 [] oldL = { "A", "B", "C", "D" }; 164 String [] newL = { "A" }; 165 String 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 [] oldL = { "A", "B" }; 184 String [] newL = { "A" }; 185 String 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 [] oldL = { "A" }; 202 String [] newL = { }; 203 String 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 [] oldL = { "A", "B", "C", "D", "E", "F", "G" }; 220 String [] newL = { "B", "C", "C1", "D", "E", "G", "H" }; 221 String 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 p(String [] o1, String o2[]) { 240 StringBuffer sb = new StringBuffer (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 |