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.GridLayout;
024: import junit.framework.TestCase;
025:
026: public class JTextArea_MultithreadedTest extends TestCase {
027: JFrame jf;
028:
029: ExtJTextArea jta;
030:
031: JTextArea bidiJta;
032:
033: JTextArea jt;
034:
035: String sLTR = "aaaa";
036:
037: String tmp;
038:
039: String sRTL = "\u05dc" + "\u05dc" + "\u05dc" + "\u05dc";
040:
041: String content = "Edison accumul\tator, Edison base: Edison battery"
042: + " Edison cap, \tEdison effect\n"
043: + "Edison screw, Edison screw cap, Edison screw \n"
044: + "holder, Edison screw lampholder, Edison screw "
045: + "plug\n"
046: + "Edison screw terminal, Edison storage battery"
047: + "Edison storage \t\tcell";
048:
049: String bidiContent = sLTR + sRTL + sRTL + " \t" + sLTR + sRTL
050: + sLTR + "\n" + sRTL + "." + sLTR + sRTL + "\t" + sRTL
051: + "\n" + sLTR + sLTR + sRTL + sRTL + sRTL + sLTR + sLTR
052: + sLTR + sRTL + sLTR + sRTL + sLTR;
053:
054: String str1 = "jazz band";
055:
056: String str2 = "jazz model";
057:
058: boolean bWasException;
059:
060: String message;
061:
062: @Override
063: protected void setUp() throws Exception {
064: super .setUp();
065: bWasException = false;
066: message = null;
067: jf = new JFrame();
068: bidiJta = new JTextArea(bidiContent);
069: jta = new ExtJTextArea(content);
070: jt = new JTextArea(bidiContent);
071: Container cont = jf.getContentPane();
072: cont.setLayout(new GridLayout(1, 2, 4, 4));
073: cont.add(jta);
074: cont.add(bidiJta);
075: jf.setLocation(200, 300);
076: jf.setSize(350, 400);
077: jf.pack();
078: }
079:
080: @Override
081: protected void tearDown() throws Exception {
082: jf.dispose();
083: super .tearDown();
084: }
085:
086: String replaceRange(final String s1, final String s2,
087: final int start, final int end) {
088: String tmp = s2 == null ? "" : s2;
089: return s1.substring(0, start) + tmp
090: + s1.substring(end, s1.length());
091: }
092:
093: public void testReplaceRange() throws Exception {
094: jta.replaceRange(str1, 5, 10);
095: tmp = replaceRange(content, str1, 5, 10);
096: assertEquals(tmp, jta.getText());
097: jta.replaceRange(null, 5, 10);
098: tmp = replaceRange(tmp, null, 5, 10);
099: assertEquals(tmp, jta.getText());
100: jta.replaceRange("", 5, 10);
101: tmp = replaceRange(tmp, "", 5, 10);
102: assertEquals(tmp, jta.getText());
103: try {
104: jta.replaceRange(str2, -1, 5);
105: } catch (IllegalArgumentException e) {
106: bWasException = true;
107: message = e.getMessage();
108: }
109: assertTrue(bWasException);
110: assertEquals("Invalid remove", message);
111: bWasException = false;
112: message = null;
113: try {
114: jta.replaceRange(str2, 1, tmp.length() + 1);
115: } catch (IllegalArgumentException e) {
116: bWasException = true;
117: message = e.getMessage();
118: }
119: assertTrue(bWasException);
120: assertEquals("Invalid remove", message);
121: bWasException = false;
122: message = null;
123: try {
124: jta.replaceRange(str2, 10, 5);
125: } catch (IllegalArgumentException e) {
126: bWasException = true;
127: message = e.getMessage();
128: }
129: assertTrue(bWasException);
130: assertEquals("end before start", message);
131: }
132:
133: String insertString(final String s1, final String s2,
134: final int index) {
135: return s1.substring(0, index) + s2
136: + s1.substring(index, s1.length());
137: }
138:
139: public void testInsert() throws Exception {
140: jta.insert(str1, 5);
141: tmp = insertString(content, str1, 5);
142: assertEquals(tmp, jta.getText());
143: jta.insert(null, 5);
144: assertEquals(tmp, jta.getText());
145: jta.insert("", 5);
146: assertEquals(tmp, jta.getText());
147: try {
148: jta.insert(str2, -1);
149: } catch (IllegalArgumentException e) {
150: bWasException = true;
151: message = e.getMessage();
152: }
153: assertTrue(bWasException);
154: assertEquals("Invalid insert", message);
155: bWasException = false;
156: message = null;
157: try {
158: jta.insert(str2, tmp.length() + 1);
159: } catch (IllegalArgumentException e) {
160: bWasException = true;
161: message = e.getMessage();
162: }
163: assertTrue(bWasException);
164: assertEquals("Invalid insert", message);
165: bWasException = false;
166: message = null;
167: try {
168: jta.insert(null, tmp.length() + 1);
169: } catch (IllegalArgumentException e) {
170: bWasException = true;
171: message = e.getMessage();
172: }
173: assertFalse(bWasException);
174: assertNull(message);
175: }
176:
177: public void testAppend() throws Exception {
178: jta.append(str1);
179: tmp = content + str1;
180: assertEquals(tmp, jta.getText());
181: jta.append(null);
182: assertEquals(tmp, jta.getText());
183: jta.append("");
184: assertEquals(tmp, jta.getText());
185: }
186: }
|