From: David ‘Bombe’ Roden Date: Mon, 7 Jul 2014 18:36:00 +0000 (+0200) Subject: Add unit test for default option implementation. X-Git-Tag: 0.9-rc1^2~3^2~214 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ad5d4046c53129d23b3acf4eb6aa8643dbef2f86;p=Sone.git Add unit test for default option implementation. --- diff --git a/src/test/java/net/pterodactylus/sone/core/DefaultOptionTest.java b/src/test/java/net/pterodactylus/sone/core/DefaultOptionTest.java new file mode 100644 index 0000000..d7ca110 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/DefaultOptionTest.java @@ -0,0 +1,106 @@ +package net.pterodactylus.sone.core; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import javax.annotation.Nullable; + +import net.pterodactylus.sone.core.Options.DefaultOption; +import net.pterodactylus.sone.core.Options.Option; +import net.pterodactylus.sone.core.Options.OptionWatcher; + +import com.google.common.base.Predicate; +import org.junit.Test; + +/** + * Unit test for {@link DefaultOption}. + * + * @author David ‘Bombe’ Roden + */ +public class DefaultOptionTest { + + private final Object defaultValue = new Object(); + private final Object acceptedValue = new Object(); + private final Predicate matchesAcceptedValue = new Predicate() { + @Override + public boolean apply(@Nullable Object object) { + return acceptedValue.equals(object); + } + }; + + @Test + public void defaultOptionReturnsDefaultValueWhenUnset() { + DefaultOption defaultOption = new DefaultOption(defaultValue); + assertThat(defaultOption.get(), is(defaultValue)); + } + + @Test + public void defaultOptionReturnsNullForRealWhenUnset() { + DefaultOption defaultOption = new DefaultOption(defaultValue); + assertThat(defaultOption.getReal(), nullValue()); + } + + @Test + public void defaultOptionWillReturnSetValue() { + DefaultOption defaultOption = new DefaultOption(defaultValue); + Object newValue = new Object(); + defaultOption.set(newValue); + assertThat(defaultOption.get(), is(newValue)); + } + + @Test + public void defaultOptionWithValidatorAcceptsValidValues() { + DefaultOption defaultOption = new DefaultOption(defaultValue, matchesAcceptedValue); + defaultOption.set(acceptedValue); + assertThat(defaultOption.get(), is(acceptedValue)); + } + + @Test(expected = IllegalArgumentException.class) + public void defaultOptionWithValidatorRejectsInvalidValues() { + DefaultOption defaultOption = new DefaultOption(defaultValue, matchesAcceptedValue); + defaultOption.set(new Object()); + } + + @Test + public void watcherIsNotifiedOnChange() { + final AtomicReference changedObject = new AtomicReference(); + Object newValue = new Object(); + DefaultOption defaultOption = new DefaultOption(defaultValue, new OptionWatcher() { + @Override + public void optionChanged(Option option, Object oldValue, Object newValue) { + assertThat(oldValue, nullValue()); + changedObject.set(newValue); + } + }); + defaultOption.set(newValue); + assertThat(defaultOption.get(), is(newValue)); + assertThat(changedObject.get(), is(newValue)); + } + + @Test + public void watcherIsNotNotifiedIfValueIsSetTwice() { + final AtomicInteger changeCounter = new AtomicInteger(); + Object newValue = new Object(); + DefaultOption defaultOption = new DefaultOption(defaultValue, new OptionWatcher() { + @Override + public void optionChanged(Option option, Object oldValue, Object newValue) { + changeCounter.incrementAndGet(); + } + }); + defaultOption.set(newValue); + defaultOption.set(newValue); + assertThat(changeCounter.get(), is(1)); + } + + @Test + public void defaultOptionValidatesObjectsCorrectly() { + DefaultOption defaultOption = new DefaultOption(defaultValue, matchesAcceptedValue); + assertThat(defaultOption.validate(acceptedValue), is(true)); + assertThat(defaultOption.validate(new Object()), is(false)); + } + +}