public void guiTestDocumentSwap() throws Exception {
final JTextField textField = new JTextField();
final AbstractDocument documentA = (AbstractDocument) textField.getDocument();
documentA.insertString(0, "documentA", null);
final AbstractDocument documentB = new PlainDocument();
documentB.insertString(0, "documentB", null);
final int originalDocumentAListenerCount = documentA.getDocumentListeners().length;
final int originalDocumentBListenerCount = documentB.getDocumentListeners().length;
// using the textField in a TextComponentMatcherEditor will install a DocumentListener
TextComponentMatcherEditor<String> textMatcherEditor = new TextComponentMatcherEditor<String>(textField, GlazedLists.toStringTextFilterator(), true);
assertEquals(originalDocumentAListenerCount+1, documentA.getDocumentListeners().length);
assertEquals(originalDocumentBListenerCount, documentB.getDocumentListeners().length);
TextMatcher textMatcher = (TextMatcher) textMatcherEditor.getMatcher();
assertEquals("documentA", textMatcher.getSearchTerms()[0].getText());
// replace DocumentA with DocumentB, which should update the filter with the text from DocumentB
textField.setDocument(documentB);
assertEquals(0, documentA.getDocumentListeners().length);
assertEquals(originalDocumentBListenerCount+3, documentB.getDocumentListeners().length);
textMatcher = (TextMatcher) textMatcherEditor.getMatcher();
assertEquals("documentB", textMatcher.getSearchTerms()[0].getText());
// changing the text in DocumentB should alter the filter
documentB.replace(0, documentB.getLength(), "blah", null);
textMatcher = (TextMatcher) textMatcherEditor.getMatcher();
assertEquals("blah", textMatcher.getSearchTerms()[0].getText());
}