34
34
import java .sql .ResultSetMetaData ;
35
35
import java .sql .SQLException ;
36
36
import java .sql .Timestamp ;
37
+ import java .util .ArrayList ;
38
+ import java .util .Arrays ;
39
+ import java .util .Collections ;
37
40
import java .util .List ;
38
41
import java .util .Optional ;
39
42
import java .util .stream .Collectors ;
42
45
import org .apache .avro .file .DataFileReader ;
43
46
import org .apache .avro .file .DataFileWriter ;
44
47
import org .apache .avro .file .SeekableByteArrayInput ;
48
+ import org .apache .avro .generic .GenericData ;
45
49
import org .apache .avro .generic .GenericDatumReader ;
46
50
import org .apache .avro .generic .GenericDatumWriter ;
47
51
import org .apache .avro .generic .GenericRecord ;
52
+ import org .apache .avro .util .Utf8 ;
48
53
import org .junit .Assert ;
49
54
import org .junit .BeforeClass ;
50
55
import org .junit .Test ;
@@ -62,7 +67,7 @@ public static void beforeAll() throws SQLException, ClassNotFoundException {
62
67
63
68
@ Test
64
69
public void shouldCreateSchema () throws ClassNotFoundException , SQLException {
65
- final int fieldCount = 12 ;
70
+ final int fieldCount = 14 ;
66
71
final Schema actual =
67
72
JdbcAvroSchema .createSchemaByReadingOneRow (
68
73
DbTestHelper .createConnection (CONNECTION_URL ),
@@ -92,7 +97,9 @@ public void shouldCreateSchema() throws ClassNotFoundException, SQLException {
92
97
"CREATED" ,
93
98
"UPDATED" ,
94
99
"UID" ,
95
- "ROWNUM" ),
100
+ "ROWNUM" ,
101
+ "INT_ARR" ,
102
+ "TEXT_ARR" ),
96
103
actual .getFields ().stream ().map (Schema .Field ::name ).collect (Collectors .toList ()));
97
104
for (Schema .Field f : actual .getFields ()) {
98
105
Assert .assertEquals (Schema .Type .UNION , f .schema ().getType ());
@@ -128,7 +135,7 @@ public void shouldCreateSchema() throws ClassNotFoundException, SQLException {
128
135
129
136
@ Test
130
137
public void shouldCreateSchemaWithLogicalTypes () throws ClassNotFoundException , SQLException {
131
- final int fieldCount = 12 ;
138
+ final int fieldCount = 14 ;
132
139
final Schema actual =
133
140
JdbcAvroSchema .createSchemaByReadingOneRow (
134
141
DbTestHelper .createConnection (CONNECTION_URL ),
@@ -163,8 +170,10 @@ public void shouldEncodeResultSetToValidAvro()
163
170
throws ClassNotFoundException , SQLException , IOException {
164
171
final ResultSet rs =
165
172
DbTestHelper .createConnection (CONNECTION_URL )
166
- .createStatement ()
173
+ .createStatement (ResultSet . TYPE_SCROLL_SENSITIVE , ResultSet . CONCUR_READ_ONLY )
167
174
.executeQuery ("SELECT * FROM COFFEES" );
175
+
176
+ rs .first ();
168
177
final Schema schema =
169
178
JdbcAvroSchema .createAvroSchema (
170
179
rs , "dbeam_generated" , "connection" , Optional .empty (), "doc" , false );
@@ -173,6 +182,7 @@ public void shouldEncodeResultSetToValidAvro()
173
182
new DataFileWriter <>(new GenericDatumWriter <>(schema ));
174
183
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
175
184
dataFileWriter .create (schema , outputStream );
185
+ rs .previous ();
176
186
// convert and write
177
187
while (rs .next ()) {
178
188
dataFileWriter .appendEncoded (converter .convertResultSetIntoAvroBytes ());
@@ -194,8 +204,11 @@ public void shouldEncodeResultSetToValidAvro()
194
204
.findFirst ()
195
205
.orElseThrow (() -> new IllegalArgumentException ("not found" ));
196
206
197
- Assert .assertEquals (12 , record .getSchema ().getFields ().size ());
207
+ Assert .assertEquals (14 , record .getSchema ().getFields ().size ());
198
208
Assert .assertEquals (schema , record .getSchema ());
209
+ List <String > actualTxtArray =
210
+ ((GenericData .Array <Utf8 >) record .get (13 )).stream ().map (x -> x .toString ()).collect (
211
+ Collectors .toList ());
199
212
final Coffee actual =
200
213
Coffee .create (
201
214
record .get (0 ).toString (),
@@ -209,7 +222,9 @@ public void shouldEncodeResultSetToValidAvro()
209
222
new java .sql .Timestamp ((Long ) record .get (8 )),
210
223
Optional .ofNullable ((Long ) record .get (9 )).map (Timestamp ::new ),
211
224
TestHelper .byteBufferToUuid ((ByteBuffer ) record .get (10 )),
212
- (Long ) record .get (11 ));
225
+ (Long ) record .get (11 ),
226
+ new ArrayList <>((GenericData .Array <Integer >) record .get (12 )),
227
+ actualTxtArray );
213
228
Assert .assertEquals (Coffee .COFFEE1 , actual );
214
229
}
215
230
0 commit comments