View Javadoc
1   /*
2    *    Copyright 2009-2024 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  package org.apache.ibatis.executor;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.verify;
21  import static org.mockito.Mockito.when;
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.apache.ibatis.reflection.MetaObject;
29  import org.apache.ibatis.reflection.factory.ObjectFactory;
30  import org.apache.ibatis.session.Configuration;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.extension.ExtendWith;
35  import org.mockito.Mock;
36  import org.mockito.junit.jupiter.MockitoExtension;
37  
38  @ExtendWith(MockitoExtension.class)
39  class ResultExtractorTest {
40  
41    private ResultExtractor resultExtractor;
42  
43    @Mock
44    private Configuration configuration;
45    @Mock
46    private ObjectFactory objectFactory;
47  
48    @BeforeEach
49    void setUp() {
50      resultExtractor = new ResultExtractor(configuration, objectFactory);
51    }
52  
53    @Test
54    void shouldExtractNullForNullTargetType() {
55      final Object result = resultExtractor.extractObjectFromList(null, null);
56      assertThat(result).isNull();
57    }
58  
59    @Test
60    void shouldExtractList() {
61      final List<Object> list = Arrays.asList(1, 2, 3);
62      final Object result = resultExtractor.extractObjectFromList(list, List.class);
63      assertThat(result).isInstanceOf(List.class);
64      final List resultList = (List) result;
65      assertThat(resultList).isEqualTo(list);
66    }
67  
68    @Test
69    void shouldExtractArray() {
70      final List<Object> list = Arrays.asList(1, 2, 3);
71      final Object result = resultExtractor.extractObjectFromList(list, Integer[].class);
72      assertThat(result).isInstanceOf(Integer[].class);
73      final Integer[] resultArray = (Integer[]) result;
74      assertThat(resultArray).isEqualTo(new Integer[] { 1, 2, 3 });
75    }
76  
77    @Test
78    void shouldExtractSet() {
79      final List<Object> list = Arrays.asList(1, 2, 3);
80      final Class<Set> targetType = Set.class;
81      final Set set = new HashSet();
82      final MetaObject metaObject = mock(MetaObject.class);
83      when(objectFactory.isCollection(targetType)).thenReturn(true);
84      when(objectFactory.create(targetType)).thenReturn(set);
85      when(configuration.newMetaObject(set)).thenReturn(metaObject);
86  
87      final Set result = (Set) resultExtractor.extractObjectFromList(list, targetType);
88      assertThat(result).isSameAs(set);
89  
90      verify(metaObject).addAll(list);
91    }
92  
93    @Test
94    void shouldExtractSingleObject() {
95      final List<Object> list = List.of("single object");
96      assertThat((String) resultExtractor.extractObjectFromList(list, String.class)).isEqualTo("single object");
97      assertThat((String) resultExtractor.extractObjectFromList(list, null)).isEqualTo("single object");
98      assertThat((String) resultExtractor.extractObjectFromList(list, Integer.class)).isEqualTo("single object");
99    }
100 
101   @Test
102   void shouldFailWhenMutipleItemsInList() {
103     final List<Object> list = Arrays.asList("first object", "second object");
104     Assertions.assertThrows(ExecutorException.class, () -> resultExtractor.extractObjectFromList(list, String.class));
105   }
106 }