TypedQueryImpl extends AbstractQuery implements TypedQuery<X> { @Override public Stream<X> getResultStream() { final boolean isEntityType = metamodelProvider.isEntityType(resultType); final Descriptor instDescriptor = descriptor != null ? descriptor : new EntityDescriptor(); try { return executeQueryForStream(row -> { if (isEntityType) { return loadEntityInstance(row, instDescriptor); } else { return loadResultValue(row); } }); } catch (OntoDriverException e) { markTransactionForRollback(); throw queryEvaluationException(e); } catch (RuntimeException e) { if (exceptionCausesRollback(e)) { markTransactionForRollback(); } throw e; } } TypedQueryImpl(final QueryHolder query, final Class<X> resultType,
                          final ConnectionWrapper connection, MetamodelProvider metamodelProvider); void setUnitOfWork(UnitOfWork uow); @Override List<X> getResultList(); @Override X getSingleResult(); @Override Stream<X> getResultStream(); @Override TypedQuery<X> setMaxResults(int maxResults); @Override TypedQuery<X> setFirstResult(int startPosition); @Override TypedQuery<X> setParameter(int position, Object value); @Override TypedQuery<X> setParameter(int position, String value, String language); @Override TypedQuery<X> setParameter(String name, Object value); @Override TypedQuery<X> setParameter(String name, String value, String language); @Override TypedQuery<X> setParameter(Parameter<T> parameter, T value); @Override TypedQuery<X> setParameter(Parameter<String> parameter, String value, String language); @Override TypedQuery<X> setUntypedParameter(int position, Object value); @Override TypedQuery<X> setUntypedParameter(String name, Object value); @Override TypedQuery<X> setUntypedParameter(Parameter<T> parameter, T value); @Override TypedQuery<X> setDescriptor(Descriptor descriptor);  }
@Test void getResultStreamRetrievesResultStreamFromUnderlyingResultSet() throws Exception { final List<String> uris = Arrays.asList(Generators.createIndividualIdentifier().toString(), Generators.createIndividualIdentifier().toString()); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, true, false); when(resultSetMock.isBound(0)).thenReturn(true); when(resultSetMock.getString(0)).thenReturn(uris.get(0), uris.get(1)); when(uowMock.readObject(eq(OWLClassA.class), eq(URI.create(uris.get(0))), any(Descriptor.class))) .thenReturn(new OWLClassA(URI.create(uris.get(0)))); when(uowMock.readObject(eq(OWLClassA.class), eq(URI.create(uris.get(1))), any(Descriptor.class))) .thenReturn(new OWLClassA(URI.create(uris.get(1)))); final TypedQuery<OWLClassA> sut = create(SELECT_QUERY, OWLClassA.class); final Stream<OWLClassA> result = sut.getResultStream(); final List<OWLClassA> asList = result.collect(Collectors.toList()); assertEquals(uris.size(), asList.size()); assertTrue(uris.containsAll(asList.stream().map(a -> a.getUri().toString()).collect(Collectors.toList()))); }
@Test void getResultStreamClosesStatementWhenStreamIsProcessed() throws Exception { final List<String> uris = Collections.singletonList(Generators.createIndividualIdentifier().toString()); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, false); when(resultSetMock.isBound(0)).thenReturn(true); when(resultSetMock.getString(0)).thenReturn(uris.get(0)); when(uowMock.readObject(eq(OWLClassA.class), eq(URI.create(uris.get(0))), any(Descriptor.class))) .thenReturn(new OWLClassA(URI.create(uris.get(0)))); final TypedQuery<OWLClassA> sut = create(SELECT_QUERY, OWLClassA.class); sut.getResultStream().forEach(a -> assertTrue(uris.contains(a.getUri().toString()))); verify(statementMock).close(); }
@Test void getResultStreamClosesStatementWhenStreamProcessingThrowsException() throws Exception { final List<String> uris = Collections.singletonList(Generators.createIndividualIdentifier().toString()); when(resultSetMock.isOpen()).thenReturn(true); when(resultSetMock.hasNext()).thenReturn(true, false); when(resultSetMock.isBound(0)).thenReturn(true); when(resultSetMock.getString(0)).thenReturn(uris.get(0)); when(uowMock.readObject(eq(OWLClassA.class), eq(URI.create(uris.get(0))), any(Descriptor.class))) .thenThrow(OWLPersistenceException.class); final TypedQuery<OWLClassA> sut = create(SELECT_QUERY, OWLClassA.class); try { assertThrows(OWLPersistenceException.class, () -> sut.getResultStream().forEach(a -> assertTrue(uris.contains(a.getUri().toString())))); } finally { verify(statementMock).close(); } }