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: package org.apache.poi.poifs.filesystem;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: import org.apache.poi.poifs.property.DirectoryProperty;
027: import org.apache.poi.poifs.property.DocumentProperty;
028: import org.apache.poi.poifs.storage.RawDataBlock;
029:
030: /**
031: * Class to test DocumentOutputStream functionality
032: *
033: * @author Marc Johnson
034: */
035:
036: public class TestDocumentOutputStream extends TestCase {
037:
038: /**
039: * Constructor TestDocumentOutputStream
040: *
041: * @param name
042: *
043: * @exception IOException
044: */
045:
046: public TestDocumentOutputStream(String name) throws IOException {
047: super (name);
048: }
049:
050: /**
051: * test write(int) behavior
052: *
053: * @exception IOException
054: */
055:
056: public void testWrite1() throws IOException {
057: ByteArrayOutputStream stream = new ByteArrayOutputStream();
058: DocumentOutputStream dstream = new DocumentOutputStream(stream,
059: 25);
060:
061: for (int j = 0; j < 25; j++) {
062: dstream.write(j);
063: }
064: try {
065: dstream.write(0);
066: fail("Should have caught IOException");
067: } catch (IOException ignored) {
068: }
069: byte[] output = stream.toByteArray();
070:
071: assertEquals(25, output.length);
072: for (int j = 0; j < 25; j++) {
073: assertEquals((byte) j, output[j]);
074: }
075: stream.close();
076: }
077:
078: /**
079: * test write(byte[]) behavior
080: *
081: * @exception IOException
082: */
083:
084: public void testWrite2() throws IOException {
085: ByteArrayOutputStream stream = new ByteArrayOutputStream();
086: DocumentOutputStream dstream = new DocumentOutputStream(stream,
087: 25);
088:
089: for (int j = 0; j < 6; j++) {
090: byte[] array = new byte[4];
091:
092: Arrays.fill(array, (byte) j);
093: dstream.write(array);
094: }
095: try {
096: byte[] array = new byte[4];
097:
098: Arrays.fill(array, (byte) 7);
099: dstream.write(array);
100: fail("Should have caught IOException");
101: } catch (IOException ignored) {
102: }
103: byte[] output = stream.toByteArray();
104:
105: assertEquals(24, output.length);
106: for (int j = 0; j < 6; j++) {
107: for (int k = 0; k < 4; k++) {
108: assertEquals(String.valueOf((j * 4) + k), (byte) j,
109: output[(j * 4) + k]);
110: }
111: }
112: stream.close();
113: }
114:
115: /**
116: * test write(byte[], int, int) behavior
117: *
118: * @exception IOException
119: */
120:
121: public void testWrite3() throws IOException {
122: ByteArrayOutputStream stream = new ByteArrayOutputStream();
123: DocumentOutputStream dstream = new DocumentOutputStream(stream,
124: 25);
125: byte[] array = new byte[50];
126:
127: for (int j = 0; j < 50; j++) {
128: array[j] = (byte) j;
129: }
130: dstream.write(array, 1, 25);
131: try {
132: dstream.write(array, 0, 1);
133: fail("Should have caught IOException");
134: } catch (IOException ignored) {
135: }
136: byte[] output = stream.toByteArray();
137:
138: assertEquals(25, output.length);
139: for (int j = 0; j < 25; j++) {
140: assertEquals((byte) (j + 1), output[j]);
141: }
142: stream.close();
143: }
144:
145: /**
146: * test writeFiller()
147: *
148: * @exception IOException
149: */
150:
151: public void testWriteFiller() throws IOException {
152: ByteArrayOutputStream stream = new ByteArrayOutputStream();
153: DocumentOutputStream dstream = new DocumentOutputStream(stream,
154: 25);
155:
156: for (int j = 0; j < 25; j++) {
157: dstream.write(j);
158: }
159: try {
160: dstream.write(0);
161: fail("Should have caught IOException");
162: } catch (IOException ignored) {
163: }
164: dstream.writeFiller(100, (byte) 0xff);
165: byte[] output = stream.toByteArray();
166:
167: assertEquals(100, output.length);
168: for (int j = 0; j < 25; j++) {
169: assertEquals((byte) j, output[j]);
170: }
171: for (int j = 25; j < 100; j++) {
172: assertEquals(String.valueOf(j), (byte) 0xff, output[j]);
173: }
174: stream.close();
175: }
176:
177: /**
178: * main method to run the unit tests
179: *
180: * @param ignored_args
181: */
182:
183: public static void main(String[] ignored_args) {
184: System.out
185: .println("Testing org.apache.poi.poifs.filesystem.DocumentOutputStream");
186: junit.textui.TestRunner.run(TestDocumentOutputStream.class);
187: }
188: }
|