Source Code Cross Referenced for AbstractDocument_ListenerTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Alexey A. Ivanov
019:         * @version $Revision$
020:         */package javax.swing.text;
021:
022:        import java.util.EventListener;
023:        import javax.swing.event.DocumentEvent;
024:        import javax.swing.event.DocumentListener;
025:        import javax.swing.event.UndoableEditEvent;
026:        import javax.swing.event.UndoableEditListener;
027:        import javax.swing.text.AbstractDocumentTest.DisAbstractedDocument;
028:        import javax.swing.undo.AbstractUndoableEdit;
029:        import javax.swing.undo.UndoableEdit;
030:        import junit.framework.TestCase;
031:
032:        public class AbstractDocument_ListenerTest extends TestCase implements 
033:                DocumentListener, UndoableEditListener {
034:            private AbstractDocument doc;
035:
036:            DocumentEvent change;
037:
038:            DocumentEvent insert;
039:
040:            DocumentEvent remove;
041:
042:            UndoableEditEvent undo;
043:
044:            /**
045:             * Initializes fixture for tests.
046:             */
047:            @Override
048:            protected void setUp() throws Exception {
049:                super .setUp();
050:                doc = new DisAbstractedDocument(new GapContent());
051:                change = null;
052:                insert = null;
053:                remove = null;
054:                undo = null;
055:            }
056:
057:            /**
058:             * Merely saves the event in change field.
059:             */
060:            public void changedUpdate(final DocumentEvent event) {
061:                change = event;
062:            }
063:
064:            /**
065:             * Merely saves the event in insert field.
066:             */
067:            public void insertUpdate(final DocumentEvent event) {
068:                insert = event;
069:            }
070:
071:            /**
072:             * Merely saves the event in remove field.
073:             */
074:            public void removeUpdate(final DocumentEvent event) {
075:                remove = event;
076:            }
077:
078:            /**
079:             * Merely saves the event in undo field.
080:             */
081:            public void undoableEditHappened(final UndoableEditEvent event) {
082:                undo = event;
083:            }
084:
085:            /**
086:             * Checks that the fields, for which array item is true, are not
087:             * null (i.e. listener has been called).
088:             * Array elements correspond to fields in this order:
089:             * change, insert, remove, undo.
090:             *
091:             * @param state states of fields
092:             */
093:            private void checkCalledEvents(final boolean[] state) {
094:                if (state[0]) {
095:                    assertNotNull("change IS null", change);
096:                } else {
097:                    assertNull("change IS NOT null", change);
098:                }
099:                if (state[1]) {
100:                    assertNotNull("insert IS null", insert);
101:                } else {
102:                    assertNull("insert IS NOT null", insert);
103:                }
104:                if (state[2]) {
105:                    assertNotNull("remove IS null", remove);
106:                } else {
107:                    assertNull("remove IS NOT null", remove);
108:                }
109:                if (state[3]) {
110:                    assertNotNull("undo IS null", undo);
111:                } else {
112:                    assertNull("undo IS NOT null", undo);
113:                }
114:            }
115:
116:            /**
117:             * Helper method which constructs the array before calling its
118:             * counterpart to perform real checking.
119:             *
120:             * @param change true if changeUpdate is supposed to be called
121:             * @param insert true if insertUpdate is supposed to be called
122:             * @param remove true if insertUpdate is supposed to be called
123:             * @param undo   true if undoableEditHapped is supposed to be called
124:             */
125:            private void checkCalledEvents(final boolean change,
126:                    final boolean insert, final boolean remove,
127:                    final boolean undo) {
128:                checkCalledEvents(new boolean[] { change, insert, remove, undo });
129:            }
130:
131:            public void testAddDocumentListener() throws BadLocationException {
132:                doc.insertString(0, "text", null);
133:                checkCalledEvents(false, false, false, false);
134:                doc.addDocumentListener(this );
135:                doc.insertString(0, "test", null);
136:                checkCalledEvents(false, true, false, false);
137:                insert = null;
138:                doc.remove(0, 4);
139:                checkCalledEvents(false, false, true, false);
140:                remove = null;
141:                doc.replace(2, 1, "s", null);
142:                checkCalledEvents(false, true, true, false);
143:                assertNotSame(insert, remove);
144:            }
145:
146:            public void testRemoveDocumentListener()
147:                    throws BadLocationException {
148:                doc.addDocumentListener(this );
149:                doc.insertString(0, "text", null);
150:                checkCalledEvents(false, true, false, false);
151:                doc.removeDocumentListener(this );
152:                insert = null;
153:                doc.insertString(0, "test", null);
154:                checkCalledEvents(false, false, false, false);
155:                insert = null;
156:                doc.remove(0, 4);
157:                checkCalledEvents(false, false, false, false);
158:                remove = null;
159:                doc.replace(2, 1, "s", null);
160:                checkCalledEvents(false, false, false, false);
161:            }
162:
163:            static final DocumentListener docListener = new DocumentListener() {
164:                public void changedUpdate(final DocumentEvent event) {
165:                }
166:
167:                public void insertUpdate(final DocumentEvent event) {
168:                }
169:
170:                public void removeUpdate(final DocumentEvent event) {
171:                }
172:            };
173:
174:            static final UndoableEditListener undoListener = new UndoableEditListener() {
175:                public void undoableEditHappened(final UndoableEditEvent event) {
176:                }
177:            };
178:
179:            public void testGetDocumentListeners() {
180:                doc.addDocumentListener(this );
181:                doc.addDocumentListener(docListener);
182:                doc.addUndoableEditListener(undoListener);
183:                DocumentListener[] listeners = doc.getDocumentListeners();
184:                assertEquals(2, listeners.length);
185:            }
186:
187:            public void testGetListeners() {
188:                doc.addDocumentListener(this );
189:                doc.addDocumentListener(docListener);
190:                doc.addUndoableEditListener(undoListener);
191:                EventListener[] listeners;
192:                listeners = doc.getListeners(DocumentListener.class);
193:                assertEquals(2, listeners.length);
194:                listeners = doc.getListeners(UndoableEditListener.class);
195:                assertEquals(1, listeners.length);
196:            }
197:
198:            public void testGetUndoableEditListeners() {
199:                doc.addDocumentListener(this );
200:                doc.addDocumentListener(docListener);
201:                doc.addUndoableEditListener(undoListener);
202:                UndoableEditListener[] listeners = doc
203:                        .getUndoableEditListeners();
204:                assertEquals(1, listeners.length);
205:            }
206:
207:            public void testAddUndoableEditListener()
208:                    throws BadLocationException {
209:                doc.insertString(0, "text", null);
210:                checkCalledEvents(false, false, false, false);
211:                doc.addUndoableEditListener(this );
212:                doc.insertString(0, "test", null);
213:                checkCalledEvents(false, false, false, true);
214:                undo = null;
215:                doc.remove(0, 4);
216:                checkCalledEvents(false, false, false, true);
217:                undo = null;
218:                doc.replace(2, 1, "s", null);
219:                checkCalledEvents(false, false, false, true);
220:            }
221:
222:            public void testRemoveUndoableEditListener()
223:                    throws BadLocationException {
224:                doc.addUndoableEditListener(this );
225:                doc.insertString(0, "text", null);
226:                checkCalledEvents(false, false, false, true);
227:                doc.removeUndoableEditListener(this );
228:                undo = null;
229:                doc.insertString(0, "test", null);
230:                checkCalledEvents(false, false, false, false);
231:                undo = null;
232:                doc.remove(0, 4);
233:                checkCalledEvents(false, false, false, false);
234:                undo = null;
235:                doc.replace(2, 1, "s", null);
236:                checkCalledEvents(false, false, false, false);
237:            }
238:
239:            private static DocumentEvent docEvent = new DocumentEvent() {
240:                public int getLength() {
241:                    return 0;
242:                }
243:
244:                public int getOffset() {
245:                    return 0;
246:                }
247:
248:                public EventType getType() {
249:                    return null;
250:                }
251:
252:                public Document getDocument() {
253:                    return null;
254:                }
255:
256:                public ElementChange getChange(final Element element) {
257:                    return null;
258:                }
259:            };
260:
261:            public void testFireUndoableEditUpdate() {
262:                UndoableEditEvent undoEvent = new UndoableEditEvent(doc,
263:                        new AbstractUndoableEdit());
264:                doc.addUndoableEditListener(this );
265:                doc.fireUndoableEditUpdate(undoEvent);
266:                checkCalledEvents(false, false, false, true);
267:                assertSame(undoEvent, undo);
268:            }
269:
270:            public void testFireRemoveUpdate() {
271:                doc.addDocumentListener(this );
272:                doc.fireRemoveUpdate(docEvent);
273:                checkCalledEvents(false, false, true, false);
274:                assertSame(docEvent, remove);
275:            }
276:
277:            public void testFireInsertUpdate() {
278:                doc.addDocumentListener(this );
279:                doc.fireInsertUpdate(docEvent);
280:                checkCalledEvents(false, true, false, false);
281:                assertSame(docEvent, insert);
282:            }
283:
284:            public void testFireChangedUpdate() {
285:                doc.addDocumentListener(this );
286:                doc.fireChangedUpdate(docEvent);
287:                checkCalledEvents(true, false, false, false);
288:                assertSame(docEvent, change);
289:            }
290:
291:            /**
292:             * Tests if listeners get called when inserting empty/null string and
293:             * when removing zero-length text.
294:             */
295:            public void testInsertString01() throws BadLocationException {
296:                doc.addDocumentListener(this );
297:                doc.addUndoableEditListener(this );
298:                doc.insertString(0, "", null);
299:                checkCalledEvents(false, false, false, false);
300:                doc.insertString(0, null, null);
301:                checkCalledEvents(false, false, false, false);
302:                doc.remove(0, 0);
303:                checkCalledEvents(false, false, false, false);
304:            }
305:
306:            private static class NoUndoContent extends GapContent {
307:                private static final long serialVersionUID = 1L;
308:
309:                @Override
310:                public UndoableEdit insertString(int offset, String str)
311:                        throws BadLocationException {
312:                    super .insertString(offset, str);
313:                    return null;
314:                }
315:
316:                @Override
317:                public UndoableEdit remove(int offset, int length)
318:                        throws BadLocationException {
319:                    super .remove(offset, length);
320:                    return null;
321:                }
322:            }
323:
324:            /**
325:             * Tests which events are fired at text insert
326:             * when content doesn't support undo.
327:             */
328:            public void testInsertString02() throws Exception {
329:                doc = new DisAbstractedDocument(new NoUndoContent());
330:                doc.addDocumentListener(this );
331:                doc.addUndoableEditListener(this );
332:                doc.insertString(0, "test string\nthe second line", null);
333:                checkCalledEvents(false, true, false, false);
334:            }
335:
336:            /**
337:             * Tests which events are fired at RTL-text insert
338:             * when content doesn't support undo.
339:             */
340:            public void testInsertString03() throws Exception {
341:                doc = new DisAbstractedDocument(new NoUndoContent());
342:                doc.addDocumentListener(this );
343:                doc.addUndoableEditListener(this );
344:                doc.insertString(0, "\u05DC\u05DD", null);
345:                checkCalledEvents(false, true, false, false);
346:                assertNotNull(insert.getChange(doc.getBidiRootElement()));
347:            }
348:
349:            /**
350:             * Tests which events are fired at text remove
351:             * when content doesn't support undo.
352:             */
353:            public void testRemove01() throws Exception {
354:                doc = new DisAbstractedDocument(new NoUndoContent());
355:                doc.insertString(0, "test string\nthe second line", null);
356:                doc.addDocumentListener(this );
357:                doc.addUndoableEditListener(this );
358:                doc.remove(0, 4);
359:                checkCalledEvents(false, false, true, false);
360:            }
361:
362:            /**
363:             * Tests which events are fired at RTL-text remove
364:             * when content doesn't support undo.
365:             */
366:            public void testRemove02() throws Exception {
367:                doc = new DisAbstractedDocument(new NoUndoContent());
368:                doc.insertString(0, "\u05DC\u05DD test string", null);
369:                doc.addDocumentListener(this );
370:                doc.addUndoableEditListener(this );
371:                doc.remove(0, 2);
372:                checkCalledEvents(false, false, true, false);
373:                assertNotNull(remove.getChange(doc.getBidiRootElement()));
374:            }
375:
376:            /**
377:             * Tests which events are fired when content support undo for
378:             * remove but doesn't support one for insert.
379:             */
380:            public void testInsertRemove() throws Exception {
381:                doc = new DisAbstractedDocument(new GapContent() {
382:                    private static final long serialVersionUID = 1L;
383:
384:                    @Override
385:                    public UndoableEdit insertString(int where, String str)
386:                            throws BadLocationException {
387:                        super .insertString(where, str);
388:                        return null;
389:                    }
390:                });
391:                doc.addDocumentListener(this );
392:                doc.addUndoableEditListener(this );
393:                doc.insertString(0, "text", null);
394:                checkCalledEvents(false, true, false, false);
395:                insert = null;
396:                doc.remove(0, 2);
397:                checkCalledEvents(false, false, true, true);
398:            }
399:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.