|
| 1 | +package com.rollbar.notifier.util; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Rule; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.rules.ExpectedException; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | + |
| 10 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | +import static org.hamcrest.core.Is.is; |
| 12 | + |
| 13 | +public class ObjectsUtilsTest { |
| 14 | + private static final Object[] EQUALS_VALUES = {"123", 123, null}; |
| 15 | + private static final String[] HASH_VALUES = {"one", "two", "three"}; |
| 16 | + private static String TEST = "Test"; |
| 17 | + private static String ERROR_DESCRIPTION = "Error description"; |
| 18 | + |
| 19 | + @Rule |
| 20 | + public ExpectedException expectedException; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() { |
| 24 | + expectedException = ExpectedException.none(); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testEquals() { |
| 29 | + for(int i = 0; i < EQUALS_VALUES.length; i++) |
| 30 | + for(int j = 0; j < EQUALS_VALUES.length; j++) { Object first = EQUALS_VALUES[i]; |
| 31 | + Object second = EQUALS_VALUES[j]; |
| 32 | + boolean expected = (i == j); |
| 33 | + boolean result = ObjectsUtils.equals(first, second); |
| 34 | + assertThat(String.format("Expected %b when equating %s to %s, got %b instead.", expected, first, second, result), result, is(expected)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testHash() { |
| 40 | + int result = ObjectsUtils.hash((Object[])null); |
| 41 | + int expected = 0; |
| 42 | + assertThat(result, is(expected)); |
| 43 | + result = ObjectsUtils.hash("one", "two", "three"); |
| 44 | + expected = Arrays.hashCode(HASH_VALUES); |
| 45 | + assertThat(result, is(expected)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testNonNull() { |
| 50 | + String result = ObjectsUtils.requireNonNull(TEST, ERROR_DESCRIPTION); |
| 51 | + String expected = TEST; |
| 52 | + assertThat(result, is(expected)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testNull() { |
| 57 | + expectedException.expect(NullPointerException.class); |
| 58 | + expectedException.expectMessage(ERROR_DESCRIPTION); |
| 59 | + ObjectsUtils.requireNonNull(null, ERROR_DESCRIPTION); |
| 60 | + } |
| 61 | +} |
0 commit comments