#370 - Added example of how to use Vavr's Try.

Temporarily upgrade to Lovelace to make use of the new Try support
This commit is contained in:
Oliver Gierke
2018-05-20 22:30:03 +02:00
parent 0a9a8f4e60
commit 6653f260af
3 changed files with 46 additions and 0 deletions

View File

@@ -10,6 +10,10 @@
<artifactId>spring-data-jpa-vavr</artifactId>
<name>Spring Data JPA - Vavr integration</name>
<properties>
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>

View File

@@ -19,6 +19,7 @@ import io.vavr.collection.Map;
import io.vavr.collection.Seq;
import io.vavr.collection.Set;
import io.vavr.control.Option;
import io.vavr.control.Try;
import java.util.List;
import java.util.Optional;
@@ -51,4 +52,12 @@ public interface PersonRepository extends Repository<Person, Long> {
* @return
*/
Seq<Person> findByFirstnameContaining(String firstname);
/**
* Returning a {@link Try} is supported out of the box with all exceptions being handled by {@link Try} immediately.
*
* @param lastname
* @return
*/
Try<Option<Person>> findByLastnameContaining(String lastname);
}

View File

@@ -19,6 +19,9 @@ import static org.assertj.core.api.Assertions.*;
import io.vavr.collection.Seq;
import io.vavr.control.Option;
import io.vavr.control.Try;
import javax.persistence.NonUniqueResultException;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -71,4 +74,34 @@ public class PersonRepositoryIntegrationTests {
assertThat(result.contains(carter)).isTrue();
assertThat(result.contains(dave)).isFalse();
}
/**
* @see #370
*/
@Test
public void returnsSuccessOfOption() {
Person dave = people.save(new Person("Dave", "Matthews"));
people.save(new Person("Carter", "Beauford"));
Try<Option<Person>> result = people.findByLastnameContaining("w");
assertThat(result.isSuccess()).isTrue();
assertThat(result.get()).contains(dave);
}
/**
* @see #370
*/
@Test
public void returnsFailureOfOption() {
people.save(new Person("Dave", "Matthews"));
people.save(new Person("Carter", "Beauford"));
Try<Option<Person>> result = people.findByLastnameContaining("e");
assertThat(result.isFailure()).isTrue();
assertThat(result.getCause()).isInstanceOf(NonUniqueResultException.class);
}
}