SqlToPdxInstance { public void addMapping(String columnName, String pdxFieldName, FieldType pdxFieldType) { columnToPdxFieldMap.put(columnName, new PdxFieldInfo(pdxFieldName, pdxFieldType)); }  void setPdxTemplate(PdxInstance template); void addMapping(String columnName, String pdxFieldName, FieldType pdxFieldType); PdxInstance create(ResultSet resultSet);  }
@Test public void readReturnsDataFromAllResultColumns() throws Exception { when(metaData.getColumnCount()).thenReturn(2); when(metaData.getColumnName(2)).thenReturn(COLUMN_NAME_2); when(resultSet.getString(1)).thenReturn("column1"); when(resultSet.getString(2)).thenReturn("column2"); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, FieldType.STRING); sqlToPdxInstance.addMapping(COLUMN_NAME_2, PDX_FIELD_NAME_2, FieldType.STRING); PdxInstance result = createPdxInstance(); assertThat(result).isSameAs(writablePdxInstance); verify((WritablePdxInstance) result).setField(PDX_FIELD_NAME_1, "column1"); verify((WritablePdxInstance) result).setField(PDX_FIELD_NAME_2, "column2"); verifyNoMoreInteractions(result); }
@Test public void skipsUnmappedColumns() throws Exception { when(metaData.getColumnCount()).thenReturn(2); when(metaData.getColumnName(2)).thenReturn(COLUMN_NAME_2); when(resultSet.getString(1)).thenReturn("column1"); when(resultSet.getString(2)).thenReturn("column2"); sqlToPdxInstance.addMapping(COLUMN_NAME_2, PDX_FIELD_NAME_2, FieldType.STRING); assertThatThrownBy(() -> createPdxInstance()).isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("The jdbc-mapping does not contain the column name \"" + COLUMN_NAME_1 + "\". This is probably caused by a column being added to the table after the jdbc-mapping was created."); }
@Test public void fieldsAreNotWrittenIfNoColumns() throws Exception { FieldType fieldType = FieldType.CHAR; when(metaData.getColumnCount()).thenReturn(0); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); assertThat(result).isSameAs(writablePdxInstance); verifyNoMoreInteractions(result); }
@Test public void readOfCharFieldWithEmptyStringWritesCharZero() throws Exception { char expectedValue = 0; FieldType fieldType = FieldType.CHAR; when(metaData.getColumnType(1)).thenReturn(Types.CHAR); when(resultSet.getString(1)).thenReturn(""); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfDateFieldWithDateColumnWritesDate() throws Exception { FieldType fieldType = FieldType.DATE; when(metaData.getColumnType(1)).thenReturn(Types.DATE); java.sql.Date sqlDate = java.sql.Date.valueOf("1979-09-11"); Date expectedValue = new Date(sqlDate.getTime()); when(resultSet.getDate(1)).thenReturn(sqlDate); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfByteArrayFieldWithBlob() throws Exception { FieldType fieldType = FieldType.BYTE_ARRAY; when(metaData.getColumnType(1)).thenReturn(Types.BLOB); byte[] expectedValue = new byte[] {1, 2, 3}; Blob blob = mock(Blob.class); when(blob.length()).thenReturn((long) expectedValue.length); when(blob.getBytes(1, expectedValue.length)).thenReturn(expectedValue); when(resultSet.getBlob(1)).thenReturn(blob); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfByteArrayFieldWithNullBlob() throws Exception { FieldType fieldType = FieldType.BYTE_ARRAY; when(metaData.getColumnType(1)).thenReturn(Types.BLOB); when(resultSet.getBlob(1)).thenReturn(null); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(null, result); verify(resultSet).getBlob(1); }
@Test public void readOfByteArrayFieldWithHugeBlobThrows() throws Exception { FieldType fieldType = FieldType.BYTE_ARRAY; when(metaData.getColumnType(1)).thenReturn(Types.BLOB); Blob blob = mock(Blob.class); when(blob.length()).thenReturn((long) Integer.MAX_VALUE + 1); when(resultSet.getBlob(1)).thenReturn(blob); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); assertThatThrownBy(() -> createPdxInstance()).isInstanceOf(JdbcConnectorException.class) .hasMessageContaining( "Blob of length 2147483648 is too big to be converted to a byte array."); }
@Test public void readOfObjectFieldWithBlob() throws Exception { FieldType fieldType = FieldType.OBJECT; when(metaData.getColumnType(1)).thenReturn(Types.BLOB); byte[] expectedValue = new byte[] {1, 2, 3}; Blob blob = mock(Blob.class); when(blob.length()).thenReturn((long) expectedValue.length); when(blob.getBytes(1, expectedValue.length)).thenReturn(expectedValue); when(resultSet.getBlob(1)).thenReturn(blob); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfDateFieldWithTimeColumnWritesDate() throws Exception { FieldType fieldType = FieldType.DATE; when(metaData.getColumnType(1)).thenReturn(Types.TIME); java.sql.Time sqlTime = java.sql.Time.valueOf("22:33:44"); Date expectedValue = new Date(sqlTime.getTime()); when(resultSet.getTime(1)).thenReturn(sqlTime); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfDateFieldWithTimestampColumnWritesDate() throws Exception { FieldType fieldType = FieldType.DATE; when(metaData.getColumnType(1)).thenReturn(Types.TIMESTAMP); java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("1979-09-11 22:33:44.567"); Date expectedValue = new Date(sqlTimestamp.getTime()); when(resultSet.getTimestamp(1)).thenReturn(sqlTimestamp); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfObjectFieldWithDateColumnWritesDate() throws Exception { FieldType fieldType = FieldType.OBJECT; java.sql.Date sqlDate = java.sql.Date.valueOf("1979-09-11"); Date expectedValue = new Date(sqlDate.getTime()); when(resultSet.getObject(1)).thenReturn(sqlDate); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfObjectFieldWithJavaUtilDateWritesDate() throws Exception { FieldType fieldType = FieldType.OBJECT; sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); Date expectedValue = new Date(); when(resultSet.getObject(1)).thenReturn(expectedValue); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfObjectFieldWithTimeColumnWritesDate() throws Exception { FieldType fieldType = FieldType.OBJECT; sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); java.sql.Time sqlTime = java.sql.Time.valueOf("22:33:44"); Date expectedValue = new Date(sqlTime.getTime()); when(resultSet.getObject(1)).thenReturn(sqlTime); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test public void readOfObjectFieldWithTimestampColumnWritesDate() throws Exception { FieldType fieldType = FieldType.OBJECT; sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("1979-09-11 22:33:44.567"); Date expectedValue = new Date(sqlTimestamp.getTime()); when(resultSet.getObject(1)).thenReturn(sqlTimestamp); PdxInstance result = createPdxInstance(); verifyResult(expectedValue, result); }
@Test @Parameters({"BOOLEAN_ARRAY", "OBJECT_ARRAY", "CHAR_ARRAY", "SHORT_ARRAY", "INT_ARRAY", "LONG_ARRAY", "FLOAT_ARRAY", "DOUBLE_ARRAY", "STRING_ARRAY", "ARRAY_OF_BYTE_ARRAYS"}) public void throwsExceptionWhenReadWritesUnsupportedType(FieldType fieldType) throws Exception { sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, fieldType); String returnValue = "ReturnValue"; when(resultSet.getObject(1)).thenReturn(returnValue); assertThatThrownBy(() -> createPdxInstance()).isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("Could not convert "); }
@Test public void throwsExceptionIfMoreThanOneResultReturned() throws Exception { when(metaData.getColumnCount()).thenReturn(2); when(metaData.getColumnName(2)).thenReturn(COLUMN_NAME_2); sqlToPdxInstance.addMapping(COLUMN_NAME_1, PDX_FIELD_NAME_1, FieldType.STRING); sqlToPdxInstance.addMapping(COLUMN_NAME_2, PDX_FIELD_NAME_2, FieldType.STRING); when(resultSet.next()).thenReturn(true); assertThatThrownBy(() -> createPdxInstance()).isInstanceOf(JdbcConnectorException.class) .hasMessageContaining("Multiple rows returned for query: "); }