Source Code Cross Referenced for DefaultStyledDocument_CreateInsertTest.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.List;
023:        import javax.swing.event.DocumentEvent;
024:        import javax.swing.event.DocumentListener;
025:        import javax.swing.event.DocumentEvent.EventType;
026:        import javax.swing.text.AbstractDocument.AbstractElement;
027:        import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
028:        import javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit;
029:        import javax.swing.text.DefaultStyledDocument.ElementBuffer;
030:        import javax.swing.text.DefaultStyledDocument.ElementSpec;
031:        import junit.framework.TestCase;
032:
033:        /**
034:         * Tests <code>create()</code> and <code>insert()</code> methods of
035:         * DefaultStyledDocument.
036:         *
037:         */
038:        public class DefaultStyledDocument_CreateInsertTest extends TestCase
039:                implements  DocumentListener {
040:            private DefaultStyledDocument doc;
041:
042:            private ElementBuffer buf;
043:
044:            private Element root;
045:
046:            private DocumentEvent event;
047:
048:            private static final String plainText = "plain";
049:
050:            private static final String boldText = "bold";
051:
052:            private static final String italicText = "italic";
053:
054:            private static final String newLine = "\n";
055:
056:            private static final String paragraph2Text = "line\n";
057:
058:            private static final AttributeSet bold = DefStyledDoc_Helpers.bold;
059:
060:            private static final AttributeSet italic = DefStyledDoc_Helpers.italic;
061:
062:            @Override
063:            protected void setUp() throws Exception {
064:                super .setUp();
065:                doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
066:                root = doc.getDefaultRootElement();
067:                buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
068:                        root);
069:                doc.buffer = buf;
070:            }
071:
072:            /**
073:             * Calls <code>create()</code> on empty document.
074:             * The attributes of the default root modified before the call to make
075:             * sure they are reset on call.
076:             */
077:            public void testCreateWithoutText() throws BadLocationException {
078:                final ElementSpec[] specs = {
079:                        new ElementSpec(null, ElementSpec.StartTagType),
080:                        new ElementSpec(null, ElementSpec.StartTagType),
081:                        new ElementSpec(null, ElementSpec.ContentType,
082:                                plainText.toCharArray(), 0, plainText.length()),
083:                        new ElementSpec(bold, ElementSpec.ContentType, boldText
084:                                .toCharArray(), 0, boldText.length()),
085:                        new ElementSpec(italic, ElementSpec.ContentType,
086:                                italicText.toCharArray(), 0, italicText
087:                                        .length()),
088:                        new ElementSpec(null, ElementSpec.ContentType, newLine
089:                                .toCharArray(), 0, newLine.length()),
090:                        new ElementSpec(null, ElementSpec.EndTagType),
091:                        new ElementSpec(null, ElementSpec.StartTagType),
092:                        new ElementSpec(null, ElementSpec.ContentType,
093:                                paragraph2Text.toCharArray(), 0, paragraph2Text
094:                                        .length()),
095:                        new ElementSpec(null, ElementSpec.EndTagType), };
096:                specs[0].setDirection(ElementSpec.JoinPreviousDirection);
097:                // Add an attribute to the root
098:                doc.writeLock();
099:                try {
100:                    ((AbstractElement) root).addAttributes(bold);
101:                } finally {
102:                    doc.writeUnlock();
103:                }
104:                doc.addDocumentListener(this );
105:                assertNull(event);
106:                doc.create(specs);
107:                assertNotNull(event);
108:                assertEquals(0, event.getOffset());
109:                assertEquals(plainText.length() + boldText.length()
110:                        + italicText.length() + newLine.length()
111:                        + paragraph2Text.length(), event.getLength());
112:                assertSame(EventType.INSERT, event.getType());
113:                assertChildren(root.getElement(0), plainText + boldText
114:                        + italicText + newLine, new int[] { 0, 5, 5, 9, 9, 15,
115:                        15, 16 },
116:                        new AttributeSet[] { null, bold, italic, null });
117:                assertChildren(root.getElement(1), paragraph2Text, new int[] {
118:                        16, 21 }, new AttributeSet[] { null });
119:                assertEquals(0, root.getElement(1).getAttributes()
120:                        .getAttributeCount());
121:                assertChildren(root.getElement(2), newLine,
122:                        new int[] { 21, 22 }, new AttributeSet[] { null });
123:                final List<?> edits = DefStyledDoc_Helpers
124:                        .getEdits((DefaultDocumentEvent) event);
125:                assertEquals(6, edits.size());
126:                final AttributeUndoableEdit undo = (AttributeUndoableEdit) edits
127:                        .get(1);
128:                assertSame(root, undo.element);
129:                assertTrue(undo.copy.isEqual(bold));
130:                assertEquals(0, undo.newAttributes.getAttributeCount());
131:                assertTrue(undo.isReplacing);
132:                assertChange(edits.get(2), root.getElement(root
133:                        .getElementCount() - 1), 0, new int[] { 0, 22 },
134:                        new int[] { 21, 22 });
135:                assertChange(edits.get(3), root.getElement(0), 0, new int[] {},
136:                        new int[] { 0, 5, 5, 9, 9, 15, 15, 16 });
137:                assertChange(edits.get(4), root.getElement(1), 0, new int[] {},
138:                        new int[] { 16, 21 });
139:                assertChange(edits.get(5), root, 0, new int[] {}, new int[] {
140:                        0, 16, 16, 21 });
141:            }
142:
143:            /**
144:             * Calls <code>create</code> on non-empty document.
145:             * This asserts the document contents is removed before applying
146:             * new structure.
147:             */
148:            public void testCreateWithText() throws BadLocationException {
149:                doc.insertString(0, "some text", bold);
150:                doc.insertString(doc.getLength(), "\nmore text", italic);
151:                final ElementSpec[] specs = {
152:                        new ElementSpec(null, ElementSpec.StartTagType),
153:                        new ElementSpec(null, ElementSpec.StartTagType),
154:                        new ElementSpec(null, ElementSpec.ContentType,
155:                                plainText.toCharArray(), 0, plainText.length()),
156:                        new ElementSpec(bold, ElementSpec.ContentType, boldText
157:                                .toCharArray(), 0, boldText.length()),
158:                        new ElementSpec(italic, ElementSpec.ContentType,
159:                                italicText.toCharArray(), 0, italicText
160:                                        .length()),
161:                        new ElementSpec(null, ElementSpec.ContentType, newLine
162:                                .toCharArray(), 0, newLine.length()),
163:                        new ElementSpec(null, ElementSpec.EndTagType),
164:                        new ElementSpec(null, ElementSpec.StartTagType),
165:                        new ElementSpec(null, ElementSpec.ContentType,
166:                                paragraph2Text.toCharArray(), 0, paragraph2Text
167:                                        .length()),
168:                        new ElementSpec(null, ElementSpec.EndTagType), };
169:                doc.addDocumentListener(this );
170:                assertNull(event);
171:                doc.create(specs);
172:                assertNotNull(event);
173:                assertEquals(0, event.getOffset());
174:                assertEquals(plainText.length() + boldText.length()
175:                        + italicText.length() + newLine.length()
176:                        + paragraph2Text.length(), event.getLength());
177:                assertSame(EventType.INSERT, event.getType());
178:                assertChildren(root.getElement(0), plainText + boldText
179:                        + italicText + newLine, new int[] { 0, 5, 5, 9, 9, 15,
180:                        15, 16 },
181:                        new AttributeSet[] { null, bold, italic, null });
182:                assertChildren(root.getElement(1), paragraph2Text, new int[] {
183:                        16, 21 }, new AttributeSet[] { null });
184:                assertEquals(0, root.getElement(1).getAttributes()
185:                        .getAttributeCount());
186:                assertChildren(root.getElement(2), newLine,
187:                        new int[] { 21, 22 }, new AttributeSet[] { null });
188:                final List<?> edits = DefStyledDoc_Helpers
189:                        .getEdits((DefaultDocumentEvent) event);
190:                assertEquals(6, edits.size());
191:                assertChange(edits.get(2), root.getElement(root
192:                        .getElementCount() - 1), 0, new int[] { 0, 22 },
193:                        new int[] { 21, 22 });
194:                assertChange(edits.get(3), root.getElement(0), 0, new int[] {},
195:                        new int[] { 0, 5, 5, 9, 9, 15, 15, 16 });
196:                assertChange(edits.get(4), root.getElement(1), 0, new int[] {},
197:                        new int[] { 16, 21 });
198:                assertChange(edits.get(5), root, 0, new int[] {}, new int[] {
199:                        0, 16, 16, 21 });
200:            }
201:
202:            public void testInsertWithStartTag() throws BadLocationException {
203:                doc.insertString(0, "some  text", bold);
204:                doc.insertString(doc.getLength(), "\nmore text", italic);
205:                ElementSpec[] specs = {
206:                        new ElementSpec(null, ElementSpec.ContentType,
207:                                plainText.toCharArray(), 0, plainText.length()),
208:                        new ElementSpec(bold, ElementSpec.ContentType, boldText
209:                                .toCharArray(), 0, boldText.length()),
210:                        new ElementSpec(italic, ElementSpec.ContentType,
211:                                italicText.toCharArray(), 0, italicText
212:                                        .length()),
213:                        new ElementSpec(null, ElementSpec.ContentType, newLine
214:                                .toCharArray(), 0, newLine.length()),
215:                        new ElementSpec(null, ElementSpec.EndTagType),
216:                        new ElementSpec(null, ElementSpec.StartTagType),
217:                        new ElementSpec(null, ElementSpec.ContentType,
218:                                paragraph2Text.toCharArray(), 0, paragraph2Text
219:                                        .length()),
220:                        //It is mandatory for our implementation.
221:                        new ElementSpec(null, ElementSpec.EndTagType),
222:                        new ElementSpec(null, ElementSpec.StartTagType) };
223:                specs[specs.length - 1]
224:                        .setDirection(ElementSpec.JoinFractureDirection);
225:                doc.addDocumentListener(this );
226:                assertNull(event);
227:                doc.insert(5, specs);
228:                assertNotNull(event);
229:                assertEquals(5, event.getOffset());
230:                assertEquals(plainText.length() + boldText.length()
231:                        + italicText.length() + newLine.length()
232:                        + paragraph2Text.length(), event.getLength());
233:                assertSame(EventType.INSERT, event.getType());
234:                assertChildren(root.getElement(0), "some " + plainText
235:                        + boldText + italicText + newLine, new int[] { 0, 5, 5,
236:                        10, 10, 14, 14, 20, 20, 21 }, new AttributeSet[] {
237:                        bold, null, bold, italic, null });
238:                assertChildren(root.getElement(1), paragraph2Text, new int[] {
239:                        21, 26 }, new AttributeSet[] { null });
240:                assertEquals(0, root.getElement(1).getAttributes()
241:                        .getAttributeCount());
242:                assertChildren(root.getElement(2), " text\n", new int[] { 26,
243:                        31, 31, 32 }, new AttributeSet[] { bold, italic });
244:                assertChildren(root.getElement(3), "more text\n", new int[] {
245:                        32, 41, 41, 42 }, new AttributeSet[] { italic, null });
246:            }
247:
248:            /**
249:             * Calls <code>insert()</code> on non-empty document.
250:             * The <code>ElementSpec</code>s applied contain no
251:             * <code>StartTagType</code> specs.
252:             */
253:            public void testInsertNoStartTags() throws BadLocationException {
254:                doc.insertString(0, "some  text", bold);
255:                doc.insertString(doc.getLength(), "\nmore text", italic);
256:                ElementSpec[] specs = {
257:                        new ElementSpec(null, ElementSpec.ContentType,
258:                                plainText.toCharArray(), 0, plainText.length()),
259:                        new ElementSpec(bold, ElementSpec.ContentType, boldText
260:                                .toCharArray(), 0, boldText.length()),
261:                        new ElementSpec(italic, ElementSpec.ContentType,
262:                                italicText.toCharArray(), 0, italicText
263:                                        .length()),
264:                        new ElementSpec(null, ElementSpec.ContentType, newLine
265:                                .toCharArray(), 0, newLine.length()),
266:                        new ElementSpec(null, ElementSpec.ContentType,
267:                                paragraph2Text.toCharArray(), 0, paragraph2Text
268:                                        .length()), };
269:                doc.addDocumentListener(this );
270:                assertNull(event);
271:                doc.insert(5, specs);
272:                assertNotNull(event);
273:                assertEquals(5, event.getOffset());
274:                assertEquals(plainText.length() + boldText.length()
275:                        + italicText.length() + newLine.length()
276:                        + paragraph2Text.length(), event.getLength());
277:                assertSame(EventType.INSERT, event.getType());
278:                assertChildren(root.getElement(0), "some " + plainText
279:                        + boldText + italicText + newLine + paragraph2Text
280:                        + " text\n", new int[] { 0, 5, 5, 10, 10, 14, 14, 20,
281:                        20, 21, 21, 26, 26, 31, 31, 32 }, new AttributeSet[] {
282:                        bold, null, bold, italic, null, null, bold, italic });
283:                assertChildren(root.getElement(1), "more text\n", new int[] {
284:                        32, 41, 41, 42 }, new AttributeSet[] { italic, null });
285:            }
286:
287:            private static void assertChange(final Object change,
288:                    final Element element, final int index,
289:                    final int[] removedOffsets, final int[] addedOffsets) {
290:                DefStyledDoc_Helpers.assertChange(change, element, index,
291:                        removedOffsets, addedOffsets);
292:            }
293:
294:            private static void assertChildren(final Element element,
295:                    final String text, final int[] offsets,
296:                    final AttributeSet[] attributes)
297:                    throws BadLocationException {
298:                DefStyledDoc_Helpers.assertChildren(element, offsets,
299:                        attributes);
300:                assertEquals("element.text " + element, text, getText(element));
301:            }
302:
303:            private static String getText(final Element element)
304:                    throws BadLocationException {
305:                final int start = element.getStartOffset();
306:                final int end = element.getEndOffset();
307:                return element.getDocument().getText(start, end - start);
308:            }
309:
310:            public void insertUpdate(DocumentEvent e) {
311:                event = e;
312:            }
313:
314:            public void removeUpdate(DocumentEvent e) {
315:            }
316:
317:            public void changedUpdate(DocumentEvent e) {
318:                fail("changedUpdate is not expected to be called");
319:            }
320:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.