|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.style; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.nio.charset.Charset; |
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Nested; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for {@link SimpleValueStyler}. |
| 32 | + * |
| 33 | + * @author Sam Brannen |
| 34 | + * @since 6.0 |
| 35 | + */ |
| 36 | +class SimpleValueStylerTests { |
| 37 | + |
| 38 | + @Nested |
| 39 | + class CommonStyling { |
| 40 | + |
| 41 | + private final SimpleValueStyler styler = new SimpleValueStyler(); |
| 42 | + |
| 43 | + @Test |
| 44 | + void styleBasics() throws NoSuchMethodException { |
| 45 | + assertThat(styler.style(null)).isEqualTo("null"); |
| 46 | + assertThat(styler.style(true)).isEqualTo("true"); |
| 47 | + assertThat(styler.style(99.9)).isEqualTo("99.9"); |
| 48 | + assertThat(styler.style("str")).isEqualTo("\"str\""); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void stylePlainObject() { |
| 53 | + Object obj = new Object(); |
| 54 | + |
| 55 | + assertThat(styler.style(obj)).isEqualTo(String.valueOf(obj)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void styleMaps() { |
| 60 | + assertThat(styler.style(Map.of())).isEqualTo("{}"); |
| 61 | + assertThat(styler.style(Map.of("key", 1))).isEqualTo("{\"key\" -> 1}"); |
| 62 | + |
| 63 | + Map<String, Integer> map = new LinkedHashMap<>() {{ |
| 64 | + put("key1", 1); |
| 65 | + put("key2", 2); |
| 66 | + }}; |
| 67 | + assertThat(styler.style(map)).isEqualTo("{\"key1\" -> 1, \"key2\" -> 2}"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void styleMapEntries() { |
| 72 | + Map<String, Integer> map = Map.of("key1", 1, "key2", 2); |
| 73 | + |
| 74 | + assertThat(map.entrySet()).map(styler::style).containsExactlyInAnyOrder("\"key1\" -> 1", "\"key2\" -> 2"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void styleLists() { |
| 79 | + assertThat(styler.style(List.of())).isEqualTo("[]"); |
| 80 | + assertThat(styler.style(List.of(1))).isEqualTo("[1]"); |
| 81 | + assertThat(styler.style(List.of(1, 2))).isEqualTo("[1, 2]"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void stylePrimitiveArrays() { |
| 86 | + int[] array = new int[0]; |
| 87 | + assertThat(styler.style(array)).isEqualTo("[]"); |
| 88 | + |
| 89 | + array = new int[] { 1 }; |
| 90 | + assertThat(styler.style(array)).isEqualTo("[1]"); |
| 91 | + |
| 92 | + array = new int[] { 1, 2 }; |
| 93 | + assertThat(styler.style(array)).isEqualTo("[1, 2]"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void styleObjectArrays() { |
| 98 | + String[] array = new String[0]; |
| 99 | + assertThat(styler.style(array)).isEqualTo("[]"); |
| 100 | + |
| 101 | + array = new String[] { "str1" }; |
| 102 | + assertThat(styler.style(array)).isEqualTo("[\"str1\"]"); |
| 103 | + |
| 104 | + array = new String[] { "str1", "str2" }; |
| 105 | + assertThat(styler.style(array)).isEqualTo("[\"str1\", \"str2\"]"); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @Nested |
| 111 | + class DefaultClassAndMethodStylers { |
| 112 | + |
| 113 | + private final SimpleValueStyler styler = new SimpleValueStyler(); |
| 114 | + |
| 115 | + @Test |
| 116 | + void styleClass() { |
| 117 | + assertThat(styler.style(String.class)).isEqualTo("java.lang.String"); |
| 118 | + assertThat(styler.style(getClass())).isEqualTo(getClass().getCanonicalName()); |
| 119 | + assertThat(styler.style(String[].class)).isEqualTo("java.lang.String[]"); |
| 120 | + assertThat(styler.style(int[][].class)).isEqualTo("int[][]"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void styleMethod() throws NoSuchMethodException { |
| 125 | + assertThat(styler.style(String.class.getMethod("toString"))).isEqualTo("toString()"); |
| 126 | + assertThat(styler.style(String.class.getMethod("getBytes", Charset.class))).isEqualTo("getBytes(Charset)"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void styleClassMap() { |
| 131 | + Map<String, Class<?>> map = new LinkedHashMap<>() {{ |
| 132 | + put("key1", Integer.class); |
| 133 | + put("key2", DefaultClassAndMethodStylers.class); |
| 134 | + }}; |
| 135 | + assertThat(styler.style(map)).isEqualTo( |
| 136 | + "{\"key1\" -> java.lang.Integer, \"key2\" -> %s}", |
| 137 | + DefaultClassAndMethodStylers.class.getCanonicalName()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void styleClassList() { |
| 142 | + assertThat(styler.style(List.of(Integer.class, String.class))) |
| 143 | + .isEqualTo("[java.lang.Integer, java.lang.String]"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void styleClassArray() { |
| 148 | + Class<?>[] array = new Class<?>[] { Integer.class, getClass() }; |
| 149 | + assertThat(styler.style(array)) |
| 150 | + .isEqualTo("[%s, %s]", Integer.class.getCanonicalName(), getClass().getCanonicalName()); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + @Nested |
| 156 | + class CustomClassAndMethodStylers { |
| 157 | + |
| 158 | + private final SimpleValueStyler styler = new SimpleValueStyler(Class::getSimpleName, Method::toGenericString); |
| 159 | + |
| 160 | + @Test |
| 161 | + void styleClass() { |
| 162 | + assertThat(styler.style(String.class)).isEqualTo("String"); |
| 163 | + assertThat(styler.style(getClass())).isEqualTo(getClass().getSimpleName()); |
| 164 | + assertThat(styler.style(String[].class)).isEqualTo("String[]"); |
| 165 | + assertThat(styler.style(int[][].class)).isEqualTo("int[][]"); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void styleMethod() throws NoSuchMethodException { |
| 170 | + Method method = String.class.getMethod("toString"); |
| 171 | + assertThat(styler.style(method)).isEqualTo(method.toGenericString()); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void styleClassMap() { |
| 176 | + Map<String, Class<?>> map = new LinkedHashMap<>() {{ |
| 177 | + put("key1", Integer.class); |
| 178 | + put("key2", CustomClassAndMethodStylers.class); |
| 179 | + }}; |
| 180 | + assertThat(styler.style(map)).isEqualTo( |
| 181 | + "{\"key1\" -> %s, \"key2\" -> %s}", |
| 182 | + Integer.class.getSimpleName(), CustomClassAndMethodStylers.class.getSimpleName()); |
| 183 | + } |
| 184 | + @Test |
| 185 | + void styleClassList() { |
| 186 | + assertThat(styler.style(List.of(Integer.class, String.class))).isEqualTo("[Integer, String]"); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + void styleClassArray() { |
| 191 | + Class<?>[] array = new Class<?>[] { Integer.class, getClass() }; |
| 192 | + assertThat(styler.style(array)).isEqualTo("[%s, %s]", Integer.class.getSimpleName(), getClass().getSimpleName()); |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments