|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.microsphere.convert.multiple; |
| 18 | + |
| 19 | +import io.microsphere.convert.StringConverter; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import static java.lang.Integer.MAX_VALUE; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@link StringToIterableConverter} Test |
| 38 | + * |
| 39 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> |
| 40 | + * @see StringToIterableConverter |
| 41 | + * @since 1.0.0 |
| 42 | + */ |
| 43 | +class StringToIterableConverterTest { |
| 44 | + |
| 45 | + private StringToCollectionConverter converter; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + converter = new StringToCollectionConverter(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testAccept() { |
| 54 | + assertTrue(converter.accept(String.class, Collection.class)); |
| 55 | + assertTrue(converter.accept(String.class, List.class)); |
| 56 | + assertTrue(converter.accept(String.class, Set.class)); |
| 57 | + assertFalse(converter.accept(String.class, String.class)); |
| 58 | + assertFalse(converter.accept(null, null)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testConvert() { |
| 63 | + Collection<Integer> result = (Collection<Integer>) converter.convert("1,2,3", Collection.class, Integer.class); |
| 64 | + assertNotNull(result); |
| 65 | + assertEquals(3, result.size()); |
| 66 | + assertTrue(result.contains(1)); |
| 67 | + assertTrue(result.contains(2)); |
| 68 | + assertTrue(result.contains(3)); |
| 69 | + |
| 70 | + Collection<String> strResult = (Collection<String>) converter.convert("a,b", Collection.class, String.class); |
| 71 | + assertNotNull(strResult); |
| 72 | + assertEquals(2, strResult.size()); |
| 73 | + assertTrue(strResult.contains("a")); |
| 74 | + assertTrue(strResult.contains("b")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testConvertOnNoStringConverter() { |
| 79 | + // No StringConverter registered for Object.class → returns null |
| 80 | + assertNull(converter.convert(new String[]{"a"}, 1, Collection.class, Object.class)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testConvertOnNonCollectionIterable() { |
| 85 | + // When createMultiValue returns a non-Collection Iterable, the elements are not added; |
| 86 | + // the iterable is returned as-is once a StringConverter is found for the element type. |
| 87 | + StringToIterableConverter<Iterable> iterableConverter = new StringToIterableConverter<Iterable>() { |
| 88 | + @Override |
| 89 | + protected Iterable createMultiValue(int size, Class<?> multiValueType) { |
| 90 | + return Collections::emptyIterator; |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + Object result = iterableConverter.convert(new String[]{"1", "2"}, 2, Iterable.class, Integer.class); |
| 95 | + assertNotNull(result); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testGetStringConverter() { |
| 100 | + // Integer has a registered StringConverter |
| 101 | + Optional<StringConverter> intConverter = converter.getStringConverter(Integer.class); |
| 102 | + assertTrue(intConverter.isPresent()); |
| 103 | + |
| 104 | + // Object.class has no registered StringConverter |
| 105 | + Optional<StringConverter> objectConverter = converter.getStringConverter(Object.class); |
| 106 | + assertFalse(objectConverter.isPresent()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testGetSupportedType() { |
| 111 | + assertEquals(Collection.class, converter.getSupportedType()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testGetPriority() { |
| 116 | + // Collection has 1 interface level above Iterable, so level=1 → priority = MAX_VALUE - 1 |
| 117 | + assertEquals(MAX_VALUE - 1, converter.getPriority()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testGetSourceType() { |
| 122 | + assertEquals(String.class, converter.getSourceType()); |
| 123 | + } |
| 124 | +} |
0 commit comments