Source Code Cross Referenced for TestLittleEndian.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » util » 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.util 
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.util;
019:
020:        import junit.framework.TestCase;
021:        import org.apache.poi.util.LittleEndian.BufferUnderrunException;
022:
023:        import java.io.ByteArrayInputStream;
024:        import java.io.IOException;
025:        import java.io.InputStream;
026:
027:        /**
028:         * Class to test LittleEndian functionality
029:         *
030:         * @author Marc Johnson
031:         */
032:
033:        public class TestLittleEndian extends TestCase {
034:
035:            /**
036:             * Constructor TestLittleEndian
037:             *
038:             * @param name
039:             */
040:            public TestLittleEndian(String name) {
041:                super (name);
042:            }
043:
044:            /**
045:             * test the getShort() method
046:             */
047:
048:            public void testGetShort() {
049:                byte[] testdata = new byte[LittleEndian.SHORT_SIZE + 1];
050:
051:                testdata[0] = 0x01;
052:                testdata[1] = (byte) 0xFF;
053:                testdata[2] = 0x02;
054:                short expected[] = new short[2];
055:
056:                expected[0] = (short) 0xFF01;
057:                expected[1] = 0x02FF;
058:                assertEquals(expected[0], LittleEndian.getShort(testdata));
059:                assertEquals(expected[1], LittleEndian.getShort(testdata, 1));
060:            }
061:
062:            public void testGetUShort() {
063:                byte[] testdata = new byte[LittleEndian.SHORT_SIZE + 1];
064:
065:                testdata[0] = 0x01;
066:                testdata[1] = (byte) 0xFF;
067:                testdata[2] = 0x02;
068:
069:                byte[] testdata2 = new byte[LittleEndian.SHORT_SIZE + 1];
070:
071:                testdata2[0] = 0x0D;
072:                testdata2[1] = (byte) 0x93;
073:                testdata2[2] = (byte) 0xFF;
074:
075:                int expected[] = new int[4];
076:
077:                expected[0] = 0xFF01;
078:                expected[1] = 0x02FF;
079:                expected[2] = 0x930D;
080:                expected[3] = 0xFF93;
081:                assertEquals(expected[0], LittleEndian.getUShort(testdata));
082:                assertEquals(expected[1], LittleEndian.getUShort(testdata, 1));
083:                assertEquals(expected[2], LittleEndian.getUShort(testdata2));
084:                assertEquals(expected[3], LittleEndian.getUShort(testdata2, 1));
085:
086:                byte[] testdata3 = new byte[LittleEndian.SHORT_SIZE + 1];
087:                LittleEndian.putShort(testdata3, 0, (short) expected[2]);
088:                LittleEndian.putShort(testdata3, 1, (short) expected[3]);
089:                assertEquals(testdata3[0], 0x0D);
090:                assertEquals(testdata3[1], (byte) 0x93);
091:                assertEquals(testdata3[2], (byte) 0xFF);
092:                assertEquals(expected[2], LittleEndian.getUShort(testdata3));
093:                assertEquals(expected[3], LittleEndian.getUShort(testdata3, 1));
094:                //System.out.println("TD[1][0]: "+LittleEndian.getUShort(testdata)+" expecting 65281");
095:                //System.out.println("TD[1][1]: "+LittleEndian.getUShort(testdata, 1)+" expecting 767");
096:                //System.out.println("TD[2][0]: "+LittleEndian.getUShort(testdata2)+" expecting 37645");
097:                //System.out.println("TD[2][1]: "+LittleEndian.getUShort(testdata2, 1)+" expecting 65427");
098:                //System.out.println("TD[3][0]: "+LittleEndian.getUShort(testdata3)+" expecting 37645");
099:                //System.out.println("TD[3][1]: "+LittleEndian.getUShort(testdata3, 1)+" expecting 65427");
100:
101:            }
102:
103:            private static final byte[] _double_array = { 56, 50, -113, -4,
104:                    -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64 };
105:            private static final byte[] _nan_double_array = { (byte) 0x00,
106:                    (byte) 0x00, (byte) 0x3C, (byte) 0x00, (byte) 0x20,
107:                    (byte) 0x04, (byte) 0xFF, (byte) 0xFF };
108:            private static final double[] _doubles = { 1.23456, 2.47912,
109:                    Double.NaN };
110:
111:            /**
112:             * test the getDouble() method
113:             */
114:
115:            public void testGetDouble() {
116:                assertEquals(_doubles[0],
117:                        LittleEndian.getDouble(_double_array), 0.000001);
118:                assertEquals(_doubles[1], LittleEndian.getDouble(_double_array,
119:                        LittleEndian.DOUBLE_SIZE), 0.000001);
120:                assertTrue(Double.isNaN(LittleEndian
121:                        .getDouble(_nan_double_array)));
122:
123:                double nan = LittleEndian.getDouble(_nan_double_array);
124:                byte[] data = new byte[8];
125:                LittleEndian.putDouble(data, nan);
126:                for (int i = 0; i < data.length; i++) {
127:                    byte b = data[i];
128:                    assertEquals(data[i], _nan_double_array[i]);
129:                }
130:            }
131:
132:            /**
133:             * test the getInt() method
134:             */
135:
136:            public void testGetInt() {
137:                byte[] testdata = new byte[LittleEndian.INT_SIZE + 1];
138:
139:                testdata[0] = 0x01;
140:                testdata[1] = (byte) 0xFF;
141:                testdata[2] = (byte) 0xFF;
142:                testdata[3] = (byte) 0xFF;
143:                testdata[4] = 0x02;
144:                int expected[] = new int[2];
145:
146:                expected[0] = 0xFFFFFF01;
147:                expected[1] = 0x02FFFFFF;
148:                assertEquals(expected[0], LittleEndian.getInt(testdata));
149:                assertEquals(expected[1], LittleEndian.getInt(testdata, 1));
150:            }
151:
152:            /**
153:             * test the getLong method
154:             */
155:
156:            public void testGetLong() {
157:                byte[] testdata = new byte[LittleEndian.LONG_SIZE + 1];
158:
159:                testdata[0] = 0x01;
160:                testdata[1] = (byte) 0xFF;
161:                testdata[2] = (byte) 0xFF;
162:                testdata[3] = (byte) 0xFF;
163:                testdata[4] = (byte) 0xFF;
164:                testdata[5] = (byte) 0xFF;
165:                testdata[6] = (byte) 0xFF;
166:                testdata[7] = (byte) 0xFF;
167:                testdata[8] = 0x02;
168:                long expected[] = new long[2];
169:
170:                expected[0] = 0xFFFFFFFFFFFFFF01L;
171:                expected[1] = 0x02FFFFFFFFFFFFFFL;
172:                assertEquals(expected[0], LittleEndian.getLong(testdata));
173:                assertEquals(expected[1], LittleEndian.getLong(testdata, 1));
174:            }
175:
176:            /**
177:             * test the PutShort method
178:             */
179:
180:            public void testPutShort() {
181:                byte[] expected = new byte[LittleEndian.SHORT_SIZE + 1];
182:
183:                expected[0] = 0x01;
184:                expected[1] = (byte) 0xFF;
185:                expected[2] = 0x02;
186:                byte[] received = new byte[LittleEndian.SHORT_SIZE + 1];
187:                short testdata[] = new short[2];
188:
189:                testdata[0] = (short) 0xFF01;
190:                testdata[1] = 0x02FF;
191:                LittleEndian.putShort(received, testdata[0]);
192:                assertTrue(ba_equivalent(received, expected, 0,
193:                        LittleEndian.SHORT_SIZE));
194:                LittleEndian.putShort(received, 1, testdata[1]);
195:                assertTrue(ba_equivalent(received, expected, 1,
196:                        LittleEndian.SHORT_SIZE));
197:            }
198:
199:            /**
200:             * test the putInt method
201:             */
202:
203:            public void testPutInt() {
204:                byte[] expected = new byte[LittleEndian.INT_SIZE + 1];
205:
206:                expected[0] = 0x01;
207:                expected[1] = (byte) 0xFF;
208:                expected[2] = (byte) 0xFF;
209:                expected[3] = (byte) 0xFF;
210:                expected[4] = 0x02;
211:                byte[] received = new byte[LittleEndian.INT_SIZE + 1];
212:                int testdata[] = new int[2];
213:
214:                testdata[0] = 0xFFFFFF01;
215:                testdata[1] = 0x02FFFFFF;
216:                LittleEndian.putInt(received, testdata[0]);
217:                assertTrue(ba_equivalent(received, expected, 0,
218:                        LittleEndian.INT_SIZE));
219:                LittleEndian.putInt(received, 1, testdata[1]);
220:                assertTrue(ba_equivalent(received, expected, 1,
221:                        LittleEndian.INT_SIZE));
222:            }
223:
224:            /**
225:             * test the putDouble methods
226:             */
227:
228:            public void testPutDouble() {
229:                byte[] received = new byte[LittleEndian.DOUBLE_SIZE + 1];
230:
231:                LittleEndian.putDouble(received, _doubles[0]);
232:                assertTrue(ba_equivalent(received, _double_array, 0,
233:                        LittleEndian.DOUBLE_SIZE));
234:                LittleEndian.putDouble(received, 1, _doubles[1]);
235:                byte[] expected = new byte[LittleEndian.DOUBLE_SIZE + 1];
236:
237:                System.arraycopy(_double_array, LittleEndian.DOUBLE_SIZE,
238:                        expected, 1, LittleEndian.DOUBLE_SIZE);
239:                assertTrue(ba_equivalent(received, expected, 1,
240:                        LittleEndian.DOUBLE_SIZE));
241:            }
242:
243:            /**
244:             * test the putLong method
245:             */
246:
247:            public void testPutLong() {
248:                byte[] expected = new byte[LittleEndian.LONG_SIZE + 1];
249:
250:                expected[0] = 0x01;
251:                expected[1] = (byte) 0xFF;
252:                expected[2] = (byte) 0xFF;
253:                expected[3] = (byte) 0xFF;
254:                expected[4] = (byte) 0xFF;
255:                expected[5] = (byte) 0xFF;
256:                expected[6] = (byte) 0xFF;
257:                expected[7] = (byte) 0xFF;
258:                expected[8] = 0x02;
259:                byte[] received = new byte[LittleEndian.LONG_SIZE + 1];
260:                long testdata[] = new long[2];
261:
262:                testdata[0] = 0xFFFFFFFFFFFFFF01L;
263:                testdata[1] = 0x02FFFFFFFFFFFFFFL;
264:                LittleEndian.putLong(received, testdata[0]);
265:                assertTrue(ba_equivalent(received, expected, 0,
266:                        LittleEndian.LONG_SIZE));
267:                LittleEndian.putLong(received, 1, testdata[1]);
268:                assertTrue(ba_equivalent(received, expected, 1,
269:                        LittleEndian.LONG_SIZE));
270:            }
271:
272:            private static byte[] _good_array = { 0x01, 0x02, 0x01, 0x02, 0x01,
273:                    0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01,
274:                    0x02 };
275:            private static byte[] _bad_array = { 0x01 };
276:
277:            /**
278:             * test the readShort method
279:             */
280:
281:            public void testReadShort() throws IOException {
282:                short expected_value = 0x0201;
283:                InputStream stream = new ByteArrayInputStream(_good_array);
284:                int count = 0;
285:
286:                while (true) {
287:                    short value = LittleEndian.readShort(stream);
288:
289:                    if (value == 0) {
290:                        break;
291:                    }
292:                    assertEquals(value, expected_value);
293:                    count++;
294:                }
295:                assertEquals(count, _good_array.length
296:                        / LittleEndianConsts.SHORT_SIZE);
297:                stream = new ByteArrayInputStream(_bad_array);
298:                try {
299:                    LittleEndian.readShort(stream);
300:                    fail("Should have caught BufferUnderrunException");
301:                } catch (BufferUnderrunException ignored) {
302:
303:                    // as expected
304:                }
305:            }
306:
307:            /**
308:             * test the readInt method
309:             */
310:
311:            public void testReadInt() throws IOException {
312:                int expected_value = 0x02010201;
313:                InputStream stream = new ByteArrayInputStream(_good_array);
314:                int count = 0;
315:
316:                while (true) {
317:                    int value = LittleEndian.readInt(stream);
318:
319:                    if (value == 0) {
320:                        break;
321:                    }
322:                    assertEquals(value, expected_value);
323:                    count++;
324:                }
325:                assertEquals(count, _good_array.length
326:                        / LittleEndianConsts.INT_SIZE);
327:                stream = new ByteArrayInputStream(_bad_array);
328:                try {
329:                    LittleEndian.readInt(stream);
330:                    fail("Should have caught BufferUnderrunException");
331:                } catch (BufferUnderrunException ignored) {
332:
333:                    // as expected
334:                }
335:            }
336:
337:            /**
338:             * test the readLong method
339:             */
340:
341:            public void testReadLong() throws IOException {
342:                long expected_value = 0x0201020102010201L;
343:                InputStream stream = new ByteArrayInputStream(_good_array);
344:                int count = 0;
345:
346:                while (true) {
347:                    long value = LittleEndian.readLong(stream);
348:
349:                    if (value == 0) {
350:                        break;
351:                    }
352:                    assertEquals(value, expected_value);
353:                    count++;
354:                }
355:                assertEquals(count, _good_array.length
356:                        / LittleEndianConsts.LONG_SIZE);
357:                stream = new ByteArrayInputStream(_bad_array);
358:                try {
359:                    LittleEndian.readLong(stream);
360:                    fail("Should have caught BufferUnderrunException");
361:                } catch (BufferUnderrunException ignored) {
362:
363:                    // as expected
364:                }
365:            }
366:
367:            /**
368:             * test the readFromStream method
369:             */
370:
371:            public void testReadFromStream() throws IOException {
372:                InputStream stream = new ByteArrayInputStream(_good_array);
373:                byte[] value = LittleEndian.readFromStream(stream,
374:                        _good_array.length);
375:
376:                assertTrue(ba_equivalent(value, _good_array, 0,
377:                        _good_array.length));
378:                stream = new ByteArrayInputStream(_good_array);
379:                try {
380:                    value = LittleEndian.readFromStream(stream,
381:                            _good_array.length + 1);
382:                    fail("Should have caught BufferUnderrunException");
383:                } catch (BufferUnderrunException ignored) {
384:
385:                    // as expected
386:                }
387:            }
388:
389:            public void testUnsignedByteToInt() throws Exception {
390:                assertEquals(255, LittleEndian.ubyteToInt((byte) 255));
391:            }
392:
393:            private boolean ba_equivalent(byte[] received, byte[] expected,
394:                    int offset, int size) {
395:                boolean result = true;
396:
397:                for (int j = offset; j < offset + size; j++) {
398:                    if (received[j] != expected[j]) {
399:                        System.out.println("difference at index " + j);
400:                        result = false;
401:                        break;
402:                    }
403:                }
404:                return result;
405:            }
406:
407:            public void testUnsignedShort() throws Exception {
408:                assertEquals(0xffff, LittleEndian.getUShort(new byte[] {
409:                        (byte) 0xff, (byte) 0xff }, 0));
410:            }
411:
412:            /**
413:             * main method to run the unit tests
414:             *
415:             * @param ignored_args
416:             */
417:
418:            public static void main(String[] ignored_args) {
419:                System.out.println("Testing util.LittleEndian functionality");
420:                junit.textui.TestRunner.run(TestLittleEndian.class);
421:            }
422:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.