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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.GridLayout;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener; /*
029: import java.io.FileInputStream;
030: import java.io.FileOutputStream;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033: */
034: import javax.accessibility.AccessibleContext;
035: import javax.accessibility.AccessibleState;
036: import javax.accessibility.AccessibleStateSet;
037: import javax.swing.text.AbstractDocument;
038: import javax.swing.text.BadLocationException;
039: import javax.swing.text.Document;
040: import javax.swing.text.Element;
041: import javax.swing.text.PlainDocument;
042:
043: public class JTextAreaTest extends SwingTestCase {
044: JFrame jf;
045:
046: ExtJTextArea jta;
047:
048: JTextArea bidiJta;
049:
050: JTextArea jt;
051:
052: String sLTR = "aaaa";
053:
054: String sRTL = "\u05dc" + "\u05dc" + "\u05dc" + "\u05dc";
055:
056: String content = "Edison accumul\tator, Edison base: Edison battery"
057: + " Edison cap, \tEdison effect\n"
058: + "Edison screw, Edison screw cap, Edison screw \n"
059: + "holder, Edison screw lampholder, Edison screw "
060: + "plug\n"
061: + "Edison screw terminal, Edison storage battery"
062: + "Edison storage \t\tcell";
063:
064: String bidiContent = sLTR + sRTL + sRTL + " \t" + sLTR + sRTL
065: + sLTR + "\n" + sRTL + "." + sLTR + sRTL + "\t" + sRTL
066: + "\n" + sLTR + sLTR + sRTL + sRTL + sRTL + sLTR + sLTR
067: + sLTR + sRTL + sLTR + sRTL + sLTR;
068:
069: String str1 = "jazz band";
070:
071: String str2 = "jazz model";
072:
073: boolean bWasException;
074:
075: String message;
076:
077: SimplePropertyChangeListener listener;
078:
079: class SimplePropertyChangeListener implements
080: PropertyChangeListener {
081: PropertyChangeEvent event;
082:
083: public void propertyChange(final PropertyChangeEvent e) {
084: if (e.getPropertyName() != "ancestor") {
085: event = e;
086: }
087: }
088:
089: PropertyChangeEvent getEvent() {
090: PropertyChangeEvent e = event;
091: event = null;
092: return e;
093: }
094: }
095:
096: void assertEqualsPropertyChangeEvent(final String name,
097: final Object oldValue, final Object newValue,
098: final PropertyChangeEvent e) {
099: assertEquals(name, e.getPropertyName());
100: assertEquals(oldValue, e.getOldValue());
101: assertEquals(newValue, e.getNewValue());
102: }
103:
104: void isElement(final Object[] a, final Object b, final int count) {
105: assertNotNull(a);
106: boolean cond = false;
107: int k = 0;
108: for (int j = 0; j < a.length; j++) {
109: if (a[j] == b) {
110: cond = true;
111: k += 1;
112: }
113: }
114: assertTrue(cond);
115: assertEquals(count, k);
116: }
117:
118: @Override
119: protected void setUp() throws Exception {
120: super .setUp();
121: listener = new SimplePropertyChangeListener();
122: bWasException = false;
123: message = null;
124: jf = new JFrame();
125: bidiJta = new JTextArea(bidiContent);
126: jta = new ExtJTextArea(content);
127: jt = new JTextArea(bidiContent);
128: jta.addPropertyChangeListener(listener);
129: Container cont = jf.getContentPane();
130: cont.setLayout(new GridLayout(1, 2, 4, 4));
131: cont.add(jta);
132: cont.add(bidiJta);
133: jf.setLocation(200, 300);
134: jf.setSize(350, 400);
135: jf.pack();
136: }
137:
138: @Override
139: protected void tearDown() throws Exception {
140: jf.dispose();
141: super .tearDown();
142: }
143:
144: public void testGetScrollableTracksViewportWidth() throws Exception {
145: assertFalse(jta.getScrollableTracksViewportWidth());
146: jta.setLineWrap(true);
147: assertTrue(jta.getScrollableTracksViewportWidth());
148: }
149:
150: public void testJTextAreaDocumentStringintint() {
151: Document doc = new PlainDocument();
152: try {
153: doc.insertString(0, str2, null);
154: ExtJTextArea ta = new ExtJTextArea(doc, str1, 3, 8);
155: assertEquals(str1, ta.getText());
156: assertEquals(doc, ta.getDocument());
157: assertEquals(3, ta.getRows());
158: assertEquals(8, ta.getColumns());
159: assertFalse(ta.getLineWrap());
160: assertFalse(ta.getWrapStyleWord());
161: doc = new PlainDocument();
162: doc.insertString(0, str2, null);
163: ta = new ExtJTextArea(doc, null, 5, 6);
164: assertEquals(str2, ta.getText());
165: ta = new ExtJTextArea(doc, "", 5, 6);
166: assertEquals("", ta.getText());
167: try {
168: ta = new ExtJTextArea(doc, str1, -1, 4);
169: } catch (IllegalArgumentException e) {
170: bWasException = true;
171: message = e.getMessage();
172: }
173: assertTrue(bWasException);
174: assertEquals("rows: -1", message);
175: bWasException = false;
176: message = null;
177: try {
178: ta = new ExtJTextArea(doc, str2, 1, -3);
179: } catch (IllegalArgumentException e) {
180: bWasException = true;
181: message = e.getMessage();
182: }
183: assertTrue(bWasException);
184: assertEquals("columns: -3", message);
185: } catch (BadLocationException e) {
186: assertTrue("Unexpected exception: " + e.getMessage(), false);
187: }
188: }
189:
190: public void testJTextAreaDocument() {
191: Document doc = new PlainDocument();
192: try {
193: doc.insertString(0, str2, null);
194: } catch (BadLocationException e) {
195: assertTrue("Unexpected exception: " + e.getMessage(), false);
196: }
197: ExtJTextArea ta = new ExtJTextArea(doc);
198: assertEquals(str2, ta.getText());
199: assertEquals(doc, ta.getDocument());
200: assertEquals(0, ta.getRows());
201: assertEquals(0, ta.getColumns());
202: assertFalse(ta.getLineWrap());
203: assertFalse(ta.getWrapStyleWord());
204: }
205:
206: public void testJTextAreaStringintint() {
207: ExtJTextArea ta = new ExtJTextArea(str1, 3, 6);
208: assertEquals(str1, ta.getText());
209: assertTrue(ta.getDocument() instanceof PlainDocument);
210: assertEquals(3, ta.getRows());
211: assertEquals(6, ta.getColumns());
212: assertFalse(ta.getLineWrap());
213: assertFalse(ta.getWrapStyleWord());
214: ta = new ExtJTextArea(null, 5, 6);
215: assertEquals("", ta.getText());
216: try {
217: ta = new ExtJTextArea(str1, -1, 4);
218: } catch (IllegalArgumentException e) {
219: bWasException = true;
220: message = e.getMessage();
221: }
222: assertTrue(bWasException);
223: assertEquals("rows: -1", message);
224: bWasException = false;
225: message = null;
226: try {
227: ta = new ExtJTextArea(str2, 1, -3);
228: } catch (IllegalArgumentException e) {
229: bWasException = true;
230: message = e.getMessage();
231: }
232: assertTrue(bWasException);
233: assertEquals("columns: -3", message);
234: }
235:
236: public void testJTextAreaString() {
237: ExtJTextArea ta = new ExtJTextArea(str1);
238: assertEquals(str1, ta.getText());
239: assertTrue(ta.getDocument() instanceof PlainDocument);
240: assertEquals(0, ta.getRows());
241: assertEquals(0, ta.getColumns());
242: assertFalse(ta.getLineWrap());
243: assertFalse(ta.getWrapStyleWord());
244: }
245:
246: public void testJTextAreaintint() {
247: ExtJTextArea ta = new ExtJTextArea(5, 6);
248: assertEquals("", ta.getText());
249: assertTrue(ta.getDocument() instanceof PlainDocument);
250: assertEquals(5, ta.getRows());
251: assertEquals(6, ta.getColumns());
252: assertFalse(ta.getLineWrap());
253: assertFalse(ta.getWrapStyleWord());
254: try {
255: ta = new ExtJTextArea(-1, 4);
256: } catch (IllegalArgumentException e) {
257: bWasException = true;
258: message = e.getMessage();
259: }
260: assertTrue(bWasException);
261: assertEquals("rows: -1", message);
262: bWasException = false;
263: message = null;
264: try {
265: ta = new ExtJTextArea(1, -3);
266: } catch (IllegalArgumentException e) {
267: bWasException = true;
268: message = e.getMessage();
269: }
270: assertTrue(bWasException);
271: assertEquals("columns: -3", message);
272: }
273:
274: public void testJTextArea() {
275: ExtJTextArea ta = new ExtJTextArea();
276: assertEquals("", ta.getText());
277: assertTrue(ta.getDocument() instanceof PlainDocument);
278: assertEquals(0, ta.getRows());
279: assertEquals(0, ta.getColumns());
280: assertFalse(ta.getLineWrap());
281: assertFalse(ta.getWrapStyleWord());
282: }
283:
284: public void testCreateDefaultModel() {
285: Document doc = jta.createDefaultModel();
286: Document doc1 = jta.createDefaultModel();
287: assertTrue(doc instanceof PlainDocument);
288: assertNotSame(jta.getDocument(), doc);
289: assertNotSame(doc1, doc);
290: }
291:
292: public void testGetAccessibleContext() {
293: AccessibleContext accessible = jta.getAccessibleContext();
294: assertTrue(accessible instanceof JTextArea.AccessibleJTextArea);
295: assertEquals(jta.getAccessibleContext(), accessible);
296: JTextArea.AccessibleJTextArea access = (JTextArea.AccessibleJTextArea) accessible;
297: AccessibleStateSet ass = access.getAccessibleStateSet();
298: assertNotSame(ass, access.getAccessibleStateSet());
299: assertTrue(ass.contains(AccessibleState.MULTI_LINE));
300: }
301:
302: String replaceRange(final String s1, final String s2,
303: final int start, final int end) {
304: String tmp = s2 == null ? "" : s2;
305: return s1.substring(0, start) + tmp
306: + s1.substring(end, s1.length());
307: }
308:
309: public void testReplaceRange() throws Exception {
310: String tmp;
311: jta.replaceRange(str1, 5, 10);
312: tmp = replaceRange(content, str1, 5, 10);
313: assertEquals(tmp, jta.getText());
314: jta.replaceRange(null, 5, 10);
315: tmp = replaceRange(tmp, null, 5, 10);
316: assertEquals(tmp, jta.getText());
317: jta.replaceRange("", 5, 10);
318: tmp = replaceRange(tmp, "", 5, 10);
319: assertEquals(tmp, jta.getText());
320: try {
321: jta.replaceRange(str2, -1, 5);
322: } catch (IllegalArgumentException e) {
323: bWasException = true;
324: message = e.getMessage();
325: }
326: assertTrue(bWasException);
327: assertEquals("Invalid remove", message);
328: bWasException = false;
329: message = null;
330: try {
331: jta.replaceRange(str2, 1, tmp.length() + 1);
332: } catch (IllegalArgumentException e) {
333: bWasException = true;
334: message = e.getMessage();
335: }
336: assertTrue(bWasException);
337: assertEquals("Invalid remove", message);
338: bWasException = false;
339: message = null;
340: try {
341: jta.replaceRange(str2, 10, 5);
342: } catch (IllegalArgumentException e) {
343: bWasException = true;
344: message = e.getMessage();
345: }
346: assertTrue(bWasException);
347: assertEquals("end before start", message);
348: jta.replaceRange(str2, 7, 14);
349: tmp = replaceRange(tmp, str2, 7, 14);
350: assertEquals(tmp, jta.getText());
351: jta.replaceRange(null, 8, 17);
352: tmp = replaceRange(tmp, null, 8, 17);
353: assertEquals(tmp, jta.getText());
354: jta.replaceRange("", 5, 12);
355: tmp = replaceRange(tmp, "", 5, 12);
356: assertEquals(tmp, jta.getText());
357: }
358:
359: String insertString(final String s1, final String s2,
360: final int index) {
361: return s1.substring(0, index) + s2
362: + s1.substring(index, s1.length());
363: }
364:
365: public void testInsert() throws Exception {
366: String tmp;
367: jta.insert(str1, 5);
368: tmp = insertString(content, str1, 5);
369: assertEquals(tmp, jta.getText());
370: jta.insert(null, 5);
371: assertEquals(tmp, jta.getText());
372: jta.insert("", 5);
373: assertEquals(tmp, jta.getText());
374: try {
375: jta.insert(str2, -1);
376: } catch (IllegalArgumentException e) {
377: bWasException = true;
378: message = e.getMessage();
379: }
380: assertTrue(bWasException);
381: assertEquals("Invalid insert", message);
382: bWasException = false;
383: message = null;
384: try {
385: jta.insert(str2, tmp.length() + 1);
386: } catch (IllegalArgumentException e) {
387: bWasException = true;
388: message = e.getMessage();
389: }
390: assertTrue(bWasException);
391: assertEquals("Invalid insert", message);
392: bWasException = false;
393: message = null;
394: try {
395: jta.insert(null, tmp.length() + 1);
396: } catch (IllegalArgumentException e) {
397: bWasException = true;
398: message = e.getMessage();
399: }
400: assertFalse(bWasException);
401: assertNull(message);
402: jta.insert(str2, 7);
403: tmp = insertString(tmp, str2, 7);
404: assertEquals(tmp, jta.getText());
405: jta.insert(null, 7);
406: assertEquals(tmp, jta.getText());
407: jta.insert("", 7);
408: assertEquals(tmp, jta.getText());
409: }
410:
411: public void testAppend() throws Exception {
412: String tmp;
413: jta.append(str1);
414: tmp = content + str1;
415: assertEquals(tmp, jta.getText());
416: jta.append(null);
417: assertEquals(tmp, jta.getText());
418: jta.append("");
419: assertEquals(tmp, jta.getText());
420: jta.append(str2);
421: tmp = content + str1 + str2;
422: assertEquals(tmp, jta.getText());
423: jta.append(null);
424: assertEquals(tmp, jta.getText());
425: jta.append("");
426: assertEquals(tmp, jta.getText());
427: }
428:
429: // Implementation dependent
430: /*
431: public void testParamString() {
432: String str = "," + jta.getX() + "," + jta.getY() + ","
433: + jta.getSize().width + "x" + jta.getSize().height + ","
434: + "layout=" + jta.getLayout() + ",";
435: str = str.replaceFirst("@[^,}]*", "");
436: str +=
437: "alignmentX=" + "null"+ "," + //1.4.2
438: "alignmentY=" + "null"+ "," + //1.4.2
439: //"alignmentX=" + "0.0" + "," + //1.5.0
440: // "alignmentY=" + "0.0" + "," + //1.5.0
441: "border=" + jta.getBorder() + "," + "flags=296" + ","
442: + "maximumSize=,minimumSize=,preferredSize=," + "caretColor="
443: + jta.getCaretColor() + "," + "disabledTextColor="
444: + jta.getDisabledTextColor() + "," + "editable="
445: + jta.isEditable() + "," + "margin=" + jta.getMargin() + ","
446: + "selectedTextColor=" + jta.getSelectedTextColor() + ","
447: + "selectionColor=" + jta.getSelectionColor() + ","
448: + "columns=" + jta.getColumns() + "," + "columnWidth="
449: + jta.getColumnWidth() + "," + "rows=" + jta.getRows() + ","
450: + "rowHeight=" + jta.getRowHeight() + "," + "word="
451: + jta.getWrapStyleWord() + "," + "wrap=" + jta.getLineWrap();
452: assertEquals(changeString(str), changeString(jta.paramString()));
453: } */
454: String changeString(final String s) {
455: return s.replaceFirst("layout[^,]*,", "").replaceFirst(
456: "flag[^,]*,", "");
457: }
458:
459: public void testGetUIClassID() {
460: assertEquals("TextAreaUI", jta.getUIClassID());
461: assertEquals("TextAreaUI", bidiJta.getUIClassID());
462: }
463:
464: public void testGetScrollableUnitIncrement() throws Exception {
465: assertEquals(jta.getColumnWidth(), jta
466: .getScrollableUnitIncrement(null,
467: SwingConstants.HORIZONTAL, -1));
468: assertEquals(jta.getColumnWidth(), jta
469: .getScrollableUnitIncrement(null,
470: SwingConstants.HORIZONTAL, 0));
471: assertEquals(jta.getColumnWidth(), jta
472: .getScrollableUnitIncrement(null,
473: SwingConstants.HORIZONTAL, 1));
474: assertEquals(jta.getRowHeight(), jta
475: .getScrollableUnitIncrement(null,
476: SwingConstants.VERTICAL, -1));
477: assertEquals(jta.getRowHeight(), jta
478: .getScrollableUnitIncrement(null,
479: SwingConstants.VERTICAL, 0));
480: assertEquals(jta.getRowHeight(), jta
481: .getScrollableUnitIncrement(null,
482: SwingConstants.VERTICAL, 1));
483: try {
484: jta.getScrollableUnitIncrement(null, 3, 1);
485: } catch (IllegalArgumentException e) {
486: bWasException = true;
487: message = e.getMessage();
488: }
489: assertTrue(bWasException);
490: assertEquals("Invalid orientation: 3", message);
491: Font newFont = new java.awt.Font("SimSun", 0, 12);
492: jta.setFont(newFont);
493: assertEquals(jta.getColumnWidth(), jta
494: .getScrollableUnitIncrement(null,
495: SwingConstants.HORIZONTAL, -1));
496: assertEquals(jta.getColumnWidth(), jta
497: .getScrollableUnitIncrement(null,
498: SwingConstants.HORIZONTAL, 0));
499: assertEquals(jta.getColumnWidth(), jta
500: .getScrollableUnitIncrement(null,
501: SwingConstants.HORIZONTAL, 1));
502: assertEquals(jta.getRowHeight(), jta
503: .getScrollableUnitIncrement(null,
504: SwingConstants.VERTICAL, -1));
505: assertEquals(jta.getRowHeight(), jta
506: .getScrollableUnitIncrement(null,
507: SwingConstants.VERTICAL, 0));
508: assertEquals(jta.getRowHeight(), jta
509: .getScrollableUnitIncrement(null,
510: SwingConstants.VERTICAL, 1));
511: }
512:
513: public void testSetFont() throws Exception {
514: Font oldFont = jta.getFont();
515: FontMetrics fm = jta.getFontMetrics(oldFont);
516: assertEquals(fm.getHeight(), jta.getRowHeight());
517: assertEquals(fm.charWidth('m'), jta.getColumnWidth());
518: jta.wasCallRevalidate = false;
519: Font newFont = new java.awt.Font("SimSun", 0, 12);
520: jta.setFont(newFont);
521: assertTrue(jta.wasCallRevalidate);
522: assertEqualsPropertyChangeEvent("font", oldFont, newFont,
523: listener.event);
524: fm = jta.getFontMetrics(newFont);
525: assertEquals(fm.getHeight(), jta.getRowHeight());
526: assertEquals(fm.charWidth('m'), jta.getColumnWidth());
527: }
528:
529: Dimension getPrefferedSize(final JTextArea jta) {
530: Dimension dim1 = jta.getPreferredScrollableViewportSize();
531: int width1 = dim1.width;
532: int height1 = dim1.height;
533: Dimension dim2 = jta.getUI().getPreferredSize(jta);
534: int width2 = dim2.width;
535: int height2 = dim2.height;
536: return new Dimension(Math.max(width1, width2), Math.max(
537: height1, height2));
538: }
539:
540: public void testGetPreferredSize() throws Exception {
541: assertEquals(jta.getPreferredSize(), jta
542: .getPreferredScrollableViewportSize());
543: jta.setColumns(5);
544: jta.setRows(2);
545: assertEquals(getPrefferedSize(jta), jta.getPreferredSize());
546: jta.setColumns(500);
547: jta.setRows(1);
548: assertEquals(getPrefferedSize(jta), jta.getPreferredSize());
549: jta.setColumns(1);
550: jta.setRows(500);
551: assertEquals(getPrefferedSize(jta), jta.getPreferredSize());
552: jta.setColumns(500);
553: jta.setRows(200);
554: assertEquals(getPrefferedSize(jta), jta.getPreferredSize());
555: }
556:
557: public void testGetPreferredScrollableViewportSize()
558: throws Exception {
559: assertEquals(jta.getPreferredSize(), jta
560: .getPreferredScrollableViewportSize());
561: jta.setColumns(5);
562: jta.setRows(2);
563: Dimension dim = new Dimension(5 * jta.getColumnWidth(), 2 * jta
564: .getRowHeight());
565: assertEquals(dim, jta.getPreferredScrollableViewportSize());
566: jta.setColumns(500);
567: jta.setRows(200);
568: dim = new Dimension(500 * jta.getColumnWidth(), 200 * jta
569: .getRowHeight());
570: assertEquals(dim, jta.getPreferredScrollableViewportSize());
571: jta.setColumns(0);
572: jta.setRows(200);
573: assertEquals(new Dimension(jta.getPreferredSize().width,
574: 200 * jta.getRowHeight()), jta
575: .getPreferredScrollableViewportSize());
576: jta.setColumns(1);
577: jta.setRows(0);
578: assertEquals(new Dimension(1 * jta.getColumnWidth(), jta
579: .getPreferredSize().height), jta
580: .getPreferredScrollableViewportSize());
581: }
582:
583: public void testGetLineStartEndOffset() {
584: AbstractDocument doc = (AbstractDocument) jta.getDocument();
585: int count = jta.getLineCount();
586: doc.readLock();
587: Element root = doc.getDefaultRootElement();
588: for (int j = 0; j < count; j++) {
589: Element currentElement = root.getElement(j);
590: try {
591: assertEquals(currentElement.getStartOffset(), jta
592: .getLineStartOffset(j));
593: int end = currentElement.getEndOffset();
594: if (j == count - 1) {
595: end--;
596: }
597: assertEquals(end, jta.getLineEndOffset(j));
598: } catch (BadLocationException e) {
599: }
600: }
601: doc.readUnlock();
602: try {
603: jta.getLineStartOffset(count);
604: } catch (BadLocationException e) {
605: bWasException = true;
606: message = e.getMessage();
607: }
608: assertTrue(bWasException);
609: assertEquals("No such line", message);
610: bWasException = false;
611: message = null;
612: try {
613: jta.getLineStartOffset(5000);
614: } catch (BadLocationException e) {
615: bWasException = true;
616: message = e.getMessage();
617: }
618: assertTrue(bWasException);
619: assertEquals("No such line", message);
620: bWasException = false;
621: message = null;
622: try {
623: jta.getLineStartOffset(-1);
624: } catch (BadLocationException e) {
625: bWasException = true;
626: message = e.getMessage();
627: }
628: assertTrue(bWasException);
629: assertEquals("Negative line", message);
630: bWasException = false;
631: message = null;
632: try {
633: jta.getLineEndOffset(count);
634: } catch (BadLocationException e) {
635: bWasException = true;
636: message = e.getMessage();
637: }
638: assertTrue(bWasException);
639: assertEquals("No such line", message);
640: bWasException = false;
641: message = null;
642: try {
643: jta.getLineEndOffset(5000);
644: } catch (BadLocationException e) {
645: bWasException = true;
646: message = e.getMessage();
647: }
648: assertTrue(bWasException);
649: assertEquals("No such line", message);
650: bWasException = false;
651: message = null;
652: try {
653: jta.getLineEndOffset(-1);
654: } catch (BadLocationException e) {
655: bWasException = true;
656: message = e.getMessage();
657: }
658: assertTrue(bWasException);
659: assertEquals("Negative line", message);
660: }
661:
662: public void testGetLineOfOffset() {
663: AbstractDocument doc = (AbstractDocument) jta.getDocument();
664: int length = doc.getLength();
665: doc.readLock();
666: Element root = doc.getDefaultRootElement();
667: for (int j = 0; j < length; j++) {
668: try {
669: assertEquals(root.getElementIndex(j), jta
670: .getLineOfOffset(j));
671: } catch (BadLocationException e) {
672: }
673: }
674: doc.readUnlock();
675: try {
676: jta.getLineOfOffset(length + 1);
677: } catch (BadLocationException e) {
678: bWasException = true;
679: message = e.getMessage();
680: }
681: assertTrue(bWasException);
682: assertEquals("Can't translate offset to line", message);
683: bWasException = false;
684: message = null;
685: try {
686: jta.getLineOfOffset(5000);
687: } catch (BadLocationException e) {
688: bWasException = true;
689: message = e.getMessage();
690: }
691: assertTrue(bWasException);
692: assertEquals("Can't translate offset to line", message);
693: bWasException = false;
694: message = null;
695: try {
696: jta.getLineOfOffset(-1);
697: } catch (BadLocationException e) {
698: bWasException = true;
699: message = e.getMessage();
700: }
701: assertTrue(bWasException);
702: assertEquals("Can't translate offset to line", message);
703: bWasException = false;
704: message = null;
705: }
706:
707: public void testSerialization() throws Exception {
708: /*
709: jt.setRows(6);
710: jt.setColumns(8);
711: jt.setLineWrap(true);
712: jt.setWrapStyleWord(true);
713: jt.setFont(new java.awt.Font("SimSun", 0, 12));
714:
715: JTextArea jta1 = new JTextArea();
716: FileOutputStream fo = new FileOutputStream("tmp");
717: ObjectOutputStream so = new ObjectOutputStream(fo);
718: so.writeObject(jt);
719: so.flush();
720: so.close();
721: FileInputStream fi = new FileInputStream("tmp");
722: ObjectInputStream si = new ObjectInputStream(fi);
723: jta1 = (JTextArea) si.readObject();
724: si.close();
725:
726: assertTrue(jta1.getLineWrap());
727: assertTrue(jta1.getWrapStyleWord());
728: assertEquals(bidiContent, jta1.getText());
729: assertEquals(6, jta1.getRows());
730: assertEquals(8, jta1.getColumns());
731: assertEquals(new java.awt.Font("SimSun", 0, 12), jta1.getFont());
732: */
733: }
734:
735: public void testSetGetWrapStyleWord() throws Exception {
736: assertFalse(jta.getWrapStyleWord());
737: jta.setWrapStyleWord(true);
738: assertTrue(jta.getWrapStyleWord());
739: assertEqualsPropertyChangeEvent("wrapStyleWord", Boolean.FALSE,
740: Boolean.TRUE, listener.event);
741: }
742:
743: public void testSetGetLineWrap() throws Exception {
744: assertFalse(jta.getLineWrap());
745: jta.setLineWrap(true);
746: assertTrue(jta.getLineWrap());
747: assertEqualsPropertyChangeEvent("lineWrap", Boolean.FALSE,
748: Boolean.TRUE, listener.event);
749: }
750:
751: public void testSetGetTabSize() throws Exception {
752: assertEquals(8, jta.getTabSize());
753: assertEquals(new Integer(8), jta.getDocument().getProperty(
754: "tabSize"));
755: jta.setTabSize(5);
756: assertEquals(5, jta.getTabSize());
757: assertEquals(new Integer(5), jta.getDocument().getProperty(
758: "tabSize"));
759: assertEquals(new Integer(5), jta.getDocument().getProperty(
760: "tabSize"));
761: assertEqualsPropertyChangeEvent("tabSize", new Integer(8),
762: new Integer(5), listener.event);
763: jta.setTabSize(-2);
764: assertEquals(new Integer(-2), jta.getDocument().getProperty(
765: "tabSize"));
766: assertEquals(-2, jta.getTabSize());
767: }
768:
769: public void testSetGetRows() throws Exception {
770: assertEquals(0, jta.getRows());
771: jta.wasCallInvalidate = false;
772: jta.setRows(6);
773: assertEquals(6, jta.getRows());
774: assertTrue(jta.wasCallInvalidate);
775: try {
776: jta.setRows(-1);
777: } catch (IllegalArgumentException e) {
778: bWasException = true;
779: message = e.getMessage();
780: }
781: assertTrue(bWasException);
782: assertEquals("rows less than zero.", message);
783: }
784:
785: public void testGetLineCount() {
786: AbstractDocument doc_jta = (AbstractDocument) jta.getDocument();
787: doc_jta.readLock();
788: assertEquals(jta.getLineCount(), doc_jta
789: .getDefaultRootElement().getElementCount());
790: doc_jta.readUnlock();
791: doc_jta = (AbstractDocument) bidiJta.getDocument();
792: doc_jta.readLock();
793: assertEquals(bidiJta.getLineCount(), doc_jta
794: .getDefaultRootElement().getElementCount());
795: doc_jta.readUnlock();
796: }
797:
798: public void testSetGetColumns() throws Exception {
799: assertEquals(0, jta.getColumns());
800: jta.wasCallInvalidate = false;
801: jta.setColumns(6);
802: assertEquals(6, jta.getColumns());
803: assertTrue(jta.wasCallInvalidate);
804: try {
805: jta.setColumns(-1);
806: } catch (IllegalArgumentException e) {
807: bWasException = true;
808: message = e.getMessage();
809: }
810: assertTrue(bWasException);
811: assertEquals("columns less than zero.", message);
812: }
813: }
|