Skip to main content
summaryrefslogtreecommitdiffstats
blob: e980515201ac9e048918a2982d4459ab126d3860 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
 * Copyright (c) 2007, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.tests.internal.model.value.swing;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent.EventType;
import javax.swing.text.Document;
import junit.framework.TestCase;
import org.eclipse.jpt.utility.internal.ReflectionTools;
import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.utility.internal.model.value.swing.DocumentAdapter;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
import org.eclipse.jpt.utility.tests.internal.TestTools;

@SuppressWarnings("nls")
public class DocumentAdapterTests extends TestCase {
	private WritablePropertyValueModel<String> stringHolder;
	Document documentAdapter;
	boolean eventFired;

	public DocumentAdapterTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.stringHolder = new SimplePropertyValueModel<String>("0123456789");
		this.documentAdapter = new DocumentAdapter(this.stringHolder) {
			@Override
			protected PropertyChangeListener buildStringListener() {
				return this.buildStringListener_();
			}
		};
	}

	@Override
	protected void tearDown() throws Exception {
		TestTools.clear(this);
		super.tearDown();
	}

	public void testRemove() throws Exception {
		this.eventFired = false;
		this.documentAdapter.addDocumentListener(new TestDocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				DocumentAdapterTests.this.eventFired = true;
				assertEquals(EventType.REMOVE, e.getType());
				assertEquals(DocumentAdapterTests.this.documentAdapter, e.getDocument());
				// this will be the removal of "23456"
				assertEquals(2, e.getOffset());
				assertEquals(5, e.getLength());
			}
		});
		this.documentAdapter.remove(2, 5);
		assertTrue(this.eventFired);
		assertEquals("01789", this.stringHolder.getValue());
	}

	public void testInsert() throws Exception {
		this.eventFired = false;
		this.documentAdapter.addDocumentListener(new TestDocumentListener() {
			@Override
			public void insertUpdate(DocumentEvent e) {
				DocumentAdapterTests.this.eventFired = true;
				assertEquals(EventType.INSERT, e.getType());
				assertEquals(DocumentAdapterTests.this.documentAdapter, e.getDocument());
				// this will be the insert of "xxxxxx"
				assertEquals(2, e.getOffset());
				assertEquals(5, e.getLength());
			}
		});
		this.documentAdapter.insertString(2, "xxxxx", null);
		assertTrue(this.eventFired);
		assertEquals("01xxxxx23456789", this.stringHolder.getValue());
	}

	public void testSetValue() throws Exception {
		this.eventFired = false;
		this.documentAdapter.addDocumentListener(new TestDocumentListener() {
			@Override
			public void insertUpdate(DocumentEvent e) {
				DocumentAdapterTests.this.eventFired = true;
				assertEquals(EventType.INSERT, e.getType());
				assertEquals(DocumentAdapterTests.this.documentAdapter, e.getDocument());
				// this will be the insert of "foo"
				assertEquals(0, e.getOffset());
				assertEquals(3, e.getLength());
			}
			@Override
			public void removeUpdate(DocumentEvent e) {
				assertEquals(EventType.REMOVE, e.getType());
				assertEquals(DocumentAdapterTests.this.documentAdapter, e.getDocument());
				// this will be the removal of "0123456789"
				assertEquals(0, e.getOffset());
				assertEquals(10, e.getLength());
			}
		});
		assertEquals("0123456789", this.documentAdapter.getText(0, this.documentAdapter.getLength()));
		this.stringHolder.setValue("foo");
		assertTrue(this.eventFired);
		assertEquals("foo", this.documentAdapter.getText(0, this.documentAdapter.getLength()));
	}

	public void testHasListeners() throws Exception {
		SimplePropertyValueModel<String> localStringHolder = (SimplePropertyValueModel<String>) this.stringHolder;
		assertFalse(localStringHolder.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.verifyHasNoListeners(this.documentAdapter);

		DocumentListener listener = new TestDocumentListener();
		this.documentAdapter.addDocumentListener(listener);
		assertTrue(localStringHolder.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.verifyHasListeners(this.documentAdapter);

		this.documentAdapter.removeDocumentListener(listener);
		assertFalse(localStringHolder.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		this.verifyHasNoListeners(this.documentAdapter);
	}

	private void verifyHasNoListeners(Object document) throws Exception {
		Object delegate = ReflectionTools.getFieldValue(document, "delegate");
		Object[] listeners = (Object[]) ReflectionTools.executeMethod(delegate, "getDocumentListeners");
		assertEquals(0, listeners.length);
	}

	private void verifyHasListeners(Object document) throws Exception {
		Object delegate = ReflectionTools.getFieldValue(document, "delegate");
		Object[] listeners = (Object[]) ReflectionTools.executeMethod(delegate, "getDocumentListeners");
		assertFalse(listeners.length == 0);
	}


private class TestDocumentListener implements DocumentListener {
	TestDocumentListener() {
		super();
	}
	public void changedUpdate(DocumentEvent e) {
		fail("unexpected event");
	}
	public void insertUpdate(DocumentEvent e) {
		fail("unexpected event");
	}
	public void removeUpdate(DocumentEvent e) {
		fail("unexpected event");
	}
}

}

Back to the top