Lines 50-55
Link Here
|
50 |
} |
50 |
} |
51 |
|
51 |
|
52 |
public static void main(java.lang.String[] args) { |
52 |
public static void main(java.lang.String[] args) { |
|
|
53 |
if (args.length == 1) { |
54 |
junit.textui.TestRunner.run (new LineSetTest (args[0])); |
55 |
} |
53 |
junit.textui.TestRunner.run(suite()); |
56 |
junit.textui.TestRunner.run(suite()); |
54 |
} |
57 |
} |
55 |
|
58 |
|
Lines 63-68
Link Here
|
63 |
protected void setUp () { |
66 |
protected void setUp () { |
64 |
ic = new InstanceContent (); |
67 |
ic = new InstanceContent (); |
65 |
support = new CES (this, new AbstractLookup (ic)); |
68 |
support = new CES (this, new AbstractLookup (ic)); |
|
|
69 |
ic.add (this); |
66 |
} |
70 |
} |
67 |
|
71 |
|
68 |
public void testLineSetIsEmpty () throws Exception { |
72 |
public void testLineSetIsEmpty () throws Exception { |
Lines 90-95
Link Here
|
90 |
|
94 |
|
91 |
Line line = set.getCurrent (1); |
95 |
Line line = set.getCurrent (1); |
92 |
assertEquals ("Line number is one", 1, line.getLineNumber ()); |
96 |
assertEquals ("Line number is one", 1, line.getLineNumber ()); |
|
|
97 |
assertGetOriginal ("Original number is of course 1", set, line, 1); |
93 |
|
98 |
|
94 |
doc.insertString (0, "New line\n", null); |
99 |
doc.insertString (0, "New line\n", null); |
95 |
|
100 |
|
Lines 107-112
Link Here
|
107 |
|
112 |
|
108 |
Line currentLineTwo = support.getLineSet ().getOriginal (2); |
113 |
Line currentLineTwo = support.getLineSet ().getOriginal (2); |
109 |
assertEquals ("This is our original line", line, currentLineTwo); |
114 |
assertEquals ("This is our original line", line, currentLineTwo); |
|
|
115 |
assertGetOriginal ("Original number of the line was 1", set, line, 1); |
110 |
|
116 |
|
111 |
assertEquals ("Original set still has two lines", 2, set.getLines ().size ()); |
117 |
assertEquals ("Original set still has two lines", 2, set.getLines ().size ()); |
112 |
assertEquals ("Index of current line 1 is 0 in old set", 1, set.getLines ().indexOf (line)); |
118 |
assertEquals ("Index of current line 1 is 0 in old set", 1, set.getLines ().indexOf (line)); |
Lines 165-170
Link Here
|
165 |
assertEquals ("They will not part", one, two); |
171 |
assertEquals ("They will not part", one, two); |
166 |
|
172 |
|
167 |
assertEquals ("Line number is 0", 0, one.getLineNumber ()); |
173 |
assertEquals ("Line number is 0", 0, one.getLineNumber ()); |
|
|
174 |
} |
175 |
|
176 |
public void testGetLinesIndexOfDoesNotCreateAllLines () throws Exception { |
177 |
content = "0\n1\n2\n3\n4\n"; |
178 |
javax.swing.text.Document doc = support.openDocument (); |
179 |
|
180 |
Line.Set set = support.getLineSet (); |
181 |
Line two = set.getOriginal (2); |
182 |
|
183 |
assertEquals ("Line index is two", 2, set.getLines ().indexOf (two)); |
184 |
assertNumberOfLines (1, set); |
185 |
|
186 |
assertEquals ("Really two", 2, new java.util.ArrayList (set.getLines ()).indexOf (two)); |
187 |
} |
188 |
|
189 |
public void testLinesAreNonMutable () throws Exception { |
190 |
content = "0\n1\n2\n3\n4\n"; |
191 |
javax.swing.text.Document doc = support.openDocument (); |
192 |
|
193 |
assertNonmutable (support.getLineSet ().getLines ()); |
194 |
} |
195 |
|
196 |
public void testGetLinesIndexWorksAfterModifications () throws Exception { |
197 |
content = "0\n1\n2\n3\n4\n"; |
198 |
javax.swing.text.Document doc = support.openDocument (); |
199 |
|
200 |
Line.Set set = support.getLineSet (); |
201 |
|
202 |
int offset = 4; |
203 |
assertEquals ("2 is on the second line", "2", doc.getText (4, 1)); |
204 |
doc.insertString (offset, "x\n", null); |
205 |
assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2)); |
206 |
|
207 |
|
208 |
Line two = set.getOriginal (2); |
209 |
assertEquals ("2\n is the line text", "2\n", two.getText ()); |
210 |
|
211 |
assertEquals ("Line index is two", 2, set.getLines ().indexOf (two)); |
212 |
assertNumberOfLines (1, set); |
213 |
|
214 |
assertEquals ("Really two", 2, new java.util.ArrayList (set.getLines ()).indexOf (two)); |
215 |
} |
216 |
|
217 |
public void testWhatHappensWhenAskingForLineOutOfBounds () throws Exception { |
218 |
content = "0"; |
219 |
javax.swing.text.Document doc = support.openDocument (); |
220 |
|
221 |
Line.Set set = support.getLineSet (); |
222 |
|
223 |
try { |
224 |
Line l = set.getCurrent (1); |
225 |
fail ("Should thrown IndexOutOfBoundsException"); |
226 |
} catch (IndexOutOfBoundsException ex) { |
227 |
// ok |
228 |
} |
229 |
|
230 |
try { |
231 |
Line n = set.getOriginal (1); |
232 |
fail ("Should thrown IndexOutOfBoundsException"); |
233 |
} catch (IndexOutOfBoundsException ex) { |
234 |
// ok |
235 |
} |
236 |
try { |
237 |
Line l = set.getCurrent (-1); |
238 |
fail ("Should thrown IndexOutOfBoundsException"); |
239 |
} catch (IndexOutOfBoundsException ex) { |
240 |
// ok |
241 |
} |
242 |
|
243 |
try { |
244 |
Line n = set.getOriginal (-1); |
245 |
fail ("Should thrown IndexOutOfBoundsException"); |
246 |
} catch (IndexOutOfBoundsException ex) { |
247 |
// ok |
248 |
} |
249 |
|
250 |
Line l = set.getCurrent (0); |
251 |
Line n = set.getOriginal (0); |
252 |
|
253 |
assertNotNull (l); |
254 |
assertNotNull (n); |
255 |
|
256 |
assertEquals ("Lines are the same", l, n); |
257 |
assertEquals ("Text is", "0", l.getText ()); |
258 |
} |
259 |
|
260 |
|
261 |
public void testGetLinesIndexWorksForNewlyAddedLines () throws Exception { |
262 |
content = "0\n1\n2\n3\n4\n"; |
263 |
javax.swing.text.Document doc = support.openDocument (); |
264 |
|
265 |
Line.Set set = support.getLineSet (); |
266 |
|
267 |
int offset = 4; |
268 |
assertEquals ("2 is on the second line", "2", doc.getText (4, 1)); |
269 |
doc.insertString (offset, "x\n", null); |
270 |
assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2)); |
271 |
|
272 |
|
273 |
Line two = set.getCurrent (2); |
274 |
assertEquals ("x\n is the line text", "x\n", two.getText ()); |
275 |
|
276 |
assertEquals ("Line index is -1 as it is not present", -1, set.getLines ().indexOf (two)); |
277 |
// two lines created as we need to verify that the current two line is not |
278 |
// in the original set |
279 |
// of course that it can be one if somebody implements this query |
280 |
// in better way |
281 |
assertNumberOfLines (2, set); |
282 |
assertEquals ("Query on set works and does not produce new lines", 2, set.getOriginal (two)); |
283 |
assertNumberOfLines (2, set); |
284 |
|
285 |
// now few additinal checks |
286 |
assertGetOriginal ("However if one asks the line set it works", set, two, 2); |
287 |
assertEquals ("Really missing from the list", -1, new java.util.ArrayList (set.getLines ()).indexOf (two)); |
288 |
} |
289 |
|
290 |
private void assertNumberOfLines (int cnt, Line.Set set) throws Exception { |
291 |
class MF implements org.netbeans.junit.MemoryFilter { |
292 |
private java.util.HashSet counted = new java.util.HashSet (); |
293 |
public int cnt; |
294 |
public boolean reject(Object obj) { |
295 |
if (obj instanceof Line) { |
296 |
Line l = (Line)obj; |
297 |
if (counted.add (obj)) { |
298 |
if (l.getLookup ().lookup (LineSetTest.class) == LineSetTest.this) { |
299 |
cnt++; |
300 |
} |
301 |
} |
302 |
} |
303 |
return false; |
304 |
} |
305 |
} |
306 |
|
307 |
MF mf = new MF (); |
308 |
|
309 |
// just travel thru the memory |
310 |
assertSize ( |
311 |
"Just one line", |
312 |
java.util.Collections.singleton (set), |
313 |
Integer.MAX_VALUE, |
314 |
mf |
315 |
); |
316 |
|
317 |
if (mf.cnt > cnt) { |
318 |
fail ("Only given number of instance of line created (" + cnt + ") but was: " + mf.cnt); |
319 |
} |
320 |
} |
321 |
|
322 |
private static void assertGetOriginal (String s, Line.Set set, Line line, int expected) { |
323 |
assertEquals (s + " - Overriden DocumentLine.Set.getOriginal as well", expected, set.getOriginal (line)); |
324 |
assertEquals (s + " - The default Line.Set.computeOriginal method works", expected, Line.Set.computeOriginal (set, line)); |
325 |
} |
326 |
|
327 |
private static void assertNonmutable (java.util.List l) throws Exception { |
328 |
try { |
329 |
l.add (new Object ()); |
330 |
fail ("add should fail"); |
331 |
} catch (java.lang.UnsupportedOperationException ex) { |
332 |
// ok |
333 |
} |
334 |
try { |
335 |
l.add (0, new Object ()); |
336 |
fail ("add should fail"); |
337 |
} catch (java.lang.UnsupportedOperationException ex) { |
338 |
// ok |
339 |
} |
340 |
|
341 |
try { |
342 |
l.remove (new Object ()); |
343 |
fail ("remove should fail"); |
344 |
} catch (java.lang.UnsupportedOperationException ex) { |
345 |
// ok |
346 |
} |
347 |
|
348 |
try { |
349 |
l.addAll (java.util.Collections.EMPTY_LIST); |
350 |
fail ("addAll should fail"); |
351 |
} catch (java.lang.UnsupportedOperationException ex) { |
352 |
// ok |
353 |
} |
354 |
try { |
355 |
l.addAll (0, java.util.Collections.EMPTY_LIST); |
356 |
fail ("addAll should fail"); |
357 |
} catch (java.lang.UnsupportedOperationException ex) { |
358 |
// ok |
359 |
} |
360 |
try { |
361 |
l.removeAll (java.util.Collections.EMPTY_LIST); |
362 |
fail ("removeAll should fail"); |
363 |
} catch (java.lang.UnsupportedOperationException ex) { |
364 |
// ok |
365 |
} |
366 |
|
367 |
try { |
368 |
l.retainAll (java.util.Collections.EMPTY_LIST); |
369 |
fail ("retainAll should fail"); |
370 |
} catch (java.lang.UnsupportedOperationException ex) { |
371 |
// ok |
372 |
} |
373 |
try { |
374 |
l.set (0, null); |
375 |
fail ("set should fail"); |
376 |
} catch (java.lang.UnsupportedOperationException ex) { |
377 |
// ok |
378 |
} |
168 |
} |
379 |
} |
169 |
|
380 |
|
170 |
// |
381 |
// |