mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#2246 - ENH: Add DtoQuery findStream() and findIterate()
This commit is contained in:
@@ -6,10 +6,13 @@ import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -30,6 +33,73 @@ public class DtoQuery2Test extends BaseTestCase {
|
||||
assertThat(list).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findIterator_closeWithResources() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
int counter = 0;
|
||||
try (QueryIterator<DCust> iterator = server()
|
||||
.findDto(DCust.class, "select id, name from o_customer where id > ?")
|
||||
.setParameter(0)
|
||||
.findIterate()) {
|
||||
|
||||
if (iterator.hasNext()) {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(counter).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findIterator() {
|
||||
ResetBasicData.reset();
|
||||
final int expectedCount = server().find(Customer.class).findCount();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
int counter = 0;
|
||||
try (final QueryIterator<DCust> iterator = server().findDto(DCust.class, "select id, name from o_customer where id > :id")
|
||||
.setParameter("id", 0)
|
||||
.findIterate()) {
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
final DCust cust = iterator.next();
|
||||
counter++;
|
||||
assertThat(cust).isNotNull();
|
||||
assertThat(cust.getName()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(counter).isEqualTo(expectedCount);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("select id, name from o_customer where id > ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findStream() {
|
||||
ResetBasicData.reset();
|
||||
final int expectedCount = server().find(Customer.class).findCount();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
try (final Stream<DCust> stream =
|
||||
server()
|
||||
.findDto(DCust.class, "select id, name from o_customer where id > ?")
|
||||
.setParameter(0)
|
||||
.findStream()) {
|
||||
|
||||
final List<String> names = stream
|
||||
.map(DCust::getName)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(names.size()).isEqualTo(expectedCount);
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertSql(sql.get(0)).contains("select id, name from o_customer where id > ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dto_findEach_constructorMatch() {
|
||||
|
||||
|
||||
@@ -453,6 +453,16 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
public <T> void findDtoEachWhile(SpiDtoQuery<T> query, Predicate<T> consumer) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> QueryIterator<T> findDtoIterate(SpiDtoQuery<T> query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Stream<T> findDtoStream(SpiDtoQuery<T> query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> findDtoList(SpiDtoQuery<T> query) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user