From 50e9758074f301a99143df008f78edfa8e767185 Mon Sep 17 00:00:00 2001 From: David Noble Date: Sun, 15 Sep 2019 21:19:31 -0700 Subject: [PATCH] Added, revised, and debugged some tests and test issues --- .../azure/data/cosmos/core/Utf8String.java | 4 +- .../data/cosmos/core/Utf8StringTest.java | 215 ++++++++++++++---- 2 files changed, 171 insertions(+), 48 deletions(-) diff --git a/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java b/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java index e8be3bf..2eaaa8e 100644 --- a/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java +++ b/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java @@ -237,7 +237,7 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable return false; } Utf8String that = (Utf8String) other; - return this.utf16CodeUnitCount == that.utf16CodeUnitCount && Objects.equal(this.buffer, that.buffer); + return Objects.equal(this.buffer, that.buffer); } /** @@ -279,7 +279,7 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable @Override public int hashCode() { - return this.buffer.hashCode(); + return this.buffer == null ? 0 : this.buffer.hashCode(); } /** diff --git a/java/src/test/java/com/azure/data/cosmos/core/Utf8StringTest.java b/java/src/test/java/com/azure/data/cosmos/core/Utf8StringTest.java index f69645c..b1cc24f 100644 --- a/java/src/test/java/com/azure/data/cosmos/core/Utf8StringTest.java +++ b/java/src/test/java/com/azure/data/cosmos/core/Utf8StringTest.java @@ -14,6 +14,8 @@ import java.util.Iterator; import java.util.Optional; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertThrows; import static org.testng.Assert.assertTrue; @@ -73,6 +75,8 @@ public class Utf8StringTest { assertEquals(optional.get().compareTo(value), 0); assertEquals(optional.get().compareTo(item.value()), 0); + // TODO: DANOBLE: create new test from the following assertions because they don't relate to the item under test + assertThrows(NullPointerException.class, () -> { //noinspection ConstantConditions,ResultOfMethodCallIgnored value.compareTo((Utf8String) null); @@ -84,80 +88,199 @@ public class Utf8StringTest { assertEquals(Utf8String.EMPTY.compareTo(Utf8String.NULL), 1); } - @Test - public void testEncodedLength() { + @Test(dataProvider = "unicodeTextDataProvider") + public void testEncodedLength(UnicodeTextItem item) { + final int encodedLength = item.buffer.length; + assertEquals(Utf8String.from(item.byteBuf()).orElseThrow(AssertionError::new).encodedLength(), encodedLength); + assertEquals(Utf8String.fromUnsafe(item.byteBuf()).encodedLength(), encodedLength); + assertEquals(Utf8String.transcodeUtf16(item.value()).encodedLength(), encodedLength); + } + + @Test(dataProvider = "unicodeTextDataProvider") + public void testTestEquals(UnicodeTextItem item) { + + TestEquals testEquals = new TestEquals(item); + + testEquals.invoke(item.byteBuf()); + testEquals.invoke(item.value()); + testEquals.invoke(Utf8String.fromUnsafe(item.byteBuf())); + testEquals.invoke((Object)Utf8String.fromUnsafe(item.byteBuf())); + + TestNotEquals testNotEquals = new TestNotEquals(item); + + testNotEquals.invoke(Utf8String.EMPTY); + testNotEquals.invoke(Unpooled.EMPTY_BUFFER); + testNotEquals.invoke((Object) Utf8String.EMPTY); + testNotEquals.invoke(""); + + testNotEquals.invoke(Utf8String.NULL); + testNotEquals.invoke((ByteBuf) null); + testNotEquals.invoke((Object) null); + testNotEquals.invoke((String) null); + + testNotEquals.invoke((Object) item.byteBuf()); + testNotEquals.invoke((Object) item.value()); + + testNotEquals.invoke((Utf8String) null); + } + + @Test(dataProvider = "unicodeTextDataProvider") + public void testFrom(UnicodeTextItem item) { + Optional value = Utf8String.from(item.byteBuf()); + assertTrue(value.isPresent()); + assertTrue(value.get().equals(value.get())); + assertTrue(value.get().equals(item.value())); + assertTrue(value.get().equals(item.byteBuf())); + } + + @Test(dataProvider = "unicodeTextDataProvider") + public void testFromUnsafe(UnicodeTextItem item) { + Utf8String value = Utf8String.fromUnsafe(item.byteBuf()); + assertTrue(value.equals(value)); + assertTrue(value.equals(item.value())); + assertTrue(value.equals(item.byteBuf())); + } + + @Test(dataProvider = "unicodeTextDataProvider") + public void testHashCode(UnicodeTextItem item) { + Utf8String value = Utf8String.fromUnsafe(item.byteBuf()); + assertEquals(value.hashCode(), item.byteBuf().hashCode()); + } + + @Test(dataProvider = "unicodeTextDataProvider") + public void testLength(UnicodeTextItem item) { + assertEquals(Utf8String.fromUnsafe(item.byteBuf()).length(), item.value().length()); } @Test - public void testTestEquals() { + public void testToString() { } - @Test - public void testTestEquals1() { + @Test(dataProvider = "unicodeTextDataProvider") + public void testToUtf16(UnicodeTextItem item) { + assertEquals(Utf8String.fromUnsafe(item.byteBuf()).toUtf16(), item.value()); } - @Test - public void testTestEquals2() { - } - - @Test - public void testTestEquals3() { - } - - @Test - public void testFrom() { - } - - @Test - public void testFromUnsafe() { - } - - @Test - public void testTestHashCode() { - } - - @Test - public void testLength() { - } - - @Test - public void testTestToString() { - } - - @Test - public void testToUtf16() { - } - - @Test - public void testTranscodeUtf16() { + @Test(dataProvider = "unicodeTextDataProvider") + public void testTranscodeUtf16(UnicodeTextItem item) { + assertEquals(Utf8String.transcodeUtf16(item.value()).toUtf16(), item.value()); } @DataProvider(name = "unicodeTextDataProvider") private static Iterator unicodeTextData() { ImmutableList items = ImmutableList.of( + // US ASCII (7-bit encoding) // ..English new UnicodeTextItem("The quick brown fox jumps over the lazy dog."), - // ISO-8859-1 (8-bit encoding that adds Latin-1 supplement to US ASCII) + + // ISO-8859-1 (8-bit encoding) // ..German new UnicodeTextItem("Der schnelle braune Fuchs springt über den faulen Hund."), - // ..Swedish - new UnicodeTextItem("Den snabbbruna räven hoppar över den lata hunden."), - // ISO 8859-7 (11-bit encoding that covers the Greek and Coptic alphabets) + // ..Icelandic + new UnicodeTextItem("Skjótur brúni refurinn hoppar yfir lata hundinn."), + // ..Spanish + new UnicodeTextItem("El rápido zorro marrón salta sobre el perro perezoso."), + + // ISO 8859-7 (11-bit encoding) // ..Greek new UnicodeTextItem("Η γρήγορη καφέ αλεπού πηδάει πάνω από το τεμπέλικο σκυλί."), - // Katakana code block (16-bit encoding containing katakana characters for the Japanese and Ainu languages) + + // Katakana code block (16-bit encoding) // ..Japanese new UnicodeTextItem("速い茶色のキツネは怠laな犬を飛び越えます。"), - // Deseret code block (21-bit encoding containing an English alphabet invented by the LDS Church) - // ..Deseret + + // Deseret code block (21-bit encoding containing an English alphabet invented by the LDS Church) + // ..Deseret new UnicodeTextItem("\uD801\uDC10\uD801\uDC2F\uD801\uDC4A\uD801\uDC2C, \uD801\uDC38\uD801\uDC35 \uD801\uDC2A\uD801\uDC49 \uD801\uDC4F?") ); return items.stream().map(item -> new Object[] { item }).iterator(); } + private static class TestEquals { + + private final UnicodeTextItem item; + private final Utf8String[] variants; + + public TestEquals(UnicodeTextItem item) { + + this.item = item; + + this.variants = new Utf8String[] { + Utf8String.from(this.item.byteBuf()).orElseThrow(AssertionError::new), + Utf8String.fromUnsafe(this.item.byteBuf()), + Utf8String.transcodeUtf16(this.item.value()) + }; + } + + public void invoke(ByteBuf other) { + for (Utf8String variant : this.variants) { + assertTrue(variant.equals(other)); + } + } + + public void invoke(Object other) { + for (Utf8String variant : this.variants) { + assertTrue(variant.equals(other)); + } + } + + public void invoke(String other) { + for (Utf8String variant : this.variants) { + assertTrue(variant.equals(other)); + } + } + + public void invoke(Utf8String other) { + for (Utf8String variant : this.variants) { + assertTrue(variant.equals(other)); + } + } + } + + private static class TestNotEquals { + + private final UnicodeTextItem item; + private final Utf8String[] variants; + + public TestNotEquals(UnicodeTextItem item) { + + this.item = item; + + this.variants = new Utf8String[] { + Utf8String.from(this.item.byteBuf()).orElseThrow(AssertionError::new), + Utf8String.fromUnsafe(this.item.byteBuf()), + Utf8String.transcodeUtf16(this.item.value()) + }; + } + + public void invoke(ByteBuf other) { + for (Utf8String variant : this.variants) { + assertFalse(variant.equals(other)); + } + } + + public void invoke(Object other) { + for (Utf8String variant : this.variants) { + assertFalse(variant.equals(other)); + } + } + + public void invoke(String other) { + for (Utf8String variant : this.variants) { + assertFalse(variant.equals(other)); + } + } + + public void invoke(Utf8String other) { + for (Utf8String variant : this.variants) { + assertFalse(variant.equals(other)); + } + } + } + private static class UnicodeTextItem { private final byte[] buffer;