From 1b2e5f2ac6b1d527048bfe2273dd690164d20f1b Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Wed, 24 Apr 2024 10:46:10 +0200 Subject: [PATCH] Undo: Restore original test --- .../test/java/org/kitodo/utils/GuardTest.java | 176 ++++++++++-------- 1 file changed, 96 insertions(+), 80 deletions(-) diff --git a/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java b/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java index 237e77ca576..f433f231676 100644 --- a/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java +++ b/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java @@ -24,105 +24,120 @@ public class GuardTest { - @Test - public void canCastShouldCast() { - Object input = "Hello world!"; - String greet = Guard.canCast("input", input, String.class); - assertTrue("should return String", greet instanceof String); - } + public void canCastTest() { + // valid cast + try { + Object input = "Hello world!"; + String greet = Guard.canCast("input", input, String.class); + assertTrue("should return String", greet instanceof String); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void canCastShouldCastToSuperclass() { - Object input = new GregorianCalendar(); - Calendar calendar = Guard.canCast("input", input, Calendar.class); - assertTrue("should return Calendar", calendar instanceof Calendar); - } + // valid cast to superclass + try { + Object input = new GregorianCalendar(); + Calendar calendar = Guard.canCast("input", input, Calendar.class); + assertTrue("should return Calendar", calendar instanceof Calendar); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void canCastShouldNotMiscast() { + // invalid cast try { Object input = new ArrayList(); - Guard.canCast("input", input, String.class); + @SuppressWarnings("unused") String uncastable = Guard.canCast("input", input, String.class); fail("should have throws IllegalArgumentException"); } catch (IllegalArgumentException e) { assertEquals("java.util.ArrayList 'input' is not a (subclass of) java.lang.String", e.getMessage()); } } - @Test - public void isInRangeShouldFailBelowLowerBound() { + public void isInRangeTest() { + // input below range try { - long dice = 0; - Guard.isInRange("dice", dice, 1, 6); + long input = 0; + Guard.isInRange("input", input, 1, 6); fail("should have throws IllegalArgumentException"); } catch (IllegalArgumentException e) { - assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage()); + assertEquals("'input' out of range: 0 not in [1..6]", e.getMessage()); } - } - - @Test - public void isInRangeShouldNotFailOnLowerBound() { - long dice = 1; - Guard.isInRange("dice", dice, 1, 6); - } + // input OK (lower bound) + try { + long input = 1; + Guard.isInRange("input", input, 1, 6); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isInRangeShouldNotFailInRange() { - long dice = 3; - Guard.isInRange("dice", dice, 1, 6); - } + // input OK (middle) + try { + long input = 3; + Guard.isInRange("input", input, 1, 6); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isInRangeShouldNotFailOnUpperBound() { - long dice = 6; - Guard.isInRange("dice", dice, 1, 6); - } + // input OK (upper bound) + try { + long input = 6; + Guard.isInRange("input", input, 1, 6); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isInRangeShouldFailAboveUpperBound() { + // input above range try { - long dice = 7; - Guard.isInRange("dice", dice, 1, 6); + long input = 42; + Guard.isInRange("input", input, 1, 6); fail("should have throws IllegalArgumentException"); } catch (IllegalArgumentException e) { - assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage()); + assertEquals("'input' out of range: 42 not in [1..6]", e.getMessage()); } } @Test - public void isNotNullShouldNotFailForInitializedObjekt() { - Object object = "Hello world!"; - Guard.isNotNull("object", object); - } + public void isNotNullTest() { + // input is not null + try { + Object input = "Hello world!"; + Guard.isNotNull("input", input); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isNotNullShouldFailForNullObjekt() { + // input is null try { - Object object = null; - Guard.isNotNull("object", object); + Object input = null; + Guard.isNotNull("input", input); fail("should have throws IllegalArgumentException"); } catch (IllegalArgumentException e) { - assertEquals("'object' must not be null", e.getMessage()); + assertEquals("'input' must not be null", e.getMessage()); } } @Test - public void isPositiveDoubleShouldNotFailForPositiveValue() { - double value = 42; - Guard.isPositive("value", value); - } + public void isPositiveDoubleTest() { + // input is positive + try { + double value = 42; + Guard.isPositive("value", value); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isPositiveDoubleShouldNotFailForPositiveBoundary() { - double value = Double.MIN_VALUE; - Guard.isPositive("value", value); - } + // input is still positive, but very very tiny (boundary) + try { + double value = Double.MIN_VALUE; + Guard.isPositive("value", value); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isPositiveDoubleShouldFailForZero() { + // input is zero (boundary) try { double value = 0; Guard.isPositive("value", value); @@ -130,10 +145,8 @@ public void isPositiveDoubleShouldFailForZero() { } catch (IllegalArgumentException e) { assertEquals("'value' out of range: 0.0 not > 0", e.getMessage()); } - } - @Test - public void isPositiveDoubleShouldFailForNegative() { + // input is negative light speed try { double value = -299_792_458; Guard.isPositive("value", value); @@ -144,19 +157,24 @@ public void isPositiveDoubleShouldFailForNegative() { } @Test - public void isPositiveLongShouldNotFailForPositiveUpperBound() { - long value = Long.MAX_VALUE; - Guard.isPositive("value", value); - } + public void isPositiveLongTest() { + // input is positive + try { + long value = Long.MAX_VALUE; + Guard.isPositive("value", value); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isPositiveLongShouldNotFailForPositiveLowerBound() { - long value = 1; - Guard.isPositive("value", value); - } + // input is one (boundary) + try { + long value = 1; + Guard.isPositive("value", value); + } catch (IllegalArgumentException e) { + fail("should return without exception"); + } - @Test - public void isPositiveLongShouldFailForZero() { + // input is zero (boundary) try { long value = 0; Guard.isPositive("value", value); @@ -164,12 +182,10 @@ public void isPositiveLongShouldFailForZero() { } catch (IllegalArgumentException e) { assertEquals("'value' out of range: 0 not > 0", e.getMessage()); } - } - @Test - public void isPositiveLongShouldFailForNegative() { + // input is negative (due to integer overflow) try { - long value = Long.MAX_VALUE + 1; // is negative due to integer overflow + long value = Long.MAX_VALUE + 1; Guard.isPositive("value", value); fail("should have throws IllegalArgumentException"); } catch (IllegalArgumentException e) {