aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcases')
-rw-r--r--src/testcases/org/apache/poi/hssf/data/ex45582-22397.xlsbin0 -> 37888 bytes
-rw-r--r--src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java2
-rw-r--r--src/testcases/org/apache/poi/hssf/model/TestSheet.java4
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java49
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestSanityChecker.java32
-rw-r--r--src/testcases/org/apache/poi/util/TestLittleEndian.java453
-rw-r--r--src/testcases/org/apache/poi/util/TestLongField.java56
7 files changed, 214 insertions, 382 deletions
diff --git a/src/testcases/org/apache/poi/hssf/data/ex45582-22397.xls b/src/testcases/org/apache/poi/hssf/data/ex45582-22397.xls
new file mode 100644
index 0000000000..2dc16f5e33
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/data/ex45582-22397.xls
Binary files differ
diff --git a/src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java b/src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java
index c4a5ec84b3..88f620ca44 100644
--- a/src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java
+++ b/src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java
@@ -63,7 +63,7 @@ public final class TestEventRecordFactory extends TestCase {
bof.setVersion((short)0x06);
bof.setHistoryBitMask(BOFRecord.HISTORY_MASK);
- EOFRecord eof = new EOFRecord();
+ EOFRecord eof = EOFRecord.instance;
byte[] bytes = new byte[bof.getRecordSize() + eof.getRecordSize()];
int offset = 0;
offset = bof.serialize(offset,bytes);
diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheet.java b/src/testcases/org/apache/poi/hssf/model/TestSheet.java
index 22defb942e..40b349a54e 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestSheet.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestSheet.java
@@ -54,7 +54,7 @@ public final class TestSheet extends TestCase {
List records = new ArrayList();
records.add( new BOFRecord() );
records.add( new DimensionsRecord() );
- records.add( new EOFRecord() );
+ records.add(EOFRecord.instance);
Sheet sheet = Sheet.createSheet( records, 0, 0 );
int pos = 0;
@@ -396,7 +396,7 @@ public final class TestSheet extends TestCase {
records.add(new BOFRecord());
records.add(new UncalcedRecord());
records.add(new DimensionsRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
Sheet sheet = Sheet.createSheet(records, 0, 0);
int estimatedSize = sheet.getSize();
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index 37359231f8..d8b7cde292 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -1,19 +1,19 @@
-/*
-* 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.
-*/
+/* ====================================================================
+ 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.hssf.usermodel;
@@ -29,8 +29,10 @@ import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RecordFormatException;
import org.apache.poi.hssf.record.RecordInputStream;
import org.apache.poi.hssf.record.formula.Area3DPtg;
+import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.TempFile;
/**
*
@@ -512,4 +514,17 @@ public final class TestHSSFWorkbook extends TestCase {
return 8;
}
}
- }
+
+ /**
+ * The sample file provided with bug 45582 seems to have one extra byte after the EOFRecord
+ */
+ public void testExtraDataAfterEOFRecord() {
+ try {
+ HSSFTestDataSamples.openSampleWorkbook("ex45582-22397.xls");
+ } catch (RecordFormatException e) {
+ if (e.getCause() instanceof LittleEndian.BufferUnderrunException) {
+ throw new AssertionFailedError("Identified bug 45582");
+ }
+ }
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestSanityChecker.java b/src/testcases/org/apache/poi/hssf/usermodel/TestSanityChecker.java
index d32c1a1b03..f2144700d9 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestSanityChecker.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestSanityChecker.java
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
+
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
@@ -27,27 +26,20 @@ import java.util.ArrayList;
import org.apache.poi.hssf.record.*;
/**
+ * A Test case for a test utility class.<br/>
* Okay, this may seem strange but I need to test my test logic.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
-public class TestSanityChecker
- extends TestCase
-{
- public TestSanityChecker( String s )
- {
- super( s );
- }
+public final class TestSanityChecker extends TestCase {
- public void testCheckRecordOrder()
- throws Exception
- {
+ public void testCheckRecordOrder() {
final SanityChecker c = new SanityChecker();
List records = new ArrayList();
records.add(new BOFRecord());
records.add(new InterfaceHdrRecord());
records.add(new BoundSheetRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
final SanityChecker.CheckRecord[] check = {
new SanityChecker.CheckRecord(BOFRecord.class, '1'),
new SanityChecker.CheckRecord(InterfaceHdrRecord.class, '0'),
@@ -74,7 +66,7 @@ public class TestSanityChecker
records.add(new BOFRecord());
records.add(new BoundSheetRecord());
records.add(new InterfaceHdrRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
@@ -88,7 +80,7 @@ public class TestSanityChecker
records.add(new InterfaceHdrRecord());
records.add(new BoundSheetRecord());
records.add(new InterfaceHdrRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
@@ -101,7 +93,7 @@ public class TestSanityChecker
records.add(new BOFRecord());
records.add(new BoundSheetRecord());
records.add(new NameRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
records.add(new NameRecord());
c.checkRecordOrder(records, check);
}
@@ -114,7 +106,7 @@ public class TestSanityChecker
List records = new ArrayList();
records.add(new InterfaceHdrRecord());
records.add(new BoundSheetRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
@@ -126,7 +118,7 @@ public class TestSanityChecker
List records = new ArrayList();
records.add(new BOFRecord());
records.add(new InterfaceHdrRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
@@ -139,7 +131,7 @@ public class TestSanityChecker
records.add(new InterfaceHdrRecord());
records.add(new BoundSheetRecord());
records.add(new BOFRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
@@ -152,7 +144,7 @@ public class TestSanityChecker
records.add(new BOFRecord());
records.add(new BoundSheetRecord());
records.add(new InterfaceHdrRecord());
- records.add(new EOFRecord());
+ records.add(EOFRecord.instance);
c.checkRecordOrder(records, check);
}
});
diff --git a/src/testcases/org/apache/poi/util/TestLittleEndian.java b/src/testcases/org/apache/poi/util/TestLittleEndian.java
index 4ee659cc9d..0ffcd189da 100644
--- a/src/testcases/org/apache/poi/util/TestLittleEndian.java
+++ b/src/testcases/org/apache/poi/util/TestLittleEndian.java
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,7 +14,6 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.util;
@@ -31,79 +29,54 @@ import java.io.InputStream;
*
* @author Marc Johnson
*/
-
-public class TestLittleEndian
- extends TestCase
-{
-
- /**
- * Constructor TestLittleEndian
- *
- * @param name
- */
- public TestLittleEndian(String name)
- {
- super(name);
- }
+public final class TestLittleEndian extends TestCase {
/**
* test the getShort() method
*/
-
- public void testGetShort()
- {
+ public void testGetShort() {
byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ];
- testdata[ 0 ] = 0x01;
- testdata[ 1 ] = ( byte ) 0xFF;
- testdata[ 2 ] = 0x02;
- short expected[] = new short[ 2 ];
+ testdata[0] = 0x01;
+ testdata[1] = (byte) 0xFF;
+ testdata[2] = 0x02;
+ short expected[] = new short[2];
- expected[ 0 ] = ( short ) 0xFF01;
- expected[ 1 ] = 0x02FF;
- assertEquals(expected[ 0 ], LittleEndian.getShort(testdata));
- assertEquals(expected[ 1 ], LittleEndian.getShort(testdata, 1));
+ expected[0] = ( short ) 0xFF01;
+ expected[1] = 0x02FF;
+ assertEquals(expected[0], LittleEndian.getShort(testdata));
+ assertEquals(expected[1], LittleEndian.getShort(testdata, 1));
}
- public void testGetUShort()
- {
- byte[] testdata = new byte[ LittleEndian.SHORT_SIZE + 1 ];
-
- testdata[ 0 ] = 0x01;
- testdata[ 1 ] = ( byte ) 0xFF;
- testdata[ 2 ] = 0x02;
-
- byte[] testdata2 = new byte[ LittleEndian.SHORT_SIZE + 1 ];
-
- testdata2[ 0 ] = 0x0D;
- testdata2[ 1 ] = ( byte )0x93;
- testdata2[ 2 ] = ( byte )0xFF;
-
- int expected[] = new int[ 4 ];
-
- expected[ 0 ] = 0xFF01;
- expected[ 1 ] = 0x02FF;
- expected[ 2 ] = 0x930D;
- expected[ 3 ] = 0xFF93;
- assertEquals(expected[ 0 ], LittleEndian.getUShort(testdata));
- assertEquals(expected[ 1 ], LittleEndian.getUShort(testdata, 1));
- assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata2));
- assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata2, 1));
+ public void testGetUShort() {
+ byte[] testdata = {
+ (byte) 0x01,
+ (byte) 0xFF,
+ (byte) 0x02,
+ };
+ byte[] testdata2 = {
+ (byte) 0x0D,
+ (byte) 0x93,
+ (byte) 0xFF,
+ };
+
+ int expected0 = 0xFF01;
+ int expected1 = 0x02FF;
+ int expected2 = 0x930D;
+ int expected3 = 0xFF93;
+ assertEquals(expected0, LittleEndian.getUShort(testdata));
+ assertEquals(expected1, LittleEndian.getUShort(testdata, 1));
+ assertEquals(expected2, LittleEndian.getUShort(testdata2));
+ assertEquals(expected3, LittleEndian.getUShort(testdata2, 1));
byte[] testdata3 = new byte[ LittleEndian.SHORT_SIZE + 1 ];
- LittleEndian.putShort(testdata3, 0, ( short ) expected[2] );
- LittleEndian.putShort(testdata3, 1, ( short ) expected[3] );
- assertEquals(testdata3[ 0 ], 0x0D);
- assertEquals(testdata3[ 1 ], (byte)0x93);
- assertEquals(testdata3[ 2 ], (byte)0xFF);
- assertEquals(expected[ 2 ], LittleEndian.getUShort(testdata3));
- assertEquals(expected[ 3 ], LittleEndian.getUShort(testdata3, 1));
- //System.out.println("TD[1][0]: "+LittleEndian.getUShort(testdata)+" expecting 65281");
- //System.out.println("TD[1][1]: "+LittleEndian.getUShort(testdata, 1)+" expecting 767");
- //System.out.println("TD[2][0]: "+LittleEndian.getUShort(testdata2)+" expecting 37645");
- //System.out.println("TD[2][1]: "+LittleEndian.getUShort(testdata2, 1)+" expecting 65427");
- //System.out.println("TD[3][0]: "+LittleEndian.getUShort(testdata3)+" expecting 37645");
- //System.out.println("TD[3][1]: "+LittleEndian.getUShort(testdata3, 1)+" expecting 65427");
+ LittleEndian.putUShort(testdata3, 0, expected2);
+ LittleEndian.putUShort(testdata3, 1, expected3);
+ assertEquals(testdata3[0], 0x0D);
+ assertEquals(testdata3[1], (byte)0x93);
+ assertEquals(testdata3[2], (byte)0xFF);
+ assertEquals(expected2, LittleEndian.getUShort(testdata3));
+ assertEquals(expected3, LittleEndian.getUShort(testdata3, 1));
}
@@ -123,19 +96,15 @@ public class TestLittleEndian
/**
* test the getDouble() method
*/
-
- public void testGetDouble()
- {
- assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), 0.000001 );
- assertEquals(_doubles[ 1 ], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
+ public void testGetDouble() {
+ assertEquals(_doubles[0], LittleEndian.getDouble(_double_array), 0.000001 );
+ assertEquals(_doubles[1], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array)));
double nan = LittleEndian.getDouble(_nan_double_array);
byte[] data = new byte[8];
LittleEndian.putDouble(data, nan);
- for ( int i = 0; i < data.length; i++ )
- {
- byte b = data[i];
+ for ( int i = 0; i < data.length; i++ ) {
assertEquals(data[i], _nan_double_array[i]);
}
}
@@ -143,192 +112,154 @@ public class TestLittleEndian
/**
* test the getInt() method
*/
-
- public void testGetInt()
- {
- byte[] testdata = new byte[ LittleEndian.INT_SIZE + 1 ];
-
- testdata[ 0 ] = 0x01;
- testdata[ 1 ] = ( byte ) 0xFF;
- testdata[ 2 ] = ( byte ) 0xFF;
- testdata[ 3 ] = ( byte ) 0xFF;
- testdata[ 4 ] = 0x02;
- int expected[] = new int[ 2 ];
-
- expected[ 0 ] = 0xFFFFFF01;
- expected[ 1 ] = 0x02FFFFFF;
- assertEquals(expected[ 0 ], LittleEndian.getInt(testdata));
- assertEquals(expected[ 1 ], LittleEndian.getInt(testdata, 1));
+ public void testGetInt() {
+ // reading 4 byte data from a 5 byte buffer
+ byte[] testdata = {
+ (byte) 0x01,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0x02,
+ };
+
+ assertEquals(0xFFFFFF01, LittleEndian.getInt(testdata));
+ assertEquals(0x02FFFFFF, LittleEndian.getInt(testdata, 1));
}
/**
* test the getLong method
*/
-
- public void testGetLong()
- {
- byte[] testdata = new byte[ LittleEndian.LONG_SIZE + 1 ];
-
- testdata[ 0 ] = 0x01;
- testdata[ 1 ] = ( byte ) 0xFF;
- testdata[ 2 ] = ( byte ) 0xFF;
- testdata[ 3 ] = ( byte ) 0xFF;
- testdata[ 4 ] = ( byte ) 0xFF;
- testdata[ 5 ] = ( byte ) 0xFF;
- testdata[ 6 ] = ( byte ) 0xFF;
- testdata[ 7 ] = ( byte ) 0xFF;
- testdata[ 8 ] = 0x02;
- long expected[] = new long[ 2 ];
-
- expected[ 0 ] = 0xFFFFFFFFFFFFFF01L;
- expected[ 1 ] = 0x02FFFFFFFFFFFFFFL;
- assertEquals(expected[ 0 ], LittleEndian.getLong(testdata));
- assertEquals(expected[ 1 ], LittleEndian.getLong(testdata, 1));
+ public void testGetLong() {
+
+ // reading 8 byte values from a 9 byte buffer
+ byte[] testdata = {
+ (byte) 0x01,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0x02,
+ };
+
+ assertEquals(0xFFFFFFFFFFFFFF01L, LittleEndian.getLong(testdata));
+ assertEquals(0x02FFFFFFFFFFFFFFL, LittleEndian.getLong(testdata, 1));
}
/**
* test the PutShort method
*/
-
- public void testPutShort()
- {
+ public void testPutShort() {
byte[] expected = new byte[ LittleEndian.SHORT_SIZE + 1 ];
- expected[ 0 ] = 0x01;
- expected[ 1 ] = ( byte ) 0xFF;
- expected[ 2 ] = 0x02;
+ expected[0] = 0x01;
+ expected[1] = (byte) 0xFF;
+ expected[2] = 0x02;
byte[] received = new byte[ LittleEndian.SHORT_SIZE + 1 ];
- short testdata[] = new short[ 2 ];
-
- testdata[ 0 ] = ( short ) 0xFF01;
- testdata[ 1 ] = 0x02FF;
- LittleEndian.putShort(received, testdata[ 0 ]);
- assertTrue(ba_equivalent(received, expected, 0,
- LittleEndian.SHORT_SIZE));
- LittleEndian.putShort(received, 1, testdata[ 1 ]);
- assertTrue(ba_equivalent(received, expected, 1,
- LittleEndian.SHORT_SIZE));
+ short testdata[] = new short[2];
+
+ testdata[0] = ( short ) 0xFF01;
+ testdata[1] = 0x02FF;
+ LittleEndian.putShort(received, testdata[0]);
+ assertTrue(compareByteArrays(received, expected, 0, LittleEndian.SHORT_SIZE));
+ LittleEndian.putShort(received, 1, testdata[1]);
+ assertTrue(compareByteArrays(received, expected, 1, LittleEndian.SHORT_SIZE));
}
/**
* test the putInt method
*/
-
- public void testPutInt()
- {
- byte[] expected = new byte[ LittleEndian.INT_SIZE + 1 ];
-
- expected[ 0 ] = 0x01;
- expected[ 1 ] = ( byte ) 0xFF;
- expected[ 2 ] = ( byte ) 0xFF;
- expected[ 3 ] = ( byte ) 0xFF;
- expected[ 4 ] = 0x02;
- byte[] received = new byte[ LittleEndian.INT_SIZE + 1 ];
- int testdata[] = new int[ 2 ];
-
- testdata[ 0 ] = 0xFFFFFF01;
- testdata[ 1 ] = 0x02FFFFFF;
- LittleEndian.putInt(received, testdata[ 0 ]);
- assertTrue(ba_equivalent(received, expected, 0,
- LittleEndian.INT_SIZE));
- LittleEndian.putInt(received, 1, testdata[ 1 ]);
- assertTrue(ba_equivalent(received, expected, 1,
- LittleEndian.INT_SIZE));
+ public void testPutInt() {
+ // writing 4 byte data to a 5 byte buffer
+ byte[] expected = {
+ (byte) 0x01,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0x02,
+ };
+ byte[] received = new byte[ LittleEndian.INT_SIZE + 1 ];
+
+ LittleEndian.putInt(received, 0xFFFFFF01);
+ assertTrue(compareByteArrays(received, expected, 0, LittleEndian.INT_SIZE));
+ LittleEndian.putInt(received, 1, 0x02FFFFFF);
+ assertTrue(compareByteArrays(received, expected, 1, LittleEndian.INT_SIZE));
}
/**
* test the putDouble methods
*/
-
- public void testPutDouble()
- {
+ public void testPutDouble() {
byte[] received = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];
- LittleEndian.putDouble(received, _doubles[ 0 ]);
- assertTrue(ba_equivalent(received, _double_array, 0,
- LittleEndian.DOUBLE_SIZE));
- LittleEndian.putDouble(received, 1, _doubles[ 1 ]);
+ LittleEndian.putDouble(received, _doubles[0]);
+ assertTrue(compareByteArrays(received, _double_array, 0, LittleEndian.DOUBLE_SIZE));
+ LittleEndian.putDouble(received, 1, _doubles[1]);
byte[] expected = new byte[ LittleEndian.DOUBLE_SIZE + 1 ];
System.arraycopy(_double_array, LittleEndian.DOUBLE_SIZE, expected,
1, LittleEndian.DOUBLE_SIZE);
- assertTrue(ba_equivalent(received, expected, 1,
- LittleEndian.DOUBLE_SIZE));
+ assertTrue(compareByteArrays(received, expected, 1, LittleEndian.DOUBLE_SIZE));
}
/**
* test the putLong method
*/
-
- public void testPutLong()
- {
- byte[] expected = new byte[ LittleEndian.LONG_SIZE + 1 ];
-
- expected[ 0 ] = 0x01;
- expected[ 1 ] = ( byte ) 0xFF;
- expected[ 2 ] = ( byte ) 0xFF;
- expected[ 3 ] = ( byte ) 0xFF;
- expected[ 4 ] = ( byte ) 0xFF;
- expected[ 5 ] = ( byte ) 0xFF;
- expected[ 6 ] = ( byte ) 0xFF;
- expected[ 7 ] = ( byte ) 0xFF;
- expected[ 8 ] = 0x02;
+ public void testPutLong() {
+ // writing 8 byte values to a 9 byte buffer
+ byte[] expected = {
+ (byte) 0x01,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0xFF,
+ (byte) 0x02,
+ };
byte[] received = new byte[ LittleEndian.LONG_SIZE + 1 ];
- long testdata[] = new long[ 2 ];
-
- testdata[ 0 ] = 0xFFFFFFFFFFFFFF01L;
- testdata[ 1 ] = 0x02FFFFFFFFFFFFFFL;
- LittleEndian.putLong(received, testdata[ 0 ]);
- assertTrue(ba_equivalent(received, expected, 0,
- LittleEndian.LONG_SIZE));
- LittleEndian.putLong(received, 1, testdata[ 1 ]);
- assertTrue(ba_equivalent(received, expected, 1,
- LittleEndian.LONG_SIZE));
+
+ long testdata0 = 0xFFFFFFFFFFFFFF01L;
+ long testdata1 = 0x02FFFFFFFFFFFFFFL;
+ LittleEndian.putLong(received, testdata0);
+ assertTrue(compareByteArrays(received, expected, 0, LittleEndian.LONG_SIZE));
+ LittleEndian.putLong(received, 1, testdata1);
+ assertTrue(compareByteArrays(received, expected, 1, LittleEndian.LONG_SIZE));
}
- private static byte[] _good_array =
- {
- 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01,
- 0x02, 0x01, 0x02, 0x01, 0x02
+ private static byte[] _good_array = {
+ 0x01, 0x02, 0x01, 0x02,
+ 0x01, 0x02, 0x01, 0x02,
+ 0x01, 0x02, 0x01, 0x02,
+ 0x01, 0x02, 0x01, 0x02,
};
- private static byte[] _bad_array =
- {
+ private static byte[] _bad_array = {
0x01
};
/**
* test the readShort method
*/
-
- public void testReadShort()
- throws IOException
- {
+ public void testReadShort() throws IOException {
short expected_value = 0x0201;
InputStream stream = new ByteArrayInputStream(_good_array);
int count = 0;
- while (true)
- {
+ while (stream.available() > 0) {
short value = LittleEndian.readShort(stream);
-
- if (value == 0)
- {
- break;
- }
assertEquals(value, expected_value);
count++;
}
assertEquals(count,
_good_array.length / LittleEndianConsts.SHORT_SIZE);
stream = new ByteArrayInputStream(_bad_array);
- try
- {
+ try {
LittleEndian.readShort(stream);
fail("Should have caught BufferUnderrunException");
- }
- catch (BufferUnderrunException ignored)
- {
-
+ } catch (BufferUnderrunException ignored) {
// as expected
}
}
@@ -336,34 +267,22 @@ public class TestLittleEndian
/**
* test the readInt method
*/
-
- public void testReadInt()
- throws IOException
- {
+ public void testReadInt() throws IOException {
int expected_value = 0x02010201;
InputStream stream = new ByteArrayInputStream(_good_array);
int count = 0;
- while (true)
- {
+ while (stream.available() > 0) {
int value = LittleEndian.readInt(stream);
-
- if (value == 0)
- {
- break;
- }
assertEquals(value, expected_value);
count++;
}
assertEquals(count, _good_array.length / LittleEndianConsts.INT_SIZE);
stream = new ByteArrayInputStream(_bad_array);
- try
- {
+ try {
LittleEndian.readInt(stream);
fail("Should have caught BufferUnderrunException");
- }
- catch (BufferUnderrunException ignored)
- {
+ } catch (BufferUnderrunException ignored) {
// as expected
}
@@ -372,104 +291,60 @@ public class TestLittleEndian
/**
* test the readLong method
*/
-
- public void testReadLong()
- throws IOException
- {
+ public void testReadLong() throws IOException {
long expected_value = 0x0201020102010201L;
InputStream stream = new ByteArrayInputStream(_good_array);
int count = 0;
- while (true)
- {
+ while (stream.available() > 0) {
long value = LittleEndian.readLong(stream);
-
- if (value == 0)
- {
- break;
- }
assertEquals(value, expected_value);
count++;
}
assertEquals(count,
_good_array.length / LittleEndianConsts.LONG_SIZE);
stream = new ByteArrayInputStream(_bad_array);
- try
- {
+ try {
LittleEndian.readLong(stream);
fail("Should have caught BufferUnderrunException");
- }
- catch (BufferUnderrunException ignored)
- {
-
+ } catch (BufferUnderrunException ignored) {
// as expected
}
}
- /**
- * test the readFromStream method
- */
-
- public void testReadFromStream()
- throws IOException
- {
- InputStream stream = new ByteArrayInputStream(_good_array);
- byte[] value = LittleEndian.readFromStream(stream,
- _good_array.length);
-
- assertTrue(ba_equivalent(value, _good_array, 0, _good_array.length));
- stream = new ByteArrayInputStream(_good_array);
- try
- {
- value = LittleEndian.readFromStream(stream,
- _good_array.length + 1);
- fail("Should have caught BufferUnderrunException");
- }
- catch (BufferUnderrunException ignored)
- {
-
- // as expected
- }
- }
-
- public void testUnsignedByteToInt()
- throws Exception
- {
+// public void testReadFromStream() throws IOException {
+// int actual;
+// actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 5, -128, }));
+// assertEquals(32773, actual);
+//
+// actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, }));
+// assertEquals(513, actual);
+//
+// try {
+// LittleEndian.readInt(new ByteArrayInputStream(new byte[] { 1, 2, 3, }));
+// fail("Should have caught BufferUnderrunException");
+// } catch (BufferUnderrunException ignored) {
+// // as expected
+// }
+// }
+
+ public void testUnsignedByteToInt() {
assertEquals(255, LittleEndian.ubyteToInt((byte)255));
}
- private boolean ba_equivalent(byte [] received, byte [] expected,
- int offset, int size)
- {
- boolean result = true;
+ private static boolean compareByteArrays(byte [] received, byte [] expected,
+ int offset, int size) {
- for (int j = offset; j < offset + size; j++)
- {
- if (received[ j ] != expected[ j ])
- {
+ for (int j = offset; j < offset + size; j++) {
+ if (received[j] != expected[j]) {
System.out.println("difference at index " + j);
- result = false;
- break;
+ return false;
}
}
- return result;
+ return true;
}
- public void testUnsignedShort()
- throws Exception
- {
+ public void testUnsignedShort() {
assertEquals(0xffff, LittleEndian.getUShort(new byte[] { (byte)0xff, (byte)0xff }, 0));
}
-
- /**
- * main method to run the unit tests
- *
- * @param ignored_args
- */
-
- public static void main(String [] ignored_args)
- {
- System.out.println("Testing util.LittleEndian functionality");
- junit.textui.TestRunner.run(TestLittleEndian.class);
- }
}
diff --git a/src/testcases/org/apache/poi/util/TestLongField.java b/src/testcases/org/apache/poi/util/TestLongField.java
index 05ba965f96..a37e88ed3b 100644
--- a/src/testcases/org/apache/poi/util/TestLongField.java
+++ b/src/testcases/org/apache/poi/util/TestLongField.java
@@ -1,4 +1,3 @@
-
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
@@ -15,7 +14,6 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
package org.apache.poi.util;
@@ -28,31 +26,13 @@ import java.io.*;
*
* @author Marc Johnson (mjohnson at apache dot org)
*/
-
-public class TestLongField
- extends TestCase
-{
-
- /**
- * Constructor
- *
- * @param name
- */
-
- public TestLongField(String name)
- {
- super(name);
- }
+public final class TestLongField extends TestCase {
static private final long[] _test_array =
{
Long.MIN_VALUE, -1L, 0L, 1L, Long.MAX_VALUE
};
- /**
- * Test constructors.
- */
-
public void testConstructors()
{
try
@@ -121,10 +101,6 @@ public class TestLongField
}
}
- /**
- * Test set() methods
- */
-
public void testSet()
{
LongField field = new LongField(0);
@@ -163,10 +139,6 @@ public class TestLongField
}
}
- /**
- * Test readFromBytes
- */
-
public void testReadFromBytes()
{
LongField field = new LongField(1);
@@ -198,12 +170,6 @@ public class TestLongField
}
}
- /**
- * Test readFromStream
- *
- * @exception IOException
- */
-
public void testReadFromStream()
throws IOException
{
@@ -212,8 +178,8 @@ public class TestLongField
for (int j = 0; j < _test_array.length; j++)
{
- buffer[ (j * 8) + 0 ] = ( byte ) (_test_array[ j ] % 256);
- buffer[ (j * 8) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
+ buffer[ (j * 8) + 0 ] = ( byte ) ((_test_array[ j ] >> 0) % 256);
+ buffer[ (j * 8) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
buffer[ (j * 8) + 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256);
buffer[ (j * 8) + 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256);
buffer[ (j * 8) + 4 ] = ( byte ) ((_test_array[ j ] >> 32) % 256);
@@ -230,10 +196,6 @@ public class TestLongField
}
}
- /**
- * test writeToBytes
- */
-
public void testWriteToBytes()
{
LongField field = new LongField(0);
@@ -256,16 +218,4 @@ public class TestLongField
assertEquals("testing ", _test_array[ j ], val);
}
}
-
- /**
- * Main
- *
- * @param args
- */
-
- public static void main(String [] args)
- {
- System.out.println("Testing util.LongField functionality");
- junit.textui.TestRunner.run(TestLongField.class);
- }
}