QueryImpl extends AbstractQuery implements Query { @Override public Stream getResultStream() { try { return executeQueryForStream(this::extractRowAsOptional); } catch (OntoDriverException e) { markTransactionForRollback(); throw queryEvaluationException(e); } catch (RuntimeException e) { if (exceptionCausesRollback(e)) { markTransactionForRollback(); } throw e; } } QueryImpl(final QueryHolder query, final ConnectionWrapper connection); @Override List getResultList(); @Override Object getSingleResult(); @Override Stream getResultStream(); @Override Query setMaxResults(int maxResults); @Override Query setFirstResult(int startPosition);  }
@SuppressWarnings("unchecked") @Test void getResultStreamRetrievesResultStreamFromUnderlyingResultSet() throws Exception { final Query sut = createQuery(SELECT_QUERY); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.getColumnCount()).thenReturn(2); when(resultSetMock.isBound(anyInt())).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, true, false); when(resultSetMock.getObject(anyInt())).thenReturn("str"); final Stream result = sut.getResultStream(); assertNotNull(result); final List asList = (List) result.collect(Collectors.toList()); assertEquals(2, asList.size()); final Object[] expected = new Object[]{"str", "str"}; asList.forEach(item -> assertArrayEquals(expected, (Object[]) item)); }
@SuppressWarnings("unchecked") @Test void getResultStreamClosesStatementWhenStreamIsProcessed() throws Exception { final Query sut = createQuery(SELECT_QUERY); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.getColumnCount()).thenReturn(2); when(resultSetMock.isBound(anyInt())).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, true, false); when(resultSetMock.getObject(anyInt())).thenReturn("str"); sut.getResultStream().forEach(Assertions::assertNotNull); verify(statementMock).close(); }
@SuppressWarnings("unchecked") @Test void getResultStreamClosesStatementWhenStreamProcessingThrowsException() throws Exception { final Query sut = createQuery(SELECT_QUERY); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.getColumnCount()).thenReturn(2); when(resultSetMock.isBound(anyInt())).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, true, false); when(resultSetMock.getObject(anyInt())).thenThrow(OntoDriverException.class); try { assertThrows(OWLPersistenceException.class, () -> sut.getResultStream().forEach(Assertions::assertNotNull)); } finally { verify(statementMock).close(); } }