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.io.ByteArrayOutputStream;
023: import java.io.PrintStream;
024: import java.util.Enumeration;
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.text.AbstractDocument.AbstractElement;
027: import junit.framework.TestCase;
028:
029: /**
030: * Tests AbstractDocument.AbstractElement class.
031: *
032: */
033: public class AbstractDocument_AbstractElementTest extends TestCase {
034: /**
035: * Shared document object.
036: */
037: protected static DisAbstractedDocument aDocument;
038:
039: /**
040: * Shared element object.
041: */
042: protected static DisAbstractedDocument.DisAbstractedElement aElement;
043:
044: /**
045: * Shared element with parent (= aElement).
046: */
047: protected static AbstractElement parented;
048:
049: /**
050: * Attribute set used to initialize <code>parented</code>.
051: */
052: protected static AttributeSet aSet;
053:
054: /**
055: * Implementor of abtract methods in AbstractDocument.
056: */
057: protected static class DisAbstractedDocument extends
058: AbstractDocument {
059: private static final long serialVersionUID = 1L;
060:
061: protected DisAbstractedDocument(final Content content) {
062: super (content);
063: }
064:
065: /**
066: * Implementor of abstract methods in AbstractElement.
067: *
068: */
069: class DisAbstractedElement extends
070: AbstractDocument.AbstractElement {
071: private static final long serialVersionUID = 1L;
072:
073: public DisAbstractedElement(final Element initParent,
074: final AttributeSet initAttributes) {
075: super (initParent, initAttributes);
076: }
077:
078: @Override
079: public Element getElement(final int a0) {
080: return null;
081: }
082:
083: @SuppressWarnings("unchecked")
084: @Override
085: public Enumeration<?> children() {
086: return null;
087: }
088:
089: @Override
090: public int getElementIndex(final int a0) {
091: return 0;
092: }
093:
094: @Override
095: public boolean isLeaf() {
096: return false;
097: }
098:
099: @Override
100: public boolean getAllowsChildren() {
101: return false;
102: }
103:
104: @Override
105: public int getStartOffset() {
106: return 0;
107: }
108:
109: @Override
110: public int getEndOffset() {
111: return 0;
112: }
113:
114: @Override
115: public int getElementCount() {
116: return 0;
117: }
118: }
119:
120: @Override
121: public Element getParagraphElement(final int a0) {
122: return null;
123: }
124:
125: @Override
126: public Element getDefaultRootElement() {
127: return null;
128: }
129: };
130:
131: @Override
132: protected void setUp() throws Exception {
133: super .setUp();
134: init();
135: aDocument.writeLock();
136: }
137:
138: @Override
139: protected void tearDown() throws Exception {
140: super .tearDown();
141: aDocument.writeUnlock();
142: }
143:
144: /**
145: * Helper method used from <code>setUp</code> to initialize
146: * shared objects which are declared static.
147: */
148: protected static void init() {
149: StyleContextTest.sc = StyleContext.getDefaultStyleContext();
150: aSet = StyleContextTest.addAttribute(null, 5, 3);
151: aDocument = new DisAbstractedDocument(new GapContent());
152: aDocument.writeLock();
153: aElement = aDocument.new DisAbstractedElement(null,
154: StyleContextTest.addAttribute(2));
155: parented = aDocument.new DisAbstractedElement(aElement, aSet);
156: aDocument.writeUnlock();
157: }
158:
159: /**
160: * Simple test of constructor.
161: */
162: public void testAbstractElement01() {
163: assertSame(aElement, parented.getParentElement());
164: assertTrue(parented.containsAttributes(aSet));
165: assertSame(aElement, parented.getResolveParent());
166: // Test with null attribute set
167: aElement = aDocument.new DisAbstractedElement(null, null);
168: assertEquals(0, aElement.getAttributeCount());
169: // Following assertion fails, the reason being Element contains method
170: // doesn't take into account parent element
171: //assertTrue(parented.containsAttributes(aElement));
172: }
173:
174: /**
175: * Test non-null attributes argument when there's no writeLock.
176: */
177: public void testAbstractElement02() {
178: aDocument.writeUnlock();
179: // When attributes are null, the exception isn't thrown
180: aElement = aDocument.new DisAbstractedElement(null, null);
181: try {
182: // When attributes are not null, though empty, the exception
183: // is thrown.
184: aElement = aDocument.new DisAbstractedElement(null,
185: StyleContext.getDefaultStyleContext().getEmptySet());
186: fail("Error 'Illegal cast to MutableAttributeSet' "
187: + "should be thrown");
188: } catch (Error e) {
189: // Do nothing
190: } finally {
191: // Lock the document again, so there'll be no error when
192: // unlocking from tearDown
193: aDocument.writeLock();
194: }
195: }
196:
197: public void testCopyAttributes() {
198: AttributeSet copy = aElement.copyAttributes();
199: assertNotSame(copy, aElement);
200: assertFalse(copy instanceof Element);
201: assertNull(aElement.getParentElement());
202: assertNull(copy.getResolveParent());
203: assertNull(aElement.getResolveParent());
204: assertTrue(copy.isEqual(aElement));
205: copy = parented.copyAttributes();
206: assertNotSame(copy, parented);
207: assertFalse(copy instanceof Element);
208: assertNotNull(parented.getParentElement());
209: assertNull(copy.getResolveParent());
210: assertNotNull(parented.getResolveParent());
211: assertTrue(copy.isEqual(parented));
212: }
213:
214: public void testGetParentElement() {
215: assertNull(aElement.getParentElement());
216: assertNotNull(parented.getParentElement());
217: assertSame(aElement, parented.getParentElement());
218: }
219:
220: public void testGetDocument() {
221: assertSame(aDocument, aElement.getDocument());
222: }
223:
224: public void testGetName() {
225: assertNull(aElement.getName());
226: assertNull(parented.getName());
227: }
228:
229: public void testDump() throws BadLocationException {
230: String dump1 = "<bidi root>\n" + " <bidi level\n"
231: + " bidiLevel=0\n" + " >\n" + " [0,6][01234\n"
232: + "]\n" + "<content>\n" + " [0,3][012]\n";
233: String dump2 = "<bidi root>\n" + " <bidi level\n"
234: + " bidiLevel=0\n" + " >\n" + " [0,6][01234\n"
235: + "]\n" + "<content>\n" + " [0,3][012]\n";
236: ByteArrayOutputStream out = new ByteArrayOutputStream();
237: PrintStream ps = new PrintStream(out);
238: aDocument.insertString(0, "01234", null);
239: ((AbstractElement) aDocument.getBidiRootElement()).dump(ps, 0);
240: aDocument.new LeafElement(null, null, 0, 3).dump(ps, 0);
241: String dump = BasicSwingTestCase.isHarmony() ? dump2 : dump1;
242: assertEquals(dump, AbstractDocumentTest.filterNewLines(out
243: .toString()));
244: }
245:
246: private static final String dumpTextAtLimit = "123456789\n" + // 10 chars / 10
247: "123456789\n" + // 10 chars / 20
248: "123456789\n" + // 10 chars / 30
249: "aaabbbccc"; // 9 chars / 39 + 1 def '\n'
250:
251: private static final String dumpTextBeyondLimit = "123456789\n" + // 10 chars / 10
252: "123456789\n" + // 10 chars / 20
253: "123456789\n" + // 10 chars / 30
254: "aaabbbccc1234567890"; // 19 chars / 49 + 1 def '\n'
255:
256: public void testDumpAtTextLimit() throws BadLocationException {
257: String dump = "<bidi root>\n" + " <bidi level\n"
258: + " bidiLevel=0\n" + " >\n"
259: + " [0,40][123456789\n" + "123456789\n"
260: + "123456789\n" + "aaabbbccc\n" + "]\n";
261: ByteArrayOutputStream out = new ByteArrayOutputStream();
262: PrintStream ps = new PrintStream(out);
263: aDocument.insertString(0, dumpTextAtLimit, null);
264: ((AbstractElement) aDocument.getBidiRootElement()).dump(ps, 0);
265: assertEquals(dump, AbstractDocumentTest.filterNewLines(out
266: .toString()));
267: }
268:
269: public void testDumpBeyondTextLimit() throws BadLocationException {
270: String dump = "<bidi root>\n" + " <bidi level\n"
271: + " bidiLevel=0\n" + " >\n"
272: + " [0,50][123456789\n" + "123456789\n"
273: + "123456789\n" + "aaabbbccc1...]\n";
274: ByteArrayOutputStream out = new ByteArrayOutputStream();
275: PrintStream ps = new PrintStream(out);
276: aDocument.insertString(0, dumpTextBeyondLimit, null);
277: ((AbstractElement) aDocument.getBidiRootElement()).dump(ps, 0);
278: assertEquals(dump, AbstractDocumentTest.filterNewLines(out
279: .toString()));
280: }
281:
282: public void testGetAttributeThruParent() {
283: assertFalse(parented.isDefined(StyleContextTest.attr[0]));
284: assertTrue(aElement.isDefined(StyleContextTest.attr[0]));
285: // containsAttribute method DOES NOT look up attributes in parent
286: assertFalse(parented.containsAttribute(
287: StyleContextTest.attr[1], parented
288: .getAttribute(StyleContextTest.attr[0])));
289: // getAttribute DOES look up attributes in parent
290: assertSame(StyleContextTest.attr[1], parented
291: .getAttribute(StyleContextTest.attr[0]));
292: }
293:
294: public void testGetResolveParent() {
295: assertNull(aElement.getResolveParent());
296: assertSame(aElement, parented.getResolveParent());
297: }
298:
299: public void testGetResolveParentExplicit() throws Exception {
300: AttributeSet resolver = new SimpleAttributeSet();
301: parented.setResolveParent(resolver);
302: assertSame(resolver, parented.getResolveParent());
303: assertSame(aElement, parented.getParentElement());
304: }
305:
306: protected static class OtherElement implements Element {
307: public static final String key = "OE_Key";
308:
309: public static final String value = "OE_Value";
310:
311: private MutableAttributeSet attrs = new SimpleAttributeSet();
312: {
313: attrs.addAttribute(key, value);
314: }
315:
316: public boolean isAttrsNull;
317:
318: public AttributeSet getAttributes() {
319: return isAttrsNull ? null : attrs;
320: }
321:
322: public Document getDocument() {
323: return null;
324: }
325:
326: public Element getElement(int index) {
327: return null;
328: }
329:
330: public int getElementCount() {
331: return 0;
332: }
333:
334: public int getElementIndex(int offset) {
335: return 0;
336: }
337:
338: public int getEndOffset() {
339: return 0;
340: }
341:
342: public String getName() {
343: return null;
344: }
345:
346: public Element getParentElement() {
347: return null;
348: }
349:
350: public int getStartOffset() {
351: return 0;
352: }
353:
354: public boolean isLeaf() {
355: return true;
356: }
357: }
358:
359: public void testGetAttributesOtherParent() throws Exception {
360: OtherElement parent = new OtherElement();
361: parented = aDocument.new DisAbstractedElement(parent, null);
362: assertSame(OtherElement.value, parented
363: .getAttribute(OtherElement.key));
364: assertFalse(parented.isDefined(OtherElement.key));
365: parent.isAttrsNull = true;
366: assertNull(parented.getAttribute(OtherElement.key));
367: }
368:
369: public void testGetResolveParentOther() throws Exception {
370: OtherElement parent = new OtherElement();
371: parented = aDocument.new DisAbstractedElement(parent, null);
372: assertNotSame(parent, parent.getAttributes());
373: assertSame(parent.getAttributes(), parented.getResolveParent());
374: parent.isAttrsNull = true;
375: assertNull(parented.getResolveParent());
376: }
377: // These are tests for abstract methods
378: /*
379: public void testIsLeaf() {
380:
381: }
382:
383: public void testChildren() {
384:
385: }
386:
387: public void testGetAllowsChildren() {
388:
389: }
390:
391: public void testGetStartOffset() {
392:
393: }
394:
395: public void testGetEndOffset() {
396:
397: }
398:
399: public void testGetElementCount() {
400:
401: }
402: */
403: }
|