From 7ab0f53cce22db68d4688e20e567addc04ad1432 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 26 Apr 2024 20:37:37 -0700 Subject: [PATCH] More test refactoring, duplicate removal --- .../jackson/core/io/NumberOutputTest.java | 101 ++++++++++++++++ ...{TestCharTypes.java => CharTypesTest.java} | 2 +- ...{TestDelegates.java => DelegatesTest.java} | 2 +- .../jackson/core/util/TestNumberPrinting.java | 113 ------------------ .../jackson/core/util/TestVersionUtil.java | 46 ------- .../jackson/core/util/VersionUtilTest.java | 74 ++++++++---- 6 files changed, 156 insertions(+), 182 deletions(-) rename src/test/java/com/fasterxml/jackson/core/util/{TestCharTypes.java => CharTypesTest.java} (96%) rename src/test/java/com/fasterxml/jackson/core/util/{TestDelegates.java => DelegatesTest.java} (99%) delete mode 100644 src/test/java/com/fasterxml/jackson/core/util/TestNumberPrinting.java delete mode 100644 src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java diff --git a/src/test/java/com/fasterxml/jackson/core/io/NumberOutputTest.java b/src/test/java/com/fasterxml/jackson/core/io/NumberOutputTest.java index 77d77ffc6f..c88908e66e 100644 --- a/src/test/java/com/fasterxml/jackson/core/io/NumberOutputTest.java +++ b/src/test/java/com/fasterxml/jackson/core/io/NumberOutputTest.java @@ -1,12 +1,65 @@ package com.fasterxml.jackson.core.io; +import java.util.Random; + import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; class NumberOutputTest { + @Test + void intPrinting() throws Exception + { + assertIntPrint(0); + assertIntPrint(-3); + assertIntPrint(1234); + assertIntPrint(-1234); + assertIntPrint(56789); + assertIntPrint(-56789); + assertIntPrint(999999); + assertIntPrint(-999999); + assertIntPrint(1000000); + assertIntPrint(-1000000); + assertIntPrint(10000001); + assertIntPrint(-10000001); + assertIntPrint(-100000012); + assertIntPrint(100000012); + assertIntPrint(1999888777); + assertIntPrint(-1999888777); + assertIntPrint(Integer.MAX_VALUE); + assertIntPrint(Integer.MIN_VALUE); + + Random rnd = new Random(12345L); + for (int i = 0; i < 251000; ++i) { + assertIntPrint(rnd.nextInt()); + } + } + + @Test + void longPrinting() throws Exception + { + // First, let's just cover couple of edge cases + assertLongPrint(0L, 0); + assertLongPrint(1L, 0); + assertLongPrint(-1L, 0); + assertLongPrint(Long.MAX_VALUE, 0); + assertLongPrint(Long.MIN_VALUE, 0); + assertLongPrint(Long.MAX_VALUE-1L, 0); + assertLongPrint(Long.MIN_VALUE+1L, 0); + + Random rnd = new Random(12345L); + // Bigger value space, need more iterations for long + for (int i = 0; i < 678000; ++i) { + long l = ((long) rnd.nextInt() << 32) | rnd.nextInt(); + assertLongPrint(l, i); + } + } + + // // // Tests for divBy1000 + @Test void divBy1000Small() { @@ -45,4 +98,52 @@ void divBy1000FullRange() { } } } + + /* + /********************************************************** + /* Internal methods + /********************************************************** + */ + + private void assertIntPrint(int value) + { + String exp = ""+value; + String act = printToString(value); + + if (!exp.equals(act)) { + assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")"); + } + String alt = NumberOutput.toString(value); + if (!exp.equals(alt)) { + assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")"); + } + } + + private void assertLongPrint(long value, int index) + { + String exp = ""+value; + String act = printToString(value); + + if (!exp.equals(act)) { + assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")"); + } + String alt = NumberOutput.toString(value); + if (!exp.equals(alt)) { + assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")"); + } + } + + private String printToString(int value) + { + char[] buffer = new char[12]; + int offset = NumberOutput.outputInt(value, buffer, 0); + return new String(buffer, 0, offset); + } + + private String printToString(long value) + { + char[] buffer = new char[22]; + int offset = NumberOutput.outputLong(value, buffer, 0); + return new String(buffer, 0, offset); + } } \ No newline at end of file diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestCharTypes.java b/src/test/java/com/fasterxml/jackson/core/util/CharTypesTest.java similarity index 96% rename from src/test/java/com/fasterxml/jackson/core/util/TestCharTypes.java rename to src/test/java/com/fasterxml/jackson/core/util/CharTypesTest.java index 7d1da82c71..733f374ff6 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestCharTypes.java +++ b/src/test/java/com/fasterxml/jackson/core/util/CharTypesTest.java @@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -class TestCharTypes +class CharTypesTest extends com.fasterxml.jackson.core.JUnit5TestBase { @Test diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java b/src/test/java/com/fasterxml/jackson/core/util/DelegatesTest.java similarity index 99% rename from src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java rename to src/test/java/com/fasterxml/jackson/core/util/DelegatesTest.java index caf9808d6e..00e6fe5716 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestDelegates.java +++ b/src/test/java/com/fasterxml/jackson/core/util/DelegatesTest.java @@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.*; -class TestDelegates extends com.fasterxml.jackson.core.JUnit5TestBase +class DelegatesTest extends com.fasterxml.jackson.core.JUnit5TestBase { static class BogusSchema implements FormatSchema { diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestNumberPrinting.java b/src/test/java/com/fasterxml/jackson/core/util/TestNumberPrinting.java deleted file mode 100644 index a11126f966..0000000000 --- a/src/test/java/com/fasterxml/jackson/core/util/TestNumberPrinting.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.fasterxml.jackson.core.util; - -import java.util.Random; - -import com.fasterxml.jackson.core.io.NumberOutput; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * Set of basic unit tests for verifying that the low-level number - * printing methods work as expected. - */ -class TestNumberPrinting - extends com.fasterxml.jackson.core.JUnit5TestBase -{ - @Test - void intPrinting() throws Exception - { - assertIntPrint(0); - assertIntPrint(-3); - assertIntPrint(1234); - assertIntPrint(-1234); - assertIntPrint(56789); - assertIntPrint(-56789); - assertIntPrint(999999); - assertIntPrint(-999999); - assertIntPrint(1000000); - assertIntPrint(-1000000); - assertIntPrint(10000001); - assertIntPrint(-10000001); - assertIntPrint(-100000012); - assertIntPrint(100000012); - assertIntPrint(1999888777); - assertIntPrint(-1999888777); - assertIntPrint(Integer.MAX_VALUE); - assertIntPrint(Integer.MIN_VALUE); - - Random rnd = new Random(12345L); - for (int i = 0; i < 251000; ++i) { - assertIntPrint(rnd.nextInt()); - } - } - - @Test - void longPrinting() throws Exception - { - // First, let's just cover couple of edge cases - assertLongPrint(0L, 0); - assertLongPrint(1L, 0); - assertLongPrint(-1L, 0); - assertLongPrint(Long.MAX_VALUE, 0); - assertLongPrint(Long.MIN_VALUE, 0); - assertLongPrint(Long.MAX_VALUE-1L, 0); - assertLongPrint(Long.MIN_VALUE+1L, 0); - - Random rnd = new Random(12345L); - // Bigger value space, need more iterations for long - for (int i = 0; i < 678000; ++i) { - long l = ((long) rnd.nextInt() << 32) | rnd.nextInt(); - assertLongPrint(l, i); - } - } - - /* - /********************************************************** - /* Internal methods - /********************************************************** - */ - - private void assertIntPrint(int value) - { - String exp = ""+value; - String act = printToString(value); - - if (!exp.equals(act)) { - assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")"); - } - String alt = NumberOutput.toString(value); - if (!exp.equals(alt)) { - assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+")"); - } - } - - private void assertLongPrint(long value, int index) - { - String exp = ""+value; - String act = printToString(value); - - if (!exp.equals(act)) { - assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")"); - } - String alt = NumberOutput.toString(value); - if (!exp.equals(alt)) { - assertEquals(exp, act, "Expected conversion (exp '"+exp+"', len "+exp.length()+"; act len "+act.length()+"; number index "+index+")"); - } - } - - private String printToString(int value) - { - char[] buffer = new char[12]; - int offset = NumberOutput.outputInt(value, buffer, 0); - return new String(buffer, 0, offset); - } - - private String printToString(long value) - { - char[] buffer = new char[22]; - int offset = NumberOutput.outputLong(value, buffer, 0); - return new String(buffer, 0, offset); - } -} diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java b/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java deleted file mode 100644 index 40da7bab82..0000000000 --- a/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fasterxml.jackson.core.util; - -import org.junit.jupiter.api.Test; - -import com.fasterxml.jackson.core.Version; -import com.fasterxml.jackson.core.json.PackageVersion; -import com.fasterxml.jackson.core.json.UTF8JsonGenerator; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class TestVersionUtil extends com.fasterxml.jackson.core.JUnit5TestBase -{ - @Test - void versionPartParsing() - { - assertEquals(13, VersionUtil.parseVersionPart("13")); - assertEquals(27, VersionUtil.parseVersionPart("27.8")); - assertEquals(0, VersionUtil.parseVersionPart("-3")); - } - - @Test - void versionParsing() - { - assertEquals(new Version(1, 2, 15, "foo", "group", "artifact"), - VersionUtil.parseVersion("1.2.15-foo", "group", "artifact")); - } - - @SuppressWarnings("deprecation") - @Test - void mavenVersionParsing() { - assertEquals(new Version(1, 2, 3, "SNAPSHOT", "foo.bar", "foo-bar"), - VersionUtil.mavenVersionFor(TestVersionUtil.class.getClassLoader(), "foo.bar", "foo-bar")); - } - - @Test - void packageVersionMatches() { - assertEquals(PackageVersion.VERSION, VersionUtil.versionFor(UTF8JsonGenerator.class)); - } - - // [core#248]: make sure not to return `null` but `Version.unknownVersion()` - @Test - void versionForUnknownVersion() { - // expecting return version.unknownVersion() instead of null - assertEquals(Version.unknownVersion(), VersionUtil.versionFor(TestVersionUtil.class)); - } -} diff --git a/src/test/java/com/fasterxml/jackson/core/util/VersionUtilTest.java b/src/test/java/com/fasterxml/jackson/core/util/VersionUtilTest.java index 9c1c49c6e8..f9d89f5780 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/VersionUtilTest.java +++ b/src/test/java/com/fasterxml/jackson/core/util/VersionUtilTest.java @@ -3,6 +3,8 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.core.json.PackageVersion; +import com.fasterxml.jackson.core.json.UTF8JsonGenerator; import static org.junit.jupiter.api.Assertions.*; @@ -14,39 +16,69 @@ class VersionUtilTest { @Test - void parseVersionSimple() { - Version v = VersionUtil.parseVersion("1.2.3-SNAPSHOT", "group", "artifact"); - assertEquals("group/artifact/1.2.3-SNAPSHOT", v.toFullString()); - } + void versionPartParsing() + { + assertEquals(13, VersionUtil.parseVersionPart("13")); + assertEquals(27, VersionUtil.parseVersionPart("27.8")); + assertEquals(0, VersionUtil.parseVersionPart("-3")); + } + + @Test + void versionParsing() + { + assertEquals(new Version(1, 2, 15, "foo", "group", "artifact"), + VersionUtil.parseVersion("1.2.15-foo", "group", "artifact")); + Version v = VersionUtil.parseVersion("1.2.3-SNAPSHOT", "group", "artifact"); + assertEquals("group/artifact/1.2.3-SNAPSHOT", v.toFullString()); + } @Test void parseVersionPartReturningPositive() { - assertEquals(66, VersionUtil.parseVersionPart("66R")); - } + assertEquals(66, VersionUtil.parseVersionPart("66R")); + } @Test void parseVersionReturningVersionWhereGetMajorVersionIsZero() { - Version version = VersionUtil.parseVersion("#M&+m@569P", "#M&+m@569P", "com.fasterxml.jackson.core.util.VersionUtil"); + Version version = VersionUtil.parseVersion("#M&+m@569P", "#M&+m@569P", "com.fasterxml.jackson.core.util.VersionUtil"); - assertEquals(0, version.getMinorVersion()); - assertEquals(0, version.getPatchLevel()); - assertEquals(0, version.getMajorVersion()); - assertFalse(version.isSnapshot()); - assertFalse(version.isUnknownVersion()); - } + assertEquals(0, version.getMinorVersion()); + assertEquals(0, version.getPatchLevel()); + assertEquals(0, version.getMajorVersion()); + assertFalse(version.isSnapshot()); + assertFalse(version.isUnknownVersion()); + } @Test void parseVersionWithEmptyStringAndEmptyString() { - Version version = VersionUtil.parseVersion("", "", "\"g2AT"); - - assertTrue(version.isUnknownVersion()); - } + Version version = VersionUtil.parseVersion("", "", "\"g2AT"); + assertTrue(version.isUnknownVersion()); + } @Test void parseVersionWithNullAndEmptyString() { - Version version = VersionUtil.parseVersion(null, "/nUmRN)3", ""); + Version version = VersionUtil.parseVersion(null, "/nUmRN)3", ""); - assertFalse(version.isSnapshot()); - } + assertFalse(version.isSnapshot()); + } + + @Test + void packageVersionMatches() { + assertEquals(PackageVersion.VERSION, VersionUtil.versionFor(UTF8JsonGenerator.class)); + } -} \ No newline at end of file + // [core#248]: make sure not to return `null` but `Version.unknownVersion()` + @Test + void versionForUnknownVersion() { + // expecting return version.unknownVersion() instead of null + assertEquals(Version.unknownVersion(), VersionUtil.versionFor(VersionUtilTest.class)); + } + + // // // Deprecated functionality + + @SuppressWarnings("deprecation") + @Test + void mavenVersionParsing() { + assertEquals(new Version(1, 2, 3, "SNAPSHOT", "foo.bar", "foo-bar"), + VersionUtil.mavenVersionFor(VersionUtilTest.class.getClassLoader(), "foo.bar", "foo-bar")); + } +}