/** * Undo after some positions have been garbage collected. * * @throws BadLocationException */ public void testUndoGC() throws BadLocationException { content.insertString(0, "012345678"); Vector<WeakReference<Position>> pos = new Vector<WeakReference<Position>>(10); ReferenceQueue<Position> rq = new ReferenceQueue<Position>(); for (int i = 0; i < content.length(); i += 2) { pos.add(new WeakReference<Position>(content.createPosition(i), rq)); } int count = 0; int i; for (i = 0; i < 100; i++) { System.gc(); Reference<?> r; if ((r = rq.poll()) != null) { pos.remove(r); count++; if (pos.size() == 0) { break; } } } // This call causes all the garbage collected positions to be // removed from the internal list UndoableEdit ue = content.remove(0, 5); assertEquals("5678", content.getString(0, content.length() - 1)); // Test (it shouldn't fail with any NullPointerException) ue.undo(); assertEquals("012345678", content.getString(0, content.length() - 1)); }
/** * Creates positions at specified offsets and stores them in array. * * @param offsets array of offsets where positions are to be created * @param positions array of positions to store to * @throws BadLocationException */ private void addPositions(final int[] offsets, final Position[] positions) throws BadLocationException { for (int i = 0; i < offsets.length; i++) { positions[i] = content.createPosition(offsets[i]); } }