Skip to content

Commit

Permalink
Undo: Use assertThrows method for asserting exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Apr 24, 2024
1 parent 28c80e1 commit ae4f477
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.junit.Assert;
import org.junit.Test;

public class GuardTest {


@Test
public void canCastShouldCast() {
Object input = "Hello world!";
Expand All @@ -40,21 +41,25 @@ public void canCastShouldCastToSuperclass() {

@Test
public void canCastShouldNotMiscast() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
Object input = new ArrayList<File>();
Guard.canCast("input", input, String.class);
});
assertEquals("java.util.ArrayList 'input' is not a (subclass of) java.lang.String", e.getMessage());
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() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
long dice = 0;
Guard.isInRange("dice", dice, 1, 6);
});
assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage());
}
}


Expand All @@ -78,11 +83,13 @@ public void isInRangeShouldNotFailOnUpperBound() {

@Test
public void isInRangeShouldFailAboveUpperBound() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
long dice = 7;
Guard.isInRange("dice", dice, 1, 6);
});
assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage());
}
}

@Test
Expand All @@ -93,11 +100,13 @@ public void isNotNullShouldNotFailForInitializedObjekt() {

@Test
public void isNotNullShouldFailForNullObjekt() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
Object object = null;
Guard.isNotNull("object", object);
});
assertEquals("'object' must not be null", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'object' must not be null", e.getMessage());
}
}

@Test
Expand All @@ -114,20 +123,24 @@ public void isPositiveDoubleShouldNotFailForPositiveBoundary() {

@Test
public void isPositiveDoubleShouldFailForZero() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
double value = 0;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: 0.0 not > 0", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'value' out of range: 0.0 not > 0", e.getMessage());
}
}

@Test
public void isPositiveDoubleShouldFailForNegative() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
double value = -299_792_458;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage());
}
}

@Test
Expand All @@ -144,19 +157,23 @@ public void isPositiveLongShouldNotFailForPositiveLowerBound() {

@Test
public void isPositiveLongShouldFailForZero() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
try {
long value = 0;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: 0 not > 0", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'value' out of range: 0 not > 0", e.getMessage());
}
}

@Test
public void isPositiveLongShouldFailForNegative() {
IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> {
long value = Long.MAX_VALUE + 1; // negative due to integer overflow
try {
long value = Long.MAX_VALUE + 1; // is negative due to integer overflow
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage());
fail("should have throws IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage());
}
}
}

0 comments on commit ae4f477

Please sign in to comment.