From dcfa93c2c92c5de3c72ecdf9ac2c0eba96ae4b3c Mon Sep 17 00:00:00 2001 From: Romain Brenguier Date: Mon, 5 Aug 2019 14:02:22 +0100 Subject: [PATCH 1/2] Fix type of value in byteArray2Long This was wrongly declared as int, causing the result to be incorrect for long values that do not fit in a int. --- crawler4j/src/main/java/edu/uci/ics/crawler4j/util/Util.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crawler4j/src/main/java/edu/uci/ics/crawler4j/util/Util.java b/crawler4j/src/main/java/edu/uci/ics/crawler4j/util/Util.java index 2a4923bb0..679266271 100644 --- a/crawler4j/src/main/java/edu/uci/ics/crawler4j/util/Util.java +++ b/crawler4j/src/main/java/edu/uci/ics/crawler4j/util/Util.java @@ -58,10 +58,10 @@ public static int byteArray2Int(byte[] b) { } public static long byteArray2Long(byte[] b) { - int value = 0; + long value = 0; for (int i = 0; i < 8; i++) { int shift = (8 - 1 - i) * 8; - value += (b[i] & 0x000000FF) << shift; + value += (b[i] & 0x000000FFL) << shift; } return value; } From bb2c4adc75ef94632392cbe9d7a7445aab3cdd6b Mon Sep 17 00:00:00 2001 From: yuzhong Date: Mon, 5 Aug 2019 12:41:20 +0100 Subject: [PATCH 2/2] Add unit tests for edu.uic.ics.crawler4j.Util These tests were written using Diffbule Cover. --- .../ics/crawler4j/tests/util/UtilTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 crawler4j/src/test/java/edu/uci/ics/crawler4j/tests/util/UtilTest.java diff --git a/crawler4j/src/test/java/edu/uci/ics/crawler4j/tests/util/UtilTest.java b/crawler4j/src/test/java/edu/uci/ics/crawler4j/tests/util/UtilTest.java new file mode 100644 index 000000000..2e6f71dde --- /dev/null +++ b/crawler4j/src/test/java/edu/uci/ics/crawler4j/tests/util/UtilTest.java @@ -0,0 +1,108 @@ +package edu.uci.ics.crawler4j.util; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class UtilTest { + + @Rule + public final Timeout globalTimeout = new Timeout(10000); + + /* testedClasses: Util */ + // Test written by Diffblue Cover. + @Test + public void long2ByteArray() { + + // Arrange + final long l = 0x12ff34ff56ffL; + + // Act + final byte[] actual = Util.long2ByteArray(l); + + // Assert result + Assert.assertArrayEquals( + new byte[] {0, 0, 0x12, -1, 0x34, -1, 0x56, -1}, actual); + } + + // Test written by Diffblue Cover. + @Test + public void int2ByteArray() { + + // Arrange + final int value = 0x12ff34ff; + + // Act + final byte[] actual = Util.int2ByteArray(value); + + // Assert result + Assert.assertArrayEquals(new byte[] {0x12, -1, 0x34, -1}, actual); + } + + // Test written by Diffblue Cover. + @Test + public void putIntInByteArray() { + + // Arrange + final int value = 0x12ff34ff; + final byte[] buf = {0, 0, 0, 0, 0, 0}; + final int offset = 2; + + // Act + Util.putIntInByteArray(value, buf, offset); + + // Assert side effects + Assert.assertArrayEquals(new byte[] {0, 0, 0x12, -1, 0x34, -1}, buf); + } + + // Test written by Diffblue Cover. + @Test + public void byteArray2Int() { + + // Arrange + final byte[] b = {0x12, -1, 0x34, -1}; + + // Act + final int actual = Util.byteArray2Int(b); + + // Assert result + Assert.assertEquals(0x12ff34ff, actual); + } + + // Test written by Diffblue Cover. + @Test + public void byteArray2Long() { + + // Arrange + final byte[] b = {0, 0, 0, -1, 0x34, -1, 0x56, -1}; + + // Act + final long actual = Util.byteArray2Long(b); + + // Assert result + Assert.assertEquals(0x0ff34ff56ffL, actual); + } + + // Test written by Diffblue Cover. + @Test + public void hasBinaryContent() { + Assert.assertFalse(Util.hasBinaryContent("BAZ")); + Assert.assertTrue(Util.hasBinaryContent("hhhYaimage")); + } + + // Test written by Diffblue Cover. + @Test + public void hasPlainTextContent() { + Assert.assertFalse(Util.hasPlainTextContent("1")); + Assert.assertFalse(Util.hasPlainTextContent("htmlTTeXT")); + Assert.assertTrue(Util.hasPlainTextContent("YxtEXTeXT")); + } + + // Test written by Diffblue Cover. + @Test + public void hasCssTextContentOutputFalse() { + Assert.assertFalse(Util.hasCssTextContent("1234")); + } + +}