aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java
blob: 0155cf809c16665fbf1f8b261ed27462e7633166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
        

package org.apache.poi.poifs.nio;

import java.io.File;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import org.apache.poi.POIDataSamples;

import junit.framework.TestCase;

/**
 * Tests for the datasource implementations
 */
public class TestDataSource extends TestCase
{
   private static POIDataSamples data = POIDataSamples.getPOIFSInstance();
   
   public void testFile() throws Exception {
      File f = data.getFile("Notes.ole2");
      
      FileBackedDataSource ds = new FileBackedDataSource(f);
      try {
          assertEquals(8192, ds.size());
          
          // Start of file
          ByteBuffer bs; 
          bs = ds.read(4, 0);
          assertEquals(4, bs.capacity());
          assertEquals(0, bs.position());
          assertEquals(0xd0-256, bs.get(0));
          assertEquals(0xcf-256, bs.get(1));
          assertEquals(0x11-000, bs.get(2));
          assertEquals(0xe0-256, bs.get(3));
          assertEquals(0xd0-256, bs.get());
          assertEquals(0xcf-256, bs.get());
          assertEquals(0x11-000, bs.get());
          assertEquals(0xe0-256, bs.get());
          
          // Mid way through
          bs = ds.read(8, 0x400);
          assertEquals(8, bs.capacity());
          assertEquals(0, bs.position());
          assertEquals((byte)'R', bs.get(0));
          assertEquals(0, bs.get(1));
          assertEquals((byte)'o', bs.get(2));
          assertEquals(0, bs.get(3));
          assertEquals((byte)'o', bs.get(4));
          assertEquals(0, bs.get(5));
          assertEquals((byte)'t', bs.get(6));
          assertEquals(0, bs.get(7));
          
          // Can go to the end, but not past it
          bs = ds.read(8, 8190);
          assertEquals(0, bs.position()); // TODO How best to warn of a short read?
          
          // Can't go off the end
          try {
             bs = ds.read(4, 8192);
             fail("Shouldn't be able to read off the end of the file");
          } catch(IllegalArgumentException e) {}
      } finally {
          ds.close();
      }
   }
   
   public void testByteArray() throws Exception {
      byte[] data = new byte[256];
      byte b;
      for(int i=0; i<data.length; i++) {
         b = (byte)i;
         data[i] = b;
      }
      
      ByteArrayBackedDataSource ds = new ByteArrayBackedDataSource(data);
      
      // Start
      ByteBuffer bs; 
      bs = ds.read(4, 0);
      assertEquals(0, bs.position());
      assertEquals(0x00, bs.get());
      assertEquals(0x01, bs.get());
      assertEquals(0x02, bs.get());
      assertEquals(0x03, bs.get());
      
      // Middle
      bs = ds.read(4, 100);
      assertEquals(100, bs.position());
      assertEquals(100, bs.get());
      assertEquals(101, bs.get());
      assertEquals(102, bs.get());
      assertEquals(103, bs.get());
      
      // End
      bs = ds.read(4, 252);
      assertEquals(-4, bs.get());
      assertEquals(-3, bs.get());
      assertEquals(-2, bs.get());
      assertEquals(-1, bs.get());
      
      // Off the end
      bs = ds.read(4, 254);
      assertEquals(-2, bs.get());
      assertEquals(-1, bs.get());
      try {
         bs.get();
         fail("Shouldn't be able to read off the end");
      } catch(BufferUnderflowException e) {}

      // Past the end
      try {
         bs = ds.read(4, 256);
         fail("Shouldn't be able to read off the end");
      } catch(IndexOutOfBoundsException e) {}
      
      
      // Overwrite
      bs = ByteBuffer.allocate(4);
      bs.put(0, (byte)-55);
      bs.put(1, (byte)-54);
      bs.put(2, (byte)-53);
      bs.put(3, (byte)-52);
      
      assertEquals(256, ds.size());
      ds.write(bs, 40);
      assertEquals(256, ds.size());
      bs = ds.read(4, 40);
      
      assertEquals(-55, bs.get());
      assertEquals(-54, bs.get());
      assertEquals(-53, bs.get());
      assertEquals(-52, bs.get());
      
      // Append
      bs = ByteBuffer.allocate(4);
      bs.put(0, (byte)-55);
      bs.put(1, (byte)-54);
      bs.put(2, (byte)-53);
      bs.put(3, (byte)-52);
      
      assertEquals(256, ds.size());
      ds.write(bs, 256);
      assertEquals(260, ds.size());
      
      bs = ds.read(4, 256);
      assertEquals(256, bs.position());
      assertEquals(-55, bs.get());
      assertEquals(-54, bs.get());
      assertEquals(-53, bs.get());
      assertEquals(-52, bs.get());
   }
}