Skip to content

Commit

Permalink
Undo: Restore original test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Apr 24, 2024
1 parent ae4f477 commit 1b2e5f2
Showing 1 changed file with 96 additions and 80 deletions.
176 changes: 96 additions & 80 deletions Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,116 +24,129 @@

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<File>();
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);
fail("should have throws IllegalArgumentException");
} 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);
Expand All @@ -144,32 +157,35 @@ 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);
fail("should have throws IllegalArgumentException");
} 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) {
Expand Down

0 comments on commit 1b2e5f2

Please sign in to comment.