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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.io.IOException;
023: import java.io.StringWriter;
024: import java.io.Writer;
025: import javax.swing.SwingTestCase;
026:
027: public class AbstractWriterTest extends SwingTestCase {
028: private static class TestAbstractWriter extends AbstractWriter {
029: protected TestAbstractWriter(final Writer w, final Document doc) {
030: super (w, doc);
031: }
032:
033: protected TestAbstractWriter(final Writer w,
034: final Document doc, final int pos, final int len) {
035: super (w, doc, pos, len);
036: }
037:
038: protected TestAbstractWriter(final Writer w, final Element root) {
039: super (w, root);
040: }
041:
042: protected TestAbstractWriter(final Writer w,
043: final Element root, final int pos, final int len) {
044: super (w, root, pos, len);
045: }
046:
047: @Override
048: protected void write() throws IOException, BadLocationException {
049: }
050: }
051:
052: private StringWriter out;
053:
054: private Document doc;
055:
056: private AbstractWriter writer;
057:
058: public AbstractWriterTest(final String name) {
059: super (name);
060: }
061:
062: @Override
063: protected void setUp() throws Exception {
064: super .setUp();
065: out = new StringWriter();
066: doc = createDocument();
067: writer = new TestAbstractWriter(out, doc);
068: }
069:
070: @Override
071: protected void tearDown() throws Exception {
072: super .tearDown();
073: }
074:
075: public void testAbstractWriterWriterDocument() {
076: writer = new TestAbstractWriter(out, doc);
077: assertSame(doc, writer.getDocument());
078: assertEquals(0, writer.getStartOffset());
079: assertEquals(doc.getLength(), writer.getEndOffset());
080: assertSame(writer.getElementIterator().current(), doc
081: .getDefaultRootElement());
082: assertSame(out, writer.getWriter());
083: }
084:
085: public void testAbstractWriterWriterDocumentIntInt() {
086: writer = new TestAbstractWriter(out, doc, 3, 4);
087: assertSame(doc, writer.getDocument());
088: assertEquals(3, writer.getStartOffset());
089: assertEquals(3 + 4, writer.getEndOffset());
090: assertSame(writer.getElementIterator().current(), doc
091: .getDefaultRootElement());
092: assertSame(out, writer.getWriter());
093: writer = new TestAbstractWriter(out, doc, 300, 400);
094: assertSame(doc, writer.getDocument());
095: assertEquals(300, writer.getStartOffset());
096: assertEquals(300 + 400, writer.getEndOffset());
097: assertSame(writer.getElementIterator().current(), doc
098: .getDefaultRootElement());
099: assertSame(out, writer.getWriter());
100: }
101:
102: public void testAbstractWriterWriterElement() {
103: Element root = doc.getDefaultRootElement().getElement(1);
104: writer = new TestAbstractWriter(out, root);
105: assertSame(doc, writer.getDocument());
106: assertEquals(0, writer.getStartOffset());
107: assertFalse(root.getStartOffset() == writer.getStartOffset());
108: assertEquals(root.getEndOffset(), writer.getEndOffset());
109: assertSame(writer.getElementIterator().current(), root);
110: assertSame(out, writer.getWriter());
111: }
112:
113: public void testAbstractWriterWriterElementIntInt() {
114: Element root = doc.getDefaultRootElement().getElement(1);
115: writer = new TestAbstractWriter(out, root, 3, 4);
116: assertSame(doc, writer.getDocument());
117: assertEquals(3, writer.getStartOffset());
118: assertEquals(3 + 4, writer.getEndOffset());
119: assertSame(writer.getElementIterator().current(), root);
120: assertSame(out, writer.getWriter());
121: writer = new TestAbstractWriter(out, root, 300, 400);
122: assertSame(doc, writer.getDocument());
123: assertEquals(300, writer.getStartOffset());
124: assertEquals(300 + 400, writer.getEndOffset());
125: assertSame(writer.getElementIterator().current(), root);
126: assertSame(out, writer.getWriter());
127: }
128:
129: public void testGetStartOffset() {
130: assertEquals(0, writer.getStartOffset());
131: }
132:
133: public void testGetEndOffset() {
134: assertEquals(doc.getLength(), writer.getEndOffset());
135: }
136:
137: public void testSetLineSeparator() {
138: doc.putProperty(DefaultEditorKit.EndOfLineStringProperty, "~");
139: writer = new TestAbstractWriter(out, doc);
140: assertEquals("~", writer.getLineSeparator());
141: String separator = ">>";
142: writer.setLineSeparator(separator);
143: assertSame(separator, writer.getLineSeparator());
144: }
145:
146: public void testGetLineSeparator() {
147: final String lineSeparator = System
148: .getProperty("line.separator");
149: assertEquals(System.getProperty("line.separator"), writer
150: .getLineSeparator());
151: try {
152: System.setProperty("line.separator", "<SYS>");
153: assertFalse(System.getProperty("line.separator").equals(
154: writer.getLineSeparator()));
155: writer = new TestAbstractWriter(out, doc);
156: assertEquals("<SYS>", writer.getLineSeparator());
157: doc.putProperty(DefaultEditorKit.EndOfLineStringProperty,
158: "<DOC>");
159: writer = new TestAbstractWriter(out, doc);
160: assertEquals("<DOC>", writer.getLineSeparator());
161: } finally {
162: System.setProperty("line.separator", lineSeparator);
163: }
164: }
165:
166: public void testGetWriter() {
167: assertSame(out, writer.getWriter());
168: }
169:
170: public void testGetDocument() {
171: assertSame(doc, writer.getDocument());
172: }
173:
174: public void testGetElementIterator() {
175: Element root = doc.getDefaultRootElement().getElement(1);
176: writer = new TestAbstractWriter(out, root, 3, 4);
177: assertSame(writer.getElementIterator().current(), root);
178: }
179:
180: public void testGetText() throws BadLocationException {
181: Element elem = doc.getDefaultRootElement().getElement(1);
182: assertEquals(doc.getText(elem.getStartOffset(), elem
183: .getEndOffset()
184: - elem.getStartOffset()), writer.getText(elem));
185: }
186:
187: public void testInRange() {
188: Element root = doc.getDefaultRootElement();
189: Element elem = root.getElement(1);
190: writer = new TestAbstractWriter(out, doc,
191: elem.getStartOffset(), elem.getEndOffset()
192: - elem.getStartOffset());
193: assertTrue(writer.inRange(root));
194: assertTrue(writer.inRange(elem));
195: assertFalse(writer.inRange(root.getElement(0)));
196: }
197:
198: public void testSetGetLineLength() {
199: assertEquals(100, writer.getLineLength());
200: writer.setLineLength(150);
201: assertEquals(150, writer.getLineLength());
202: }
203:
204: public void testIsLineEmpty() throws IOException {
205: if (!isHarmony()) {
206: writer.writeLineSeparator();
207: }
208: assertTrue(writer.isLineEmpty());
209: writer.indent();
210: assertTrue(writer.isLineEmpty());
211: final char content[] = { 'a' };
212: writer.output(content, 0, content.length);
213: assertFalse(writer.isLineEmpty());
214: writer.writeLineSeparator();
215: assertTrue(writer.isLineEmpty());
216: }
217:
218: public void testSetGetCurrentLineLength() throws IOException {
219: char content[] = { 'a', 'b' };
220: writer.output(content, 0, content.length);
221: assertEquals(2, writer.getCurrentLineLength());
222: writer.setCurrentLineLength(0);
223: assertEquals(0, writer.getCurrentLineLength());
224: writer.output(content, 0, content.length);
225: assertEquals(2, writer.getCurrentLineLength());
226: assertEquals("abab", out.toString());
227: }
228:
229: public void testSetGetCanWrapLines() {
230: assertTrue(writer.getCanWrapLines());
231: writer.setCanWrapLines(false);
232: assertFalse(writer.getCanWrapLines());
233: }
234:
235: public void testIncrIndent() {
236: assertEquals(0, writer.getIndentLevel());
237: writer.incrIndent();
238: assertEquals(1, writer.getIndentLevel());
239: writer.incrIndent();
240: assertEquals(2, writer.getIndentLevel());
241: writer.setLineLength(writer.getIndentLevel()
242: * writer.getIndentSpace() + 1);
243: writer.incrIndent();
244: assertEquals(2, writer.getIndentLevel());
245: }
246:
247: public void testDecrIndent() {
248: assertEquals(0, writer.getIndentLevel());
249: writer.decrIndent();
250: assertEquals(-1, writer.getIndentLevel());
251: writer.incrIndent();
252: writer.incrIndent();
253: writer.incrIndent();
254: writer.decrIndent();
255: assertEquals(1, writer.getIndentLevel());
256: }
257:
258: public void testGetIndentLevel() {
259: assertEquals(0, writer.getIndentLevel());
260: writer.incrIndent();
261: writer.incrIndent();
262: writer.incrIndent();
263: writer.decrIndent();
264: assertEquals(2, writer.getIndentLevel());
265: }
266:
267: public void testIndent() throws IOException {
268: writer.indent();
269: assertEquals("", out.toString());
270: writer.setIndentSpace(3);
271: writer.incrIndent();
272: writer.incrIndent();
273: writer.indent();
274: assertEquals(" ", out.toString());
275: }
276:
277: public void testSetGetIndentSpace() {
278: assertEquals(2, writer.getIndentSpace());
279: writer.setIndentSpace(5);
280: assertEquals(5, writer.getIndentSpace());
281: }
282:
283: public void testOutput() throws IOException {
284: final char content[] = { 'h', 'e', 'l', 'l', 'o' };
285: writer.output(content, 1, 3);
286: assertEquals("ell", out.toString());
287: assertEquals(3, writer.getCurrentLineLength());
288: assertFalse(writer.isLineEmpty());
289: }
290:
291: public void testText() throws BadLocationException, IOException {
292: Element root = doc.getDefaultRootElement();
293: writer = new TestAbstractWriter(out, doc, 3, root.getElement(0)
294: .getEndOffset());
295: writer.setLineSeparator(">>");
296: writer.text(root.getElement(0));
297: assertEquals("st line>>", out.toString());
298: out = new StringWriter();
299: writer = new TestAbstractWriter(out, doc, 3, root.getElement(0)
300: .getEndOffset());
301: writer.setLineSeparator(">>");
302: writer.text(root.getElement(1));
303: assertEquals("sec", out.toString());
304: out = new StringWriter();
305: writer = new TestAbstractWriter(out, doc, 3, root.getElement(0)
306: .getEndOffset());
307: writer.text(root.getElement(2));
308: assertEquals("", out.toString());
309: }
310:
311: public void testWriteChar() throws IOException {
312: writer.incrIndent();
313: writer.setLineSeparator(">>");
314: writer.setCanWrapLines(false);
315: writer.write('g');
316: assertEquals("g", out.toString());
317: }
318:
319: public void testWriteCharArrayIntInt() throws IOException {
320: final char content[] = " first line\nsecond line\n"
321: .toCharArray();
322: // no wrap tests
323: writer.incrIndent();
324: writer.setLineSeparator(">>");
325: writer.setCanWrapLines(false);
326: writer.write(content, 1, content.length - 2);
327: assertEquals("first line>>second line", out.toString());
328: out = new StringWriter();
329: writer = new TestAbstractWriter(out, doc);
330: writer.incrIndent();
331: writer.setLineSeparator(">>");
332: writer.setCanWrapLines(false);
333: writer.setLineLength(5);
334: writer.write(content, 0, content.length);
335: assertEquals(" first line>>second line>>", out.toString());
336: assertTrue(writer.isLineEmpty());
337: // tests with wrap
338: out = new StringWriter();
339: writer = new TestAbstractWriter(out, doc);
340: writer.incrIndent();
341: writer.setLineSeparator(">>");
342: writer.write(content, 1, content.length - 1);
343: assertEquals("first line>> second line>>", out.toString());
344: out = new StringWriter();
345: writer = new TestAbstractWriter(out, doc);
346: writer.incrIndent();
347: writer.setLineSeparator(">>");
348: writer.setLineLength(2);
349: writer.write(content, 0, content.length);
350: assertEquals(" >> first >> line>> second >> line>>", out
351: .toString());
352: }
353:
354: public void testWriteString() throws IOException {
355: final String content = " first line\nsecond line\n";
356: writer.incrIndent();
357: writer.setLineSeparator(">>");
358: writer.setLineLength(2);
359: writer.write(content);
360: assertEquals(" >> first >> line>> second >> line>>", out
361: .toString());
362: }
363:
364: public void testWriteAttributes() throws IOException {
365: SimpleAttributeSet attrs = new SimpleAttributeSet();
366: attrs.addAttribute("key1", "value1");
367: attrs.addAttribute("key2", "value2");
368: attrs.addAttribute("key3", "value3");
369: writer.writeAttributes(attrs);
370: assertEquals(" key3=value3 key2=value2 key1=value1", out
371: .toString());
372: }
373:
374: public void testWriteLineSeparator() throws IOException {
375: final char chars[] = { 'a' };
376: writer.output(chars, 0, 1);
377: assertFalse(writer.isLineEmpty());
378: writer.setLineSeparator("b");
379: writer.writeLineSeparator();
380: assertTrue(writer.isLineEmpty());
381: assertEquals("ab", out.toString());
382: }
383:
384: private Document createDocument() {
385: DefaultStyledDocument d = new DefaultStyledDocument();
386: try {
387: d.insertString(0, "first line\nsecond line\nthird line",
388: null);
389: } catch (final BadLocationException e) {
390: throw new RuntimeException("Failed to create the document");
391: }
392: return d;
393: }
394: }
|