diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-12-24 18:42:29 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-12-24 18:42:29 +0000 |
commit | a0fa9e19b1196bc10034f15474d27ce23bf5865a (patch) | |
tree | 499c1eb427ebff72f7e447d13dd1de1552df8146 /src/testcases/org/apache/poi/util | |
parent | fb012041e8dd788637e68737199bc313b3e90098 (diff) | |
download | poi-a0fa9e19b1196bc10034f15474d27ce23bf5865a.tar.gz poi-a0fa9e19b1196bc10034f15474d27ce23bf5865a.zip |
#65026 - Migrate tests to Junit 5
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884783 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/util')
21 files changed, 435 insertions, 761 deletions
diff --git a/src/testcases/org/apache/poi/util/AllPOIUtilTests.java b/src/testcases/org/apache/poi/util/AllPOIUtilTests.java deleted file mode 100644 index 58155e6dca..0000000000 --- a/src/testcases/org/apache/poi/util/AllPOIUtilTests.java +++ /dev/null @@ -1,43 +0,0 @@ -/* ==================================================================== - 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.util; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -/** - * Test suite for all sub-packages of org.apache.poi.util<br> - */ -@RunWith(Suite.class) -@Suite.SuiteClasses({ - TestArrayUtil.class - , TestBitField.class - , TestByteField.class - , TestHexDump.class - , TestIntegerField.class - , TestIntList.class - , TestLittleEndian.class - , TestLongField.class - , TestPOILogFactory.class - , TestPOILogger.class - , TestShortField.class - , TestStringUtil.class - , TestTempFile.class -}) -public final class AllPOIUtilTests { -} diff --git a/src/testcases/org/apache/poi/util/MemoryLeakVerifier.java b/src/testcases/org/apache/poi/util/MemoryLeakVerifier.java index c69baa7900..138f2baccd 100644 --- a/src/testcases/org/apache/poi/util/MemoryLeakVerifier.java +++ b/src/testcases/org/apache/poi/util/MemoryLeakVerifier.java @@ -17,12 +17,12 @@ package org.apache.poi.util; +import static org.junit.jupiter.api.Assertions.assertNull; + import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertNull; - /** * A simple utility class that can verify that objects have been successfully garbage collected. * @@ -100,7 +100,6 @@ public class MemoryLeakVerifier { Thread.sleep(GC_SLEEP_TIME); } - assertNull("Object should not exist after " + MAX_GC_ITERATIONS + " collections, but still had: " + ref.get(), - ref.get()); + assertNull(ref.get(), "Object should not exist after " + MAX_GC_ITERATIONS + " collections, but still had: " + ref.get()); } } diff --git a/src/testcases/org/apache/poi/util/TestArrayUtil.java b/src/testcases/org/apache/poi/util/TestArrayUtil.java index 2718f7094d..001d5c0595 100644 --- a/src/testcases/org/apache/poi/util/TestArrayUtil.java +++ b/src/testcases/org/apache/poi/util/TestArrayUtil.java @@ -18,12 +18,12 @@ package org.apache.poi.util; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.Arrays; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test for ArrayUtil @@ -83,19 +83,11 @@ public class TestArrayUtil { } // Check can't shift more than we have - try { - ArrayUtil.arrayMoveWithin(getIntsList(), 7, 3, 5); - fail(); - } catch(IllegalArgumentException e) { - // Good, we don't have 5 from 7 onwards - } + assertThrows(IllegalArgumentException.class, () -> ArrayUtil.arrayMoveWithin(getIntsList(), 7, 3, 5)); + // Good, we don't have 5 from 7 onwards // Check can't shift where would overshoot - try { - ArrayUtil.arrayMoveWithin(getIntsList(), 2, 7, 5); - fail(); - } catch(IllegalArgumentException e) { - // Good, we can't fit 5 in starting at 7 - } + assertThrows(IllegalArgumentException.class, () -> ArrayUtil.arrayMoveWithin(getIntsList(), 2, 7, 5)); + // Good, we can't fit 5 in starting at 7 } } diff --git a/src/testcases/org/apache/poi/util/TestBitField.java b/src/testcases/org/apache/poi/util/TestBitField.java index c8c57ecaa2..d075c98fb0 100644 --- a/src/testcases/org/apache/poi/util/TestBitField.java +++ b/src/testcases/org/apache/poi/util/TestBitField.java @@ -17,11 +17,11 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Class to test BitField functionality diff --git a/src/testcases/org/apache/poi/util/TestByteField.java b/src/testcases/org/apache/poi/util/TestByteField.java index 3c6bea78bf..53e9b95738 100644 --- a/src/testcases/org/apache/poi/util/TestByteField.java +++ b/src/testcases/org/apache/poi/util/TestByteField.java @@ -18,13 +18,13 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayInputStream; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test for ByteField class @@ -86,11 +86,11 @@ public final class TestByteField { for (int j = 0; j < _test_array.length; j++) { field.set(_test_array[ j ]); - assertEquals("testing _1 " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing _1 " + j); field = new ByteField(0); field.set(_test_array[ j ], array); - assertEquals("testing _2 ", _test_array[ j ], field.get()); - assertEquals("testing _3 ", _test_array[ j ], array[ 0 ]); + assertEquals(_test_array[ j ], field.get(), "testing _2 "); + assertEquals(_test_array[ j ], array[ 0 ], "testing _3 "); } } @@ -109,7 +109,7 @@ public final class TestByteField { for (int j = 0; j < _test_array.length; j++) { array[ 0 ] = _test_array[ j ]; field.readFromBytes(array); - assertEquals("testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing " + j); } } @@ -120,7 +120,7 @@ public final class TestByteField { for (int j = 0; j < _test_array.length; j++) { field.readFromStream(stream); - assertEquals("Testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "Testing " + j); } } @@ -132,7 +132,7 @@ public final class TestByteField { for (byte b : _test_array) { field.set(b); field.writeToBytes(array); - assertEquals("testing ", b, array[ 0 ]); + assertEquals(b, array[ 0 ], "testing "); } } } diff --git a/src/testcases/org/apache/poi/util/TestHexDump.java b/src/testcases/org/apache/poi/util/TestHexDump.java index cf575c770b..d6a7d5d0e4 100644 --- a/src/testcases/org/apache/poi/util/TestHexDump.java +++ b/src/testcases/org/apache/poi/util/TestHexDump.java @@ -17,10 +17,10 @@ package org.apache.poi.util; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -28,21 +28,21 @@ import java.io.OutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; public class TestHexDump { private static PrintStream SYSTEM_OUT; - @BeforeClass + @BeforeAll public static void setUp() throws UnsupportedEncodingException { SYSTEM_OUT = System.out; System.setOut(new PrintStream(new OutputStream() {public void write(int b) {}}, false, "UTF-8")); } - @AfterClass + @AfterAll public static void tearDown() { System.setOut(SYSTEM_OUT); } @@ -55,8 +55,8 @@ public class TestHexDump { byte[] bytesAct = streamAct.toByteArray(); byte[] bytesExp = toHexDump(0, 0); - assertEquals("array size mismatch", bytesExp.length, bytesAct.length); - assertArrayEquals("array mismatch", bytesExp, bytesAct); + assertEquals(bytesExp.length, bytesAct.length, "array size mismatch"); + assertArrayEquals(bytesExp, bytesAct, "array mismatch"); // verify proper behavior with non-zero offset streamAct.reset(); @@ -64,8 +64,8 @@ public class TestHexDump { bytesAct = streamAct.toByteArray(); bytesExp = toHexDump(0x10000000L,0); - assertEquals("array size mismatch", bytesExp.length, bytesAct.length); - assertArrayEquals("array mismatch", bytesExp, bytesAct); + assertEquals(bytesExp.length, bytesAct.length, "array size mismatch"); + assertArrayEquals(bytesExp, bytesAct, "array mismatch"); // verify proper behavior with negative offset streamAct.reset(); @@ -73,8 +73,8 @@ public class TestHexDump { bytesAct = streamAct.toByteArray(); bytesExp = toHexDump(0xFF000000L,0); - assertEquals("array size mismatch", bytesExp.length, bytesAct.length); - assertArrayEquals("array mismatch", bytesExp, bytesAct); + assertEquals(bytesExp.length, bytesAct.length, "array size mismatch"); + assertArrayEquals(bytesExp, bytesAct, "array mismatch"); // verify proper behavior with non-zero index streamAct.reset(); @@ -82,36 +82,20 @@ public class TestHexDump { bytesAct = streamAct.toByteArray(); bytesExp = toHexDump(0xFF000000L,0x81); - assertEquals("array size mismatch", bytesExp.length, bytesAct.length); - assertArrayEquals("array mismatch", bytesExp, bytesAct); + assertEquals(bytesExp.length, bytesAct.length, "array size mismatch"); + assertArrayEquals(bytesExp, bytesAct, "array mismatch"); // verify proper behavior with negative index - try { - streamAct.reset(); - HexDump.dump(testArray, 0x10000000L, streamAct, -1); - fail("should have caught ArrayIndexOutOfBoundsException on negative index"); - } catch (ArrayIndexOutOfBoundsException ignored_exception) { - // as expected - } + streamAct.reset(); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000L, streamAct, -1)); // verify proper behavior with index that is too large - try { - streamAct.reset(); - HexDump.dump(testArray, 0x10000000L, streamAct, testArray.length); - fail("should have caught ArrayIndexOutOfBoundsException on large index"); - } catch (ArrayIndexOutOfBoundsException ignored_exception) { - // as expected - } + streamAct.reset(); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(testArray, 0x10000000L, streamAct, testArray.length)); // verify proper behavior with null stream - try { - HexDump.dump(testArray, 0x10000000L, null, 0); - fail("should have caught IllegalArgumentException on negative index"); - } catch (IllegalArgumentException ignored_exception) { - - // as expected - } + assertThrows(IllegalArgumentException.class, () -> HexDump.dump(testArray, 0x10000000L, null, 0)); // verify proper behaviour with empty byte array streamAct.reset(); @@ -178,28 +162,18 @@ public class TestHexDump { byte[] testArray = testArray(); String dump = HexDump.dump(testArray, 0, 0); //System.out.println("Hex: \n" + dump); - assertTrue("Had: \n" + dump, - dump.contains("0123456789:;<=>?")); + assertTrue(dump.contains("0123456789:;<=>?"), "Had: \n" + dump); dump = HexDump.dump(testArray, 2, 1); //System.out.println("Hex: \n" + dump); - assertTrue("Had: \n" + dump, - dump.contains("123456789:;<=>?@")); + assertTrue(dump.contains("123456789:;<=>?@"), "Had: \n" + dump); } - @Test(expected=ArrayIndexOutOfBoundsException.class) + @Test public void testDumpToStringOutOfIndex1() { - HexDump.dump(new byte[1], 0, -1); - } - - @Test(expected=ArrayIndexOutOfBoundsException.class) - public void testDumpToStringOutOfIndex2() { - HexDump.dump(new byte[1], 0, 2); - } - - @Test(expected=ArrayIndexOutOfBoundsException.class) - public void testDumpToStringOutOfIndex3() { - HexDump.dump(new byte[1], 0, 1); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, -1)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 2)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> HexDump.dump(new byte[1], 0, 1)); } @Test diff --git a/src/testcases/org/apache/poi/util/TestIOUtils.java b/src/testcases/org/apache/poi/util/TestIOUtils.java index fbef102f17..6a7710484c 100644 --- a/src/testcases/org/apache/poi/util/TestIOUtils.java +++ b/src/testcases/org/apache/poi/util/TestIOUtils.java @@ -17,12 +17,12 @@ package org.apache.poi.util; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -38,19 +38,19 @@ import java.nio.charset.StandardCharsets; import java.util.Random; import org.apache.poi.EmptyFileException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; /** * Class to test IOUtils */ public final class TestIOUtils { - static File TMP; - static final long LENGTH = 300+new Random().nextInt(9000); + private static File TMP; + private static final long LENGTH = 300+new Random().nextInt(9000); - @BeforeClass + @BeforeAll public static void setUp() throws IOException { TMP = File.createTempFile("poi-ioutils-", ""); OutputStream os = new FileOutputStream(TMP); @@ -62,11 +62,15 @@ public final class TestIOUtils { } - @AfterClass + @AfterAll public static void tearDown() { if (TMP != null) assertTrue(TMP.delete()); } + private static InputStream data123() { + return new ByteArrayInputStream(new byte[]{1,2,3}); + } + @Test public void testPeekFirst8Bytes() throws Exception { assertArrayEquals("01234567".getBytes(StandardCharsets.UTF_8), @@ -81,43 +85,38 @@ public final class TestIOUtils { @Test public void testPeekFirst8BytesTooLessAvailable() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3, 0, 0, 0, 0, 0}, - IOUtils.peekFirst8Bytes(new ByteArrayInputStream(new byte[] { 1, 2, 3}))); + assertArrayEquals(new byte[] { 1, 2, 3, 0, 0, 0, 0, 0}, IOUtils.peekFirst8Bytes(data123())); } - @Test(expected = EmptyFileException.class) - public void testPeekFirst8BytesEmpty() throws Exception { - IOUtils.peekFirst8Bytes(new ByteArrayInputStream(new byte[] {})); + @Test + public void testPeekFirst8BytesEmpty() { + assertThrows(EmptyFileException.class, () -> + IOUtils.peekFirst8Bytes(new ByteArrayInputStream(new byte[0]))); } @Test public void testToByteArray() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3}, - IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}))); + assertArrayEquals(new byte[] { 1, 2, 3}, IOUtils.toByteArray(data123())); } - @Test(expected = IOException.class) - public void testToByteArrayToSmall() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3}, - IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}), 10)); + @Test + public void testToByteArrayToSmall() { + assertThrows(IOException.class, () -> IOUtils.toByteArray(data123(), 10)); } - @Test(expected = IOException.class) - public void testToByteArrayMaxLengthToSmall() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3}, - IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}), 10, 10)); + @Test + public void testToByteArrayMaxLengthToSmall() { + assertThrows(IOException.class, () -> IOUtils.toByteArray(data123(), 10, 10)); } - @Test(expected = RecordFormatException.class) - public void testToByteArrayNegativeLength() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3}, - IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}), -1)); + @Test + public void testToByteArrayNegativeLength() { + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(data123(), -1)); } - @Test(expected = RecordFormatException.class) - public void testToByteArrayNegativeMaxLength() throws Exception { - assertArrayEquals(new byte[] { 1, 2, 3}, - IOUtils.toByteArray(new ByteArrayInputStream(new byte[] { 1, 2, 3}), 10, -1)); + @Test + public void testToByteArrayNegativeMaxLength() { + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(data123(), 10, -1)); } @Test @@ -148,7 +147,7 @@ public final class TestIOUtils { public void testSkipFully() throws IOException { try (InputStream is = new FileInputStream(TMP)) { long skipped = IOUtils.skipFully(is, 20000L); - assertEquals("length: " + LENGTH, LENGTH, skipped); + assertEquals(LENGTH, skipped); } } @@ -156,7 +155,7 @@ public final class TestIOUtils { public void testSkipFullyGtIntMax() throws IOException { try (InputStream is = new FileInputStream(TMP)) { long skipped = IOUtils.skipFully(is, Integer.MAX_VALUE + 20000L); - assertEquals("length: " + LENGTH, LENGTH, skipped); + assertEquals(LENGTH, skipped); } } @@ -166,7 +165,7 @@ public final class TestIOUtils { try (InputStream is = new FileInputStream(TMP)) { IOUtils.copy(is, bos); long skipped = IOUtils.skipFully(new ByteArrayInputStream(bos.toByteArray()), 20000L); - assertEquals("length: " + LENGTH, LENGTH, skipped); + assertEquals(LENGTH, skipped); } } @@ -176,7 +175,7 @@ public final class TestIOUtils { try (InputStream is = new FileInputStream(TMP)) { IOUtils.copy(is, bos); long skipped = IOUtils.skipFully(new ByteArrayInputStream(bos.toByteArray()), Integer.MAX_VALUE + 20000L); - assertEquals("length: " + LENGTH, LENGTH, skipped); + assertEquals(LENGTH, skipped); } } @@ -188,28 +187,28 @@ public final class TestIOUtils { @Test public void testZeroByte() throws IOException { long skipped = IOUtils.skipFully((new ByteArrayInputStream(new byte[0])), 100); - assertEquals("zero byte", -1L, skipped); + assertEquals(-1L, skipped); } @Test public void testSkipZero() throws IOException { try (InputStream is = new FileInputStream(TMP)) { long skipped = IOUtils.skipFully(is, 0); - assertEquals("zero length", 0, skipped); + assertEquals(0, skipped); } } - @Test(expected = IllegalArgumentException.class) + @Test public void testSkipNegative() throws IOException { try (InputStream is = new FileInputStream(TMP)) { - IOUtils.skipFully(is, -1); + assertThrows(IllegalArgumentException.class, () -> IOUtils.skipFully(is, -1)); } } - @Test(expected = RecordFormatException.class) + @Test public void testMaxLengthTooLong() throws IOException { try (InputStream is = new FileInputStream(TMP)) { - IOUtils.toByteArray(is, Integer.MAX_VALUE, 100); + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(is, Integer.MAX_VALUE, 100)); } } @@ -222,17 +221,17 @@ public final class TestIOUtils { } } - @Test(expected = RecordFormatException.class) + @Test public void testMaxLengthInvalid() throws IOException { try (InputStream is = new FileInputStream(TMP)) { - IOUtils.toByteArray(is, 90, 80); + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(is, 90, 80)); } } @Test public void testWonkyInputStream() throws IOException { long skipped = IOUtils.skipFully(new WonkyInputStream(), 10000); - assertEquals("length: "+LENGTH, 10000, skipped); + assertEquals(10000, skipped); } @Test @@ -257,16 +256,11 @@ public final class TestIOUtils { } @Test - public void testSetMaxOverrideOverLimit() throws IOException { + public void testSetMaxOverrideOverLimit() { IOUtils.setByteArrayMaxOverride(2); try { ByteArrayInputStream stream = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); - try { - IOUtils.toByteArray(stream); - fail("Should have caught an Exception here"); - } catch (RecordFormatException e) { - // expected - } + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(stream)); } finally { IOUtils.setByteArrayMaxOverride(-1); } @@ -298,12 +292,7 @@ public final class TestIOUtils { IOUtils.setByteArrayMaxOverride(2); try { ByteArrayInputStream stream = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); - try { - IOUtils.toByteArray(stream, 3, 100); - fail("Should have caught an Exception here"); - } catch (RecordFormatException e) { - // expected - } + assertThrows(RecordFormatException.class, () -> IOUtils.toByteArray(stream, 3, 100)); } finally { IOUtils.setByteArrayMaxOverride(-1); } diff --git a/src/testcases/org/apache/poi/util/TestIntList.java b/src/testcases/org/apache/poi/util/TestIntList.java index 25d427d361..1efadc0c91 100644 --- a/src/testcases/org/apache/poi/util/TestIntList.java +++ b/src/testcases/org/apache/poi/util/TestIntList.java @@ -17,15 +17,15 @@ package org.apache.poi.util; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.jupiter.api.Test; /** * Class to test IntList @@ -50,18 +50,17 @@ public final class TestIntList { @Test public void testAdd() { - IntList list = new IntList(); - int[] testArray = - { - 0, 1, 2, 3, 5 - }; + IntList list = new IntList(); + int[] testArray = + { + 0, 1, 2, 3, 5 + }; for (int element : testArray) { list.add(element); } - for (int j = 0; j < testArray.length; j++) - { - assertEquals(testArray[ j ], list.get(j)); + for (int j = 0; j < testArray.length; j++) { + assertEquals(testArray[j], list.get(j)); } assertEquals(testArray.length, list.size()); @@ -69,59 +68,44 @@ public final class TestIntList { list.add(0, -1); assertEquals(-1, list.get(0)); assertEquals(testArray.length + 1, list.size()); - for (int j = 0; j < testArray.length; j++) - { - assertEquals(testArray[ j ], list.get(j + 1)); + for (int j = 0; j < testArray.length; j++) { + assertEquals(testArray[j], list.get(j + 1)); } // add in the middle list.add(5, 4); assertEquals(4, list.get(5)); assertEquals(testArray.length + 2, list.size()); - for (int j = 0; j < list.size(); j++) - { + for (int j = 0; j < list.size(); j++) { assertEquals(j - 1, list.get(j)); } // add at the end list.add(list.size(), 6); assertEquals(testArray.length + 3, list.size()); - for (int j = 0; j < list.size(); j++) - { + for (int j = 0; j < list.size(); j++) { assertEquals(j - 1, list.get(j)); } // add past end - try - { - list.add(list.size() + 1, 8); - fail("should have thrown exception"); - } - catch (IndexOutOfBoundsException e) - { - - // as expected - } + IntList list2 = list; + assertThrows(IndexOutOfBoundsException.class, () -> list2.add(list2.size() + 1, 8)); // test growth list = new IntList(0); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } assertEquals(1000, list.size()); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(j, list.get(j)); } list = new IntList(0); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(0, j); } assertEquals(1000, list.size()); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(j, list.get(999 - j)); } } @@ -130,8 +114,7 @@ public final class TestIntList { public void testAddAll() { IntList list = new IntList(); - for (int j = 0; j < 5; j++) - { + for (int j = 0; j < 5; j++) { list.add(j); } IntList list2 = new IntList(0); @@ -139,29 +122,19 @@ public final class TestIntList { list2.addAll(list); list2.addAll(list); assertEquals(2 * list.size(), list2.size()); - for (int j = 0; j < 5; j++) - { + for (int j = 0; j < 5; j++) { assertEquals(list2.get(j), j); assertEquals(list2.get(j + list.size()), j); } IntList empty = new IntList(); - int limit = list.size(); + int limit = list.size(); - for (int j = 0; j < limit; j++) - { + for (int j = 0; j < limit; j++) { assertTrue(list.addAll(j, empty)); assertEquals(limit, list.size()); } - try - { - list.addAll(limit + 1, empty); - fail("should have thrown an exception"); - } - catch (IndexOutOfBoundsException e) - { - // as expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.addAll(limit + 1, empty)); // try add at beginning empty.addAll(0, list); @@ -215,20 +188,17 @@ public final class TestIntList { public void testClear() { IntList list = new IntList(); - for (int j = 0; j < 500; j++) - { + for (int j = 0; j < 500; j++) { list.add(j); } assertEquals(500, list.size()); list.clear(); assertEquals(0, list.size()); - for (int j = 0; j < 500; j++) - { + for (int j = 0; j < 500; j++) { list.add(j + 1); } assertEquals(500, list.size()); - for (int j = 0; j < 500; j++) - { + for (int j = 0; j < 500; j++) { assertEquals(j + 1, list.get(j)); } } @@ -237,18 +207,13 @@ public final class TestIntList { public void testContains() { IntList list = new IntList(); - for (int j = 0; j < 1000; j += 2) - { + for (int j = 0; j < 1000; j += 2) { list.add(j); } - for (int j = 0; j < 1000; j++) - { - if (j % 2 == 0) - { + for (int j = 0; j < 1000; j++) { + if (j % 2 == 0) { assertTrue(list.contains(j)); - } - else - { + } else { assertFalse(list.contains(j)); } } @@ -259,8 +224,7 @@ public final class TestIntList { IntList list = new IntList(); assertTrue(list.containsAll(list)); - for (int j = 0; j < 10; j++) - { + for (int j = 0; j < 10; j++) { list.add(j); } IntList list2 = new IntList(list); @@ -306,46 +270,27 @@ public final class TestIntList { public void testGet() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } - for (int j = 0; j < 1001; j++) - { - try - { - assertEquals(j, list.get(j)); - if (j == 1000) - { - fail("should have gotten exception"); - } - } - catch (IndexOutOfBoundsException e) - { - if (j != 1000) - { - fail("unexpected IndexOutOfBoundsException"); - } - } + for (int j = 0; j < 1000; j++) { + assertEquals(j, list.get(j)); } + + assertThrows(IndexOutOfBoundsException.class, () -> list.get(1000)); } @Test public void testIndexOf() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j / 2); } - for (int j = 0; j < 1000; j++) - { - if (j < 500) - { + for (int j = 0; j < 1000; j++) { + if (j < 500) { assertEquals(j * 2, list.indexOf(j)); - } - else - { + } else { assertEquals(-1, list.indexOf(j)); } } @@ -378,18 +323,13 @@ public final class TestIntList { public void testLastIndexOf() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j / 2); } - for (int j = 0; j < 1000; j++) - { - if (j < 500) - { + for (int j = 0; j < 1000; j++) { + if (j < 500) { assertEquals(1 + j * 2, list.lastIndexOf(j)); - } - else - { + } else { assertEquals(-1, list.indexOf(j)); } } @@ -399,48 +339,33 @@ public final class TestIntList { public void testRemove() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(j, list.remove(0)); assertEquals(999 - j, list.size()); } - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(999 - j, list.remove(999 - j)); assertEquals(999 - j, list.size()); } - try - { - list.remove(0); - fail("should have caught IndexOutOfBoundsException"); - } - catch (IndexOutOfBoundsException e) - { - // as expected - } + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0)); } @Test public void testRemoveValue() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j / 2); } - for (int j = 0; j < 1000; j++) - { - if (j < 500) - { + for (int j = 0; j < 1000; j++) { + if (j < 500) { assertTrue(list.removeValue(j)); assertTrue(list.removeValue(j)); } @@ -452,22 +377,17 @@ public final class TestIntList { public void testRemoveAll() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } IntList listCopy = new IntList(list); - IntList listOdd = new IntList(); + IntList listOdd = new IntList(); IntList listEven = new IntList(); - for (int j = 0; j < 1000; j++) - { - if (j % 2 == 0) - { + for (int j = 0; j < 1000; j++) { + if (j % 2 == 0) { listEven.add(j); - } - else - { + } else { listOdd.add(j); } } @@ -494,22 +414,17 @@ public final class TestIntList { public void testRetainAll() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } IntList listCopy = new IntList(list); - IntList listOdd = new IntList(); + IntList listOdd = new IntList(); IntList listEven = new IntList(); - for (int j = 0; j < 1000; j++) - { - if (j % 2 == 0) - { + for (int j = 0; j < 1000; j++) { + if (j % 2 == 0) { listEven.add(j); - } - else - { + } else { listOdd.add(j); } } @@ -535,43 +450,26 @@ public final class TestIntList { public void testSet() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } - for (int j = 0; j < 1001; j++) - { - try - { - list.set(j, j + 1); - if (j == 1000) - { - fail("Should have gotten exception"); - } - assertEquals(j + 1, list.get(j)); - } - catch (IndexOutOfBoundsException e) - { - if (j != 1000) - { - fail("premature exception"); - } - } + for (int j = 0; j < 1000; j++) { + list.set(j, j + 1); + assertEquals(j + 1, list.get(j)); } + assertThrows(IndexOutOfBoundsException.class, () -> list.set(1000, 1001)); } @Test public void testSize() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(j, list.size()); list.add(j); assertEquals(j + 1, list.size()); } - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { assertEquals(1000 - j, list.size()); list.removeValue(j); assertEquals(999 - j, list.size()); @@ -582,41 +480,36 @@ public final class TestIntList { public void testToArray() { IntList list = new IntList(); - for (int j = 0; j < 1000; j++) - { + for (int j = 0; j < 1000; j++) { list.add(j); } int[] a1 = list.toArray(); assertEquals(a1.length, list.size()); - for (int j = 0; j < 1000; j++) - { - assertEquals(a1[ j ], list.get(j)); + for (int j = 0; j < 1000; j++) { + assertEquals(a1[j], list.get(j)); } - int[] a2 = new int[ list.size() ]; + int[] a2 = new int[list.size()]; int[] a3 = list.toArray(a2); assertSame(a2, a3); - for (int j = 0; j < 1000; j++) - { - assertEquals(a2[ j ], list.get(j)); + for (int j = 0; j < 1000; j++) { + assertEquals(a2[j], list.get(j)); } - int[] aShort = new int[ list.size() - 1 ]; - int[] aLong = new int[ list.size() + 1 ]; - int[] a4 = list.toArray(aShort); - int[] a5 = list.toArray(aLong); + int[] aShort = new int[list.size() - 1]; + int[] aLong = new int[list.size() + 1]; + int[] a4 = list.toArray(aShort); + int[] a5 = list.toArray(aLong); assertNotSame(a4, aShort); assertNotSame(a5, aLong); assertEquals(a4.length, list.size()); - for (int j = 0; j < 1000; j++) - { - assertEquals(a3[ j ], list.get(j)); + for (int j = 0; j < 1000; j++) { + assertEquals(a3[j], list.get(j)); } assertEquals(a5.length, list.size()); - for (int j = 0; j < 1000; j++) - { - assertEquals(a5[ j ], list.get(j)); + for (int j = 0; j < 1000; j++) { + assertEquals(a5[j], list.get(j)); } } } diff --git a/src/testcases/org/apache/poi/util/TestIntegerField.java b/src/testcases/org/apache/poi/util/TestIntegerField.java index e9ca052944..517216dbf3 100644 --- a/src/testcases/org/apache/poi/util/TestIntegerField.java +++ b/src/testcases/org/apache/poi/util/TestIntegerField.java @@ -17,13 +17,13 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test IntegerField code @@ -34,48 +34,26 @@ public final class TestIntegerField { @Test public void testConstructors() { - try - { - new IntegerField(-1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new IntegerField(-1)); IntegerField field = new IntegerField(2); assertEquals(0, field.get()); - try - { - new IntegerField(-1, 1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new IntegerField(-1, 1)); field = new IntegerField(2, 0x12345678); assertEquals(0x12345678, field.get()); - byte[] array = new byte[ 6 ]; - try - { - new IntegerField(-1, 1, array); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new IntegerField(-1, 1, new byte[ 6 ])); + + byte[] array = new byte[ 6 ]; field = new IntegerField(2, 0x12345678, array); assertEquals(0x12345678, field.get()); assertEquals(( byte ) 0x78, array[ 2 ]); assertEquals(( byte ) 0x56, array[ 3 ]); assertEquals(( byte ) 0x34, array[ 4 ]); assertEquals(( byte ) 0x12, array[ 5 ]); - array = new byte[ 5 ]; - try - { - new IntegerField(2, 5, array); - fail("should have gotten ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new IntegerField(2, 5, new byte[ 5 ])); + for (int element : _test_array) { array = new byte[ 4 ]; new IntegerField(0, element, array); @@ -90,43 +68,31 @@ public final class TestIntegerField { for (int j = 0; j < _test_array.length; j++) { field.set(_test_array[ j ]); - assertEquals("testing _1 " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing _1 " + j); field = new IntegerField(0); field.set(_test_array[ j ], array); - assertEquals("testing _2 ", _test_array[ j ], field.get()); - assertEquals("testing _3.0 " + _test_array[ j ], - ( byte ) (_test_array[ j ] % 256), array[ 0 ]); - assertEquals("testing _3.1 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 8) % 256), - array[ 1 ]); - assertEquals("testing _3.2 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 16) % 256), - array[ 2 ]); - assertEquals("testing _3.3 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 24) % 256), - array[ 3 ]); + assertEquals(_test_array[ j ], field.get(), "testing _2 "); + assertEquals(( byte ) (_test_array[ j ] % 256), array[ 0 ], "testing _3.0 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 8) % 256), array[ 1 ], "testing _3.1 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 16) % 256), array[ 2 ], "testing _3.2 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 24) % 256), array[ 3 ], "testing _3.3 " + _test_array[ j ]); } } @Test public void testReadFromBytes() { - IntegerField field = new IntegerField(1); + IntegerField field1 = new IntegerField(1); byte[] array = new byte[ 4 ]; - try { - field.readFromBytes(array); - fail("should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new IntegerField(0); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> field1.readFromBytes(array)); + IntegerField field2 = new IntegerField(0); for (int j = 0; j < _test_array.length; j++) { array[ 0 ] = ( byte ) (_test_array[ j ] % 256); array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); array[ 2 ] = ( byte ) ((_test_array[ j ] >> 16) % 256); array[ 3 ] = ( byte ) ((_test_array[ j ] >> 24) % 256); - field.readFromBytes(array); - assertEquals("testing " + j, _test_array[ j ], field.get()); + field2.readFromBytes(array); + assertEquals(_test_array[ j ], field2.get(), "testing " + j); } } @@ -145,7 +111,7 @@ public final class TestIntegerField { for (int j = 0; j < buffer.length / 4; j++) { field.readFromStream(stream); - assertEquals("Testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "Testing " + j); } } @@ -163,7 +129,7 @@ public final class TestIntegerField { val += (array[ 2 ] << 16) & 0x00FF0000; val += (array[ 1 ] << 8) & 0x0000FF00; val += (array[ 0 ] & 0x000000FF); - assertEquals("testing ", b, val); + assertEquals(b, val, "testing "); } } } diff --git a/src/testcases/org/apache/poi/util/TestLittleEndian.java b/src/testcases/org/apache/poi/util/TestLittleEndian.java index 24072fa656..30d3a8e96e 100644 --- a/src/testcases/org/apache/poi/util/TestLittleEndian.java +++ b/src/testcases/org/apache/poi/util/TestLittleEndian.java @@ -17,16 +17,16 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.util.LittleEndian.BufferUnderrunException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Class to test LittleEndian functionality @@ -81,7 +81,7 @@ public final class TestLittleEndian { assertEquals(testdata3[2], (byte)0xFF); assertEquals(expected2, LittleEndian.getUShort(testdata3)); assertEquals(expected3, LittleEndian.getUShort(testdata3, 1)); - + } private static final byte[] _double_array = @@ -239,13 +239,13 @@ public final class TestLittleEndian { assertTrue(compareByteArrays(received, expected, 1, LittleEndianConsts.LONG_SIZE)); } - private static byte[] _good_array = { - 0x01, 0x02, 0x01, 0x02, - 0x01, 0x02, 0x01, 0x02, + private static final 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 final byte[] _bad_array = { 0x01 }; @@ -269,13 +269,8 @@ public final class TestLittleEndian { } assertEquals(count, _good_array.length / LittleEndianConsts.SHORT_SIZE); - stream = new ByteArrayInputStream(_bad_array); - try { - LittleEndian.readShort(stream); - fail("Should have caught BufferUnderrunException"); - } catch (BufferUnderrunException ignored) { - // as expected - } + ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array); + assertThrows(BufferUnderrunException.class, () -> LittleEndian.readShort(stream2)); } /** @@ -297,14 +292,8 @@ public final class TestLittleEndian { } } assertEquals(count, _good_array.length / LittleEndianConsts.INT_SIZE); - stream = new ByteArrayInputStream(_bad_array); - try { - LittleEndian.readInt(stream); - fail("Should have caught BufferUnderrunException"); - } catch (BufferUnderrunException ignored) { - - // as expected - } + ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array); + assertThrows(BufferUnderrunException.class, () -> LittleEndian.readInt(stream2)); } /** @@ -325,18 +314,12 @@ public final class TestLittleEndian { break; } } - assertEquals(count, - _good_array.length / LittleEndianConsts.LONG_SIZE); - stream = new ByteArrayInputStream(_bad_array); - try { - LittleEndian.readLong(stream); - fail("Should have caught BufferUnderrunException"); - } catch (BufferUnderrunException ignored) { - // as expected - } + assertEquals(count, _good_array.length / LittleEndianConsts.LONG_SIZE); + ByteArrayInputStream stream2 = new ByteArrayInputStream(_bad_array); + assertThrows(BufferUnderrunException.class, () -> LittleEndian.readLong(stream2)); } - @Test(expected = BufferUnderrunException.class) + @Test public void testReadFromStream() throws IOException { int actual; actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 5, -128, })); @@ -345,7 +328,8 @@ public final class TestLittleEndian { actual = LittleEndian.readUShort(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, })); assertEquals(513, actual); - LittleEndian.readInt(new ByteArrayInputStream(new byte[] { 1, 2, 3, })); + assertThrows(BufferUnderrunException.class, () -> + LittleEndian.readInt(new ByteArrayInputStream(new byte[] { 1, 2, 3, }))); } @Test diff --git a/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java b/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java index 7e048dbc3a..36af4524e9 100644 --- a/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java +++ b/src/testcases/org/apache/poi/util/TestLittleEndianStreams.java @@ -17,17 +17,17 @@ package org.apache.poi.util; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Class to test {@link LittleEndianInputStream} and {@link LittleEndianOutputStream} @@ -74,8 +74,8 @@ public final class TestLittleEndianStreams { byte[] actBuf = new byte[4]; lei.readFully(actBuf); - assertFalse("Identified bug in readFully() - source buffer was modified", - actBuf[0] == 0x00 && srcBuf[0] == 0x77 && srcBuf[3] == 0x44); + assertFalse(actBuf[0] == 0x00 && srcBuf[0] == 0x77 && srcBuf[3] == 0x44, + "Identified bug in readFully() - source buffer was modified"); byte[] expBuf = HexRead.readFromString("77 66 55 44"); assertArrayEquals(actBuf, expBuf); diff --git a/src/testcases/org/apache/poi/util/TestLocaleUtil.java b/src/testcases/org/apache/poi/util/TestLocaleUtil.java index aeb79b973c..0b6b8d6642 100644 --- a/src/testcases/org/apache/poi/util/TestLocaleUtil.java +++ b/src/testcases/org/apache/poi/util/TestLocaleUtil.java @@ -17,17 +17,17 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assume.assumeFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assumptions.assumeFalse; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class TestLocaleUtil { @@ -41,7 +41,7 @@ public class TestLocaleUtil { * Reset the Locale to the user default before the test so that it isn't influenced * by the LocaleUtil's state being changed by previous tests. */ - @Before + @BeforeEach @SuppressForbidden("implementation around default locales in POI") public void setUp() { // reset the user locale and time zone so that tests do not interfere with each other @@ -60,7 +60,7 @@ public class TestLocaleUtil { * Reset the Locale to the user default after the test so that it doesn't influence * other tests. */ - @After + @AfterEach public void tearDown() { LocaleUtil.resetUserLocale(); LocaleUtil.resetUserTimeZone(); @@ -118,10 +118,10 @@ public class TestLocaleUtil { private static void assertCalendarNotEquals(Calendar expected, Calendar actual) { // FIXME: add more tests to compare calendars, ignoring whether the dates are equal - assertNotEquals("time zone", expected.getTimeZone(), actual.getTimeZone()); + assertNotEquals(expected.getTimeZone(), actual.getTimeZone(), "time zone"); } private static void assertCalendarEquals(Calendar expected, Calendar actual) { // FIXME: add more tests to compare calendars, ignoring whether the set dates are equal - assertEquals("time zone", expected.getTimeZone(), actual.getTimeZone()); + assertEquals(expected.getTimeZone(), actual.getTimeZone(), "time zone"); } } diff --git a/src/testcases/org/apache/poi/util/TestLongField.java b/src/testcases/org/apache/poi/util/TestLongField.java index 7f50bd97cd..952af51bb3 100644 --- a/src/testcases/org/apache/poi/util/TestLongField.java +++ b/src/testcases/org/apache/poi/util/TestLongField.java @@ -17,13 +17,14 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayInputStream; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test LongField code @@ -37,33 +38,21 @@ public final class TestLongField { @Test public void testConstructors() { - try { - new LongField(-1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - LongField field = new LongField(2); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new LongField(-1)); - assertEquals(0L, field.get()); - try { - new LongField(-1, 1L); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new LongField(2, 0x123456789ABCDEF0L); - assertEquals(0x123456789ABCDEF0L, field.get()); - byte[] array = new byte[ 10 ]; + LongField field1 = new LongField(2); + assertEquals(0L, field1.get()); - try { - new LongField(-1, 1L, array); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } - catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new LongField(2, 0x123456789ABCDEF0L, array); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new LongField(-1, 1L)); + + LongField field2 = new LongField(2, 0x123456789ABCDEF0L); + assertEquals(0x123456789ABCDEF0L, field2.get()); + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new LongField(-1, 1L, new byte[ 10 ])); + + + byte[] array = new byte[ 10 ]; + LongField field = new LongField(2, 0x123456789ABCDEF0L, array); assertEquals(0x123456789ABCDEF0L, field.get()); assertEquals(( byte ) 0xF0, array[ 2 ]); assertEquals(( byte ) 0xDE, array[ 3 ]); @@ -73,13 +62,9 @@ public final class TestLongField { assertEquals(( byte ) 0x56, array[ 7 ]); assertEquals(( byte ) 0x34, array[ 8 ]); assertEquals(( byte ) 0x12, array[ 9 ]); - array = new byte[ 9 ]; - try { - new LongField(2, 5L, array); - fail("should have gotten ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new LongField(2, 5L, new byte[ 9 ])); + for (long element : _test_array) { array = new byte[ 8 ]; new LongField(0, element, array); @@ -94,33 +79,18 @@ public final class TestLongField { for (int j = 0; j < _test_array.length; j++) { field.set(_test_array[ j ]); - assertEquals("testing _1 " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing _1 " + j); field = new LongField(0); field.set(_test_array[ j ], array); - assertEquals("testing _2 ", _test_array[ j ], field.get()); - assertEquals("testing _3.0 " + _test_array[ j ], - ( byte ) (_test_array[ j ] % 256), array[ 0 ]); - assertEquals("testing _3.1 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 8) % 256), - array[ 1 ]); - assertEquals("testing _3.2 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 16) % 256), - array[ 2 ]); - assertEquals("testing _3.3 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 24) % 256), - array[ 3 ]); - assertEquals("testing _3.4 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 32) % 256), - array[ 4 ]); - assertEquals("testing _3.5 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 40) % 256), - array[ 5 ]); - assertEquals("testing _3.6 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 48) % 256), - array[ 6 ]); - assertEquals("testing _3.7 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 56) % 256), - array[ 7 ]); + assertEquals(_test_array[ j ], field.get(), "testing _2 "); + assertEquals(( byte ) (_test_array[ j ] % 256), array[ 0 ], "testing _3.0 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 8) % 256), array[ 1 ], "testing _3.1 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 16) % 256), array[ 2 ], "testing _3.2 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 24) % 256), array[ 3 ], "testing _3.3 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 32) % 256), array[ 4 ], "testing _3.4 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 40) % 256), array[ 5 ], "testing _3.5 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 48) % 256), array[ 6 ], "testing _3.6 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 56) % 256), array[ 7 ], "testing _3.7 " + _test_array[ j ]); } } @@ -146,7 +116,7 @@ public final class TestLongField { array[ 6 ] = ( byte ) ((_test_array[ j ] >> 48) % 256); array[ 7 ] = ( byte ) ((_test_array[ j ] >> 56) % 256); field.readFromBytes(array); - assertEquals("testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing " + j); } } @@ -169,7 +139,7 @@ public final class TestLongField { for (int j = 0; j < buffer.length / 8; j++) { field.readFromStream(stream); - assertEquals("Testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "Testing " + j); } } @@ -191,7 +161,7 @@ public final class TestLongField { val += ((( long ) array[ 2 ]) << 16) & 0x0000000000FF0000L; val += ((( long ) array[ 1 ]) << 8) & 0x000000000000FF00L; val += (array[ 0 ] & 0x00000000000000FFL); - assertEquals("testing ", element, val); + assertEquals(element, val, "testing "); } } } diff --git a/src/testcases/org/apache/poi/util/TestPOILogFactory.java b/src/testcases/org/apache/poi/util/TestPOILogFactory.java index 666d1d9b2f..a4580bb04a 100644 --- a/src/testcases/org/apache/poi/util/TestPOILogFactory.java +++ b/src/testcases/org/apache/poi/util/TestPOILogFactory.java @@ -17,7 +17,7 @@ package org.apache.poi.util; -import org.junit.Test; +import org.junit.jupiter.api.Test; public final class TestPOILogFactory { diff --git a/src/testcases/org/apache/poi/util/TestPOILogger.java b/src/testcases/org/apache/poi/util/TestPOILogger.java index 0c6765cf14..d40d475ac7 100644 --- a/src/testcases/org/apache/poi/util/TestPOILogger.java +++ b/src/testcases/org/apache/poi/util/TestPOILogger.java @@ -18,11 +18,11 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests the log class. @@ -30,7 +30,7 @@ import org.junit.Test; public final class TestPOILogger implements POILogger { private String lastLog = ""; private Throwable lastEx; - + /** * Test different types of log output. */ @@ -41,23 +41,23 @@ public final class TestPOILogger implements POILogger { POILogFactory._loggerClassName = TestPOILogger.class.getName(); POILogger log = POILogFactory.getLogger( "foo" ); assertTrue(log instanceof TestPOILogger); - + TestPOILogger tLog = (TestPOILogger)log; - + log.log(POILogger.WARN, "Test = ", 1); assertEquals("Test = 1", tLog.lastLog); - + log.log(POILogger.ERROR, "Test ", 1,2,new Exception("bla")); assertEquals("Test 12", tLog.lastLog); assertNotNull(tLog.lastEx); - + log.log(POILogger.ERROR, "log\nforging", "\nevil","\nlog"); assertEquals("log forging evil log", tLog.lastLog); } finally { POILogFactory._loggerClassName = oldLCN; } } - + // ---------- POI Logger methods implemented for testing ---------- @Override diff --git a/src/testcases/org/apache/poi/util/TestRLEDecompressingInputStream.java b/src/testcases/org/apache/poi/util/TestRLEDecompressingInputStream.java index b403fc53a0..ee43e08df1 100644 --- a/src/testcases/org/apache/poi/util/TestRLEDecompressingInputStream.java +++ b/src/testcases/org/apache/poi/util/TestRLEDecompressingInputStream.java @@ -17,8 +17,8 @@ package org.apache.poi.util; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -26,33 +26,33 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class TestRLEDecompressingInputStream { - + /** * Section 3.2.1 No Compression Example - * + * * The following string illustrates an ASCII text string with a set of characters that cannot be compressed * by the compression algorithm specified in section 2.4.1. - * + * * abcdefghijklmnopqrstuv. - * + * * This example is provided to demonstrate the results of compressing and decompressing the string * using an interoperable implementation of the algorithm specified in section 2.4.1. - * + * * The following hex array represents the compressed byte array of the example string as compressed by * the compression algorithm. - * + * * 01 19 B0 00 61 62 63 64 65 66 67 68 00 69 6A 6B 6C * 6D 6E 6F 70 00 71 72 73 74 75 76 2E - * + * * The following hex array represents the decompressed byte array of the example string as * decompressed by the decompression algorithm. - * + * * 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 * 72 73 74 75 76 2E - * + * */ @Test public void noCompressionExample() { @@ -66,23 +66,23 @@ public class TestRLEDecompressingInputStream { /** * Section 3.2.2 Normal Compression Example - * + * * The following string illustrates an ASCII text string with a typical set of characters that can be * compressed by the compression algorithm. - * + * * #aaabcdefaaaaghijaaaaaklaaamnopqaaaaaaaaaaaarstuvwxyzaaa - * + * * This example is provided to demonstrate the results of compressing and decompressing the example * string using an interoperable implementation of the algorithm specified in section 2.4.1. - * + * * The following hex array represents the compressed byte array of the example string as compressed by * the compression algorithm: - * + * * 01 2F B0 00 23 61 61 61 62 63 64 65 82 66 00 70 * 61 67 68 69 6A 01 38 08 61 6B 6C 00 30 6D 6E 6F * 70 06 71 02 70 04 10 72 73 74 75 76 10 77 78 79 * 7A 00 3C - * + * * The following hex array represents the decompressed byte array of the example string as * decompressed by the decompression algorithm: * @@ -102,23 +102,23 @@ public class TestRLEDecompressingInputStream { final String expected = "#aaabcdefaaaaghijaaaaaklaaamnopqaaaaaaaaaaaarstuvwxyzaaa"; checkRLEDecompression(expected, compressed); } - + /** * Section 3.2.3 Maximum Compression Example - * + * * The following string illustrates an ASCII text string with a typical set of characters that can be * compressed by the compression algorithm. - * + * * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - * + * * This example is provided to demonstrate the results of compressing and decompressing the example * string using an interoperable implementation of the algorithm specified in section 2.4.1. - * + * * The following hex array represents the compressed byte array of the example string as compressed by * the compression algorithm: - * + * * 01 03 B0 02 61 45 00 - * + * * The following hex array represents the decompressed byte array of the example string as * decompressed by the decompression algorithm: * @@ -136,7 +136,7 @@ public class TestRLEDecompressingInputStream { final String expected = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; checkRLEDecompression(expected, compressed); } - + @Test public void decompress() throws IOException { final byte[] compressed = { @@ -146,7 +146,7 @@ public class TestRLEDecompressingInputStream { final byte[] expected = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".getBytes(StringUtil.UTF8); assertArrayEquals(expected, expanded); } - + private static void checkRLEDecompression(String expected, byte[] runLengthEncodedData) { InputStream compressedStream = new ByteArrayInputStream(runLengthEncodedData); ByteArrayOutputStream out = new ByteArrayOutputStream(); diff --git a/src/testcases/org/apache/poi/util/TestShortField.java b/src/testcases/org/apache/poi/util/TestShortField.java index c31202f2cc..2ae0cb6ef0 100644 --- a/src/testcases/org/apache/poi/util/TestShortField.java +++ b/src/testcases/org/apache/poi/util/TestShortField.java @@ -17,13 +17,14 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayInputStream; import java.io.IOException; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test ShortField code @@ -34,42 +35,24 @@ public final class TestShortField { @Test public void testConstructors() { - try { - new ShortField(-1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ShortField(-1)); ShortField field = new ShortField(2); assertEquals(0, field.get()); - try { - new ShortField(-1, ( short ) 1); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ShortField(-1, ( short ) 1)); field = new ShortField(2, ( short ) 0x1234); assertEquals(0x1234, field.get()); - byte[] array = new byte[ 4 ]; - try { - new ShortField(-1, ( short ) 1, array); - fail("Should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ShortField(-1, ( short ) 1, new byte[ 4 ])); + + byte[] array = new byte[ 4 ]; field = new ShortField(2, ( short ) 0x1234, array); assertEquals(( short ) 0x1234, field.get()); assertEquals(( byte ) 0x34, array[ 2 ]); assertEquals(( byte ) 0x12, array[ 3 ]); - array = new byte[ 3 ]; - try { - new ShortField(2, ( short ) 5, array); - fail("should have gotten ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> new ShortField(2, ( short ) 5, new byte[ 3 ])); + for (short element : _test_array) { array = new byte[ 2 ]; new ShortField(0, element, array); @@ -84,36 +67,29 @@ public final class TestShortField { for (int j = 0; j < _test_array.length; j++) { field.set(_test_array[ j ]); - assertEquals("testing _1 " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "testing _1 " + j); field = new ShortField(0); field.set(_test_array[ j ], array); - assertEquals("testing _2 ", _test_array[ j ], field.get()); - assertEquals("testing _3.0 " + _test_array[ j ], - ( byte ) (_test_array[ j ] % 256), array[ 0 ]); - assertEquals("testing _3.1 " + _test_array[ j ], - ( byte ) ((_test_array[ j ] >> 8) % 256), - array[ 1 ]); + assertEquals(_test_array[ j ], field.get(), "testing _2 "); + assertEquals(( byte ) (_test_array[ j ] % 256), array[ 0 ], "testing _3.0 " + _test_array[ j ]); + assertEquals(( byte ) ((_test_array[ j ] >> 8) % 256), array[ 1 ], "testing _3.1 " + _test_array[ j ]); } } @Test public void testReadFromBytes() { - ShortField field = new ShortField(1); + ShortField field1 = new ShortField(1); byte[] array = new byte[ 2 ]; - try { - field.readFromBytes(array); - fail("should have caught ArrayIndexOutOfBoundsException"); - } catch (ArrayIndexOutOfBoundsException ignored_e) { - // as expected - } - field = new ShortField(0); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> field1.readFromBytes(array)); + + ShortField field2 = new ShortField(0); for (int j = 0; j < _test_array.length; j++) { array[ 0 ] = ( byte ) (_test_array[ j ] % 256); array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256); - field.readFromBytes(array); - assertEquals("testing " + j, _test_array[ j ], field.get()); + field2.readFromBytes(array); + assertEquals(_test_array[ j ], field2.get(), "testing " + j); } } @@ -132,7 +108,7 @@ public final class TestShortField { for (int j = 0; j < buffer.length / 2; j++) { field.readFromStream(stream); - assertEquals("Testing " + j, _test_array[ j ], field.get()); + assertEquals(_test_array[ j ], field.get(), "Testing " + j); } } @@ -148,7 +124,7 @@ public final class TestShortField { val &= ( short ) 0xFF00; val += ( short ) (array[ 0 ] & 0x00FF); - assertEquals("testing ", element, val); + assertEquals(element, val, "testing "); } } } diff --git a/src/testcases/org/apache/poi/util/TestStringCodepointsIterable.java b/src/testcases/org/apache/poi/util/TestStringCodepointsIterable.java index e61aea653f..7152ec6cef 100644 --- a/src/testcases/org/apache/poi/util/TestStringCodepointsIterable.java +++ b/src/testcases/org/apache/poi/util/TestStringCodepointsIterable.java @@ -17,11 +17,12 @@ package org.apache.poi.util; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.ArrayList; import java.util.List; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test for StringCodepointsIterable @@ -37,8 +38,8 @@ public class TestStringCodepointsIterable { List<String> codePoints2 = new ArrayList<>(); sci.iterator().forEachRemaining(codePoints::add); sci.iterator().forEachRemaining(codePoints2::add); - Assert.assertEquals(17, codePoints.size()); - Assert.assertEquals(codePoints, codePoints2); + assertEquals(17, codePoints.size()); + assertEquals(codePoints, codePoints2); } } diff --git a/src/testcases/org/apache/poi/util/TestStringUtil.java b/src/testcases/org/apache/poi/util/TestStringUtil.java index b70a7dbda1..a03ab6a127 100644 --- a/src/testcases/org/apache/poi/util/TestStringUtil.java +++ b/src/testcases/org/apache/poi/util/TestStringUtil.java @@ -17,14 +17,15 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.nio.charset.Charset; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit test for StringUtil @@ -66,82 +67,60 @@ public class TestStringUtil { StringUtil.putCompressedUnicode( input, output, 0 ); for ( int j = 0; j < expected_output.length; j++ ) { - assertEquals( "testing offset " + j, expected_output[j], - output[j] ); + assertEquals( expected_output[j], output[j], "testing offset " + j ); } StringUtil.putCompressedUnicode( input, output, 100 - expected_output.length ); for ( int j = 0; j < expected_output.length; j++ ) { - assertEquals( "testing offset " + j, expected_output[j], - output[100 + j - expected_output.length] ); - } - try - { - StringUtil.putCompressedUnicode( input, output, - 101 - expected_output.length ); - fail( "Should have caught ArrayIndexOutOfBoundsException" ); - } - catch ( ArrayIndexOutOfBoundsException ignored ) - { - // as expected + assertEquals( expected_output[j], output[100 + j - expected_output.length], "testing offset " + j ); } + + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> StringUtil.putCompressedUnicode( input, output, 101 - expected_output.length )); } @Test public void testPutUncompressedUnicode() { byte[] output = new byte[100]; String input = "Hello World"; - byte[] expected_output = - { - (byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l', - (byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0, - (byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o', - (byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0, - (byte) 'd', (byte) 0 - }; + byte[] expected_output = { + (byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l', + (byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0, + (byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o', + (byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0, + (byte) 'd', (byte) 0 + }; StringUtil.putUnicodeLE( input, output, 0 ); - for ( int j = 0; j < expected_output.length; j++ ) - { - assertEquals( "testing offset " + j, expected_output[j], - output[j] ); + for ( int j = 0; j < expected_output.length; j++ ) { + assertEquals( expected_output[j], output[j], "testing offset " + j ); } - StringUtil.putUnicodeLE( input, output, - 100 - expected_output.length ); - for ( int j = 0; j < expected_output.length; j++ ) - { - assertEquals( "testing offset " + j, expected_output[j], - output[100 + j - expected_output.length] ); - } - try - { - StringUtil.putUnicodeLE( input, output, - 101 - expected_output.length ); - fail( "Should have caught ArrayIndexOutOfBoundsException" ); - } - catch ( ArrayIndexOutOfBoundsException ignored ) - { - // as expected + StringUtil.putUnicodeLE( input, output, 100 - expected_output.length ); + for ( int j = 0; j < expected_output.length; j++ ) { + assertEquals( expected_output[j], output[100 + j - expected_output.length], "testing offset " + j ); } + + assertThrows(ArrayIndexOutOfBoundsException.class, () -> + StringUtil.putUnicodeLE( input, output, 101 - expected_output.length )); } @Test public void startsWithIgnoreCase() { - assertTrue("same string", StringUtil.startsWithIgnoreCase("Apache POI", "Apache POI")); - assertTrue("longer string", StringUtil.startsWithIgnoreCase("Apache POI project", "Apache POI")); - assertTrue("different case", StringUtil.startsWithIgnoreCase("APACHE POI", "Apache POI")); - assertFalse("leading whitespace should not be ignored", StringUtil.startsWithIgnoreCase(" Apache POI project", "Apache POI")); - assertFalse("shorter string", StringUtil.startsWithIgnoreCase("Apache", "Apache POI")); + assertTrue(StringUtil.startsWithIgnoreCase("Apache POI", "Apache POI"), "same string"); + assertTrue(StringUtil.startsWithIgnoreCase("Apache POI project", "Apache POI"), "longer string"); + assertTrue(StringUtil.startsWithIgnoreCase("APACHE POI", "Apache POI"), "different case"); + assertFalse(StringUtil.startsWithIgnoreCase(" Apache POI project", "Apache POI"), "leading whitespace should not be ignored"); + assertFalse(StringUtil.startsWithIgnoreCase("Apache", "Apache POI"), "shorter string"); } @Test public void endsWithIgnoreCase() { - assertTrue("same string", StringUtil.endsWithIgnoreCase("Apache POI", "Apache POI")); - assertTrue("longer string", StringUtil.endsWithIgnoreCase("Project Apache POI", "Apache POI")); - assertTrue("different case", StringUtil.endsWithIgnoreCase("APACHE POI", "Apache POI")); - assertFalse("trailing whitespace should not be ignored", StringUtil.endsWithIgnoreCase("Apache POI project ", "Apache POI")); - assertFalse("shorter string", StringUtil.endsWithIgnoreCase("Apache", "Apache POI")); + assertTrue(StringUtil.endsWithIgnoreCase("Apache POI", "Apache POI"), "same string"); + assertTrue(StringUtil.endsWithIgnoreCase("Project Apache POI", "Apache POI"), "longer string"); + assertTrue(StringUtil.endsWithIgnoreCase("APACHE POI", "Apache POI"), "different case"); + assertFalse(StringUtil.endsWithIgnoreCase("Apache POI project ", "Apache POI"), "trailing whitespace should not be ignored"); + assertFalse(StringUtil.endsWithIgnoreCase("Apache", "Apache POI"), "shorter string"); } @Test @@ -152,27 +131,27 @@ public class TestStringUtil { assertEquals("5|8.5|true|string", StringUtil.join("|", 5, 8.5, true, "string")); //assumes Locale prints number decimal point as a period rather than a comma String[] arr = new String[] { "Apache", "POI", "project" }; - assertEquals("no separator", "ApachePOIproject", StringUtil.join(arr)); - assertEquals("separator", "Apache POI project", StringUtil.join(arr, " ")); + assertEquals("ApachePOIproject", StringUtil.join(arr), "no separator"); + assertEquals("Apache POI project", StringUtil.join(arr, " "), "separator"); } @Test public void count() { String test = "Apache POI project\n\u00a9 Copyright 2016"; // supports search in null or empty string - assertEquals("null", 0, StringUtil.countMatches(null, 'A')); - assertEquals("empty string", 0, StringUtil.countMatches("", 'A')); + assertEquals(0, StringUtil.countMatches(null, 'A'), "null"); + assertEquals(0, StringUtil.countMatches("", 'A'), "empty string"); - assertEquals("normal", 2, StringUtil.countMatches(test, 'e')); - assertEquals("normal, should not find a in escaped copyright", 1, StringUtil.countMatches(test, 'a')); + assertEquals(2, StringUtil.countMatches(test, 'e'), "normal"); + assertEquals(1, StringUtil.countMatches(test, 'a'), "normal, should not find a in escaped copyright"); // search for non-printable characters - assertEquals("null character", 0, StringUtil.countMatches(test, '\0')); - assertEquals("CR", 0, StringUtil.countMatches(test, '\r')); - assertEquals("LF", 1, StringUtil.countMatches(test, '\n')); + assertEquals(0, StringUtil.countMatches(test, '\0'), "null character"); + assertEquals(0, StringUtil.countMatches(test, '\r'), "CR"); + assertEquals(1, StringUtil.countMatches(test, '\n'), "LF"); // search for unicode characters - assertEquals("Unicode", 1, StringUtil.countMatches(test, '\u00a9')); + assertEquals(1, StringUtil.countMatches(test, '\u00a9'), "Unicode"); } } diff --git a/src/testcases/org/apache/poi/util/TestTempFile.java b/src/testcases/org/apache/poi/util/TestTempFile.java index ac1904dabc..8ed0e837c1 100644 --- a/src/testcases/org/apache/poi/util/TestTempFile.java +++ b/src/testcases/org/apache/poi/util/TestTempFile.java @@ -16,11 +16,11 @@ ==================================================================== */ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.io.FileOutputStream; @@ -28,20 +28,20 @@ import java.io.IOException; import java.util.Arrays; import org.apache.poi.poifs.dev.TestPOIFSDump; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public class TestTempFile { private String previousTempDir; private File tempDir; - @Before + @BeforeEach public void setUp() throws IOException { previousTempDir = System.getProperty(TempFile.JAVA_IO_TMPDIR); if(previousTempDir != null) { - assertTrue("Failed to create directory " + previousTempDir, - new File(previousTempDir).exists() || new File(previousTempDir).mkdirs()); + assertTrue(new File(previousTempDir).exists() || new File(previousTempDir).mkdirs(), + "Failed to create directory " + previousTempDir); } // use a separate tempdir for the tests to be able to check for leftover files @@ -51,19 +51,19 @@ public class TestTempFile { System.setProperty(TempFile.JAVA_IO_TMPDIR, tempDir.getAbsolutePath()); } - @After + @AfterEach public void tearDown() throws IOException { if(tempDir != null) { String[] files = tempDir.list(); assertNotNull(files); // can have the "poifiles" subdir if (files.length == 1) { - assertEquals("Had: " + Arrays.toString(files), DefaultTempFileCreationStrategy.POIFILES, files[0]); + assertEquals(DefaultTempFileCreationStrategy.POIFILES, files[0], "Had: " + Arrays.toString(files)); files = new File(tempDir, files[0]).list(); assertNotNull(files); - assertEquals("Had: " + Arrays.toString(files), 0, files.length); + assertEquals(0, files.length, "Had: " + Arrays.toString(files)); } else { - assertEquals("Had: " + Arrays.toString(files), 0, files.length); + assertEquals(0, files.length, "Had: " + Arrays.toString(files)); } // remove the directory after the tests @@ -92,41 +92,35 @@ public class TestTempFile { FileOutputStream fos = new FileOutputStream(tempFile); fos.write(1); //file can be written to fos.close(); - assertTrue("temp file exists", tempFile.exists()); - assertTrue("temp file is a file", tempFile.isFile()); - assertTrue("temp file's name should start with test", - tempFile.getName().startsWith("test")); - assertTrue("temp file's name should end with .txt", - tempFile.getName().endsWith(".txt")); - assertEquals("temp file is saved in poifiles directory", - DefaultTempFileCreationStrategy.POIFILES, tempFile.getParentFile().getName()); + assertTrue(tempFile.exists()); + assertTrue(tempFile.isFile()); + assertTrue(tempFile.getName().startsWith("test")); + assertTrue(tempFile.getName().endsWith(".txt")); + assertEquals(DefaultTempFileCreationStrategy.POIFILES, tempFile.getParentFile().getName()); // Can't think of a good way to check whether a file is actually deleted since it would require the VM to stop. // Solution: set TempFileCreationStrategy to something that the unit test can trigger a deletion" - assertTrue("Unable to delete temp file", tempFile.delete()); + assertTrue(tempFile.delete()); } @Test public void createTempFileWithDefaultSuffix() throws IOException { File tempFile = TempFile.createTempFile("test", null); - assertTrue("temp file's name should end with .tmp", - tempFile.getName().endsWith(".tmp")); + assertTrue(tempFile.getName().endsWith(".tmp")); } @Test public void testCreateTempDirectory() throws IOException { File tempDir = TempFile.createTempDirectory("testDir"); - assertTrue("testDir exists", tempDir.exists()); - assertTrue("testDir is a directory", tempDir.isDirectory()); - assertTrue("testDir's name starts with testDir", - tempDir.getName().startsWith("testDir")); - assertEquals("tempDir is saved in poifiles directory", - DefaultTempFileCreationStrategy.POIFILES, tempDir.getParentFile().getName()); + assertTrue(tempDir.exists()); + assertTrue(tempDir.isDirectory()); + assertTrue(tempDir.getName().startsWith("testDir")); + assertEquals(DefaultTempFileCreationStrategy.POIFILES, tempDir.getParentFile().getName()); // Can't think of a good way to check whether a directory is actually deleted since it would require the VM to stop. // Solution: set TempFileCreationStrategy to something that the unit test can trigger a deletion" - assertTrue("Unable to delete tempDir", tempDir.delete()); + assertTrue(tempDir.delete()); } @Test diff --git a/src/testcases/org/apache/poi/util/TestXMLHelper.java b/src/testcases/org/apache/poi/util/TestXMLHelper.java index 5536168545..478ba0dbe0 100644 --- a/src/testcases/org/apache/poi/util/TestXMLHelper.java +++ b/src/testcases/org/apache/poi/util/TestXMLHelper.java @@ -17,11 +17,11 @@ package org.apache.poi.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; @@ -36,7 +36,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.XMLReader; @@ -121,9 +121,9 @@ public class TestXMLHelper { assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); } catch (SAXNotRecognizedException e) { // can happen for older XML Parsers, e.g. we have a CI Job which runs with Xerces XML Parser - assertTrue("Had Exception about not-recognized SAX feature: " + e + " which is only expected" + - " for Xerces XML Parser, but had parser: " + reader, - reader.getClass().getName().contains("org.apache.xerces")); + assertTrue(reader.getClass().getName().contains("org.apache.xerces"), + "Had Exception about not-recognized SAX feature: " + e + " which is only expected" + + " for Xerces XML Parser, but had parser: " + reader); } readers.add(reader); } |