]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | ||
18 | package org.apache.arrow.adapter.jdbc; | |
19 | ||
20 | import static org.junit.Assert.assertEquals; | |
21 | import static org.junit.Assert.assertFalse; | |
22 | import static org.junit.Assert.assertNotNull; | |
23 | import static org.junit.Assert.assertNull; | |
24 | import static org.junit.Assert.assertTrue; | |
25 | ||
26 | import java.sql.Types; | |
27 | import java.util.Calendar; | |
28 | import java.util.HashMap; | |
29 | import java.util.Locale; | |
30 | import java.util.TimeZone; | |
31 | ||
32 | import org.apache.arrow.memory.BufferAllocator; | |
33 | import org.apache.arrow.memory.RootAllocator; | |
34 | import org.junit.Test; | |
35 | ||
36 | public class JdbcToArrowConfigTest { | |
37 | ||
38 | private static final BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); | |
39 | private static final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT); | |
40 | ||
41 | @Test(expected = NullPointerException.class) | |
42 | public void testConfigNullArguments() { | |
43 | new JdbcToArrowConfig(null, null); | |
44 | } | |
45 | ||
46 | @Test(expected = NullPointerException.class) | |
47 | public void testBuilderNullArguments() { | |
48 | new JdbcToArrowConfigBuilder(null, null); | |
49 | } | |
50 | ||
51 | @Test | |
52 | public void testConfigNullCalendar() { | |
53 | JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, null); | |
54 | assertNull(config.getCalendar()); | |
55 | } | |
56 | ||
57 | @Test | |
58 | public void testBuilderNullCalendar() { | |
59 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, null); | |
60 | JdbcToArrowConfig config = builder.build(); | |
61 | assertNull(config.getCalendar()); | |
62 | } | |
63 | ||
64 | @Test(expected = NullPointerException.class) | |
65 | public void testConfigNullAllocator() { | |
66 | new JdbcToArrowConfig(null, calendar); | |
67 | } | |
68 | ||
69 | @Test(expected = NullPointerException.class) | |
70 | public void testBuilderNullAllocator() { | |
71 | new JdbcToArrowConfigBuilder(null, calendar); | |
72 | } | |
73 | ||
74 | @Test(expected = NullPointerException.class) | |
75 | public void testSetNullAllocator() { | |
76 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar); | |
77 | builder.setAllocator(null); | |
78 | } | |
79 | ||
80 | @Test | |
81 | public void testSetNullCalendar() { | |
82 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar); | |
83 | JdbcToArrowConfig config = builder.setCalendar(null).build(); | |
84 | assertNull(config.getCalendar()); | |
85 | } | |
86 | ||
87 | @Test | |
88 | public void testConfig() { | |
89 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar); | |
90 | JdbcToArrowConfig config = builder.build(); | |
91 | ||
92 | assertTrue(allocator == config.getAllocator()); | |
93 | assertTrue(calendar == config.getCalendar()); | |
94 | ||
95 | Calendar newCalendar = Calendar.getInstance(); | |
96 | BufferAllocator newAllocator = new RootAllocator(Integer.SIZE); | |
97 | ||
98 | builder.setAllocator(newAllocator).setCalendar(newCalendar); | |
99 | config = builder.build(); | |
100 | ||
101 | assertTrue(newAllocator == config.getAllocator()); | |
102 | assertTrue(newCalendar == config.getCalendar()); | |
103 | } | |
104 | ||
105 | @Test | |
106 | public void testIncludeMetadata() { | |
107 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar, false); | |
108 | ||
109 | JdbcToArrowConfig config = builder.build(); | |
110 | assertFalse(config.shouldIncludeMetadata()); | |
111 | ||
112 | builder.setIncludeMetadata(true); | |
113 | config = builder.build(); | |
114 | assertTrue(config.shouldIncludeMetadata()); | |
115 | ||
116 | config = new JdbcToArrowConfigBuilder(allocator, calendar, true).build(); | |
117 | assertTrue(config.shouldIncludeMetadata()); | |
118 | ||
119 | config = new JdbcToArrowConfig(allocator, calendar, /* include metadata */ true, | |
120 | /* reuse vector schema root */ true, null, null, JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, null); | |
121 | assertTrue(config.shouldIncludeMetadata()); | |
122 | assertTrue(config.isReuseVectorSchemaRoot()); | |
123 | ||
124 | config = new JdbcToArrowConfig(allocator, calendar, /* include metadata */ false, | |
125 | /* reuse vector schema root */ false, null, null, JdbcToArrowConfig.NO_LIMIT_BATCH_SIZE, null); | |
126 | assertFalse(config.shouldIncludeMetadata()); | |
127 | assertFalse(config.isReuseVectorSchemaRoot()); | |
128 | } | |
129 | ||
130 | @Test | |
131 | public void testArraySubTypes() { | |
132 | JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar, false); | |
133 | JdbcToArrowConfig config = builder.build(); | |
134 | ||
135 | final int columnIndex = 1; | |
136 | final String columnName = "COLUMN"; | |
137 | ||
138 | assertNull(config.getArraySubTypeByColumnIndex(columnIndex)); | |
139 | assertNull(config.getArraySubTypeByColumnName(columnName)); | |
140 | ||
141 | final HashMap<Integer, JdbcFieldInfo> indexMapping = new HashMap<Integer, JdbcFieldInfo>(); | |
142 | indexMapping.put(2, new JdbcFieldInfo(Types.BIGINT)); | |
143 | ||
144 | final HashMap<String, JdbcFieldInfo> fieldMapping = new HashMap<String, JdbcFieldInfo>(); | |
145 | fieldMapping.put("NEW_COLUMN", new JdbcFieldInfo(Types.BINARY)); | |
146 | ||
147 | builder.setArraySubTypeByColumnIndexMap(indexMapping); | |
148 | builder.setArraySubTypeByColumnNameMap(fieldMapping); | |
149 | config = builder.build(); | |
150 | ||
151 | assertNull(config.getArraySubTypeByColumnIndex(columnIndex)); | |
152 | assertNull(config.getArraySubTypeByColumnName(columnName)); | |
153 | ||
154 | indexMapping.put(columnIndex, new JdbcFieldInfo(Types.BIT)); | |
155 | fieldMapping.put(columnName, new JdbcFieldInfo(Types.BLOB)); | |
156 | ||
157 | assertNotNull(config.getArraySubTypeByColumnIndex(columnIndex)); | |
158 | assertEquals(Types.BIT, config.getArraySubTypeByColumnIndex(columnIndex).getJdbcType()); | |
159 | assertEquals(Types.BLOB, config.getArraySubTypeByColumnName(columnName).getJdbcType()); | |
160 | } | |
161 | } |