|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2014 the original author or authors. |
| 2 | + * Copyright 2013-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 | import static org.hamcrest.CoreMatchers.*;
|
19 | 19 | import static org.junit.Assert.*;
|
20 | 20 |
|
| 21 | +import java.util.List; |
21 | 22 | import java.util.Optional;
|
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
22 | 25 |
|
| 26 | +import org.hamcrest.core.IsCollectionContaining; |
23 | 27 | import org.junit.Test;
|
24 | 28 | import org.junit.runner.RunWith;
|
25 | 29 | import org.springframework.beans.factory.annotation.Autowired;
|
|
31 | 35 | * Integration test to show the usage of Java 8 date time APIs with Spring Data JPA auditing.
|
32 | 36 | *
|
33 | 37 | * @author Oliver Gierke
|
| 38 | + * @author Thomas Darimont |
34 | 39 | */
|
35 | 40 | @RunWith(SpringJUnit4ClassRunner.class)
|
36 | 41 | @ContextConfiguration(classes = AuditingConfiguration.class)
|
@@ -66,4 +71,22 @@ public void invokesDefaultMethod() {
|
66 | 71 | assertThat(result.isPresent(), is(true));
|
67 | 72 | assertThat(result.get(), is(customer));
|
68 | 73 | }
|
| 74 | + |
| 75 | + /** |
| 76 | + * Streaming data from the store by using a repsoitory method that returns a {@link Stream}. Note, that since the |
| 77 | + * resulting {@link Stream} contains state it needs to be closed explicitly after use! |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void useJava8StreamsDirectly() { |
| 81 | + |
| 82 | + Customer customer1 = repository.save(new Customer("Customer1", "Foo")); |
| 83 | + Customer customer2 = repository.save(new Customer("Customer2", "Bar")); |
| 84 | + |
| 85 | + try (Stream<Customer> stream = repository.streamAllCustomers()) { |
| 86 | + |
| 87 | + List<Customer> customers = stream.collect(Collectors.toList()); |
| 88 | + |
| 89 | + assertThat(customers, IsCollectionContaining.<Customer> hasItems(customer1, customer2)); |
| 90 | + } |
| 91 | + } |
69 | 92 | }
|
0 commit comments