Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String>()
{
@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<String> results = runner.applyTo(Arrays.asList("a", "b"), new GenericRunnable<String, String>()
{
@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<String, String>()
{
@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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String>());
}

@Test
public void checkNotNullOrEmpty_with_message_should_return_collection_when_not_null_nor_empty()
{
List<String> 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<String>(), "custom error");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
assertThat(e.getMessage()).isEqualTo("custom error");
}
}
}
Original file line number Diff line number Diff line change
@@ -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("*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading