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.storage;
019:
020: import java.io.*;
021:
022: import junit.framework.*;
023:
024: /**
025: * Class to test RawDataBlock functionality
026: *
027: * @author Marc Johnson
028: */
029:
030: public class TestRawDataBlock extends TestCase {
031:
032: /**
033: * Constructor TestRawDataBlock
034: *
035: * @param name
036: */
037:
038: public TestRawDataBlock(String name) {
039: super (name);
040: }
041:
042: /**
043: * Test creating a normal RawDataBlock
044: *
045: * @exception IOException
046: */
047:
048: public void testNormalConstructor() throws IOException {
049: byte[] data = new byte[512];
050:
051: for (int j = 0; j < 512; j++) {
052: data[j] = (byte) j;
053: }
054: RawDataBlock block = new RawDataBlock(new ByteArrayInputStream(
055: data));
056:
057: assertTrue("Should not be at EOF", !block.eof());
058: byte[] out_data = block.getData();
059:
060: assertEquals("Should be same length", data.length,
061: out_data.length);
062: for (int j = 0; j < 512; j++) {
063: assertEquals("Should be same value at offset " + j,
064: data[j], out_data[j]);
065: }
066: }
067:
068: /**
069: * Test creating an empty RawDataBlock
070: *
071: * @exception IOException
072: */
073:
074: public void testEmptyConstructor() throws IOException {
075: byte[] data = new byte[0];
076: RawDataBlock block = new RawDataBlock(new ByteArrayInputStream(
077: data));
078:
079: assertTrue("Should be at EOF", block.eof());
080: try {
081: block.getData();
082: } catch (IOException ignored) {
083:
084: // as expected
085: }
086: }
087:
088: /**
089: * Test creating a short RawDataBlock
090: */
091:
092: public void testShortConstructor() {
093: for (int k = 1; k < 512; k++) {
094: byte[] data = new byte[k];
095:
096: for (int j = 0; j < k; j++) {
097: data[j] = (byte) j;
098: }
099: RawDataBlock block = null;
100:
101: try {
102: block = new RawDataBlock(new ByteArrayInputStream(data));
103: fail("Should have thrown IOException creating short block");
104: } catch (IOException ignored) {
105:
106: // as expected
107: }
108: }
109: }
110:
111: /**
112: * main method to run the unit tests
113: *
114: * @param ignored_args
115: */
116:
117: public static void main(String[] ignored_args) {
118: System.out
119: .println("Testing org.apache.poi.poifs.storage.RawDataBlock");
120: junit.textui.TestRunner.run(TestRawDataBlock.class);
121: }
122: }
|