diff --git a/mockdriver/pom.xml b/mockdriver/pom.xml index 2e9f37b..e261790 100644 --- a/mockdriver/pom.xml +++ b/mockdriver/pom.xml @@ -8,8 +8,8 @@ jar - 1.8 - 1.8 + 1.7 + 1.7 diff --git a/mockdriver/src/main/java/org/jaydebeapi/mockdriver/MockConnection.java b/mockdriver/src/main/java/org/jaydebeapi/mockdriver/MockConnection.java index 0bb4cb5..6c959fd 100644 --- a/mockdriver/src/main/java/org/jaydebeapi/mockdriver/MockConnection.java +++ b/mockdriver/src/main/java/org/jaydebeapi/mockdriver/MockConnection.java @@ -1,4 +1,3 @@ - package org.jaydebeapi.mockdriver; import java.lang.reflect.Field; @@ -9,93 +8,92 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; -import java.time.LocalDate; - +import java.util.Calendar; import org.mockito.Mockito; public abstract class MockConnection implements Connection { - ResultSet mockResultSet; + ResultSet mockResultSet; - - public final void mockExceptionOnCommit(String className, String exceptionMessage) throws SQLException { - Throwable exception = createException(className, exceptionMessage); - Mockito.doThrow(exception).when(this).commit(); + private static Throwable createException(String className, String exceptionMessage) { + try { + return (Throwable) Class.forName(className).getConstructor(String.class) + .newInstance(exceptionMessage); + } catch (Exception e) { + throw new RuntimeException("Couldn't initialize class " + className + ".", e); } + } - - public final void mockExceptionOnRollback(String className, String exceptionMessage) throws SQLException { - Throwable exception = createException(className, exceptionMessage); - Mockito.doThrow(exception).when(this).rollback(); + private static int extractTypeCodeForName(String sqlTypesName) { + try { + Field field = Types.class.getField(sqlTypesName); + return field.getInt(null); + } catch (NoSuchFieldException e) { + throw new IllegalArgumentException("Type " + sqlTypesName + " not found in Types class.", e); + } catch (SecurityException e) { + throw new RuntimeException(e); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); } + } + public final void mockExceptionOnCommit(String className, String exceptionMessage) + throws SQLException { + Throwable exception = createException(className, exceptionMessage); + Mockito.doThrow(exception).when(this).commit(); + } - public final void mockExceptionOnExecute(String className, String exceptionMessage) throws SQLException { - PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); - Throwable exception = createException(className, exceptionMessage); - Mockito.when(mockPreparedStatement.execute()).thenThrow(exception); - Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); - } + public final void mockExceptionOnRollback(String className, String exceptionMessage) + throws SQLException { + Throwable exception = createException(className, exceptionMessage); + Mockito.doThrow(exception).when(this).rollback(); + } + public final void mockExceptionOnExecute(String className, String exceptionMessage) + throws SQLException { + PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); + Throwable exception = createException(className, exceptionMessage); + Mockito.when(mockPreparedStatement.execute()).thenThrow(exception); + Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); + } - public final void mockDateResult(int year, int month, int day) throws SQLException { - PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); - Mockito.when(mockPreparedStatement.execute()).thenReturn(true); - mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for date)"); - Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet); - Mockito.when(mockResultSet.next()).thenReturn(true); - ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class); - Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData); - Mockito.when(mockMetaData.getColumnCount()).thenReturn(1); - Date ancientDate = Date.valueOf(LocalDate.of(year, month, day)); - Mockito.when(mockResultSet.getDate(1)).thenReturn(ancientDate); - Mockito.when(mockMetaData.getColumnType(1)).thenReturn(Types.DATE); - Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); - } + public final void mockDateResult(int year, int month, int day) throws SQLException { + PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); + Mockito.when(mockPreparedStatement.execute()).thenReturn(true); + mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for date)"); + Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet); + Mockito.when(mockResultSet.next()).thenReturn(true); + ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class); + Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData); + Mockito.when(mockMetaData.getColumnCount()).thenReturn(1); + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month - 1); + cal.set(Calendar.DAY_OF_MONTH, day); + Date ancientDate = new Date(cal.getTime().getTime()); + Mockito.when(mockResultSet.getDate(1)).thenReturn(ancientDate); + Mockito.when(mockMetaData.getColumnType(1)).thenReturn(Types.DATE); + Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); + } + public final void mockType(String sqlTypesName) throws SQLException { + PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); + Mockito.when(mockPreparedStatement.execute()).thenReturn(true); + mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for type " + sqlTypesName + ")"); + Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet); + Mockito.when(mockResultSet.next()).thenReturn(true); + ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class); + Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData); + Mockito.when(mockMetaData.getColumnCount()).thenReturn(1); + int sqlTypeCode = extractTypeCodeForName(sqlTypesName); + Mockito.when(mockMetaData.getColumnType(1)).thenReturn(sqlTypeCode); + Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); + } - public final void mockType(String sqlTypesName) throws SQLException { - PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class); - Mockito.when(mockPreparedStatement.execute()).thenReturn(true); - mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for type " + sqlTypesName + ")"); - Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet); - Mockito.when(mockResultSet.next()).thenReturn(true); - ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class); - Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData); - Mockito.when(mockMetaData.getColumnCount()).thenReturn(1); - int sqlTypeCode = extractTypeCodeForName(sqlTypesName); - Mockito.when(mockMetaData.getColumnType(1)).thenReturn(sqlTypeCode); - Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement); - } - - - public final ResultSet verifyResultSet() { - return Mockito.verify(mockResultSet); - } - - - private static Throwable createException(String className, String exceptionMessage) { - try { - return (Throwable) Class.forName(className).getConstructor(String.class).newInstance(exceptionMessage); - } catch (Exception e) { - throw new RuntimeException("Couldn't initialize class " + className + ".", e); - } - } - - - private static int extractTypeCodeForName(String sqlTypesName) { - try { - Field field = Types.class.getField(sqlTypesName); - return field.getInt(null); - } catch (NoSuchFieldException e) { - throw new IllegalArgumentException("Type " + sqlTypesName + " not found in Types class.", e); - } catch (SecurityException e) { - throw new RuntimeException(e); - } catch (IllegalArgumentException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - + public final ResultSet verifyResultSet() { + return Mockito.verify(mockResultSet); + } }