1 package net.pterodactylus.sone.utils;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.Matchers.nullValue;
7 import javax.annotation.Nullable;
9 import com.google.common.base.Predicate;
10 import org.junit.Test;
13 * Unit test for {@link DefaultOption}.
15 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
17 public class DefaultOptionTest {
19 private final Object defaultValue = new Object();
20 private final Object acceptedValue = new Object();
21 private final Predicate<Object> matchesAcceptedValue = new Predicate<Object>() {
23 public boolean apply(@Nullable Object object) {
24 return acceptedValue.equals(object);
29 public void defaultOptionReturnsDefaultValueWhenUnset() {
30 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue);
31 assertThat(defaultOption.get(), is(defaultValue));
35 public void defaultOptionReturnsNullForRealWhenUnset() {
36 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue);
37 assertThat(defaultOption.getReal(), nullValue());
41 public void defaultOptionWillReturnSetValue() {
42 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue);
43 Object newValue = new Object();
44 defaultOption.set(newValue);
45 assertThat(defaultOption.get(), is(newValue));
49 public void defaultOptionWithValidatorAcceptsValidValues() {
50 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue, matchesAcceptedValue);
51 defaultOption.set(acceptedValue);
52 assertThat(defaultOption.get(), is(acceptedValue));
55 @Test(expected = IllegalArgumentException.class)
56 public void defaultOptionWithValidatorRejectsInvalidValues() {
57 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue, matchesAcceptedValue);
58 defaultOption.set(new Object());
62 public void defaultOptionValidatesObjectsCorrectly() {
63 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue, matchesAcceptedValue);
64 assertThat(defaultOption.validate(acceptedValue), is(true));
65 assertThat(defaultOption.validate(new Object()), is(false));
69 public void settingToNullWillRestoreDefaultValue() {
70 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue);
71 defaultOption.set(null);
72 assertThat(defaultOption.get(), is(defaultValue));
76 public void validateWithoutValidatorWillValidateNull() {
77 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue);
78 assertThat(defaultOption.validate(null), is(true));
82 public void validateWithValidatorWillValidateNull() {
83 DefaultOption<Object> defaultOption = new DefaultOption<Object>(defaultValue, matchesAcceptedValue);
84 assertThat(defaultOption.validate(null), is(true));