|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.util;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.net.URI; |
| 21 | +import java.net.URL; |
20 | 22 | import java.sql.SQLException;
|
| 23 | +import java.time.LocalDate; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
21 | 26 | import java.util.Collections;
|
| 27 | +import java.util.Date; |
22 | 28 | import java.util.HashMap;
|
23 | 29 | import java.util.HashSet;
|
| 30 | +import java.util.List; |
| 31 | +import java.util.Locale; |
24 | 32 | import java.util.Set;
|
25 | 33 |
|
| 34 | +import org.junit.jupiter.api.Nested; |
26 | 35 | import org.junit.jupiter.api.Test;
|
27 | 36 |
|
28 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
| 38 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
29 | 39 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
30 | 40 | import static org.springframework.util.ObjectUtils.isEmpty;
|
31 | 41 |
|
@@ -816,7 +826,144 @@ void caseInsensitiveValueOf() {
|
816 | 826 | .withMessage("Constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes");
|
817 | 827 | }
|
818 | 828 |
|
819 |
| - private void assertEqualHashCodes(int expected, Object array) { |
| 829 | + @Nested |
| 830 | + class NullSafeConciseToStringTests { |
| 831 | + |
| 832 | + private final String truncated = " (truncated)..."; |
| 833 | + private final int truncatedLength = 100 + truncated.length(); |
| 834 | + |
| 835 | + @Test |
| 836 | + void nullSafeConciseToStringForNull() { |
| 837 | + assertThat(ObjectUtils.nullSafeConciseToString(null)).isEqualTo("null"); |
| 838 | + } |
| 839 | + |
| 840 | + @Test |
| 841 | + void nullSafeConciseToStringForClass() { |
| 842 | + assertThat(ObjectUtils.nullSafeConciseToString(String.class)).isEqualTo("java.lang.String"); |
| 843 | + } |
| 844 | + |
| 845 | + @Test |
| 846 | + void nullSafeConciseToStringForStrings() { |
| 847 | + String repeat100 = repeat("X", 100); |
| 848 | + String repeat101 = repeat("X", 101); |
| 849 | + |
| 850 | + assertThat(ObjectUtils.nullSafeConciseToString("foo")).isEqualTo("foo"); |
| 851 | + assertThat(ObjectUtils.nullSafeConciseToString(repeat100)).isEqualTo(repeat100); |
| 852 | + assertThat(ObjectUtils.nullSafeConciseToString(repeat101)).hasSize(truncatedLength).endsWith(truncated); |
| 853 | + } |
| 854 | + |
| 855 | + @Test |
| 856 | + void nullSafeConciseToStringForStringBuilders() { |
| 857 | + String repeat100 = repeat("X", 100); |
| 858 | + String repeat101 = repeat("X", 101); |
| 859 | + |
| 860 | + assertThat(ObjectUtils.nullSafeConciseToString(new StringBuilder("foo"))).isEqualTo("foo"); |
| 861 | + assertThat(ObjectUtils.nullSafeConciseToString(new StringBuilder(repeat100))).isEqualTo(repeat100); |
| 862 | + assertThat(ObjectUtils.nullSafeConciseToString(new StringBuilder(repeat101))).hasSize(truncatedLength).endsWith(truncated); |
| 863 | + } |
| 864 | + |
| 865 | + @Test |
| 866 | + void nullSafeConciseToStringForEnum() { |
| 867 | + assertThat(ObjectUtils.nullSafeConciseToString(Tropes.FOO)).isEqualTo("FOO"); |
| 868 | + } |
| 869 | + |
| 870 | + @Test |
| 871 | + void nullSafeConciseToStringForNumber() { |
| 872 | + assertThat(ObjectUtils.nullSafeConciseToString(42L)).isEqualTo("42"); |
| 873 | + assertThat(ObjectUtils.nullSafeConciseToString(99.1234D)).isEqualTo("99.1234"); |
| 874 | + } |
| 875 | + |
| 876 | + @Test |
| 877 | + void nullSafeConciseToStringForDate() { |
| 878 | + Date date = new Date(); |
| 879 | + assertThat(ObjectUtils.nullSafeConciseToString(date)).isEqualTo(date.toString()); |
| 880 | + } |
| 881 | + |
| 882 | + @Test |
| 883 | + void nullSafeConciseToStringForTemporal() { |
| 884 | + LocalDate localDate = LocalDate.now(); |
| 885 | + assertThat(ObjectUtils.nullSafeConciseToString(localDate)).isEqualTo(localDate.toString()); |
| 886 | + } |
| 887 | + |
| 888 | + @Test |
| 889 | + void nullSafeConciseToStringForUri() { |
| 890 | + String uri = "https://www.example.com/?foo=1&bar=2&baz=3"; |
| 891 | + assertThat(ObjectUtils.nullSafeConciseToString(URI.create(uri))).isEqualTo(uri); |
| 892 | + |
| 893 | + uri += "&qux=" + repeat("4", 60); |
| 894 | + assertThat(ObjectUtils.nullSafeConciseToString(URI.create(uri))) |
| 895 | + .hasSize(truncatedLength) |
| 896 | + .startsWith(uri.subSequence(0, 100)) |
| 897 | + .endsWith(truncated); |
| 898 | + } |
| 899 | + |
| 900 | + @Test |
| 901 | + void nullSafeConciseToStringForUrl() throws Exception { |
| 902 | + String url = "https://www.example.com/?foo=1&bar=2&baz=3"; |
| 903 | + assertThat(ObjectUtils.nullSafeConciseToString(new URL(url))).isEqualTo(url); |
| 904 | + |
| 905 | + url += "&qux=" + repeat("4", 60); |
| 906 | + assertThat(ObjectUtils.nullSafeConciseToString(new URL(url))) |
| 907 | + .hasSize(truncatedLength) |
| 908 | + .startsWith(url.subSequence(0, 100)) |
| 909 | + .endsWith(truncated); |
| 910 | + } |
| 911 | + |
| 912 | + @Test |
| 913 | + void nullSafeConciseToStringForLocale() { |
| 914 | + assertThat(ObjectUtils.nullSafeConciseToString(Locale.GERMANY)).isEqualTo("de_DE"); |
| 915 | + } |
| 916 | + |
| 917 | + @Test |
| 918 | + void nullSafeConciseToStringForArraysAndCollections() { |
| 919 | + List<String> list = Arrays.asList("a", "b", "c"); |
| 920 | + assertThat(ObjectUtils.nullSafeConciseToString(new int[][] {{1, 2}, {3, 4}})).startsWith(prefix(int[][].class)); |
| 921 | + assertThat(ObjectUtils.nullSafeConciseToString(list.toArray())).startsWith(prefix(String[].class)); |
| 922 | + assertThat(ObjectUtils.nullSafeConciseToString(list.toArray(new Object[0]))).startsWith(prefix(Object[].class)); |
| 923 | + assertThat(ObjectUtils.nullSafeConciseToString(list.toArray(new String[0]))).startsWith(prefix(String[].class)); |
| 924 | + assertThat(ObjectUtils.nullSafeConciseToString(new ArrayList<>(list))).startsWith(prefix(ArrayList.class)); |
| 925 | + assertThat(ObjectUtils.nullSafeConciseToString(new HashSet<>(list))).startsWith(prefix(HashSet.class)); |
| 926 | + } |
| 927 | + |
| 928 | + @Test |
| 929 | + void nullSafeConciseToStringForCustomTypes() { |
| 930 | + class ExplosiveType { |
| 931 | + @Override |
| 932 | + public String toString() { |
| 933 | + throw new UnsupportedOperationException("no-go"); |
| 934 | + } |
| 935 | + } |
| 936 | + ExplosiveType explosiveType = new ExplosiveType(); |
| 937 | + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(explosiveType::toString); |
| 938 | + assertThat(ObjectUtils.nullSafeConciseToString(explosiveType)).startsWith(prefix(ExplosiveType.class)); |
| 939 | + |
| 940 | + class WordyType { |
| 941 | + @Override |
| 942 | + public String toString() { |
| 943 | + return repeat("blah blah", 20); |
| 944 | + } |
| 945 | + } |
| 946 | + WordyType wordyType = new WordyType(); |
| 947 | + assertThat(wordyType).asString().hasSizeGreaterThanOrEqualTo(180 /* 9x20 */); |
| 948 | + assertThat(ObjectUtils.nullSafeConciseToString(wordyType)).startsWith(prefix(WordyType.class)); |
| 949 | + } |
| 950 | + |
| 951 | + private String repeat(String str, int count) { |
| 952 | + String result = ""; |
| 953 | + for (int i = 0; i < count; i++) { |
| 954 | + result += str; |
| 955 | + } |
| 956 | + return result; |
| 957 | + } |
| 958 | + |
| 959 | + private String prefix(Class<?> clazz) { |
| 960 | + return clazz.getTypeName() + "@"; |
| 961 | + } |
| 962 | + |
| 963 | + } |
| 964 | + |
| 965 | + |
| 966 | + private static void assertEqualHashCodes(int expected, Object array) { |
820 | 967 | int actual = ObjectUtils.nullSafeHashCode(array);
|
821 | 968 | assertThat(actual).isEqualTo(expected);
|
822 | 969 | assertThat(array.hashCode() != actual).isTrue();
|
|
0 commit comments