|
15 | 15 | */
|
16 | 16 | package io.vertx.db2client;
|
17 | 17 |
|
18 |
| -import static org.junit.Assume.assumeTrue; |
| 18 | +import io.vertx.core.buffer.Buffer; |
| 19 | +import io.vertx.ext.unit.TestContext; |
| 20 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 21 | +import io.vertx.sqlclient.Row; |
| 22 | +import io.vertx.sqlclient.RowSet; |
| 23 | +import io.vertx.sqlclient.Tuple; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
19 | 26 |
|
20 | 27 | import java.sql.RowId;
|
21 | 28 | import java.time.LocalDateTime;
|
22 | 29 | import java.time.temporal.ChronoField;
|
23 | 30 | import java.util.Arrays;
|
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
24 | 33 | import java.util.UUID;
|
25 | 34 |
|
26 |
| -import org.junit.Test; |
27 |
| -import org.junit.runner.RunWith; |
28 |
| - |
29 |
| -import io.vertx.core.buffer.Buffer; |
30 |
| -import io.vertx.ext.unit.TestContext; |
31 |
| -import io.vertx.ext.unit.junit.VertxUnitRunner; |
32 |
| -import io.vertx.sqlclient.Row; |
33 |
| -import io.vertx.sqlclient.RowSet; |
34 |
| -import io.vertx.sqlclient.Tuple; |
| 35 | +import static org.junit.Assume.assumeTrue; |
35 | 36 |
|
36 | 37 | @RunWith(VertxUnitRunner.class)
|
37 | 38 | public class DB2DataTypeTest extends DB2TestBase {
|
@@ -93,49 +94,75 @@ public void testByteIntoSmallIntColumn(TestContext ctx) {
|
93 | 94 | }));
|
94 | 95 | }
|
95 | 96 |
|
| 97 | + @Test |
| 98 | + public void testByteArrayIntoChar(TestContext ctx) { |
| 99 | + byte[] param = "hello world".getBytes(); |
| 100 | + byte[] expected = new byte[255]; |
| 101 | + Arrays.fill(expected, (byte) 32); |
| 102 | + System.arraycopy(param, 0, expected, 0, param.length); |
| 103 | + testByteArrayInto(ctx, "test_bytes", param, expected); |
| 104 | + } |
| 105 | + |
96 | 106 | @Test
|
97 | 107 | public void testByteArrayIntoVarchar(TestContext ctx) {
|
98 |
| - byte[] expected = "hello world".getBytes(); |
| 108 | + byte[] param = "hello world".getBytes(); |
| 109 | + testByteArrayInto(ctx, "test_vbytes", param, param); |
| 110 | + } |
| 111 | + |
| 112 | + private void testByteArrayInto(TestContext ctx, String colName, byte[] param, byte[] expected) { |
99 | 113 | connect(ctx.asyncAssertSuccess(conn -> {
|
100 | 114 | conn
|
101 |
| - .preparedQuery("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)") |
102 |
| - .execute(Tuple.of(3, "hello world".getBytes())) |
| 115 | + .preparedQuery("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)") |
| 116 | + .execute(Tuple.of(3, param)) |
103 | 117 | .onComplete(ctx.asyncAssertSuccess(insertResult -> {
|
104 |
| - conn |
105 |
| - .preparedQuery("SELECT id,test_bytes FROM db2_types WHERE id = 3") |
106 |
| - .execute() |
107 |
| - .onComplete(ctx.asyncAssertSuccess(rows -> { |
108 |
| - ctx.assertEquals(1, rows.size()); |
109 |
| - Row row = rows.iterator().next(); |
110 |
| - ctx.assertEquals(3, row.getInteger(0)); |
111 |
| - ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()), |
112 |
| - "Expecting " + Arrays.toString(expected) + " but got " |
113 |
| - + Arrays.toString(row.getBuffer(1).getBytes())); |
114 |
| - })); |
115 |
| - })); |
| 118 | + conn |
| 119 | + .preparedQuery("SELECT id," + colName + " FROM db2_types WHERE id = 3") |
| 120 | + .execute() |
| 121 | + .onComplete(ctx.asyncAssertSuccess(rows -> { |
| 122 | + ctx.assertEquals(1, rows.size()); |
| 123 | + Row row = rows.iterator().next(); |
| 124 | + ctx.assertEquals(3, row.getInteger(0)); |
| 125 | + ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()), |
| 126 | + "Expecting " + Arrays.toString(expected) + " but got " |
| 127 | + + Arrays.toString(row.getBuffer(1).getBytes())); |
| 128 | + })); |
| 129 | + })); |
116 | 130 | }));
|
117 | 131 | }
|
118 | 132 |
|
119 | 133 | @Test
|
120 |
| - public void testByteBufIntoVarchar(TestContext ctx) { |
121 |
| - byte[] expected = "hello world".getBytes(); |
| 134 | + public void testBufferIntoChar(TestContext ctx) { |
| 135 | + byte[] param = "hello world".getBytes(); |
| 136 | + byte[] expected = new byte[255]; |
| 137 | + Arrays.fill(expected, (byte) 32); |
| 138 | + System.arraycopy(param, 0, expected, 0, param.length); |
| 139 | + testBufferInto(ctx, "test_bytes", param, expected); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testBufferIntoVarchar(TestContext ctx) { |
| 144 | + byte[] param = "hello world".getBytes(); |
| 145 | + testBufferInto(ctx, "test_vbytes", param, param); |
| 146 | + } |
| 147 | + |
| 148 | + private void testBufferInto(TestContext ctx, String colName, byte[] param, byte[] expected) { |
122 | 149 | connect(ctx.asyncAssertSuccess(conn -> {
|
123 | 150 | conn
|
124 |
| - .preparedQuery("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)") |
125 |
| - .execute(Tuple.of(4, Buffer.buffer(expected))) |
| 151 | + .preparedQuery("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)") |
| 152 | + .execute(Tuple.of(4, Buffer.buffer(param))) |
126 | 153 | .onComplete(ctx.asyncAssertSuccess(insertResult -> {
|
127 |
| - conn |
128 |
| - .preparedQuery("SELECT id,test_bytes FROM db2_types WHERE id = 4") |
129 |
| - .execute() |
130 |
| - .onComplete(ctx.asyncAssertSuccess(rows -> { |
131 |
| - ctx.assertEquals(1, rows.size()); |
132 |
| - Row row = rows.iterator().next(); |
133 |
| - ctx.assertEquals(4, row.getInteger(0)); |
134 |
| - ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()), |
135 |
| - "Expecting " + Arrays.toString(expected) + " but got " |
136 |
| - + Arrays.toString(row.getBuffer(1).getBytes())); |
137 |
| - })); |
138 |
| - })); |
| 154 | + conn |
| 155 | + .preparedQuery("SELECT id," + colName + " FROM db2_types WHERE id = 4") |
| 156 | + .execute() |
| 157 | + .onComplete(ctx.asyncAssertSuccess(rows -> { |
| 158 | + ctx.assertEquals(1, rows.size()); |
| 159 | + Row row = rows.iterator().next(); |
| 160 | + ctx.assertEquals(4, row.getInteger(0)); |
| 161 | + ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()), |
| 162 | + "Expecting " + Arrays.toString(expected) + " but got " |
| 163 | + + Arrays.toString(row.getBuffer(1).getBytes())); |
| 164 | + })); |
| 165 | + })); |
139 | 166 | }));
|
140 | 167 | }
|
141 | 168 |
|
@@ -269,4 +296,9 @@ private RowId verifyRowId(TestContext ctx, RowSet<Row> rows, String msg) {
|
269 | 296 | ctx.assertEquals(22, rowid.getBytes().length);
|
270 | 297 | return rowid;
|
271 | 298 | }
|
| 299 | + |
| 300 | + @Override |
| 301 | + protected List<String> tablesToClean() { |
| 302 | + return Collections.singletonList("db2_types"); |
| 303 | + } |
272 | 304 | }
|
0 commit comments