Skip to content

Commit

Permalink
2.8.1.RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
MammatusPlatypus committed May 5, 2016
1 parent aca7220 commit 6138e8f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

group 'io.advantageous.reakt'
version '2.8.0'
version '2.8.1.RELEASE'

apply plugin: 'java'
apply plugin: 'maven'
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/io/advantageous/reakt/Expected.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ static <T> Expected<T> ofOptional(final Optional<T> value) {
*
* @return returns true if value is null
*/
boolean isAbsent() ;
boolean isAbsent();

/**
* If a value is not present (null), invoke the runnable.
*
* @param runnable executed if a value is not present
* @return self, fluent
* @return self, fluent
*/
Expected<T> ifAbsent(final Runnable runnable) ;
Expected<T> ifAbsent(final Runnable runnable);

/**
* If a value is present in this {@code Expected}, returns the value,
Expand All @@ -136,12 +136,21 @@ static <T> Expected<T> ofOptional(final Optional<T> value) {
*/
boolean isPresent();


/**
* If a value is not empty. See {@link Expected#ifEmpty(Runnable)}, and {@link Expected#isEmpty()}
* for more details.
*
* @param runnable executed if a value is not present
* @return fluent, this
*/
Expected<T> ifNotEmpty(final Runnable runnable);

/**
* If a value is not present or present and empty (empty check only works with
* collection, string, charSequence size/length 0).
* return true.
*
* <p>
* If you just want a null check, use {@code isAbsent}.
*
* @return {@code true} if there is not a value present or present and empty,
Expand All @@ -153,7 +162,7 @@ static <T> Expected<T> ofOptional(final Optional<T> value) {
/**
* If a value is not present or present and empty (collection, string, charSequence size/length 0),
* invoke the runnable. See for more details {@link Expected#isAbsent()}.
*
* <p>
* If you just want a null check, use {@code ifAbsent}.
*
* @param runnable executed if a value is not present.
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/io/advantageous/reakt/impl/ExpectedImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public boolean isAbsent() {
*/
@Override
public boolean isEmpty() {
if (this.value == null) {
if (this.value == null) {
return true;
} else if (value instanceof Collection) {
final Collection c = ((Collection) value);
Expand Down Expand Up @@ -147,6 +147,18 @@ public Expected<T> ifEmpty(final Runnable runnable) {
return this;
}

/**
* If a value is not empty.
*
* @param runnable executed if a value is not present
*/
public Expected<T> ifNotEmpty(final Runnable runnable) {
if (!isEmpty()) {
runnable.run();
}
return this;
}

/**
* If a value is not present (null), invoke the runnable.
*
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/io/advantageous/reakt/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class ResultTest {
Expand All @@ -46,7 +48,7 @@ public void testSuccess() {

@Test
public void testFail() {
final Result<Employee> rick = Result.error(new IllegalStateException("Rick"));
final Result<Employee> rick = Result.error(new IOException("Rick"));
Employee[] employee = new Employee[1];
rick.then(e -> employee[0] = e);
assertNull(employee[0]);
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/io/advantageous/reakt/impl/ExpectedImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.advantageous.reakt.impl;

import io.advantageous.reakt.Expected;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;

import static org.junit.Assert.*;

public class ExpectedImplTest {


@Test
public void isAbsent() throws Exception {

final Expected<Object> objectExpected = Expected.ofNullable(null);
assertTrue(objectExpected.isAbsent());
}

@Test
public void isEmpty() throws Exception {

final Expected<Object> expected1 = Expected.ofNullable(null);
assertTrue(expected1.isEmpty());
final Expected<Object> expected2 = Expected.ofNullable(Collections.emptyList());
assertTrue(expected2.isEmpty());
final Expected<Object> expected3 = Expected.ofNullable(new Object());
assertFalse(expected3.isEmpty());
final Expected<Object> expected4 = Expected.ofNullable("");
assertTrue(expected4.isEmpty());

final Expected<Object> expected5 = Expected.ofNullable(new Object[0]);
assertTrue(expected5.isEmpty());
}

@Test
public void ifEmpty() throws Exception {

final Expected<Object> expected = Expected.ofNullable(new Object());
expected.ifEmpty(Assert::fail);
final Expected<Object> expected2 = Expected.ofNullable(Collections.singleton(new Object()));
expected2.ifEmpty(Assert::fail);

final Expected<Object> expected3 = Expected.ofNullable("abc");
expected3.ifEmpty(Assert::fail);

}

@Test
public void ifNotEmpty() throws Exception {
final Expected<Object> expected = Expected.ofNullable(null);
expected.ifNotEmpty(Assert::fail);

final Expected<Object> expected2 = Expected.ofNullable(Collections.emptyList());
expected2.ifNotEmpty(Assert::fail);

}

@Test
public void ifAbsent() throws Exception {


final Expected<Object> expected = Expected.ofNullable(new Object());

expected.ifAbsent(Assert::fail);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public void testAsyncServiceWithInvokePromiseFail() {
assertNotNull("There were errors from async", errorRef.get());
}

@Test
public void testAsyncServiceWithInvokePromiseFail2() {


Promise<URI> promise = Promises.blockingPromise();
promise.then(this::handleSuccess)
.catchError(this::handleError);

asyncServiceDiscovery.lookupService(null).thenCallback(promise).invoke();


try {
promise.get();
fail();
} catch (Exception ex) {

}

assertNull("We do not have a return from async", returnValue.get());
assertNotNull("There were errors from async", errorRef.get());
}

@Test
public void testAsyncServiceWithReturnPromiseFail() {

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/io/advantageous/reakt/promise/PromiseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ public void testSafe() throws Exception {
testSuccessWithPromise(testService, employee, value, promise);
}

@Test
public void testSafeFail() throws Exception {

TestService testService = new TestService();
AtomicReference<Throwable> error = new AtomicReference<>();

Promise<Employee> promise = Promises.<Employee>promise().thenSafe(new Consumer<Employee>() {
@Override
public void accept(Employee employee) {
throw new IllegalStateException("BOOM.. handler failed");
}
}).catchError(error::set);

testService.simple(promise);
assertNotNull(error.get());

}

@Test
public void testSafeFinal() throws Exception {

Expand Down

0 comments on commit 6138e8f

Please sign in to comment.