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