Skip to content

Commit

Permalink
Add tests for status quo in ObjectUtils.nullSafeConciseToString()
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 4, 2023
1 parent 56b6012 commit 08bce69
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.util;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.sql.SQLException;
Expand All @@ -28,6 +30,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -232,22 +235,22 @@ void addObjectToSingleNullElementArray() {
}

@Test
void addObjectToNullArray() throws Exception {
void addObjectToNullArray() {
String newElement = "foo";
String[] newArray = ObjectUtils.addObjectToArray(null, newElement);
assertThat(newArray).hasSize(1);
assertThat(newArray[0]).isEqualTo(newElement);
}

@Test
void addNullObjectToNullArray() throws Exception {
void addNullObjectToNullArray() {
Object[] newArray = ObjectUtils.addObjectToArray(null, null);
assertThat(newArray).hasSize(1);
assertThat(newArray[0]).isNull();
}

@Test
void nullSafeEqualsWithArrays() throws Exception {
void nullSafeEqualsWithArrays() {
assertThat(ObjectUtils.nullSafeEquals(new String[] {"a", "b", "c"}, new String[] {"a", "b", "c"})).isTrue();
assertThat(ObjectUtils.nullSafeEquals(new int[] {1, 2, 3}, new int[] {1, 2, 3})).isTrue();
}
Expand Down Expand Up @@ -855,11 +858,19 @@ void nullSafeConciseToStringForEnum() {
}

@Test
void nullSafeConciseToStringForNumber() {
void nullSafeConciseToStringForPrimitivesAndWrappers() {
assertThat(ObjectUtils.nullSafeConciseToString(true)).isEqualTo("true");
assertThat(ObjectUtils.nullSafeConciseToString('X')).isEqualTo("X");
assertThat(ObjectUtils.nullSafeConciseToString(42L)).isEqualTo("42");
assertThat(ObjectUtils.nullSafeConciseToString(99.1234D)).isEqualTo("99.1234");
}

@Test
void nullSafeConciseToStringForBigNumbers() {
assertThat(ObjectUtils.nullSafeConciseToString(BigInteger.valueOf(42L))).isEqualTo("42");
assertThat(ObjectUtils.nullSafeConciseToString(BigDecimal.valueOf(99.1234D))).isEqualTo("99.1234");
}

@Test
void nullSafeConciseToStringForDate() {
Date date = new Date();
Expand Down Expand Up @@ -917,6 +928,12 @@ void nullSafeConciseToStringForArraysAndCollections() {
assertThat(ObjectUtils.nullSafeConciseToString(new HashSet<>(list))).startsWith(prefix(HashSet.class));
}

@Test
void nullSafeConciseToStringForMaps() {
Map<String, Integer> map = Map.of("a", 1, "b", 2, "c", 3);
assertThat(ObjectUtils.nullSafeConciseToString(map)).startsWith(prefix(map.getClass()));
}

@Test
void nullSafeConciseToStringForCustomTypes() {
class ExplosiveType {
Expand Down

0 comments on commit 08bce69

Please sign in to comment.