From baf45e15c1f0050e1b891fb9f30d385f6207fd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Suszy=C5=84ski=20Krzysztof?= Date: Tue, 29 Mar 2016 13:57:36 +0200 Subject: [PATCH] More tests to fix #3 --- .../eid/utils/EidPreconditions.java | 62 +++-- .../eid/utils/EidPreconditionsTest.java | 230 +++++++++++++++++- 2 files changed, 260 insertions(+), 32 deletions(-) diff --git a/src/main/java/pl/wavesoftware/eid/utils/EidPreconditions.java b/src/main/java/pl/wavesoftware/eid/utils/EidPreconditions.java index 70f4d19..5600b2a 100644 --- a/src/main/java/pl/wavesoftware/eid/utils/EidPreconditions.java +++ b/src/main/java/pl/wavesoftware/eid/utils/EidPreconditions.java @@ -122,7 +122,7 @@ protected EidPreconditions() { */ public static void checkArgument(final boolean expression, final String eid) { if (!expression) { - throw new EidIllegalArgumentException(new Eid(eid)); + throw new EidIllegalArgumentException(ensureEid(eid)); } } @@ -139,7 +139,7 @@ public static void checkArgument(final boolean expression, final String eid) { public static void checkArgument(final boolean expression, final String eid, final String messageFormat, final Object... parameters) { if (!expression) { - throw new EidIllegalArgumentException(new Eid(eid), messageFormat, parameters); + throw new EidIllegalArgumentException(ensureEid(eid), messageFormat, parameters); } } @@ -153,7 +153,7 @@ public static void checkArgument(final boolean expression, final String eid, */ public static void checkArgument(final boolean expression, final Eid eid) { if (!expression) { - throw new EidIllegalArgumentException(eid); + throw new EidIllegalArgumentException(ensureEid(eid)); } } @@ -170,7 +170,7 @@ public static void checkArgument(final boolean expression, final Eid eid) { public static void checkArgument(final boolean expression, final Eid eid, final String messageFormat, final Object... parameters) { if (!expression) { - throw new EidIllegalArgumentException(eid, messageFormat, parameters); + throw new EidIllegalArgumentException(ensureEid(eid), messageFormat, parameters); } } @@ -185,7 +185,7 @@ public static void checkArgument(final boolean expression, final Eid eid, */ public static void checkState(final boolean expression, final String eid) { if (!expression) { - throw new EidIllegalStateException(new Eid(eid)); + throw new EidIllegalStateException(ensureEid(eid)); } } @@ -203,7 +203,7 @@ public static void checkState(final boolean expression, final String eid) { public static void checkState(final boolean expression, final String eid, final String messageFormat, final Object... parameters) { if (!expression) { - throw new EidIllegalStateException(new Eid(eid), messageFormat, parameters); + throw new EidIllegalStateException(ensureEid(eid), messageFormat, parameters); } } @@ -218,7 +218,7 @@ public static void checkState(final boolean expression, final String eid, */ public static void checkState(final Boolean expression, final Eid eid) { if (!expression) { - throw new EidIllegalStateException(eid); + throw new EidIllegalStateException(ensureEid(eid)); } } @@ -236,7 +236,7 @@ public static void checkState(final Boolean expression, final Eid eid) { public static void checkState(final boolean expression, final Eid eid, final String messageFormat, final Object... parameters) { if (!expression) { - throw new EidIllegalStateException(eid, messageFormat, parameters); + throw new EidIllegalStateException(ensureEid(eid), messageFormat, parameters); } } @@ -252,7 +252,7 @@ public static void checkState(final boolean expression, final Eid eid, */ public static T checkNotNull(@Nullable final T reference, final String eid) { if (reference == null) { - throw new EidNullPointerException(new Eid(eid)); + throw new EidNullPointerException(ensureEid(eid)); } return reference; } @@ -272,7 +272,7 @@ public static T checkNotNull(@Nullable final T reference, final String eid) public static T checkNotNull(@Nullable final T reference, final String eid, final String messageFormat, final Object... parameters) { if (reference == null) { - throw new EidNullPointerException(new Eid(eid), messageFormat, parameters); + throw new EidNullPointerException(ensureEid(eid), messageFormat, parameters); } return reference; } @@ -289,7 +289,7 @@ public static T checkNotNull(@Nullable final T reference, final String eid, */ public static T checkNotNull(@Nullable final T reference, final Eid eid) { if (reference == null) { - throw new EidNullPointerException(eid); + throw new EidNullPointerException(ensureEid(eid)); } return reference; } @@ -310,7 +310,7 @@ public static T checkNotNull(@Nullable final T reference, final Eid eid) { public static T checkNotNull(@Nullable final T reference, final Eid eid, final String messageFormat, final Object... parameters) { if (reference == null) { - throw new EidNullPointerException(eid, messageFormat, parameters); + throw new EidNullPointerException(ensureEid(eid), messageFormat, parameters); } return reference; } @@ -328,10 +328,10 @@ public static T checkNotNull(@Nullable final T reference, final Eid eid, */ public static int checkElementIndex(int index, int size, final String eid) { if (isSizeIllegal(size)) { - throw new EidIllegalArgumentException(new Eid(eid)); + throw new EidIllegalArgumentException(ensureEid(eid)); } if (isIndexAndSizeIllegal(index, size)) { - throw new EidIndexOutOfBoundsException(new Eid(eid)); + throw new EidIndexOutOfBoundsException(ensureEid(eid)); } return index; } @@ -352,10 +352,10 @@ public static int checkElementIndex(int index, int size, final String eid) { public static int checkElementIndex(int index, int size, final String eid, final String messageFormat, final Object... parameters) { if (isSizeIllegal(size)) { - throw new EidIllegalArgumentException(new Eid(eid), messageFormat, parameters); + throw new EidIllegalArgumentException(ensureEid(eid), messageFormat, parameters); } if (isIndexAndSizeIllegal(index, size)) { - throw new EidIndexOutOfBoundsException(new Eid(eid), messageFormat, parameters); + throw new EidIndexOutOfBoundsException(ensureEid(eid), messageFormat, parameters); } return index; } @@ -373,10 +373,10 @@ public static int checkElementIndex(int index, int size, final String eid, */ public static int checkElementIndex(int index, int size, final Eid eid) { if (isSizeIllegal(size)) { - throw new EidIllegalArgumentException(eid); + throw new EidIllegalArgumentException(ensureEid(eid)); } if (isIndexAndSizeIllegal(index, size)) { - throw new EidIndexOutOfBoundsException(eid); + throw new EidIndexOutOfBoundsException(ensureEid(eid)); } return index; } @@ -397,10 +397,10 @@ public static int checkElementIndex(int index, int size, final Eid eid) { public static int checkElementIndex(int index, int size, final Eid eid, final String messageFormat, final Object... parameters) { if (isSizeIllegal(size)) { - throw new EidIllegalArgumentException(eid, messageFormat, parameters); + throw new EidIllegalArgumentException(ensureEid(eid), messageFormat, parameters); } if (isIndexAndSizeIllegal(index, size)) { - throw new EidIndexOutOfBoundsException(eid, messageFormat, parameters); + throw new EidIndexOutOfBoundsException(ensureEid(eid), messageFormat, parameters); } return index; } @@ -427,7 +427,7 @@ public static R tryToExecute(final UnsafeSupplier supplier, final String try { return supplier.get(); } catch (Exception throwable) { - throw new EidRuntimeException(new Eid(eid), throwable); + throw new EidRuntimeException(ensureEid(eid), throwable); } } @@ -443,7 +443,7 @@ public static void tryToExecute(final UnsafeProcedure procedure, final String ei try { procedure.execute(); } catch (Exception throwable) { - throw new EidRuntimeException(new Eid(eid), throwable); + throw new EidRuntimeException(ensureEid(eid), throwable); } } @@ -473,7 +473,7 @@ public static R tryToExecute(final UnsafeSupplier supplier, final Eid eid try { return supplier.get(); } catch (Exception throwable) { - throw new EidRuntimeException(eid, throwable); + throw new EidRuntimeException(ensureEid(eid), throwable); } } @@ -500,7 +500,7 @@ public static void tryToExecute(final UnsafeProcedure procedure, final Eid eid) try { procedure.execute(); } catch (Exception throwable) { - throw new EidRuntimeException(eid, throwable); + throw new EidRuntimeException(ensureEid(eid), throwable); } } @@ -536,4 +536,18 @@ public interface UnsafeProcedure { */ void execute() throws Exception; } + + private static Eid ensureEid(@Nullable Eid eid) { + if (eid == null) { + return new Eid("20160329:132823", "EID-NULL"); + } + return eid; + } + + private static Eid ensureEid(@Nullable String eid) { + if (eid == null) { + return new Eid("20160329:133052", "EID-NULL"); + } + return new Eid(eid); + } } diff --git a/src/test/java/pl/wavesoftware/eid/utils/EidPreconditionsTest.java b/src/test/java/pl/wavesoftware/eid/utils/EidPreconditionsTest.java index b04687a..5eb083a 100644 --- a/src/test/java/pl/wavesoftware/eid/utils/EidPreconditionsTest.java +++ b/src/test/java/pl/wavesoftware/eid/utils/EidPreconditionsTest.java @@ -33,9 +33,7 @@ import java.text.ParseException; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.CoreMatchers.*; import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; /** @@ -82,6 +80,28 @@ public void testCheckArgument_Ok_WithMessage() { assertThat(thrown).isNotNull(); } + @Test + public void testCheckArgument_Eid_WithMessage() { + // given + boolean expression = falsyValue(); + // then + thrown.expect(EidIllegalArgumentException.class); + thrown.expectMessage(containsString(eid)); + thrown.expectMessage(containsString("PI value is 3.14")); + // when + EidPreconditions.checkArgument(expression, new Eid(eid), "PI value is %.2f", Math.PI); + } + + @Test + public void testCheckArgument_Eid_Ok_WithMessage() { + // given + boolean expression = truthyValue(); + // when + EidPreconditions.checkArgument(expression, new Eid(eid), "PI value is %.2f", Math.PI); + // then + assertThat(thrown).isNotNull(); + } + @Test public void testCheckArgument_Ok() { // when @@ -125,6 +145,28 @@ public void testCheckState_Ok_WithMessage() { assertThat(thrown).isNotNull(); } + @Test + public void testCheckState_Eid_WithMessage() { + // given + boolean expression = falsyValue(); + // then + thrown.expect(EidIllegalStateException.class); + thrown.expectMessage(containsString(eid)); + thrown.expectMessage(containsString("PI is 3.1416")); + // when + EidPreconditions.checkState(expression, new Eid(eid), "PI is %.4f", Math.PI); + } + + @Test + public void testCheckState_Ok_Eid_WithMessage() { + // given + boolean expression = truthyValue(); + // when + EidPreconditions.checkState(expression, new Eid(eid), "PI is %.4f", Math.PI); + // then + assertThat(thrown).isNotNull(); + } + @Test public void testCheckState_Ok() { // when @@ -179,6 +221,28 @@ public void testCheckNotNull_Ok_WithMessage() { assertThat(checked).isSameAs(reference); } + @Test + public void testCheckNotNull_Eid_WithMessage() { + // given + Object reference = nullyValue(); + // then + thrown.expect(EidNullPointerException.class); + thrown.expectMessage(containsString(eid)); + thrown.expectMessage(containsString("π <=> 3.142")); + // when + EidPreconditions.checkNotNull(reference, new Eid(eid), "π <=> %.3f", Math.PI); + } + + @Test + public void testCheckNotNull_Ok_Eid_WithMessage() { + // given + Object reference = "A test"; + // when + Object checked = EidPreconditions.checkNotNull(reference, new Eid(eid), "π <=> %.3f", Math.PI); + // then + assertThat(checked).isSameAs(reference); + } + @Test public void testCheckElementIndex_Ok() { // given @@ -229,6 +293,19 @@ public void testCheckElementIndex_WithMessage() { EidPreconditions.checkElementIndex(index, size, eid, "Pi (π): %.2f", Math.PI); } + @Test + public void testCheckElementIndex_Eid_WithMessage() { + // given + int index = -1; + int size = 0; + // then + thrown.expect(EidIndexOutOfBoundsException.class); + thrown.expectMessage(containsString(eid)); + thrown.expectMessage(containsString("Pi (π): 3.14")); + // when + EidPreconditions.checkElementIndex(index, size, new Eid(eid), "Pi (π): %.2f", Math.PI); + } + @Test public void testCheckElementIndex_SizeIllegal_WithMessage() { // given @@ -242,6 +319,19 @@ public void testCheckElementIndex_SizeIllegal_WithMessage() { EidPreconditions.checkElementIndex(index, size, eid, "Pi (π): %.3f", Math.PI); } + @Test + public void testCheckElementIndex_SizeIllegal_Eid_WithMessage() { + // given + int index = 0; + int size = -45; + // then + thrown.expect(EidIllegalArgumentException.class); + thrown.expectMessage(containsString(eid)); + thrown.expectMessage(containsString("Pi (π): 3.142")); + // when + EidPreconditions.checkElementIndex(index, size, new Eid(eid), "Pi (π): %.3f", Math.PI); + } + @Test public void testCheckElementIndex_Ok_WithMessage() { // given @@ -253,6 +343,17 @@ public void testCheckElementIndex_Ok_WithMessage() { assertThat(checked).isEqualTo(index); } + @Test + public void testCheckElementIndex_Ok_Eid_WithMessage() { + // given + int index = 234; + int size = 450; + // when + int checked = EidPreconditions.checkElementIndex(index, size, new Eid(eid), "Pi (π): %.1f", Math.PI); + // then + assertThat(checked).isEqualTo(index); + } + @Test public void testCheckElementIndex_SizeInvalid() { // given @@ -319,10 +420,22 @@ public boolean matches(Object item) { public void testCheckArgument_boolean_Eid_Null() { // given boolean expression = falsyValue(); - Eid eidObject = getNullEid(); + Eid eidObject = nullyValue(); // then - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Pass not-null Eid to EidPreconditions first!"); + thrown.expect(EidIllegalArgumentException.class); + thrown.expectMessage(containsString("20160329:132823|EID-NULL")); + // when + EidPreconditions.checkArgument(expression, eidObject); + } + + @Test + public void testCheckArgument_boolean_String_Null() { + // given + boolean expression = falsyValue(); + String eidObject = nullyValue(); + // then + thrown.expect(EidIllegalArgumentException.class); + thrown.expectMessage(containsString("20160329:133052|EID-NULL")); // when EidPreconditions.checkArgument(expression, eidObject); } @@ -468,8 +581,109 @@ public void execute() { assertThat(procedure).isNotNull(); } - private Eid getNullEid() { - return nullyValue(); + @Test + public void testTryToExecute_UnsafeProcedure_Eid() { + // given + final String causeMessage = "An error occured while parsing JSON document at char 178"; + EidPreconditions.UnsafeProcedure procedure = new EidPreconditions.UnsafeProcedure() { + @Override + public void execute() throws ParseException { + throw new ParseException(causeMessage, 178); + } + }; + // then + thrown.expect(EidRuntimeException.class); + thrown.expectCause(isA(ParseException.class)); + thrown.expectCause(hasMessage(equalTo(causeMessage))); + // when + EidPreconditions.tryToExecute(procedure, new Eid(eid)); + } + + @Test + public void testTryToExecute_UnsafeProcedure_Eid_Ok() { + // given + EidPreconditions.UnsafeProcedure procedure = new EidPreconditions.UnsafeProcedure() { + @Override + public void execute() { + // nothing special here, for unit test + } + }; + // when + EidPreconditions.tryToExecute(procedure, new Eid(eid)); + // then + assertThat(procedure).isNotNull(); + } + + @Test + public void testTryToExecute_UnsafeSupplier_String() { + // given + final String causeMessage = "An error occured while parsing JSON document at char 178"; + EidPreconditions.UnsafeSupplier supplier = new EidPreconditions.UnsafeSupplier() { + + @Override + public String get() throws ParseException { + throw new ParseException(causeMessage, 178); + } + }; + // then + thrown.expect(EidRuntimeException.class); + thrown.expectCause(isA(ParseException.class)); + thrown.expectCause(hasMessage(equalTo(causeMessage))); + // when + EidPreconditions.tryToExecute(supplier, eid); + } + + @Test + public void testTryToExecute_UnsafeSupplier_String_Ok() { + // given + final String returning = "An answer to universe and everything"; + EidPreconditions.UnsafeSupplier supplier = new EidPreconditions.UnsafeSupplier() { + @Override + public String get() throws ParseException { + return returning; + } + }; + // when + String answer = EidPreconditions.tryToExecute(supplier, eid); + // then + assertThat(supplier).isNotNull(); + assertThat(answer).isNotNull().isNotEmpty().isEqualTo(returning); + } + + @Test + public void testTryToExecute_UnsafeSupplier_Eid() { + // given + final String causeMessage = "An error occured while parsing JSON document at char 178"; + EidPreconditions.UnsafeSupplier supplier = new EidPreconditions.UnsafeSupplier() { + + @Override + public String get() throws ParseException { + throw new ParseException(causeMessage, 178); + } + }; + // then + thrown.expect(EidRuntimeException.class); + thrown.expectCause(isA(ParseException.class)); + thrown.expectCause(hasMessage(equalTo(causeMessage))); + // when + EidPreconditions.tryToExecute(supplier, new Eid(eid)); + } + + @Test + public void testTryToExecute_UnsafeSupplier_Eid_Ok() { + // given + final String returning = "An answer to universe and everything"; + EidPreconditions.UnsafeSupplier supplier = new EidPreconditions.UnsafeSupplier() { + @Override + public String get() throws ParseException { + return returning; + } + }; + // when + String answer = EidPreconditions.tryToExecute(supplier, new Eid(eid)); + // then + assertThat(supplier).isNotNull(); + assertThat(answer).isNotNull().isNotEmpty().isEqualTo(returning); } @Nonnull