You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ki...@apache.org on 2019/12/27 23:00:20 UTC
svn commit: r1872041 [18/23] - in /poi/trunk/src:
excelant/testcases/org/apache/poi/ss/examples/formula/
excelant/testcases/org/apache/poi/ss/excelant/
excelant/testcases/org/apache/poi/ss/excelant/util/
java/org/apache/poi/hssf/record/aggregates/ java...
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java Fri Dec 27 23:00:13 2019
@@ -17,19 +17,24 @@
package org.apache.poi.poifs.filesystem;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
/**
* Class to test POIFSDocumentPath functionality
- *
- * @author Marc Johnson
*/
-public final class TestPOIFSDocumentPath extends TestCase {
+public final class TestPOIFSDocumentPath {
/**
* Test default constructor
*/
+ @Test
public void testDefaultConstructor() {
POIFSDocumentPath path = new POIFSDocumentPath();
@@ -39,36 +44,28 @@ public final class TestPOIFSDocumentPath
/**
* Test full path constructor
*/
+ @Test
public void testFullPathConstructor() {
- String[] components =
- {
- "foo", "bar", "foobar", "fubar"
- };
+ String[] components = {"foo", "bar", "foobar", "fubar"};
- for (int j = 0; j < components.length; j++)
- {
+ for (int j = 0; j < components.length; j++) {
String[] params = new String[ j ];
System.arraycopy(components, 0, params, 0, j);
POIFSDocumentPath path = new POIFSDocumentPath(params);
assertEquals(j, path.length());
- for (int k = 0; k < j; k++)
- {
+ for (int k = 0; k < j; k++) {
assertEquals(components[ k ], path.getComponent(k));
}
- if (j == 0)
- {
+ if (j == 0) {
assertNull(path.getParent());
- }
- else
- {
+ } else {
POIFSDocumentPath parent = path.getParent();
assertNotNull(parent);
assertEquals(j - 1, parent.length());
- for (int k = 0; k < j - 1; k++)
- {
+ for (int k = 0; k < j - 1; k++) {
assertEquals(components[ k ], parent.getComponent(k));
}
}
@@ -76,227 +73,132 @@ public final class TestPOIFSDocumentPath
// test weird variants
assertEquals(0, new POIFSDocumentPath(null).length());
- try
- {
- new POIFSDocumentPath(new String[]
- {
- "fu", ""
- });
+ try {
+ new POIFSDocumentPath(new String[]{"fu", ""});
fail("should have caught IllegalArgumentException");
- }
- catch (IllegalArgumentException ignored)
- {
- }
- try
- {
- new POIFSDocumentPath(new String[]
- {
- "fu", null
- });
+ } catch (IllegalArgumentException ignored) { }
+ try {
+ new POIFSDocumentPath(new String[]{"fu", null});
fail("should have caught IllegalArgumentException");
- }
- catch (IllegalArgumentException ignored)
- {
- }
+ } catch (IllegalArgumentException ignored) { }
}
/**
* Test relative path constructor
*/
+ @Test
public void testRelativePathConstructor() {
- String[] initialComponents =
- {
- "a", "b", "c"
- };
+ String[] initialComponents = {"a", "b", "c"};
- for (int n = 0; n < initialComponents.length; n++)
- {
+ for (int n = 0; n < initialComponents.length; n++) {
String[] initialParams = new String[ n ];
System.arraycopy(initialComponents, 0, initialParams, 0, n);
- POIFSDocumentPath base =
- new POIFSDocumentPath(initialParams);
- String[] components =
- {
- "foo", "bar", "foobar", "fubar"
- };
+ POIFSDocumentPath base = new POIFSDocumentPath(initialParams);
+ String[] components = {"foo", "bar", "foobar", "fubar"};
- for (int j = 0; j < components.length; j++)
- {
+ for (int j = 0; j < components.length; j++) {
String[] params = new String[ j ];
System.arraycopy(components, 0, params, 0, j);
POIFSDocumentPath path = new POIFSDocumentPath(base, params);
assertEquals(j + n, path.length());
- for (int k = 0; k < n; k++)
- {
- assertEquals(initialComponents[ k ],
- path.getComponent(k));
+ for (int k = 0; k < n; k++) {
+ assertEquals(initialComponents[ k ], path.getComponent(k));
}
- for (int k = 0; k < j; k++)
- {
+ for (int k = 0; k < j; k++) {
assertEquals(components[ k ], path.getComponent(k + n));
}
- if ((j + n) == 0)
- {
+ if ((j + n) == 0) {
assertNull(path.getParent());
- }
- else
- {
+ } else {
POIFSDocumentPath parent = path.getParent();
assertNotNull(parent);
assertEquals(j + n - 1, parent.length());
- for (int k = 0; k < (j + n - 1); k++)
- {
- assertEquals(path.getComponent(k),
- parent.getComponent(k));
+ for (int k = 0; k < (j + n - 1); k++) {
+ assertEquals(path.getComponent(k), parent.getComponent(k));
}
}
}
// Test weird variants
-
+
// This one is allowed, even if it's really odd
assertEquals(n, new POIFSDocumentPath(base, null).length());
- new POIFSDocumentPath(base, new String[]
- {
- "fu", ""
- });
-
+ new POIFSDocumentPath(base, new String[]{"fu", ""});
+
// This one is allowed too
- new POIFSDocumentPath(base, new String[]
- {
- "", "fu"
- });
-
+ new POIFSDocumentPath(base, new String[]{"", "fu"});
+
// This one shouldn't be allowed
- try
- {
- new POIFSDocumentPath(base, new String[]
- {
- "fu", null
- });
+ try {
+ new POIFSDocumentPath(base, new String[]{"fu", null});
fail("should have caught IllegalArgumentException");
- }
- catch (IllegalArgumentException ignored)
- {
- }
-
+ } catch (IllegalArgumentException ignored) { }
+
// Ditto
- try
- {
- new POIFSDocumentPath(base, new String[]
- {
- null, "fu"
- });
+ try {
+ new POIFSDocumentPath(base, new String[]{null, "fu"});
fail("should have caught IllegalArgumentException");
- }
- catch (IllegalArgumentException ignored)
- {
- }
+ } catch (IllegalArgumentException ignored) { }
}
}
/**
* test equality
*/
+ @Test
public void testEquality() {
POIFSDocumentPath a1 = new POIFSDocumentPath();
POIFSDocumentPath a2 = new POIFSDocumentPath(null);
POIFSDocumentPath a3 = new POIFSDocumentPath(new String[ 0 ]);
POIFSDocumentPath a4 = new POIFSDocumentPath(a1, null);
- POIFSDocumentPath a5 = new POIFSDocumentPath(a1,
- new String[ 0 ]);
- POIFSDocumentPath[] paths =
- {
- a1, a2, a3, a4, a5
- };
+ POIFSDocumentPath a5 = new POIFSDocumentPath(a1, new String[ 0 ]);
- for (int j = 0; j < paths.length; j++)
- {
- for (int k = 0; k < paths.length; k++)
- {
- assertEquals(j + "<>" + k,
- paths[ j ], paths[ k ]);
+ POIFSDocumentPath[] paths = {a1, a2, a3, a4, a5};
+
+ for (int j = 0; j < paths.length; j++) {
+ for (int k = 0; k < paths.length; k++) {
+ assertEquals(j + "<>" + k, paths[ j ], paths[ k ]);
}
}
- a2 = new POIFSDocumentPath(a1, new String[]
- {
- "foo"
- });
- a3 = new POIFSDocumentPath(a2, new String[]
- {
- "bar"
- });
- a4 = new POIFSDocumentPath(a3, new String[]
- {
- "fubar"
- });
- a5 = new POIFSDocumentPath(a4, new String[]
- {
- "foobar"
- });
+ a2 = new POIFSDocumentPath(a1, new String[]{"foo"});
+ a3 = new POIFSDocumentPath(a2, new String[]{"bar"});
+ a4 = new POIFSDocumentPath(a3, new String[]{"fubar"});
+ a5 = new POIFSDocumentPath(a4, new String[]{"foobar"});
POIFSDocumentPath[] builtUpPaths =
{
a1, a2, a3, a4, a5
};
- POIFSDocumentPath[] fullPaths =
- {
- new POIFSDocumentPath(), new POIFSDocumentPath(new String[]
- {
- "foo"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "bar"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "bar", "fubar"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "bar", "fubar", "foobar"
- })
- };
-
- for (int k = 0; k < builtUpPaths.length; k++)
- {
- for (int j = 0; j < fullPaths.length; j++)
- {
- if (k == j)
- {
- assertEquals(j + "<>"
- + k, fullPaths[ j ],
- builtUpPaths[ k ]);
- }
- else
- {
- assertFalse(fullPaths[j].equals(builtUpPaths[k]));
+ POIFSDocumentPath[] fullPaths = {
+ new POIFSDocumentPath(), new POIFSDocumentPath(new String[]{"foo"}),
+ new POIFSDocumentPath(new String[]{"foo", "bar"}),
+ new POIFSDocumentPath(new String[]{"foo", "bar", "fubar"}),
+ new POIFSDocumentPath(new String[]{"foo", "bar", "fubar", "foobar"})
+ };
+
+ for (int k = 0; k < builtUpPaths.length; k++) {
+ for (int j = 0; j < fullPaths.length; j++) {
+ if (k == j) {
+ assertEquals(j + "<>" + k, fullPaths[ j ], builtUpPaths[ k ]);
+ } else {
+ assertNotEquals(fullPaths[j], builtUpPaths[k]);
}
}
}
- POIFSDocumentPath[] badPaths =
- {
- new POIFSDocumentPath(new String[]
- {
- "_foo"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "_bar"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "bar", "_fubar"
- }), new POIFSDocumentPath(new String[]
- {
- "foo", "bar", "fubar", "_foobar"
- })
+ POIFSDocumentPath[] badPaths = {
+ new POIFSDocumentPath(new String[]{"_foo"}),
+ new POIFSDocumentPath(new String[]{"foo", "_bar"}),
+ new POIFSDocumentPath(new String[]{"foo", "bar", "_fubar"}),
+ new POIFSDocumentPath(new String[]{"foo", "bar", "fubar", "_foobar"})
};
for (int k = 0; k < builtUpPaths.length; k++)
{
- for (int j = 0; j < badPaths.length; j++)
- {
- assertFalse(fullPaths[k].equals(badPaths[j]));
+ for (POIFSDocumentPath badPath : badPaths) {
+ assertNotEquals(fullPaths[k], badPath);
}
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSStream.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSStream.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSStream.java Fri Dec 27 23:00:13 2019
@@ -37,7 +37,6 @@ import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
-import junit.framework.TestCase;
import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.PropertySet;
@@ -79,7 +78,7 @@ public final class TestPOIFSStream {
assertFalse(i.hasNext());
assertFalse(i.hasNext());
assertFalse(i.hasNext());
-
+
// Check the contents
assertEquals((byte)0x81, b.get());
assertEquals((byte)0x00, b.get());
@@ -89,17 +88,17 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b.get());
assertEquals((byte)0x00, b.get());
assertEquals((byte)0x00, b.get());
-
+
fs.close();
}
/**
- * Read a stream with only two blocks in it
+ * Read a stream with only two blocks in it
*/
@Test
public void testReadShortStream() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
-
+
// 97 -> 98 -> end
POIFSStream stream = new POIFSStream(fs, 97);
Iterator<ByteBuffer> i = stream.getBlockIterator();
@@ -113,7 +112,7 @@ public final class TestPOIFSStream {
assertFalse(i.hasNext());
assertFalse(i.hasNext());
assertFalse(i.hasNext());
-
+
// Check the contents of the 1st block
assertEquals((byte)0x01, b97.get());
assertEquals((byte)0x00, b97.get());
@@ -123,7 +122,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b97.get());
assertEquals((byte)0x00, b97.get());
assertEquals((byte)0x00, b97.get());
-
+
// Check the contents of the 2nd block
assertEquals((byte)0x81, b98.get());
assertEquals((byte)0x00, b98.get());
@@ -133,21 +132,21 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b98.get());
assertEquals((byte)0x00, b98.get());
assertEquals((byte)0x00, b98.get());
-
+
fs.close();
}
-
+
/**
- * Read a stream with many blocks
+ * Read a stream with many blocks
*/
@Test
public void testReadLongerStream() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
-
+
ByteBuffer b0 = null;
ByteBuffer b1 = null;
ByteBuffer b22 = null;
-
+
// The stream at 0 has 23 blocks in it
POIFSStream stream = new POIFSStream(fs, 0);
Iterator<ByteBuffer> i = stream.getBlockIterator();
@@ -163,24 +162,24 @@ public final class TestPOIFSStream {
if(count == 22) {
b22 = b;
}
-
+
count++;
}
assertEquals(23, count);
-
+
// Check the contents
// 1st block is at 0
assertEquals((byte)0x9e, b0.get());
assertEquals((byte)0x75, b0.get());
assertEquals((byte)0x97, b0.get());
assertEquals((byte)0xf6, b0.get());
-
+
// 2nd block is at 1
assertEquals((byte)0x86, b1.get());
assertEquals((byte)0x09, b1.get());
assertEquals((byte)0x22, b1.get());
assertEquals((byte)0xfb, b1.get());
-
+
// last block is at 89
assertEquals((byte)0xfe, b22.get());
assertEquals((byte)0xff, b22.get());
@@ -190,17 +189,17 @@ public final class TestPOIFSStream {
assertEquals((byte)0x01, b22.get());
assertEquals((byte)0x02, b22.get());
assertEquals((byte)0x00, b22.get());
-
+
fs.close();
}
/**
- * Read a stream with several blocks in a 4096 byte block file
+ * Read a stream with several blocks in a 4096 byte block file
*/
@Test
public void testReadStream4096() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
-
+
// 0 -> 1 -> 2 -> end
POIFSStream stream = new POIFSStream(fs, 0);
Iterator<ByteBuffer> i = stream.getBlockIterator();
@@ -217,7 +216,7 @@ public final class TestPOIFSStream {
assertFalse(i.hasNext());
assertFalse(i.hasNext());
assertFalse(i.hasNext());
-
+
// Check the contents of the 1st block
assertEquals((byte)0x9E, b0.get());
assertEquals((byte)0x75, b0.get());
@@ -227,7 +226,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x21, b0.get());
assertEquals((byte)0xD2, b0.get());
assertEquals((byte)0x11, b0.get());
-
+
// Check the contents of the 2nd block
assertEquals((byte)0x00, b1.get());
assertEquals((byte)0x00, b1.get());
@@ -237,7 +236,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b1.get());
assertEquals((byte)0x00, b1.get());
assertEquals((byte)0x00, b1.get());
-
+
// Check the contents of the 3rd block
assertEquals((byte)0x6D, b2.get());
assertEquals((byte)0x00, b2.get());
@@ -247,39 +246,39 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b2.get());
assertEquals((byte)0x46, b2.get());
assertEquals((byte)0x00, b2.get());
-
+
fs.close();
}
-
+
/**
* Craft a nasty file with a loop, and ensure we don't get stuck
*/
@Test
public void testReadFailsOnLoop() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
-
+
// Hack the FAT so that it goes 0->1->2->0
fs.setNextBlock(0, 1);
fs.setNextBlock(1, 2);
fs.setNextBlock(2, 0);
-
+
// Now try to read
POIFSStream stream = new POIFSStream(fs, 0);
Iterator<ByteBuffer> i = stream.getBlockIterator();
assertTrue(i.hasNext());
-
+
// 1st read works
i.next();
assertTrue(i.hasNext());
-
+
// 2nd read works
i.next();
assertTrue(i.hasNext());
-
+
// 3rd read works
i.next();
assertTrue(i.hasNext());
-
+
// 4th read blows up as it loops back to 0
try {
i.next();
@@ -288,7 +287,7 @@ public final class TestPOIFSStream {
// Good, it was detected
}
assertTrue(i.hasNext());
-
+
fs.close();
}
@@ -300,7 +299,7 @@ public final class TestPOIFSStream {
public void testReadMiniStreams() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
POIFSMiniStore ministore = fs.getMiniStore();
-
+
// 178 -> 179 -> 180 -> end
POIFSStream stream = new POIFSStream(ministore, 178);
Iterator<ByteBuffer> i = stream.getBlockIterator();
@@ -316,7 +315,7 @@ public final class TestPOIFSStream {
assertFalse(i.hasNext());
assertFalse(i.hasNext());
assertFalse(i.hasNext());
-
+
// Check the contents of the 1st block
assertEquals((byte)0xfe, b178.get());
assertEquals((byte)0xff, b178.get());
@@ -326,7 +325,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x01, b178.get());
assertEquals((byte)0x02, b178.get());
assertEquals((byte)0x00, b178.get());
-
+
// And the 2nd
assertEquals((byte)0x6c, b179.get());
assertEquals((byte)0x00, b179.get());
@@ -336,7 +335,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b179.get());
assertEquals((byte)0x00, b179.get());
assertEquals((byte)0x00, b179.get());
-
+
// And the 3rd
assertEquals((byte)0x30, b180.get());
assertEquals((byte)0x00, b180.get());
@@ -346,7 +345,7 @@ public final class TestPOIFSStream {
assertEquals((byte)0x00, b180.get());
assertEquals((byte)0x00, b180.get());
assertEquals((byte)0x80, b180.get());
-
+
fs.close();
}
@@ -356,22 +355,22 @@ public final class TestPOIFSStream {
@Test
public void testReplaceStream() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
-
+
byte[] data = new byte[512];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
-
+
// 98 is actually the last block in a two block stream...
POIFSStream stream = new POIFSStream(fs, 98);
stream.updateContents(data);
-
+
// Check the reading of blocks
Iterator<ByteBuffer> it = stream.getBlockIterator();
assertTrue(it.hasNext());
ByteBuffer b = it.next();
assertFalse(it.hasNext());
-
+
// Now check the contents
data = new byte[512];
b.get(data);
@@ -379,10 +378,10 @@ public final class TestPOIFSStream {
byte exp = (byte)(i%256);
assertEquals(exp, data[i]);
}
-
+
fs.close();
}
-
+
/**
* Writes less data than before, some blocks will need
* to be freed
@@ -424,7 +423,7 @@ public final class TestPOIFSStream {
}
}
}
-
+
/**
* Writes more data than before, new blocks will be needed
*/
@@ -471,14 +470,14 @@ public final class TestPOIFSStream {
assertEquals(3, count);
}
}
-
+
/**
* Writes to a new stream in the file
*/
@Test
public void testWriteNewStream() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
-
+
// 100 is our first free one
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(100));
@@ -486,17 +485,17 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(102));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(103));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(104));
-
-
+
+
// Add a single block one
byte[] data = new byte[512];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
-
+
POIFSStream stream = new POIFSStream(fs);
stream.updateContents(data);
-
+
// Check it was allocated properly
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(100));
@@ -504,7 +503,7 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(102));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(103));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(104));
-
+
// And check the contents
Iterator<ByteBuffer> it = stream.getBlockIterator();
int count = 0;
@@ -519,17 +518,17 @@ public final class TestPOIFSStream {
count++;
}
assertEquals(1, count);
-
-
+
+
// And a multi block one
data = new byte[512*3];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
-
+
stream = new POIFSStream(fs);
stream.updateContents(data);
-
+
// Check it was allocated properly
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(100));
@@ -537,7 +536,7 @@ public final class TestPOIFSStream {
assertEquals(103, fs.getNextBlock(102));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(103));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(104));
-
+
// And check the contents
it = stream.getBlockIterator();
count = 0;
@@ -552,7 +551,7 @@ public final class TestPOIFSStream {
count++;
}
assertEquals(3, count);
-
+
// Free it
stream.free();
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
@@ -561,10 +560,10 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(102));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(103));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(104));
-
+
fs.close();
}
-
+
/**
* Writes to a new stream in the file, where we've not enough
* free blocks so new FAT segments will need to be allocated
@@ -573,7 +572,7 @@ public final class TestPOIFSStream {
@Test
public void testWriteNewStreamExtraFATs() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
-
+
// Allocate almost all the blocks
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(99));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(100));
@@ -584,7 +583,7 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(127));
assertTrue(fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
-
+
// Write a 3 block stream
byte[] data = new byte[512*3];
for(int i=0; i<data.length; i++) {
@@ -592,11 +591,11 @@ public final class TestPOIFSStream {
}
POIFSStream stream = new POIFSStream(fs);
stream.updateContents(data);
-
+
// Check we got another BAT
assertFalse(fs.getBATBlockAndIndex(0).getBlock().hasFreeSectors());
assertTrue(fs.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
-
+
// the BAT will be in the first spot of the new block
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(126));
assertEquals(129, fs.getNextBlock(127));
@@ -604,10 +603,10 @@ public final class TestPOIFSStream {
assertEquals(130, fs.getNextBlock(129));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(130));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(131));
-
+
fs.close();
}
-
+
/**
* Replaces data in an existing stream, with a bit
* more data than before, in a 4096 byte block file
@@ -615,27 +614,27 @@ public final class TestPOIFSStream {
@Test
public void testWriteStream4096() throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
-
+
// 0 -> 1 -> 2 -> end
assertEquals(1, fs.getNextBlock(0));
assertEquals(2, fs.getNextBlock(1));
assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
assertEquals(4, fs.getNextBlock(3));
-
+
// First free one is at 15
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(14));
assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(15));
-
-
- // Write a 5 block file
+
+
+ // Write a 5 block file
byte[] data = new byte[4096*5];
for(int i=0; i<data.length; i++) {
data[i] = (byte)(i%256);
}
POIFSStream stream = new POIFSStream(fs, 0);
stream.updateContents(data);
-
-
+
+
// Check it
assertEquals(1, fs.getNextBlock(0));
assertEquals(2, fs.getNextBlock(1));
@@ -660,10 +659,10 @@ public final class TestPOIFSStream {
count++;
}
assertEquals(5, count);
-
+
fs.close();
}
-
+
/**
* Tests that we can write into the mini stream
*/
@@ -895,7 +894,7 @@ public final class TestPOIFSStream {
}
}
}
-
+
/**
* Tests adding a new stream, writing and reading it.
*/
@@ -964,7 +963,7 @@ public final class TestPOIFSStream {
assertFalse(it.hasNext());
}
}
-
+
/**
* Writes a stream, then replaces it
*/
@@ -1002,16 +1001,16 @@ public final class TestPOIFSStream {
assertEquals(4106, normal.getSize());
assertEquals(4106, ((DocumentNode)normal).getProperty().getSize());
-
+
// Replace with one still big enough for a main stream, but one block smaller
byte[] main4096 = new byte[4096];
main4096[0] = -10;
main4096[4095] = -11;
-
+
DocumentOutputStream nout = new DocumentOutputStream(normal);
nout.write(main4096);
nout.close();
-
+
// Will have dropped to 8
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK,bat.getValueAt(1));
@@ -1029,12 +1028,12 @@ public final class TestPOIFSStream {
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(4096, normal.getSize());
assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
-
-
+
+
// Write and check
fs = writeOutAndReadBack(fs);
bat = fs.getBATBlockAndIndex(0).getBlock();
-
+
// No change after write
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(0)); // Properties
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK,bat.getValueAt(1));
@@ -1048,12 +1047,12 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(9)); // End of Normal
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(10));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
-
+
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(4096, normal.getSize());
assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
-
-
+
+
// Make longer, take 1 block at the end
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
nout = new DocumentOutputStream(normal);
@@ -1073,19 +1072,19 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Normal
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
-
+
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(4106, normal.getSize());
assertEquals(4106, ((DocumentNode)normal).getProperty().getSize());
-
+
// Make it small, will trigger the SBAT stream and free lots up
byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
nout = new DocumentOutputStream(normal);
nout.write(mini);
nout.close();
-
+
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(1));
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2)); // SBAT
@@ -1099,17 +1098,17 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(10));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
-
+
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(7, normal.getSize());
assertEquals(7, ((DocumentNode)normal).getProperty().getSize());
-
+
// Finally back to big again
nout = new DocumentOutputStream(normal);
nout.write(main4096);
nout.close();
-
+
// Will keep the mini stream, now empty
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(1));
@@ -1125,16 +1124,16 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(13));
-
+
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(4096, normal.getSize());
assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
-
-
+
+
// Save, re-load, re-check
fs = writeOutAndReadBack(fs);
bat = fs.getBATBlockAndIndex(0).getBlock();
-
+
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(0));
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(1));
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2)); // SBAT
@@ -1149,11 +1148,11 @@ public final class TestPOIFSStream {
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(11));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(13));
-
+
normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
assertEquals(4096, normal.getSize());
assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
-
+
fs.close();
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPropertySorter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPropertySorter.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPropertySorter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPropertySorter.java Fri Dec 27 23:00:13 2019
@@ -17,22 +17,25 @@
package org.apache.poi.poifs.filesystem;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
-
-import junit.framework.ComparisonFailure;
-import junit.framework.TestCase;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.property.DirectoryProperty;
import org.apache.poi.poifs.property.Property;
+import org.junit.Test;
/**
* Verify the order of entries <code>DirectoryProperty</code> .
@@ -40,10 +43,8 @@ import org.apache.poi.poifs.property.Pro
* In particular it is important to serialize ROOT._VBA_PROJECT_CUR.VBA node.
* See bug 39234 in bugzilla. Thanks to Bill Seddon for providing the solution.
* </p>
- *
- * @author Yegor Kozlov
*/
-public final class TestPropertySorter extends TestCase {
+public final class TestPropertySorter {
//the correct order of entries in the test file
private static final String[] _entries = {
@@ -64,6 +65,7 @@ public final class TestPropertySorter ex
/**
* Test sorting of properties in <code>DirectoryProperty</code>
*/
+ @Test
public void testSortProperties() throws IOException {
POIFSFileSystem fs = openSampleFS();
Property[] props = getVBAProperties(fs);
@@ -71,27 +73,31 @@ public final class TestPropertySorter ex
assertEquals(_entries.length, props.length);
// (1). See that there is a problem with the old case-sensitive property comparator
- Arrays.sort(props, OldCaseSensitivePropertyComparator);
- try {
- for (int i = 0; i < props.length; i++) {
- assertEquals(_entries[i], props[i].getName());
- }
- fail("expected old case-sensitive property comparator to return properties in wrong order");
- } catch (ComparisonFailure e){
- // expected during successful test
- assertNotNull(e.getMessage());
- }
+ Arrays.sort(props, TestPropertySorter::oldCaseSensitivePropertyCompareTo);
+
+ String exp = String.join("", _entries);
+ String actOld = Stream.of(props).map(Property::getName).collect(Collectors.joining());
+
+ assertNotEquals("expected old case-sensitive property comparator to return properties in wrong order", exp, actOld);
// (2) Verify that the fixed property comparator works right
Arrays.sort(props, new DirectoryProperty.PropertyComparator());
- for (int i = 0; i < props.length; i++) {
- assertEquals(_entries[i], props[i].getName());
- }
+ String[] actNew = Stream.of(props).map(Property::getName).toArray(String[]::new);
+
+ assertArrayEquals(_entries, actNew);
+ }
+
+ private static int oldCaseSensitivePropertyCompareTo(Property o1, Property o2) {
+ String name1 = o1.getName();
+ String name2 = o2.getName();
+ int result = name1.length() - name2.length();
+ return (result != 0) ? result : name1.compareTo(name2);
}
/**
* Serialize file system and verify that the order of properties is the same as in the original file.
*/
+ @Test
public void testSerialization() throws IOException {
POIFSFileSystem fs = openSampleFS();
@@ -104,16 +110,14 @@ public final class TestPropertySorter ex
Property[] props = getVBAProperties(fs);
Arrays.sort(props, new DirectoryProperty.PropertyComparator());
- assertEquals(_entries.length, props.length);
- for (int i = 0; i < props.length; i++) {
- assertEquals(_entries[i], props[i].getName());
- }
+ String[] act = Stream.of(props).map(Property::getName).toArray(String[]::new);
+ assertArrayEquals(_entries, act);
}
/**
* @return array of properties read from ROOT._VBA_PROJECT_CUR.VBA node
*/
- protected Property[] getVBAProperties(POIFSFileSystem fs) throws IOException {
+ private Property[] getVBAProperties(POIFSFileSystem fs) throws IOException {
String _VBA_PROJECT_CUR = "_VBA_PROJECT_CUR";
String VBA = "VBA";
@@ -130,22 +134,4 @@ public final class TestPropertySorter ex
}
return lst.toArray(new Property[ 0 ]);
}
-
- /**
- * Old version of case-sensitive PropertyComparator to demonstrate the problem
- */
- private static final Comparator<Property> OldCaseSensitivePropertyComparator = new Comparator<Property>() {
-
- @Override
- public int compare(Property o1, Property o2) {
- String name1 = o1.getName();
- String name2 = o2.getName();
- int result = name1.length() - name2.length();
-
- if (result == 0) {
- result = name1.compareTo(name2);
- }
- return result;
- }
- };
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java Fri Dec 27 23:00:13 2019
@@ -15,104 +15,115 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
+
package org.apache.poi.poifs.nio;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+
import org.apache.poi.POIDataSamples;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
-
-import java.io.*;
-import java.nio.BufferUnderflowException;
-import java.nio.ByteBuffer;
+import org.junit.Test;
/**
* 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 {
- checkDataSource(ds, false);
- } finally {
- ds.close();
- }
-
- // try a second time
- ds = new FileBackedDataSource(f);
- try {
- checkDataSource(ds, false);
- } finally {
- ds.close();
- }
- }
-
- public void testFileWritable() throws Exception {
- File temp = TempFile.createTempFile("TestDataSource", ".test");
- try {
- writeDataToFile(temp);
-
- FileBackedDataSource ds = new FileBackedDataSource(temp, false);
- try {
- checkDataSource(ds, true);
- } finally {
- ds.close();
- }
-
- // try a second time
- ds = new FileBackedDataSource(temp, false);
- try {
- checkDataSource(ds, true);
- } finally {
- ds.close();
- }
-
- writeDataToFile(temp);
- } finally {
- assertTrue(temp.exists());
- assertTrue("Could not delete file " + temp, temp.delete());
- }
- }
-
-
- public void testRewritableFile() throws Exception {
- File temp = TempFile.createTempFile("TestDataSource", ".test");
- try {
- writeDataToFile(temp);
-
- FileBackedDataSource ds = new FileBackedDataSource(temp, true);
- try {
- ByteBuffer buf = ds.read(0, 10);
- assertNotNull(buf);
- buf = ds.read(8, 0x400);
- assertNotNull(buf);
- } finally {
- ds.close();
- }
-
- // try a second time
- ds = new FileBackedDataSource(temp, true);
- try {
- ByteBuffer buf = ds.read(0, 10);
- assertNotNull(buf);
- buf = ds.read(8, 0x400);
- assertNotNull(buf);
- } finally {
- ds.close();
- }
-
- writeDataToFile(temp);
- } finally {
- assertTrue(temp.exists());
- assertTrue(temp.delete());
- }
+public class TestDataSource {
+ private static POIDataSamples data = POIDataSamples.getPOIFSInstance();
+
+ @Test
+ public void testFile() throws Exception {
+ File f = data.getFile("Notes.ole2");
+
+ FileBackedDataSource ds = new FileBackedDataSource(f);
+ try {
+ checkDataSource(ds, false);
+ } finally {
+ ds.close();
+ }
+
+ // try a second time
+ ds = new FileBackedDataSource(f);
+ try {
+ checkDataSource(ds, false);
+ } finally {
+ ds.close();
+ }
+ }
+
+ @Test
+ public void testFileWritable() throws Exception {
+ File temp = TempFile.createTempFile("TestDataSource", ".test");
+ try {
+ writeDataToFile(temp);
+
+ FileBackedDataSource ds = new FileBackedDataSource(temp, false);
+ try {
+ checkDataSource(ds, true);
+ } finally {
+ ds.close();
+ }
+
+ // try a second time
+ ds = new FileBackedDataSource(temp, false);
+ try {
+ checkDataSource(ds, true);
+ } finally {
+ ds.close();
+ }
+
+ writeDataToFile(temp);
+ } finally {
+ assertTrue(temp.exists());
+ assertTrue("Could not delete file " + temp, temp.delete());
+ }
+ }
+
+
+ @Test
+ public void testRewritableFile() throws Exception {
+ File temp = TempFile.createTempFile("TestDataSource", ".test");
+ try {
+ writeDataToFile(temp);
+
+ FileBackedDataSource ds = new FileBackedDataSource(temp, true);
+ try {
+ ByteBuffer buf = ds.read(0, 10);
+ assertNotNull(buf);
+ buf = ds.read(8, 0x400);
+ assertNotNull(buf);
+ } finally {
+ ds.close();
+ }
+
+ // try a second time
+ ds = new FileBackedDataSource(temp, true);
+ try {
+ ByteBuffer buf = ds.read(0, 10);
+ assertNotNull(buf);
+ buf = ds.read(8, 0x400);
+ assertNotNull(buf);
+ } finally {
+ ds.close();
+ }
+
+ writeDataToFile(temp);
+ } finally {
+ assertTrue(temp.exists());
+ assertTrue(temp.delete());
+ }
}
private void writeDataToFile(File temp) throws IOException {
@@ -122,14 +133,14 @@ public class TestDataSource extends Test
}
}
}
-
+
private void checkDataSource(FileBackedDataSource ds, boolean writeable) throws IOException {
assertEquals(writeable, ds.isWriteable());
assertNotNull(ds.getChannel());
-
+
// rewriting changes the size
- if(writeable) {
- assertTrue("Had: " + ds.size(), ds.size() == 8192 || ds.size() == 8198);
+ if (writeable) {
+ assertTrue("Had: " + ds.size(), ds.size() == 8192 || ds.size() == 8198);
} else {
assertEquals(8192, ds.size());
}
@@ -168,7 +179,7 @@ public class TestDataSource extends Test
// Can't go off the end
try {
ds.read(4, 8192);
- if(!writeable) {
+ if (!writeable) {
fail("Shouldn't be able to read off the end of the file");
}
} catch (IndexOutOfBoundsException e) {
@@ -176,93 +187,94 @@ public class TestDataSource extends Test
}
}
- 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) {
- // expected here
- }
-
- // Past the end
- try {
- ds.read(4, 256);
- fail("Shouldn't be able to read off the end");
- } catch(IndexOutOfBoundsException e) {
- // expected here
- }
-
-
- // 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());
- }
+ @Test
+ 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) {
+ // expected here
+ }
+
+ // Past the end
+ try {
+ ds.read(4, 256);
+ fail("Shouldn't be able to read off the end");
+ } catch (IndexOutOfBoundsException e) {
+ // expected here
+ }
+
+
+ // 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());
+ }
}
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java Fri Dec 27 23:00:13 2019
@@ -17,6 +17,10 @@
package org.apache.poi.poifs.property;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -24,16 +28,14 @@ import org.apache.poi.hpsf.DocumentSumma
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.poifs.storage.RawDataUtil;
import org.apache.poi.util.LocaleUtil;
-
-import junit.framework.TestCase;
+import org.junit.Test;
/**
* Class to test DocumentProperty functionality
- *
- * @author Marc Johnson
*/
-public final class TestDocumentProperty extends TestCase {
+public final class TestDocumentProperty {
+ @Test
public void testConstructor() throws IOException {
// test with short name, small file
verifyProperty("foo", 1234);
@@ -48,6 +50,7 @@ public final class TestDocumentProperty
verifyProperty("A.really.long.long.long.name123", 4096);
}
+ @Test
public void testReadingConstructor() throws IOException {
String[] hexData = {
"52 00 6F 00 6F 00 74 00 20 00 45 00 6E 00 74 00 72 00 79 00 00 00 00 00 00 00 00 00 00 00 00 00",
Modified: poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java Fri Dec 27 23:00:13 2019
@@ -17,18 +17,20 @@
package org.apache.poi.poifs.storage;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import junit.framework.TestCase;
+import org.junit.Test;
/**
* Class to test HeaderBlockReader functionality
- *
- * @author Marc Johnson
*/
-public final class TestHeaderBlockReading extends TestCase {
+public final class TestHeaderBlockReading {
+ @Test
public void testConstructors() throws IOException {
String[] hexData = {
"D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3B 00 03 00 FE FF 09 00",
Modified: poi/trunk/src/testcases/org/apache/poi/ss/TestSpreadsheetVersion.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/TestSpreadsheetVersion.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/TestSpreadsheetVersion.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/TestSpreadsheetVersion.java Fri Dec 27 23:00:13 2019
@@ -17,15 +17,16 @@
package org.apache.poi.ss;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
/**
* Check that all enum values are properly set
- *
- * @author Yegor Kozlov
*/
-public final class TestSpreadsheetVersion extends TestCase {
+public final class TestSpreadsheetVersion {
+ @Test
public void testExcel97(){
SpreadsheetVersion v = SpreadsheetVersion.EXCEL97;
assertEquals(1 << 8, v.getMaxColumns());
@@ -37,6 +38,7 @@ public final class TestSpreadsheetVersio
assertEquals("IV", v.getLastColumnName());
}
+ @Test
public void testExcel2007(){
SpreadsheetVersion v = SpreadsheetVersion.EXCEL2007;
assertEquals(1 << 14, v.getMaxColumns());
Modified: poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatCondition.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatCondition.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatCondition.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatCondition.java Fri Dec 27 23:00:13 2019
@@ -16,9 +16,13 @@
==================================================================== */
package org.apache.poi.ss.format;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-public class TestCellFormatCondition extends TestCase {
+import org.junit.Test;
+
+public class TestCellFormatCondition {
+ @Test
public void testSVConditions() {
CellFormatCondition lt = CellFormatCondition.getInstance("<", "1.5");
assertTrue(lt.pass(1.4));
Modified: poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatResult.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatResult.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatResult.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormatResult.java Fri Dec 27 23:00:13 2019
@@ -16,22 +16,20 @@
==================================================================== */
package org.apache.poi.ss.format;
+import static org.junit.Assert.fail;
+
import java.awt.Color;
-import junit.framework.TestCase;
+import org.junit.Test;
+
+public class TestCellFormatResult {
-public class TestCellFormatResult extends TestCase {
-
+ @Test(expected = IllegalArgumentException.class)
public void testNullTextRaisesException() {
- try {
- final boolean applies = true;
- final String text = null;
- final Color textColor = Color.BLACK;
- final CellFormatResult result = new CellFormatResult(applies, text, textColor);
- fail("Cannot initialize CellFormatResult with null text parameter");
- }
- catch (IllegalArgumentException e) {
- //Expected
- }
+ final boolean applies = true;
+ final String text = null;
+ final Color textColor = Color.BLACK;
+ final CellFormatResult result = new CellFormatResult(applies, text, textColor);
+ fail("Cannot initialize CellFormatResult with null text parameter");
}
}
\ No newline at end of file
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/TestCellCacheEntry.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/TestCellCacheEntry.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/TestCellCacheEntry.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/TestCellCacheEntry.java Fri Dec 27 23:00:13 2019
@@ -17,26 +17,26 @@
package org.apache.poi.ss.formula;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
+import org.junit.Test;
/**
* Tests {@link org.apache.poi.ss.formula.CellCacheEntry}.
- *
- * @author Josh Micich
*/
-public class TestCellCacheEntry extends TestCase {
+public class TestCellCacheEntry {
+ @Test
public void testBasic() {
CellCacheEntry pcce = new PlainValueCellCacheEntry(new NumberEval(42.0));
ValueEval ve = pcce.getValue();
assertEquals(42, ((NumberEval)ve).getNumberValue(), 0.0);
-
+
FormulaCellCacheEntry fcce = new FormulaCellCacheEntry();
fcce.updateFormulaResult(new NumberEval(10.0), CellCacheEntry.EMPTY_ARRAY, null);
-
+
ve = fcce.getValue();
assertEquals(10, ((NumberEval)ve).getNumberValue(), 0.0);
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/TestEvaluationCache.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/TestEvaluationCache.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/TestEvaluationCache.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/TestEvaluationCache.java Fri Dec 27 23:00:13 2019
@@ -17,7 +17,10 @@
package org.apache.poi.ss.formula;
-import java.io.PrintStream;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -25,17 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
import org.apache.poi.hssf.model.HSSFFormulaParser;
-import org.apache.poi.ss.formula.ptg.Ptg;
-import org.apache.poi.ss.formula.eval.BlankEval;
-import org.apache.poi.ss.formula.eval.BoolEval;
-import org.apache.poi.ss.formula.eval.ErrorEval;
-import org.apache.poi.ss.formula.eval.NumberEval;
-import org.apache.poi.ss.formula.eval.StringEval;
-import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.hssf.usermodel.FormulaExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFEvaluationTestHelper;
@@ -43,19 +36,30 @@ import org.apache.poi.hssf.usermodel.HSS
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.formula.IEvaluationListener.ICacheEntry;
import org.apache.poi.ss.formula.PlainCellCache.Loc;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.formula.eval.BlankEval;
+import org.apache.poi.ss.formula.eval.BoolEval;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.ptg.Ptg;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellReference;
+import org.junit.Test;
/**
* Tests {@link org.apache.poi.ss.formula.EvaluationCache}. Makes sure that where possible (previously calculated) cached
* values are used. Also checks that changing cell values causes the correct (minimal) set of
* dependent cached values to be cleared.
- *
- * @author Josh Micich
*/
-public class TestEvaluationCache extends TestCase {
+public class TestEvaluationCache {
private static final class FormulaCellCacheEntryComparer implements Comparator<ICacheEntry> {
@@ -307,8 +311,8 @@ public class TestEvaluationCache extends
return ms;
}
+ @Test
public void testMediumComplex() {
-
MySheet ms = createMediumComplex();
// completely fresh evaluation
confirmEvaluate(ms, "A1", 46);
@@ -385,8 +389,8 @@ public class TestEvaluationCache extends
});
}
+ @Test
public void testMediumComplexWithDependencyChange() {
-
// Changing an intermediate formula
MySheet ms = createMediumComplex();
confirmEvaluate(ms, "A1", 46);
@@ -444,6 +448,7 @@ public class TestEvaluationCache extends
* verifies that when updating a plain cell, depending (formula) cell cached values are cleared
* only when the plain cell's value actually changes
*/
+ @Test
public void testRedundantUpdate() {
MySheet ms = new MySheet();
@@ -487,8 +492,8 @@ public class TestEvaluationCache extends
* and VLOOKUP the effect can be subtle. The presence of error values can also produce this
* effect in almost every function and operator.
*/
+ @Test
public void testSimpleWithDependencyChange() {
-
MySheet ms = new MySheet();
ms.setCellFormula("A1", "INDEX(C1:E1,1,B1)");
@@ -537,9 +542,8 @@ public class TestEvaluationCache extends
});
}
+ @Test
public void testBlankCells() {
-
-
MySheet ms = new MySheet();
ms.setCellFormula("A1", "sum(B1:D4,B5:E6)");
@@ -588,6 +592,7 @@ public class TestEvaluationCache extends
* Make sure that when blank cells are changed to value/formula cells, any dependent formulas
* have their cached results cleared.
*/
+ @Test
public void testBlankCellChangedToValueCell_bug46053() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
@@ -614,16 +619,16 @@ public class TestEvaluationCache extends
cellB1.setCellValue(0.4); // changing B1, so A1 cached result should be cleared
fe.notifyUpdateCell(cellB1);
cv = fe.evaluate(cellA1);
- if (cv.getNumberValue() == 2.2) {
- // looks like left-over cached result from before change to B1
- throw new AssertionFailedError("Identified bug 46053");
- }
+
+ // looks like left-over cached result from before change to B1
+ assertNotEquals("Identified bug 46053", 2.2, cv.getNumberValue());
assertEquals(2.6, cv.getNumberValue(), 0.0);
}
/**
* same use-case as the test for bug 46053, but checking trace values too
*/
+ @Test
public void testBlankCellChangedToValueCell() {
MySheet ms = new MySheet();
@@ -661,87 +666,10 @@ public class TestEvaluationCache extends
private static void confirmLog(MySheet ms, String[] expectedLog) {
String[] actualLog = ms.getAndClearLog();
- int endIx = actualLog.length;
- PrintStream ps = System.err;
- if (endIx != expectedLog.length) {
- ps.println("Log lengths mismatch");
- dumpCompare(ps, expectedLog, actualLog);
- throw new AssertionFailedError("Log lengths mismatch");
- }
- for (int i=0; i< endIx; i++) {
- if (!actualLog[i].equals(expectedLog[i])) {
- String msg = "Log entry mismatch at index " + i;
- ps.println(msg);
- dumpCompare(ps, expectedLog, actualLog);
- throw new AssertionFailedError(msg);
- }
- }
-
- }
-
- private static void dumpCompare(PrintStream ps, String[] expectedLog, String[] actualLog) {
- int max = Math.max(actualLog.length, expectedLog.length);
- ps.println("Index\tExpected\tActual");
- for(int i=0; i<max; i++) {
- ps.print(i + "\t");
- printItem(ps, expectedLog, i);
- ps.print("\t");
- printItem(ps, actualLog, i);
- ps.println();
- }
- ps.println();
- debugPrint(ps, actualLog);
- }
-
- private static void printItem(PrintStream ps, String[] ss, int index) {
- if (index < ss.length) {
- ps.print(ss[index]);
- }
- }
-
- private static void debugPrint(PrintStream ps, String[] log) {
- for (String element : log) {
- ps.println('"' + element + "\",");
- }
+ assertArrayEquals("Log entry mismatch", expectedLog, actualLog);
}
- private static void testPlainValueCache(Workbook wb, int numberOfSheets) {
-
- Row row;
- Cell cell;
-
- //create summary sheet
- Sheet summary = wb.createSheet("summary");
- wb.setActiveSheet(wb.getSheetIndex(summary));
-
- //formula referring all sheets created below
- row = summary.createRow(0);
- Cell summaryCell = row.createCell(0);
- summaryCell.setCellFormula("SUM(A2:A" + (numberOfSheets + 2) + ")");
-
-
- //create sheets with cells having (different) numbers
- // and add a row to summary
- for (int i = 1; i < numberOfSheets; i++) {
- Sheet sheet = wb.createSheet("new" + i);
-
- row = sheet.createRow(0);
- cell = row.createCell(0);
- cell.setCellValue(i);
-
- row = summary.createRow(i);
- cell = row.createCell(0);
- cell.setCellFormula("new" + i + "!A1");
-
- }
-
-
- //calculate
- FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
- evaluator.evaluateFormulaCell(summaryCell);
- }
-
-
+ @Test
public void testPlainValueCache() {
Workbook wb = new HSSFWorkbook();
@@ -779,7 +707,7 @@ public class TestEvaluationCache extends
//calculate
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(summaryCell);
- assertEquals(8394753.0, summaryCell.getNumericCellValue());
+ assertEquals(8394753.0, summaryCell.getNumericCellValue(), 0);
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/TestFunctionRegistry.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/TestFunctionRegistry.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/TestFunctionRegistry.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/TestFunctionRegistry.java Fri Dec 27 23:00:13 2019
@@ -19,120 +19,142 @@
package org.apache.poi.ss.formula;
-import junit.framework.TestCase;
-import org.apache.poi.hssf.usermodel.*;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.atp.AnalysisToolPak;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.FunctionEval;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.apache.poi.ss.formula.eval.ValueEval;
-import org.apache.poi.ss.formula.functions.FreeRefFunction;
-import org.apache.poi.ss.formula.functions.Function;
import org.apache.poi.ss.usermodel.CellValue;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.FixMethodOrder;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runners.MethodSorters;
+
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class TestFunctionRegistry {
+
+ HSSFWorkbook wb;
+ HSSFSheet sheet;
+ HSSFRow row;
+ HSSFFormulaEvaluator fe;
+
+ @Before
+ public void setup() {
+ wb = new HSSFWorkbook();
+ sheet = wb.createSheet("Sheet1");
+ row = sheet.createRow(0);
+ fe = new HSSFFormulaEvaluator(wb);
+ }
-/**
- *
- * @author Yegor Kozlov
- */
-public class TestFunctionRegistry extends TestCase {
-
- public void testRegisterInRuntime() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("Sheet1");
- HSSFRow row = sheet.createRow(0);
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-
- HSSFCell cellA = row.createCell(0);
- cellA.setCellFormula("FISHER(A5)");
- CellValue cv;
- try {
- cv = fe.evaluate(cellA);
- fail("expectecd exception");
- } catch (NotImplementedException e) {
- }
-
- FunctionEval.registerFunction("FISHER", new Function() {
- @Override
- public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- return ErrorEval.NA;
- }
- });
+ @After
+ public void teardown() throws IOException {
+ wb.close();
+ wb = null;
+ sheet = null;
+ row = null;
+ fe = null;
+ }
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void testRegisterInRuntimeA() {
+ HSSFCell cellA = row.createCell(0);
+ cellA.setCellFormula("FISHER(A5)");
+ thrown.expect(NotImplementedException.class);
+ fe.evaluate(cellA);
+ }
- cv = fe.evaluate(cellA);
+ @Test
+ public void testRegisterInRuntimeB() {
+ HSSFCell cellA = row.createCell(0);
+ cellA.setCellFormula("FISHER(A5)");
+ FunctionEval.registerFunction("FISHER", (args, srcRowIndex, srcColumnIndex) -> ErrorEval.NA);
+ CellValue cv = fe.evaluate(cellA);
assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
+ }
+ @Test
+ public void testRegisterInRuntimeC() {
HSSFCell cellB = row.createCell(1);
cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
- try {
- cv = fe.evaluate(cellB);
- fail("expectecd exception");
- } catch (NotImplementedException e) {
- }
-
- AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", new FreeRefFunction() {
- @Override
- public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
- return ErrorEval.NUM_ERROR;
- }
- });
+ thrown.expect(NotImplementedException.class);
+ fe.evaluate(cellB);
+ }
- cv = fe.evaluate(cellB);
+ @Test
+ public void testRegisterInRuntimeD() {
+ HSSFCell cellB = row.createCell(1);
+ cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
+
+ AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", (args, ec) -> ErrorEval.NUM_ERROR);
+
+ CellValue cv = fe.evaluate(cellB);
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
}
- public void testExceptions() {
- Function func = new Function() {
- @Override
- public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
- return ErrorEval.NA;
- }
- };
- try {
- FunctionEval.registerFunction("SUM", func);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("POI already implememts SUM" +
- ". You cannot override POI's implementations of Excel functions", e.getMessage());
- }
- try {
- FunctionEval.registerFunction("SUMXXX", func);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("Unknown function: SUMXXX", e.getMessage());
- }
- try {
- FunctionEval.registerFunction("ISODD", func);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("ISODD is a function from the Excel Analysis Toolpack. " +
- "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.", e.getMessage());
- }
-
- FreeRefFunction atpFunc = new FreeRefFunction() {
- @Override
- public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
- return ErrorEval.NUM_ERROR;
- }
- };
- try {
- AnalysisToolPak.registerFunction("ISODD", atpFunc);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("POI already implememts ISODD" +
- ". You cannot override POI's implementations of Excel functions", e.getMessage());
- }
- try {
- AnalysisToolPak.registerFunction("ISODDXXX", atpFunc);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("ISODDXXX is not a function from the Excel Analysis Toolpack.", e.getMessage());
- }
- try {
- AnalysisToolPak.registerFunction("SUM", atpFunc);
- fail("expectecd exception");
- } catch (IllegalArgumentException e){
- assertEquals("SUM is a built-in Excel function. " +
- "Use FunctoinEval.registerFunction(String name, Function func) instead.", e.getMessage());
- }
+ private static ValueEval na(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ return ErrorEval.NA;
+ }
+
+ @Test
+ public void testExceptionsA() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("POI already implememts SUM. You cannot override POI's implementations of Excel functions");
+ FunctionEval.registerFunction("SUM", TestFunctionRegistry::na);
+ }
+
+ @Test
+ public void testExceptionsB() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Unknown function: SUMXXX");
+ FunctionEval.registerFunction("SUMXXX", TestFunctionRegistry::na);
+ }
+
+ @Test
+ public void testExceptionsC() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("ISODD is a function from the Excel Analysis Toolpack. " +
+ "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.");
+ FunctionEval.registerFunction("ISODD", TestFunctionRegistry::na);
+ }
+
+ private static ValueEval atpFunc(ValueEval[] args, OperationEvaluationContext ec) {
+ return ErrorEval.NUM_ERROR;
+ }
+
+ @Test
+ public void testExceptionsD() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("POI already implememts ISODD. You cannot override POI's implementations of Excel functions");
+ AnalysisToolPak.registerFunction("ISODD", TestFunctionRegistry::atpFunc);
+ }
+
+ @Test
+ public void testExceptionsE() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("ISODDXXX is not a function from the Excel Analysis Toolpack.");
+ AnalysisToolPak.registerFunction("ISODDXXX", TestFunctionRegistry::atpFunc);
+ }
+
+ @Test
+ public void testExceptionsF() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("SUM is a built-in Excel function. " +
+ "Use FunctoinEval.registerFunction(String name, Function func) instead.");
+ AnalysisToolPak.registerFunction("SUM", TestFunctionRegistry::atpFunc);
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/TestPlainCellCache.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/TestPlainCellCache.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/TestPlainCellCache.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/TestPlainCellCache.java Fri Dec 27 23:00:13 2019
@@ -19,18 +19,17 @@
package org.apache.poi.ss.formula;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
import org.apache.poi.ss.formula.PlainCellCache.Loc;
-import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.junit.Test;
-/**
- * @author Yegor Kozlov
- */
-public class TestPlainCellCache extends TestCase {
+public class TestPlainCellCache {
- /**
- *
- */
+ @Test
public void testLoc(){
PlainCellCache cache = new PlainCellCache();
for (int bookIndex = 0; bookIndex < 0x1000; bookIndex += 0x100) {
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestIfError.java Fri Dec 27 23:00:13 2019
@@ -16,6 +16,10 @@
==================================================================== */
package org.apache.poi.ss.formula.atp;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
@@ -24,73 +28,72 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
-
-import junit.framework.TestCase;
+import org.junit.Test;
/**
* Testcase for 'Analysis Toolpak' function IFERROR()
- *
- * @author Johan Karlsteen
*/
-public class TestIfError extends TestCase {
+public class TestIfError {
/**
* =IFERROR(210/35,\"Error in calculation\")" Divides 210 by 35 and returns 6.0
* =IFERROR(55/0,\"Error in calculation\")" Divides 55 by 0 and returns the error text
* =IFERROR(C1,\"Error in calculation\")" References the result of dividing 55 by 0 and returns the error text
*/
- public static void testEvaluate(){
- Workbook wb = new HSSFWorkbook();
- Sheet sh = wb.createSheet();
- Row row1 = sh.createRow(0);
- Row row2 = sh.createRow(1);
-
- // Create cells
- row1.createCell(0, CellType.NUMERIC);
- row1.createCell(1, CellType.NUMERIC);
- row1.createCell(2, CellType.NUMERIC);
- row2.createCell(0, CellType.NUMERIC);
- row2.createCell(1, CellType.NUMERIC);
-
- // Create references
- CellReference a1 = new CellReference("A1");
- CellReference a2 = new CellReference("A2");
- CellReference b1 = new CellReference("B1");
- CellReference b2 = new CellReference("B2");
- CellReference c1 = new CellReference("C1");
-
- // Set values
- sh.getRow(a1.getRow()).getCell(a1.getCol()).setCellValue(210);
- sh.getRow(a2.getRow()).getCell(a2.getCol()).setCellValue(55);
- sh.getRow(b1.getRow()).getCell(b1.getCol()).setCellValue(35);
- sh.getRow(b2.getRow()).getCell(b2.getCol()).setCellValue(0);
- sh.getRow(c1.getRow()).getCell(c1.getCol()).setCellFormula("A1/B2");
-
- Cell cell1 = sh.createRow(3).createCell(0);
- cell1.setCellFormula("IFERROR(A1/B1,\"Error in calculation\")");
- Cell cell2 = sh.createRow(3).createCell(0);
- cell2.setCellFormula("IFERROR(A2/B2,\"Error in calculation\")");
- Cell cell3 = sh.createRow(3).createCell(0);
- cell3.setCellFormula("IFERROR(C1,\"error\")");
-
- double accuracy = 1E-9;
-
- FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
-
- assertEquals("Checks that the cell is numeric",
- CellType.NUMERIC, evaluator.evaluate(cell1).getCellType());
- assertEquals("Divides 210 by 35 and returns 6.0",
- 6.0, evaluator.evaluate(cell1).getNumberValue(), accuracy);
-
-
- assertEquals("Checks that the cell is numeric",
- CellType.STRING, evaluator.evaluate(cell2).getCellType());
- assertEquals("Rounds -10 to a nearest multiple of -3 (-9)",
- "Error in calculation", evaluator.evaluate(cell2).getStringValue());
-
- assertEquals("Check that C1 returns string",
- CellType.STRING, evaluator.evaluate(cell3).getCellType());
- assertEquals("Check that C1 returns string \"error\"",
- "error", evaluator.evaluate(cell3).getStringValue());
+ @Test
+ public void testEvaluate() throws IOException {
+ try (Workbook wb = new HSSFWorkbook()) {
+ Sheet sh = wb.createSheet();
+ Row row1 = sh.createRow(0);
+ Row row2 = sh.createRow(1);
+
+ // Create cells
+ row1.createCell(0, CellType.NUMERIC);
+ row1.createCell(1, CellType.NUMERIC);
+ row1.createCell(2, CellType.NUMERIC);
+ row2.createCell(0, CellType.NUMERIC);
+ row2.createCell(1, CellType.NUMERIC);
+
+ // Create references
+ CellReference a1 = new CellReference("A1");
+ CellReference a2 = new CellReference("A2");
+ CellReference b1 = new CellReference("B1");
+ CellReference b2 = new CellReference("B2");
+ CellReference c1 = new CellReference("C1");
+
+ // Set values
+ sh.getRow(a1.getRow()).getCell(a1.getCol()).setCellValue(210);
+ sh.getRow(a2.getRow()).getCell(a2.getCol()).setCellValue(55);
+ sh.getRow(b1.getRow()).getCell(b1.getCol()).setCellValue(35);
+ sh.getRow(b2.getRow()).getCell(b2.getCol()).setCellValue(0);
+ sh.getRow(c1.getRow()).getCell(c1.getCol()).setCellFormula("A1/B2");
+
+ Cell cell1 = sh.createRow(3).createCell(0);
+ cell1.setCellFormula("IFERROR(A1/B1,\"Error in calculation\")");
+ Cell cell2 = sh.createRow(3).createCell(0);
+ cell2.setCellFormula("IFERROR(A2/B2,\"Error in calculation\")");
+ Cell cell3 = sh.createRow(3).createCell(0);
+ cell3.setCellFormula("IFERROR(C1,\"error\")");
+
+ double accuracy = 1E-9;
+
+ FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+
+ assertEquals("Checks that the cell is numeric",
+ CellType.NUMERIC, evaluator.evaluate(cell1).getCellType());
+ assertEquals("Divides 210 by 35 and returns 6.0",
+ 6.0, evaluator.evaluate(cell1).getNumberValue(), accuracy);
+
+
+ assertEquals("Checks that the cell is numeric",
+ CellType.STRING, evaluator.evaluate(cell2).getCellType());
+ assertEquals("Rounds -10 to a nearest multiple of -3 (-9)",
+ "Error in calculation", evaluator.evaluate(cell2).getStringValue());
+
+ assertEquals("Check that C1 returns string",
+ CellType.STRING, evaluator.evaluate(cell3).getCellType());
+ assertEquals("Check that C1 returns string \"error\"",
+ "error", evaluator.evaluate(cell3).getStringValue());
+ }
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestMRound.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestMRound.java?rev=1872041&r1=1872040&r2=1872041&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestMRound.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/atp/TestMRound.java Fri Dec 27 23:00:13 2019
@@ -16,25 +16,29 @@
==================================================================== */
package org.apache.poi.ss.formula.atp;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.ErrorEval;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Test;
/**
* Testcase for 'Analysis Toolpak' function MROUND()
- *
- * @author Yegor Kozlov
*/
-public class TestMRound extends TestCase {
+public class TestMRound {
/**
-=MROUND(10, 3) Rounds 10 to a nearest multiple of 3 (9)
-=MROUND(-10, -3) Rounds -10 to a nearest multiple of -3 (-9)
-=MROUND(1.3, 0.2) Rounds 1.3 to a nearest multiple of 0.2 (1.4)
-=MROUND(5, -2) Returns an error, because -2 and 5 have different signs (#NUM!) *
+ =MROUND(10, 3) Rounds 10 to a nearest multiple of 3 (9)
+ =MROUND(-10, -3) Rounds -10 to a nearest multiple of -3 (-9)
+ =MROUND(1.3, 0.2) Rounds 1.3 to a nearest multiple of 0.2 (1.4)
+ =MROUND(5, -2) Returns an error, because -2 and 5 have different signs (#NUM!) *
*/
- public static void testEvaluate(){
+ @Test
+ public void testEvaluate(){
Workbook wb = new HSSFWorkbook();
Sheet sh = wb.createSheet();
Cell cell1 = sh.createRow(0).createCell(0);
@@ -65,6 +69,6 @@ public class TestMRound extends TestCase
ErrorEval.NUM_ERROR.getErrorCode(), evaluator.evaluate(cell4).getErrorValue());
assertEquals("Returns 0 because the multiple is 0",
- 0.0, evaluator.evaluate(cell5).getNumberValue());
+ 0.0, evaluator.evaluate(cell5).getNumberValue(), 0);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org