Source Code Cross Referenced for TestSmallDocumentBlock.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » poifs » storage » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.poifs.storage 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 java.util.*;
023:
024:        import junit.framework.*;
025:
026:        /**
027:         * Class to test SmallDocumentBlock functionality
028:         *
029:         * @author Marc Johnson
030:         */
031:
032:        public class TestSmallDocumentBlock extends TestCase {
033:            static final private byte[] _testdata;
034:            static final private int _testdata_size = 2999;
035:
036:            static {
037:                _testdata = new byte[_testdata_size];
038:                for (int j = 0; j < _testdata.length; j++) {
039:                    _testdata[j] = (byte) j;
040:                }
041:            };
042:
043:            /**
044:             * constructor
045:             *
046:             * @param name
047:             */
048:
049:            public TestSmallDocumentBlock(String name) {
050:                super (name);
051:            }
052:
053:            /**
054:             * Test conversion from DocumentBlocks
055:             *
056:             * @exception IOException
057:             */
058:
059:            public void testConvert1() throws IOException {
060:                ByteArrayInputStream stream = new ByteArrayInputStream(
061:                        _testdata);
062:                List documents = new ArrayList();
063:
064:                while (true) {
065:                    DocumentBlock block = new DocumentBlock(stream);
066:
067:                    documents.add(block);
068:                    if (block.partiallyRead()) {
069:                        break;
070:                    }
071:                }
072:                SmallDocumentBlock[] results = SmallDocumentBlock.convert(
073:                        (BlockWritable[]) documents
074:                                .toArray(new DocumentBlock[0]), _testdata_size);
075:
076:                assertEquals("checking correct result size: ",
077:                        (_testdata_size + 63) / 64, results.length);
078:                ByteArrayOutputStream output = new ByteArrayOutputStream();
079:
080:                for (int j = 0; j < results.length; j++) {
081:                    results[j].writeBlocks(output);
082:                }
083:                byte[] output_array = output.toByteArray();
084:
085:                assertEquals("checking correct output size: ",
086:                        64 * results.length, output_array.length);
087:                int index = 0;
088:
089:                for (; index < _testdata_size; index++) {
090:                    assertEquals("checking output " + index, _testdata[index],
091:                            output_array[index]);
092:                }
093:                for (; index < output_array.length; index++) {
094:                    assertEquals("checking output " + index, (byte) 0xff,
095:                            output_array[index]);
096:                }
097:            }
098:
099:            /**
100:             * Test conversion from byte array
101:             *
102:             * @exception IOException;
103:             *
104:             * @exception IOException
105:             */
106:
107:            public void testConvert2() throws IOException {
108:                for (int j = 0; j < 320; j++) {
109:                    byte[] array = new byte[j];
110:
111:                    for (int k = 0; k < j; k++) {
112:                        array[k] = (byte) k;
113:                    }
114:                    SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(
115:                            array, 319);
116:
117:                    assertEquals(5, blocks.length);
118:                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
119:
120:                    for (int k = 0; k < blocks.length; k++) {
121:                        blocks[k].writeBlocks(stream);
122:                    }
123:                    stream.close();
124:                    byte[] output = stream.toByteArray();
125:
126:                    for (int k = 0; k < array.length; k++) {
127:                        assertEquals(String.valueOf(k), array[k], output[k]);
128:                    }
129:                    for (int k = array.length; k < 320; k++) {
130:                        assertEquals(String.valueOf(k), (byte) 0xFF, output[k]);
131:                    }
132:                }
133:            }
134:
135:            /**
136:             * Test read method
137:             *
138:             * @exception IOException
139:             */
140:
141:            public void testRead() throws IOException {
142:                ByteArrayInputStream stream = new ByteArrayInputStream(
143:                        _testdata);
144:                List documents = new ArrayList();
145:
146:                while (true) {
147:                    DocumentBlock block = new DocumentBlock(stream);
148:
149:                    documents.add(block);
150:                    if (block.partiallyRead()) {
151:                        break;
152:                    }
153:                }
154:                SmallDocumentBlock[] blocks = SmallDocumentBlock.convert(
155:                        (BlockWritable[]) documents
156:                                .toArray(new DocumentBlock[0]), _testdata_size);
157:
158:                for (int j = 1; j <= _testdata_size; j += 38) {
159:                    byte[] buffer = new byte[j];
160:                    int offset = 0;
161:
162:                    for (int k = 0; k < (_testdata_size / j); k++) {
163:                        SmallDocumentBlock.read(blocks, buffer, offset);
164:                        for (int n = 0; n < buffer.length; n++) {
165:                            assertEquals("checking byte " + (k * j) + n,
166:                                    _testdata[(k * j) + n], buffer[n]);
167:                        }
168:                        offset += j;
169:                    }
170:                }
171:            }
172:
173:            /**
174:             * test fill
175:             *
176:             * @exception IOException
177:             */
178:
179:            public void testFill() throws IOException {
180:                for (int j = 0; j <= 8; j++) {
181:                    List foo = new ArrayList();
182:
183:                    for (int k = 0; k < j; k++) {
184:                        foo.add(new Object());
185:                    }
186:                    int result = SmallDocumentBlock.fill(foo);
187:
188:                    assertEquals("correct big block count: ", (j + 7) / 8,
189:                            result);
190:                    assertEquals("correct small block count: ", 8 * result, foo
191:                            .size());
192:                    for (int m = j; m < foo.size(); m++) {
193:                        BlockWritable block = (BlockWritable) foo.get(m);
194:                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
195:
196:                        block.writeBlocks(stream);
197:                        byte[] output = stream.toByteArray();
198:
199:                        assertEquals("correct output size (block[ " + m
200:                                + " ]): ", 64, output.length);
201:                        for (int n = 0; n < 64; n++) {
202:                            assertEquals("correct value (block[ " + m + " ][ "
203:                                    + n + " ]): ", (byte) 0xff, output[n]);
204:                        }
205:                    }
206:                }
207:            }
208:
209:            /**
210:             * test calcSize
211:             */
212:
213:            public void testCalcSize() {
214:                for (int j = 0; j < 10; j++) {
215:                    assertEquals("testing " + j, j * 64, SmallDocumentBlock
216:                            .calcSize(j));
217:                }
218:            }
219:
220:            /**
221:             * test extract method
222:             *
223:             * @exception IOException
224:             */
225:
226:            public void testExtract() throws IOException {
227:                byte[] data = new byte[512];
228:                int offset = 0;
229:
230:                for (int j = 0; j < 8; j++) {
231:                    for (int k = 0; k < 64; k++) {
232:                        data[offset++] = (byte) (k + j);
233:                    }
234:                }
235:                RawDataBlock[] blocks = { new RawDataBlock(
236:                        new ByteArrayInputStream(data)) };
237:                List output = SmallDocumentBlock.extract(blocks);
238:                Iterator iter = output.iterator();
239:
240:                offset = 0;
241:                while (iter.hasNext()) {
242:                    byte[] out_data = ((SmallDocumentBlock) iter.next())
243:                            .getData();
244:
245:                    assertEquals("testing block at offset " + offset, 64,
246:                            out_data.length);
247:                    for (int j = 0; j < out_data.length; j++) {
248:                        assertEquals("testing byte at offset " + offset,
249:                                data[offset], out_data[j]);
250:                        offset++;
251:                    }
252:                }
253:            }
254:
255:            /**
256:             * main method to run the unit tests
257:             *
258:             * @param ignored_args
259:             */
260:
261:            public static void main(String[] ignored_args) {
262:                System.out
263:                        .println("Testing org.apache.poi.poifs.storage.SmallDocumentBlock");
264:                junit.textui.TestRunner.run(TestSmallDocumentBlock.class);
265:            }
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.