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 Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 03.03.2005
021:
022: */package javax.swing.text;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.io.OutputStream;
030: import java.io.OutputStreamWriter;
031: import java.io.StringReader;
032: import java.io.StringWriter;
033: import javax.swing.Action;
034:
035: public class DefaultEditorKitTest extends EditorKitTest {
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: kit = new DefaultEditorKit();
040: }
041:
042: public void testCreateCaret() {
043: assertNull("Caret", kit.createCaret());
044: }
045:
046: public void testCreateDefaultDocument() {
047: Document doc = kit.createDefaultDocument();
048: assertTrue("document's type", doc instanceof PlainDocument);
049: assertEquals("document's length", 0, doc.getLength());
050: assertEquals("number of root elements", 2, doc
051: .getRootElements().length);
052: assertNull("document's title", doc
053: .getProperty(Document.TitleProperty));
054: assertNull("document's StreamDescription", doc
055: .getProperty(Document.StreamDescriptionProperty));
056: }
057:
058: public void testGetActions() {
059: Action[] actions1 = kit.getActions();
060: Action[] actions2 = kit.getActions();
061: String templateStr = "insert-content, delete-previous, delete-next, "
062: + "set-read-only, set-writable, cut-to-clipboard, copy-to-clipboard, "
063: + "paste-from-clipboard, page-up, page-down, selection-page-up, "
064: + "selection-page-down, selection-page-left, selection-page-right, "
065: + "insert-break, beep, caret-forward, caret-backward, selection-forward, "
066: + "selection-backward, caret-up, caret-down, selection-up, selection-down, "
067: + "caret-begin-word, caret-end-word, selection-begin-word, selection-end-word, "
068: + "caret-previous-word, caret-next-word, selection-previous-word, selection-next-word, "
069: + "caret-begin-line, caret-end-line, selection-begin-line, selection-end-line, "
070: + "caret-begin-paragraph, caret-end-paragraph, selection-begin-paragraph, "
071: + "selection-end-paragraph, caret-begin, caret-end, selection-begin, selection-end, "
072: + "default-typed, insert-tab, select-word, select-line, select-paragraph, select-all, "
073: + "unselect, toggle-componentOrientation, dump-model, ";
074: assertEquals("number of actions", actions1.length,
075: actions2.length);
076: for (int i = 0; i < actions1.length; i++) {
077: assertTrue(i + " action is shared",
078: actions1[i] == actions2[i]);
079: }
080: assertEquals("number of actions", 53, actions1.length);
081: for (int i = 0; i < actions1.length; i++) {
082: String name = (String) actions1[i].getValue(Action.NAME);
083: name += ", ";
084: assertTrue(templateStr.indexOf(name) >= 0);
085: templateStr = templateStr.replaceFirst(name, "");
086: }
087: assertEquals("", templateStr);
088: }
089:
090: public void testGetContentType() {
091: assertEquals("content type", "text/plain", kit.getContentType());
092: }
093:
094: public void testGetViewFactory() {
095: assertNull("ViewFactory", kit.getViewFactory());
096: }
097:
098: public void testActionSharing() {
099: assertTrue(
100: "actions are being shared",
101: new DefaultEditorKit().getActions()[0] == new DefaultEditorKit()
102: .getActions()[0]);
103: assertTrue("actions are being shared", new DefaultEditorKit()
104: .getActions()[10] == new DefaultEditorKit()
105: .getActions()[10]);
106: assertTrue("actions are being shared", new DefaultEditorKit()
107: .getActions()[20] == new DefaultEditorKit()
108: .getActions()[20]);
109: }
110:
111: /*
112: * Class under test for void read(InputStream, Document, int)
113: */
114: public void testReadInputStreamDocumentint() throws Exception {
115: final ByteArrayOutputStream outFile = new ByteArrayOutputStream();
116: String str = "This is a very short plain-text document.\nIt's to be read only by the test.";
117: outFile.write(str.getBytes());
118: InputStream inFile = new ByteArrayInputStream(outFile
119: .toByteArray());
120: final Document doc = kit.createDefaultDocument();
121: kit.read(inFile, doc, 0);
122: assertEquals("document's length", str.length(), doc.getLength());
123: inFile.close();
124: inFile = new ByteArrayInputStream(outFile.toByteArray());
125: kit.read(inFile, doc, 10);
126: assertEquals("document's length", 2 * str.length(), doc
127: .getLength());
128: String head = null;
129: head = doc.getText(0, 20);
130: assertEquals("document's head", "This is a This is a ", head);
131: inFile.close();
132: testExceptionalCase(new BadLocationCase() {
133: @Override
134: public void exceptionalAction() throws Exception {
135: kit
136: .read(new ByteArrayInputStream(outFile
137: .toByteArray()), doc, 1000);
138: }
139: });
140: }
141:
142: /*
143: * Class under test for void read(Reader, Document, int)
144: */
145: public void testReadReaderDocumentint() throws Exception {
146: String str = "This is a very short plain-text document.\nIt's to be read only by the test.";
147: final ByteArrayOutputStream outFile = new ByteArrayOutputStream();
148: outFile.write(str.getBytes());
149: InputStreamReader inFile = new InputStreamReader(
150: new ByteArrayInputStream(outFile.toByteArray()));
151: final Document doc = kit.createDefaultDocument();
152: readKitReader(inFile, doc, 0);
153: assertEquals("document's length", str.length(), doc.getLength());
154: inFile.close();
155: inFile = new InputStreamReader(new ByteArrayInputStream(outFile
156: .toByteArray()));
157: readKitReader(inFile, doc, 10);
158: assertEquals("document's length", 2 * str.length(), doc
159: .getLength());
160: String head = doc.getText(0, 20);
161: assertEquals("document's head", "This is a This is a ", head);
162: inFile.close();
163: testExceptionalCase(new BadLocationCase() {
164: @Override
165: public void exceptionalAction() throws Exception {
166: kit.read(
167: new InputStreamReader(new ByteArrayInputStream(
168: outFile.toByteArray())), doc, 1000);
169: }
170: });
171: }
172:
173: /*
174: * Class under test for void write(OutputStream, Document, int, int)
175: */
176: public void testWriteOutputStreamDocumentintint() throws Exception {
177: String str = "This is a very short plain-text document.It's to be read only by the test.";
178: final Document doc = kit.createDefaultDocument();
179: doc.insertString(0, str, doc.getDefaultRootElement()
180: .getAttributes());
181: final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
182: writeKitFile(outputStream, str, doc, 0, str.length());
183: String strRead = readStringFromFile(new ByteArrayInputStream(
184: outputStream.toByteArray()));
185: assertEquals("document", str, strRead);
186: outputStream.reset();
187: writeKitFile(outputStream, str, doc, 11, str.length() - 13);
188: strRead = readStringFromFile(new ByteArrayInputStream(
189: outputStream.toByteArray()));
190: assertEquals(
191: "document",
192: "ery short plain-text document.It's to be read only by the tes",
193: strRead);
194: testExceptionalCase(new BadLocationCase() {
195: @Override
196: public void exceptionalAction() throws Exception {
197: kit.write(outputStream, doc, 0, 1000);
198: }
199: });
200: }
201:
202: /*
203: * Class under test for void write(Writer, Document, int, int)
204: */
205: public void testWriteWriterDocumentintint() throws Exception {
206: String str = "This is a very short plain-text document.It's to be read only by the test.";
207: final Document doc = kit.createDefaultDocument();
208: doc.insertString(0, str, doc.getDefaultRootElement()
209: .getAttributes());
210: final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
211: writeKitWriter(outputStream, str, doc, 0, str.length());
212: String strRead = readStringFromFile(new ByteArrayInputStream(
213: outputStream.toByteArray()));
214: assertEquals("document", str, strRead);
215: outputStream.reset();
216: writeKitWriter(outputStream, str, doc, 11, str.length() - 13);
217: strRead = readStringFromFile(new ByteArrayInputStream(
218: outputStream.toByteArray()));
219: assertEquals(
220: "document",
221: "ery short plain-text document.It's to be read only by the tes",
222: strRead);
223: testExceptionalCase(new BadLocationCase() {
224: @Override
225: public void exceptionalAction() throws Exception {
226: kit.write(new OutputStreamWriter(outputStream), doc, 0,
227: 1000);
228: }
229: });
230: }
231:
232: public void testWriteWriterFlush() throws Exception {
233: final String str = "Test text";
234: final Document doc = kit.createDefaultDocument();
235: doc.insertString(0, str, null);
236: final Marker flushMarker = new Marker();
237: final Marker closeMarker = new Marker();
238: StringWriter writer = new StringWriter() {
239: @Override
240: public void close() throws IOException {
241: closeMarker.setOccurred();
242: super .close();
243: }
244:
245: @Override
246: public void flush() {
247: flushMarker.setOccurred();
248: super .flush();
249: }
250: };
251: kit.write(writer, doc, 0, doc.getLength());
252: assertFalse(closeMarker.isOccurred());
253: assertTrue(flushMarker.isOccurred());
254: }
255:
256: public void testWriteOutputStreamFlush() throws Exception {
257: final String str = "Test text";
258: final Document doc = kit.createDefaultDocument();
259: doc.insertString(0, str, null);
260: final Marker flushMarker = new Marker();
261: final Marker closeMarker = new Marker();
262: OutputStream stream = new ByteArrayOutputStream() {
263: @Override
264: public void close() throws IOException {
265: closeMarker.setOccurred();
266: super .close();
267: }
268:
269: @Override
270: public void flush() throws IOException {
271: flushMarker.setOccurred();
272: super .flush();
273: }
274: };
275: kit.write(stream, doc, 0, doc.getLength());
276: assertFalse(closeMarker.isOccurred());
277: assertTrue(flushMarker.isOccurred());
278: }
279:
280: public void testNewLineReader() throws IOException,
281: BadLocationException {
282: String str1 = "This is a very \r\nshort plain-text document.\r\nIt's to be read only by the \r\ntest.";
283: String str2 = "This is a very \rshort plain-text document.\rIt's to be read only by the test.";
284: StringReader reader = new StringReader(str1);
285: Document doc = new PlainDocument();
286: DefaultEditorKit kit = new DefaultEditorKit();
287: kit.read(reader, doc, 0);
288: String readStr = doc.getText(0, doc.getLength());
289: assertEquals("no \"\\r\" found", -1, readStr.indexOf("\r"));
290: assertEquals("string length", str1.length() - 3, readStr
291: .length());
292: assertEquals("newLine property", "\r\n", doc
293: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
294: reader = new StringReader(str2);
295: doc = new PlainDocument();
296: kit = new DefaultEditorKit();
297: kit.read(reader, doc, 0);
298: readStr = doc.getText(0, doc.getLength());
299: assertEquals("no \"\\r\" found", -1, readStr.indexOf("\r"));
300: assertEquals("string length", str2.length(), readStr.length());
301: assertEquals("newLine property", "\r", doc
302: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
303: }
304:
305: public void testNewLineInputStream() throws IOException,
306: BadLocationException {
307: String str1 = "This is a very \r\nshort plain-text document.\r\nIt's to be read only by the \r\ntest.";
308: String str2 = "This is a very \rshort plain-text document.\rIt's to be read only by the test.";
309: InputStream reader = new ByteArrayInputStream(str1.getBytes());
310: Document doc = new PlainDocument();
311: DefaultEditorKit kit = new DefaultEditorKit();
312: kit.read(reader, doc, 0);
313: String readStr = doc.getText(0, doc.getLength());
314: assertEquals("no \"\\r\" found", -1, readStr.indexOf("\r"));
315: assertEquals("string length", str1.length() - 3, readStr
316: .length());
317: assertEquals("newLine property", "\r\n", doc
318: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
319: reader = new ByteArrayInputStream(str2.getBytes());
320: doc = new PlainDocument();
321: kit = new DefaultEditorKit();
322: kit.read(reader, doc, 0);
323: readStr = doc.getText(0, doc.getLength());
324: assertEquals("no \"\\r\" found", -1, readStr.indexOf("\r"));
325: assertEquals("string length", str2.length(), readStr.length());
326: assertEquals("newLine property", "\r", doc
327: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
328: }
329:
330: public void testNewLineWriter() throws IOException,
331: BadLocationException {
332: String str1 = "This is a very \r\nshort plain-text document.\r\nIt's to be read only by the \r\ntest.";
333: String str2 = "This is a very \rshort plain-text document.\rIt's to be read only by the \rtest.";
334: String str3 = "This is a very \nshort plain-text document.\nIt's to be read only by the \ntest.";
335: StringReader reader = new StringReader(str1);
336: StringWriter writer = new StringWriter();
337: Document doc = new PlainDocument();
338: DefaultEditorKit kit = new DefaultEditorKit();
339: kit.read(reader, doc, 0);
340: kit.write(writer, doc, 0, doc.getLength());
341: assertEquals(str1, writer.toString());
342: assertEquals(str3, doc.getText(0, doc.getLength()));
343: reader = new StringReader(str2);
344: writer = new StringWriter();
345: doc = new PlainDocument();
346: kit = new DefaultEditorKit();
347: kit.read(reader, doc, 0);
348: kit.write(writer, doc, 0, doc.getLength());
349: assertEquals(str2, writer.toString());
350: assertEquals(str3, doc.getText(0, doc.getLength()));
351: }
352:
353: public void testNewLineOutputStream() throws IOException,
354: BadLocationException {
355: String str1 = "This is a very \r\nshort plain-text document.\r\nIt's to be read only by the \r\ntest.";
356: String str2 = "This is a very \rshort plain-text document.\rIt's to be read only by the \rtest.";
357: String str3 = "This is a very \nshort plain-text document.\nIt's to be read only by the \ntest.";
358: InputStream reader = new ByteArrayInputStream(str1.getBytes());
359: OutputStream writer = new ByteArrayOutputStream();
360: Document doc = new PlainDocument();
361: DefaultEditorKit kit = new DefaultEditorKit();
362: kit.read(reader, doc, 0);
363: assertEquals(str3, doc.getText(0, doc.getLength()));
364: kit.write(writer, doc, 0, doc.getLength());
365: assertEquals(str1, writer.toString());
366: reader = new ByteArrayInputStream(str2.getBytes());
367: writer = new ByteArrayOutputStream();
368: doc = new PlainDocument();
369: kit = new DefaultEditorKit();
370: kit.read(reader, doc, 0);
371: assertEquals(str3, doc.getText(0, doc.getLength()));
372: kit.write(writer, doc, 0, doc.getLength());
373: assertEquals(str2, writer.toString());
374: }
375:
376: private void readKitReader(final InputStreamReader inFile,
377: final Document doc, final int pos) throws Exception {
378: kit.read(inFile, doc, pos);
379: }
380:
381: private String readStringFromFile(final InputStream inputStream)
382: throws Exception {
383: InputStreamReader inFile = new InputStreamReader(inputStream);
384: char[] arrayRead = new char[1000];
385: int readLength = inFile.read(arrayRead);
386: return new String(arrayRead, 0, readLength);
387: }
388:
389: private void writeKitWriter(final OutputStream outputStream,
390: final String str, final Document doc, final int pos,
391: final int length) throws Exception {
392: OutputStreamWriter outFile = new OutputStreamWriter(
393: outputStream);
394: kit.write(outFile, doc, pos, length);
395: outFile.close();
396: }
397:
398: private void writeKitFile(final OutputStream outputStream,
399: final String str, final Document doc, final int pos,
400: final int length) throws Exception {
401: kit.write(outputStream, doc, pos, length);
402: outputStream.close();
403: }
404: }
|