PostgresConnection implements IStorageConnection { public ICompiledQuery compileQuery(final IPreparedQuery preparedQuery) throws StorageException { try { return new JDBCCompiledQuery(((QueryStatement) preparedQuery).compile(connection)); } catch (SQLException e) { throw new StorageException( String.format("Error compiling query statement \"%s\": ", ((QueryStatement) preparedQuery).getBodyWriter().toString()), e); } } PostgresConnection(final ConnectionOptions options); ICompiledQuery compileQuery(final IPreparedQuery preparedQuery); boolean validate(); void close(); void commit(); void rollback();  }
@Test public void MustCorrectCompileQuery() throws Exception { QueryStatement statement = mock(QueryStatement.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); JDBCCompiledQuery query = mock(JDBCCompiledQuery.class); when(statement.compile(connection)).thenReturn(preparedStatement); whenNew(JDBCCompiledQuery.class).withArguments(preparedStatement).thenReturn(query); assertTrue(testPostgresConnection.compileQuery(statement) == query); verify(statement).compile(connection); verifyNew(JDBCCompiledQuery.class).withArguments(preparedStatement); }
@Test public void MustInCorrectCompileQueryWhenCompileThrowException() throws Exception { QueryStatement statement = mock(QueryStatement.class); Writer bodyWriter = mock(Writer.class); when(bodyWriter.toString()).thenReturn("as"); when(statement.getBodyWriter()).thenReturn(bodyWriter); when(statement.compile(connection)).thenThrow(new SQLException()); try { testPostgresConnection.compileQuery(statement); } catch (StorageException e) { verify(statement).compile(connection); return; } assertTrue("Must throw exception", false); }