1515 */
1616package io .vertx .db2client ;
1717
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 ;
1926
2027import java .sql .RowId ;
2128import java .time .LocalDateTime ;
2229import java .time .temporal .ChronoField ;
2330import java .util .Arrays ;
31+ import java .util .Collections ;
32+ import java .util .List ;
2433import java .util .UUID ;
2534
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 ;
3536
3637@ RunWith (VertxUnitRunner .class )
3738public class DB2DataTypeTest extends DB2TestBase {
3839
3940
40- // Enum for enum testing
41+ // Enum for enum testing
4142 enum Days {
4243 MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY , SATURDAY , SUNDAY
4344 }
4445
45-
46+
4647 /**
4748 * In DB2 the FLOAT and DOUBLE column types both map to an 8-byte
4849 * double-precision column (i.e. Java double). Ensure that a Java
@@ -85,41 +86,75 @@ public void testByteIntoSmallIntColumn(TestContext ctx) {
8586 }));
8687 }
8788
89+ @ Test
90+ public void testByteArrayIntoChar (TestContext ctx ) {
91+ byte [] param = "hello world" .getBytes ();
92+ byte [] expected = new byte [255 ];
93+ Arrays .fill (expected , (byte ) 32 );
94+ System .arraycopy (param , 0 , expected , 0 , param .length );
95+ testByteArrayInto (ctx , "test_bytes" , param , expected );
96+ }
97+
8898 @ Test
8999 public void testByteArrayIntoVarchar (TestContext ctx ) {
90- byte [] expected = "hello world" .getBytes ();
100+ byte [] param = "hello world" .getBytes ();
101+ testByteArrayInto (ctx , "test_vbytes" , param , param );
102+ }
103+
104+ private void testByteArrayInto (TestContext ctx , String colName , byte [] param , byte [] expected ) {
91105 connect (ctx .asyncAssertSuccess (conn -> {
92- conn .preparedQuery ("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)" )
93- .execute (Tuple .of (3 , "hello world" .getBytes ()), ctx .asyncAssertSuccess (insertResult -> {
94- conn .preparedQuery ("SELECT id,test_bytes FROM db2_types WHERE id = 3" )
95- .execute (ctx .asyncAssertSuccess (rows -> {
96- ctx .assertEquals (1 , rows .size ());
97- Row row = rows .iterator ().next ();
98- ctx .assertEquals (3 , row .getInteger (0 ));
99- ctx .assertTrue (Arrays .equals (expected , row .getBuffer (1 ).getBytes ()),
100- "Expecting " + Arrays .toString (expected ) + " but got "
101- + Arrays .toString (row .getBuffer (1 ).getBytes ()));
102- }));
103- }));
106+ conn
107+ .preparedQuery ("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)" )
108+ .execute (Tuple .of (3 , param ))
109+ .onComplete (ctx .asyncAssertSuccess (insertResult -> {
110+ conn
111+ .preparedQuery ("SELECT id," + colName + " FROM db2_types WHERE id = 3" )
112+ .execute ()
113+ .onComplete (ctx .asyncAssertSuccess (rows -> {
114+ ctx .assertEquals (1 , rows .size ());
115+ Row row = rows .iterator ().next ();
116+ ctx .assertEquals (3 , row .getInteger (0 ));
117+ ctx .assertTrue (Arrays .equals (expected , row .getBuffer (1 ).getBytes ()),
118+ "Expecting " + Arrays .toString (expected ) + " but got "
119+ + Arrays .toString (row .getBuffer (1 ).getBytes ()));
120+ }));
121+ }));
104122 }));
105123 }
106124
107125 @ Test
108- public void testByteBufIntoVarchar (TestContext ctx ) {
109- byte [] expected = "hello world" .getBytes ();
126+ public void testBufferIntoChar (TestContext ctx ) {
127+ byte [] param = "hello world" .getBytes ();
128+ byte [] expected = new byte [255 ];
129+ Arrays .fill (expected , (byte ) 32 );
130+ System .arraycopy (param , 0 , expected , 0 , param .length );
131+ testBufferInto (ctx , "test_bytes" , param , expected );
132+ }
133+
134+ @ Test
135+ public void testBufferIntoVarchar (TestContext ctx ) {
136+ byte [] param = "hello world" .getBytes ();
137+ testBufferInto (ctx , "test_vbytes" , param , param );
138+ }
139+
140+ private void testBufferInto (TestContext ctx , String colName , byte [] param , byte [] expected ) {
110141 connect (ctx .asyncAssertSuccess (conn -> {
111- conn .preparedQuery ("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)" )
112- .execute (Tuple .of (4 , Buffer .buffer (expected )), ctx .asyncAssertSuccess (insertResult -> {
113- conn .preparedQuery ("SELECT id,test_bytes FROM db2_types WHERE id = 4" )
114- .execute (ctx .asyncAssertSuccess (rows -> {
115- ctx .assertEquals (1 , rows .size ());
116- Row row = rows .iterator ().next ();
117- ctx .assertEquals (4 , row .getInteger (0 ));
118- ctx .assertTrue (Arrays .equals (expected , row .getBuffer (1 ).getBytes ()),
119- "Expecting " + Arrays .toString (expected ) + " but got "
120- + Arrays .toString (row .getBuffer (1 ).getBytes ()));
121- }));
122- }));
142+ conn
143+ .preparedQuery ("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)" )
144+ .execute (Tuple .of (4 , Buffer .buffer (param )))
145+ .onComplete (ctx .asyncAssertSuccess (insertResult -> {
146+ conn
147+ .preparedQuery ("SELECT id," + colName + " FROM db2_types WHERE id = 4" )
148+ .execute ()
149+ .onComplete (ctx .asyncAssertSuccess (rows -> {
150+ ctx .assertEquals (1 , rows .size ());
151+ Row row = rows .iterator ().next ();
152+ ctx .assertEquals (4 , row .getInteger (0 ));
153+ ctx .assertTrue (Arrays .equals (expected , row .getBuffer (1 ).getBytes ()),
154+ "Expecting " + Arrays .toString (expected ) + " but got "
155+ + Arrays .toString (row .getBuffer (1 ).getBytes ()));
156+ }));
157+ }));
123158 }));
124159 }
125160
@@ -183,7 +218,7 @@ public void testRowId(TestContext ctx) {
183218 }));
184219 }));
185220 }
186-
221+
187222 /**
188223 * Test to support using enum string values in the Row and Tuple methods.
189224 */
@@ -221,7 +256,7 @@ public void testUsingEnumOrdinalValue(TestContext ctx) {
221256 }));
222257 }));
223258 }
224-
259+
225260 private RowId verifyRowId (TestContext ctx , RowSet <Row > rows , String msg ) {
226261 ctx .assertEquals (1 , rows .size ());
227262 Row row = rows .iterator ().next ();
@@ -231,4 +266,9 @@ private RowId verifyRowId(TestContext ctx, RowSet<Row> rows, String msg) {
231266 ctx .assertEquals (22 , rowid .getBytes ().length );
232267 return rowid ;
233268 }
269+
270+ @ Override
271+ protected List <String > tablesToClean () {
272+ return Collections .singletonList ("db2_types" );
273+ }
234274}
0 commit comments