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.type;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.mockito.ArgumentMatchers.any;
22  import static org.mockito.ArgumentMatchers.anyString;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.when;
26  
27  import java.sql.Array;
28  import java.sql.Connection;
29  import java.sql.Types;
30  
31  import org.junit.jupiter.api.Test;
32  import org.mockito.Mock;
33  
34  class ArrayTypeHandlerTest extends BaseTypeHandlerTest {
35  
36    private static final TypeHandler<Object> TYPE_HANDLER = new ArrayTypeHandler();
37  
38    @Mock
39    Array mockArray;
40  
41    @Override
42    @Test
43    public void shouldSetParameter() throws Exception {
44      TYPE_HANDLER.setParameter(ps, 1, mockArray, null);
45      verify(ps).setArray(1, mockArray);
46    }
47  
48    @Test
49    public void shouldSetStringArrayParameter() throws Exception {
50      Connection connection = mock(Connection.class);
51      when(ps.getConnection()).thenReturn(connection);
52  
53      Array array = mock(Array.class);
54      when(connection.createArrayOf(anyString(), any(String[].class))).thenReturn(array);
55  
56      TYPE_HANDLER.setParameter(ps, 1, new String[] { "Hello World" }, JdbcType.ARRAY);
57      verify(ps).setArray(1, array);
58      verify(array).free();
59    }
60  
61    @Test
62    public void shouldSetNullParameter() throws Exception {
63      TYPE_HANDLER.setParameter(ps, 1, null, JdbcType.ARRAY);
64      verify(ps).setNull(1, Types.ARRAY);
65    }
66  
67    @Test
68    public void shouldFailForNonArrayParameter() {
69      assertThrows(TypeException.class, () -> {
70        TYPE_HANDLER.setParameter(ps, 1, "unsupported parameter type", null);
71      });
72    }
73  
74    @Override
75    @Test
76    public void shouldGetResultFromResultSetByName() throws Exception {
77      when(rs.getArray("column")).thenReturn(mockArray);
78      String[] stringArray = { "a", "b" };
79      when(mockArray.getArray()).thenReturn(stringArray);
80      assertEquals(stringArray, TYPE_HANDLER.getResult(rs, "column"));
81      verify(mockArray).free();
82    }
83  
84    @Override
85    @Test
86    public void shouldGetResultNullFromResultSetByName() throws Exception {
87      when(rs.getArray("column")).thenReturn(null);
88      assertNull(TYPE_HANDLER.getResult(rs, "column"));
89    }
90  
91    @Override
92    @Test
93    public void shouldGetResultFromResultSetByPosition() throws Exception {
94      when(rs.getArray(1)).thenReturn(mockArray);
95      String[] stringArray = { "a", "b" };
96      when(mockArray.getArray()).thenReturn(stringArray);
97      assertEquals(stringArray, TYPE_HANDLER.getResult(rs, 1));
98      verify(mockArray).free();
99    }
100 
101   @Override
102   @Test
103   public void shouldGetResultNullFromResultSetByPosition() throws Exception {
104     when(rs.getArray(1)).thenReturn(null);
105     assertNull(TYPE_HANDLER.getResult(rs, 1));
106   }
107 
108   @Override
109   @Test
110   public void shouldGetResultFromCallableStatement() throws Exception {
111     when(cs.getArray(1)).thenReturn(mockArray);
112     String[] stringArray = { "a", "b" };
113     when(mockArray.getArray()).thenReturn(stringArray);
114     assertEquals(stringArray, TYPE_HANDLER.getResult(cs, 1));
115     verify(mockArray).free();
116   }
117 
118   @Override
119   @Test
120   public void shouldGetResultNullFromCallableStatement() throws Exception {
121     when(cs.getArray(1)).thenReturn(null);
122     assertNull(TYPE_HANDLER.getResult(cs, 1));
123   }
124 
125 }