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:
019: package org.apache.tools.ant.util;
020:
021: import java.io.IOException;
022: import junit.framework.TestCase;
023:
024: /**
025: */
026:
027: public class LineOrientedOutputStreamTest extends TestCase {
028:
029: private static String LINE = "This is a line";
030: private DummyStream stream;
031:
032: public LineOrientedOutputStreamTest(String name) {
033: super (name);
034: }
035:
036: public void setUp() {
037: stream = new DummyStream();
038: }
039:
040: public void tearDown() throws IOException {
041: if (stream != null) {
042: stream.close();
043: }
044: }
045:
046: public void testLineWithLinefeedArray() throws IOException {
047: writeByteArray();
048: writeAsArray('\n');
049: stream.assertInvoked();
050: }
051:
052: public void testLineWithLinefeedSingleBytes() throws IOException {
053: writeSingleBytes();
054: stream.write('\n');
055: stream.assertInvoked();
056: }
057:
058: public void testLineWithCariagereturnArray() throws IOException {
059: writeByteArray();
060: writeAsArray('\r');
061: stream.assertInvoked();
062: }
063:
064: public void testLineWithCariagereturnSingleBytes()
065: throws IOException {
066: writeSingleBytes();
067: stream.write('\r');
068: stream.assertInvoked();
069: }
070:
071: public void testLineWithCariagereturnLinefeedArray()
072: throws IOException {
073: writeByteArray();
074: writeAsArray('\r');
075: writeAsArray('\n');
076: stream.assertInvoked();
077: }
078:
079: public void testLineWithCariagereturnLinefeedSingleBytes()
080: throws IOException {
081: writeSingleBytes();
082: stream.write('\r');
083: stream.write('\n');
084: stream.assertInvoked();
085: }
086:
087: public void testFlushArray() throws IOException {
088: writeByteArray();
089: stream.flush();
090: stream.assertInvoked();
091: }
092:
093: public void testFlushSingleBytes() throws IOException {
094: writeSingleBytes();
095: stream.flush();
096: stream.assertInvoked();
097: }
098:
099: public void testCloseArray() throws IOException {
100: writeByteArray();
101: stream.close();
102: stream.assertInvoked();
103: stream = null;
104: }
105:
106: public void testCloseSingleBytes() throws IOException {
107: writeSingleBytes();
108: stream.close();
109: stream.assertInvoked();
110: stream = null;
111: }
112:
113: private void writeByteArray() throws IOException {
114: stream.write(LINE.getBytes(), 0, LINE.length());
115: }
116:
117: private void writeSingleBytes() throws IOException {
118: byte[] b = LINE.getBytes();
119: for (int i = 0; i < b.length; i++) {
120: stream.write(b[i]);
121: }
122: }
123:
124: private void writeAsArray(char c) throws IOException {
125: stream.write(new byte[] { (byte) c }, 0, 1);
126: }
127:
128: private class DummyStream extends LineOrientedOutputStream {
129: private boolean invoked;
130:
131: protected void processLine(String line) {
132: LineOrientedOutputStreamTest.this .assertFalse(
133: "Only one line", invoked);
134: LineOrientedOutputStreamTest.this .assertEquals(LINE, line);
135: invoked = true;
136: }
137:
138: private void assertInvoked() {
139: LineOrientedOutputStreamTest.this .assertTrue(
140: "At least one line", invoked);
141: }
142: }
143: }// LineOrientedOutputStreamTest
|