|
| 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 org.apache.dubbo.common.utils; |
| 18 | + |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | + |
| 27 | +class TypeUtilsTest { |
| 28 | + |
| 29 | + static class StringList extends java.util.ArrayList<String> {} |
| 30 | + |
| 31 | + static interface StringIntegerMap extends Map<String, Integer> {} |
| 32 | + |
| 33 | + @Test |
| 34 | + void testIsParameterizedType() { |
| 35 | + // String is not parameterized |
| 36 | + assertFalse(TypeUtils.isParameterizedType(String.class)); |
| 37 | + |
| 38 | + // Get the generic superclass of our StringList (which is ArrayList<String>) |
| 39 | + Type genericType = StringList.class.getGenericSuperclass(); |
| 40 | + assertTrue(TypeUtils.isParameterizedType(genericType)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testGetRawClass() { |
| 45 | + Type genericType = StringList.class.getGenericSuperclass(); // ArrayList<String> |
| 46 | + assertEquals(java.util.ArrayList.class, TypeUtils.getRawClass(genericType)); |
| 47 | + |
| 48 | + // Test with a normal class |
| 49 | + assertEquals(String.class, TypeUtils.getRawClass(String.class)); |
| 50 | + |
| 51 | + // Test with null |
| 52 | + assertNull(TypeUtils.getRawClass(null)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testFindActualTypeArguments() { |
| 57 | + // Check StringList (should find String) |
| 58 | + List<Class<?>> args = TypeUtils.findActualTypeArguments(StringList.class, List.class); |
| 59 | + assertEquals(1, args.size()); |
| 60 | + assertEquals(String.class, args.get(0)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testGetClassName() { |
| 65 | + assertEquals(String.class.getName(), TypeUtils.getClassName(String.class)); |
| 66 | + } |
| 67 | +} |
0 commit comments