2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
20 package org
.apache
.poi
.poifs
.storage
;
23 import java
.util
.Random
;
25 import org
.apache
.poi
.util
.DummyPOILogger
;
26 import org
.apache
.poi
.util
.POILogFactory
;
28 import junit
.framework
.*;
31 * Class to test RawDataBlock functionality
33 * @author Marc Johnson
36 public class TestRawDataBlock
41 * Constructor TestRawDataBlock
46 public TestRawDataBlock(String name
)
50 // We always want to use our own
53 "org.apache.poi.util.POILogger",
54 "org.apache.poi.util.DummyPOILogger"
59 * Test creating a normal RawDataBlock
61 * @exception IOException
64 public void testNormalConstructor()
67 byte[] data
= new byte[ 512 ];
69 for (int j
= 0; j
< 512; j
++)
71 data
[ j
] = ( byte ) j
;
73 RawDataBlock block
= new RawDataBlock(new ByteArrayInputStream(data
));
75 assertTrue("Should not be at EOF", !block
.eof());
76 byte[] out_data
= block
.getData();
78 assertEquals("Should be same length", data
.length
, out_data
.length
);
79 for (int j
= 0; j
< 512; j
++)
81 assertEquals("Should be same value at offset " + j
, data
[ j
],
87 * Test creating an empty RawDataBlock
89 * @exception IOException
92 public void testEmptyConstructor()
95 byte[] data
= new byte[ 0 ];
96 RawDataBlock block
= new RawDataBlock(new ByteArrayInputStream(data
));
98 assertTrue("Should be at EOF", block
.eof());
103 catch (IOException ignored
)
111 * Test creating a short RawDataBlock
112 * Will trigger a warning, but no longer an IOException,
113 * as people seem to have "valid" truncated files
115 public void testShortConstructor() throws Exception
117 // Get the logger to be used
118 DummyPOILogger logger
= (DummyPOILogger
)POILogFactory
.getLogger(
121 assertEquals(0, logger
.logged
.size());
123 // Test for various data sizes
124 for (int k
= 1; k
<= 512; k
++)
126 byte[] data
= new byte[ k
];
128 for (int j
= 0; j
< k
; j
++)
130 data
[ j
] = ( byte ) j
;
132 RawDataBlock block
= null;
135 assertEquals(0, logger
.logged
.size());
138 block
= new RawDataBlock(new ByteArrayInputStream(data
));
139 assertNotNull(block
);
141 // Check for the warning is there for <512
144 "Warning on " + k
+ " byte short block",
145 1, logger
.logged
.size()
148 // Build the expected warning message, and check
149 String bts
= k
+ " byte";
155 "7 - Unable to read entire block; "+bts
+" read before EOF; expected 512 bytes. Your document has probably been truncated!",
156 (String
)(logger
.logged
.get(0))
159 assertEquals(0, logger
.logged
.size());
165 * Tests that when using a slow input stream, which
166 * won't return a full block at a time, we don't
167 * incorrectly think that there's not enough data
169 public void testSlowInputStream() throws Exception
{
170 // Get the logger to be used
171 DummyPOILogger logger
= (DummyPOILogger
)POILogFactory
.getLogger(
174 assertEquals(0, logger
.logged
.size());
176 // Test for various ok data sizes
177 for (int k
= 1; k
< 512; k
++) {
178 byte[] data
= new byte[ 512 ];
179 for (int j
= 0; j
< data
.length
; j
++) {
183 // Shouldn't complain, as there is enough data,
184 // even if it dribbles through
186 new RawDataBlock(new SlowInputStream(data
, k
));
187 assertFalse(block
.eof());
190 // But if there wasn't enough data available, will
192 for (int k
= 1; k
< 512; k
++) {
193 byte[] data
= new byte[ 511 ];
194 for (int j
= 0; j
< data
.length
; j
++) {
199 assertEquals(0, logger
.logged
.size());
201 // Should complain, as there isn't enough data
203 new RawDataBlock(new SlowInputStream(data
, k
));
204 assertNotNull(block
);
206 "Warning on " + k
+ " byte short block",
207 1, logger
.logged
.size()
213 * An input stream which will return a maximum of
214 * a given number of bytes to read, and often claims
215 * not to have any data
217 public static class SlowInputStream
extends InputStream
{
218 private Random rnd
= new Random();
220 private int chunkSize
;
223 public SlowInputStream(byte[] data
, int chunkSize
) {
224 this.chunkSize
= chunkSize
;
229 * 75% of the time, claim there's no data available
231 private boolean claimNoData() {
232 if(rnd
.nextFloat() < 0.25f
) {
238 public int read() throws IOException
{
239 if(pos
>= data
.length
) {
245 if(ret
< 0) ret
+= 256;
250 * Reads the requested number of bytes, or the chunk
251 * size, whichever is lower.
252 * Quite often will simply claim to have no data
254 public int read(byte[] b
, int off
, int len
) throws IOException
{
255 // Keep the length within the chunk size
256 if(len
> chunkSize
) {
259 // Don't read off the end of the data
260 if(pos
+ len
> data
.length
) {
261 len
= data
.length
- pos
;
263 // Spot when we're out of data
269 // 75% of the time, claim there's no data
274 // Copy, and return what we read
275 System
.arraycopy(data
, pos
, b
, off
, len
);
280 public int read(byte[] b
) throws IOException
{
281 return read(b
, 0, b
.length
);
287 * main method to run the unit tests
289 * @param ignored_args
292 public static void main(String
[] ignored_args
)
295 .println("Testing org.apache.poi.poifs.storage.RawDataBlock");
296 junit
.textui
.TestRunner
.run(TestRawDataBlock
.class);