Skip to content

Commit

Permalink
Make Optional assertions extensible by introducing AbstractOptionalAs…
Browse files Browse the repository at this point in the history
…sert
  • Loading branch information
joel-costigliola committed Apr 6, 2015
1 parent 9e690e7 commit 4277a82
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 78 deletions.
107 changes: 107 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractOptionalAssert.java
@@ -0,0 +1,107 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2015 the original author or authors.
*/
package org.assertj.core.api;

import static org.assertj.core.error.OptionalShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.OptionalShouldContain.shouldContain;

import java.util.Optional;

/**
* Assertions for {@link java.util.Optional}.
*
* @param <T> type of the value contained in the {@link java.util.Optional}.
* @author Jean-Christophe Gay
*/
public abstract class AbstractOptionalAssert<S extends AbstractOptionalAssert<S, T>, T> extends
AbstractAssert<S, Optional<T>> {

protected AbstractOptionalAssert(Optional<T> actual, Class<?> selfType) {
super(actual, selfType);
}

/**
* Verifies that there is a value present in the actual {@link java.util.Optional}.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).isPresent();
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.empty()).isPresent();
* </code></pre>
*
* @return this assertion object.
*/
public S isPresent() {
isNotNull();
if (!actual.isPresent()) throw failure(shouldBePresent());
return myself;
}

/**
* Verifies that the actual {@link java.util.Optional} is empty.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.empty()).isEmpty();
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).isEmpty();
* </code></pre>
*
* @return this assertion object.
*/
public S isEmpty() {
isNotNull();
if (actual.isPresent()) throw failure(shouldBeEmpty(actual));
return myself;
}

/**
* Verifies that the actual {@link java.util.Optional} contains the value in argument.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).contains("something");
* assertThat(Optional.of(10)).contains(10);
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).contains("something else");
* assertThat(Optional.of(20)).contains(10);
* </code></pre>
*
* @param expectedValue the expected value inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public S contains(T expectedValue) {
isNotNull();
if (expectedValue == null) throw new IllegalArgumentException("The expected value should not be <null>.");
if (!actual.isPresent()) throw failure(shouldContain(expectedValue));
if (!actual.get().equals(expectedValue)) throw failure(shouldContain(actual, expectedValue));
return myself;
}
}
79 changes: 1 addition & 78 deletions src/main/java/org/assertj/core/api/OptionalAssert.java
Expand Up @@ -12,10 +12,6 @@
*/
package org.assertj.core.api;

import static org.assertj.core.error.OptionalShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.OptionalShouldContain.shouldContain;

import java.util.Optional;

/**
Expand All @@ -24,83 +20,10 @@
* @param <T> type of the value contained in the {@link java.util.Optional}.
* @author Jean-Christophe Gay
*/
public class OptionalAssert<T> extends AbstractAssert<OptionalAssert<T>, Optional<T>> {
public class OptionalAssert<T> extends AbstractOptionalAssert<OptionalAssert<T>, T> {

protected OptionalAssert(Optional<T> actual) {
super(actual, OptionalAssert.class);
}

/**
* Verifies that there is a value present in the actual {@link java.util.Optional}.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).isPresent();
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.empty()).isPresent();
* </code></pre>
*
* @return this assertion object.
*/
public OptionalAssert<T> isPresent() {
isNotNull();
if (!actual.isPresent()) throw failure(shouldBePresent());
return this;
}

/**
* Verifies that the actual {@link java.util.Optional} is empty.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.empty()).isEmpty();
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).isEmpty();
* </code></pre>
*
* @return this assertion object.
*/
public OptionalAssert<T> isEmpty() {
isNotNull();
if (actual.isPresent()) throw failure(shouldBeEmpty(actual));
return this;
}

/**
* Verifies that the actual {@link java.util.Optional} contains the value in argument.
* </p>
* Assertion will pass :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).contains("something");
* assertThat(Optional.of(10)).contains(10);
* </code></pre>
*
* Assertion will fail :
*
* <pre><code class='java'>
* assertThat(Optional.of("something")).contains("something else");
* assertThat(Optional.of(20)).contains(10);
* </code></pre>
*
* @param expectedValue the expected value inside the {@link java.util.Optional}.
* @return this assertion object.
*/
public OptionalAssert<T> contains(T expectedValue) {
isNotNull();
if (expectedValue == null) throw new IllegalArgumentException("The expected value should not be <null>.");
if (!actual.isPresent()) throw failure(shouldContain(expectedValue));
if (!actual.get().equals(expectedValue)) throw failure(shouldContain(actual, expectedValue));
return this;
}
}

0 comments on commit 4277a82

Please sign in to comment.