diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/ArrayUtilsTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/ArrayUtilsTest.java new file mode 100644 index 00000000..1b51afc5 --- /dev/null +++ b/org.moreunit.core.test/test/org/moreunit/core/util/ArrayUtilsTest.java @@ -0,0 +1,22 @@ +package org.moreunit.core.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class ArrayUtilsTest +{ + @Test + public void array_should_return_elements() + { + String[] strings = ArrayUtils.array("a", "b", "c"); + assertThat(strings).containsExactly("a", "b", "c"); + } + + @Test + public void array_should_return_empty_array_when_no_arguments() + { + Object[] objects = ArrayUtils.array(); + assertThat(objects).isEmpty(); + } +} diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/ExtendedSafeRunnerTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/ExtendedSafeRunnerTest.java new file mode 100644 index 00000000..772b169a --- /dev/null +++ b/org.moreunit.core.test/test/org/moreunit/core/util/ExtendedSafeRunnerTest.java @@ -0,0 +1,80 @@ +package org.moreunit.core.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.moreunit.core.util.ExtendedSafeRunner.GenericRunnable; + +public class ExtendedSafeRunnerTest +{ + @Test + public void applyTo_should_execute_code_and_return_result() + { + ExtendedSafeRunner runner = new ExtendedSafeRunner(); + String result = runner.applyTo("input", new GenericRunnable() + { + @Override + public String run(String element) throws Exception + { + return element.toUpperCase(); + } + + @Override + public void handleException(Throwable throwable, String element) + { + } + }); + + assertThat(result).isEqualTo("INPUT"); + } + + @Test + public void applyTo_iterable_should_execute_code_for_each_element() + { + ExtendedSafeRunner runner = new ExtendedSafeRunner(); + Iterable results = runner.applyTo(Arrays.asList("a", "b"), new GenericRunnable() + { + @Override + public String run(String element) throws Exception + { + return element.toUpperCase(); + } + + @Override + public void handleException(Throwable throwable, String element) + { + } + }); + + assertThat(results).containsExactly("A", "B"); + } + + @Test + public void applyTo_should_handle_exception() + { + ExtendedSafeRunner runner = new ExtendedSafeRunner(); + final boolean[] exceptionHandled = { false }; + + runner.applyTo("input", new GenericRunnable() + { + @Override + public String run(String element) throws Exception + { + throw new RuntimeException("failed"); + } + + @Override + public void handleException(Throwable throwable, String element) + { + exceptionHandled[0] = true; + assertThat(element).isEqualTo("input"); + assertThat(throwable.getMessage()).isEqualTo("failed"); + } + }); + + assertThat(exceptionHandled[0]).isTrue(); + } +} diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/PreconditionsTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/PreconditionsTest.java new file mode 100644 index 00000000..00e541f6 --- /dev/null +++ b/org.moreunit.core.test/test/org/moreunit/core/util/PreconditionsTest.java @@ -0,0 +1,154 @@ +package org.moreunit.core.util; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; + +public class PreconditionsTest +{ + @Test + public void checkNotNull_should_return_reference_when_not_null() + { + String ref = "abc"; + assertThat(Preconditions.checkNotNull(ref)).isSameAs(ref); + } + + @Test(expected = NullPointerException.class) + public void checkNotNull_should_throw_exception_when_null() + { + Preconditions.checkNotNull(null); + } + + @Test + public void checkNotNull_with_message_should_return_reference_when_not_null() + { + String ref = "abc"; + assertThat(Preconditions.checkNotNull(ref, "error")).isSameAs(ref); + } + + @Test + public void checkNotNull_with_message_should_throw_exception_with_message_when_null() + { + try + { + Preconditions.checkNotNull(null, "custom error"); + fail("Should have thrown NullPointerException"); + } + catch (NullPointerException e) + { + assertThat(e.getMessage()).isEqualTo("custom error"); + } + } + + @Test + public void checkArgument_should_do_nothing_when_true() + { + Preconditions.checkArgument(true); + } + + @Test(expected = IllegalArgumentException.class) + public void checkArgument_should_throw_exception_when_false() + { + Preconditions.checkArgument(false); + } + + @Test + public void checkArgument_with_message_should_do_nothing_when_true() + { + Preconditions.checkArgument(true, "error"); + } + + @Test + public void checkArgument_with_message_should_throw_exception_with_message_when_false() + { + try + { + Preconditions.checkArgument(false, "custom error"); + fail("Should have thrown IllegalArgumentException"); + } + catch (IllegalArgumentException e) + { + assertThat(e.getMessage()).isEqualTo("custom error"); + } + } + + @Test + public void checkState_should_do_nothing_when_true() + { + Preconditions.checkState(true, "error"); + } + + @Test + public void checkState_should_throw_exception_with_message_when_false() + { + try + { + Preconditions.checkState(false, "custom error"); + fail("Should have thrown IllegalStateException"); + } + catch (IllegalStateException e) + { + assertThat(e.getMessage()).isEqualTo("custom error"); + } + } + + @Test + public void checkNotNullOrEmpty_should_return_collection_when_not_null_nor_empty() + { + List list = Arrays.asList("a"); + assertThat(Preconditions.checkNotNullOrEmpty(list)).isSameAs(list); + } + + @Test(expected = NullPointerException.class) + public void checkNotNullOrEmpty_should_throw_NPE_when_null() + { + Preconditions.checkNotNullOrEmpty(null); + } + + @Test(expected = IllegalArgumentException.class) + public void checkNotNullOrEmpty_should_throw_IAE_when_empty() + { + Preconditions.checkNotNullOrEmpty(new ArrayList()); + } + + @Test + public void checkNotNullOrEmpty_with_message_should_return_collection_when_not_null_nor_empty() + { + List list = Arrays.asList("a"); + assertThat(Preconditions.checkNotNullOrEmpty(list, "error")).isSameAs(list); + } + + @Test + public void checkNotNullOrEmpty_with_message_should_throw_NPE_with_message_when_null() + { + try + { + Preconditions.checkNotNullOrEmpty(null, "custom error"); + fail("Should have thrown NullPointerException"); + } + catch (NullPointerException e) + { + assertThat(e.getMessage()).isEqualTo("custom error"); + } + } + + @Test + public void checkNotNullOrEmpty_with_message_should_throw_IAE_with_message_when_empty() + { + try + { + Preconditions.checkNotNullOrEmpty(new ArrayList(), "custom error"); + fail("Should have thrown IllegalArgumentException"); + } + catch (IllegalArgumentException e) + { + assertThat(e.getMessage()).isEqualTo("custom error"); + } + } +} diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/StringConstantsTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/StringConstantsTest.java new file mode 100644 index 00000000..f88953e6 --- /dev/null +++ b/org.moreunit.core.test/test/org/moreunit/core/util/StringConstantsTest.java @@ -0,0 +1,18 @@ +package org.moreunit.core.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class StringConstantsTest +{ + @Test + public void constants_should_have_expected_values() + { + assertThat(StringConstants.DOT).isEqualTo("."); + assertThat(StringConstants.EMPTY_STRING).isEqualTo(""); + assertThat(StringConstants.NEWLINE).isEqualTo(System.getProperty("line.separator")); + assertThat(StringConstants.SLASH).isEqualTo("/"); + assertThat(StringConstants.WILDCARD).isEqualTo("*"); + } +} diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/StringLengthComparatorTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/StringLengthComparatorTest.java index be2f769b..fbb9dd91 100644 --- a/org.moreunit.core.test/test/org/moreunit/core/util/StringLengthComparatorTest.java +++ b/org.moreunit.core.test/test/org/moreunit/core/util/StringLengthComparatorTest.java @@ -15,7 +15,7 @@ public void should_return_positive_integer_when_first_has_greater_length_than_se @Test public void should_return_negative_integer_when_second_has_greater_length_than_first_parameter() throws Exception { - assertThat(new StringLengthComparator().compare("", "Long")).isLessThanOrEqualTo(0); + assertThat(new StringLengthComparator().compare("", "Long")).isLessThan(0); } @Test diff --git a/org.moreunit.core.test/test/org/moreunit/core/util/StringsTest.java b/org.moreunit.core.test/test/org/moreunit/core/util/StringsTest.java index ec4b2d97..6e90c0fa 100644 --- a/org.moreunit.core.test/test/org/moreunit/core/util/StringsTest.java +++ b/org.moreunit.core.test/test/org/moreunit/core/util/StringsTest.java @@ -127,4 +127,10 @@ public void join_should_join_given_strings_whith_given_separator() throws Except assertThat(Strings.join(",", "1", "2", "3")).isEqualTo("1,2,3"); assertThat(Strings.join(" -> ", "aBc", "2", "DEf")).isEqualTo("aBc -> 2 -> DEf"); } + + @Test + public void emptyArray_should_return_empty_array() + { + assertThat(Strings.emptyArray()).isEmpty(); + } }