Skip to content

Commit

Permalink
Use Jupiter API for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed May 13, 2024
1 parent 9bf5b65 commit 766f485
Showing 1 changed file with 66 additions and 44 deletions.
110 changes: 66 additions & 44 deletions Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,182 @@
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
* It is licensed under GNU General License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

import org.junit.Test;

public class GuardTest {
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class GuardTest {
@Test
public void canCastShouldCast() {
@Tag("canCast")
void canCastShouldCast() {
Object input = "Hello world!";
String greet = Guard.canCast("input", input, String.class);
assertTrue("should return String", greet instanceof String);
assertTrue(greet instanceof String, "should return String");
}

@Test
public void canCastShouldCastToSuperclass() {
@Tag("canCast")
void canCastShouldCastToSuperclass() {
Object input = new GregorianCalendar();
Calendar calendar = Guard.canCast("input", input, Calendar.class);
assertTrue("should return Calendar", calendar instanceof Calendar);
assertTrue(calendar instanceof Calendar, "should return Calendar");
}

@Test
public void canCastShouldNotMiscast() {
@Tag("canCast")
void canCastShouldNotMiscast() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
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());
}, "should throw IllegalArgumentException");
assertEquals("java.util.ArrayList 'input' is not a (subclass of) java.lang.String", e.getMessage(),
"should provide an understandable description");
}


@Test
public void isInRangeShouldFailBelowLowerBound() {
@Tag("isInRange")
void isInRangeShouldFailBelowLowerBound() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
long dice = 0;
Guard.isInRange("dice", dice, 1, 6);
});
assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage());
assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage(),
"should provide an understandable description");
}


@Test
public void isInRangeShouldNotFailOnLowerBound() {
@Tag("isInRange")
void isInRangeShouldNotFailOnLowerBound() {
long dice = 1;
Guard.isInRange("dice", dice, 1, 6);
}

@Test
public void isInRangeShouldNotFailInRange() {
@Tag("isInRange")
void isInRangeShouldNotFailInRange() {
long dice = 3;
Guard.isInRange("dice", dice, 1, 6);
}

@Test
public void isInRangeShouldNotFailOnUpperBound() {
@Tag("isInRange")
void isInRangeShouldNotFailOnUpperBound() {
long dice = 6;
Guard.isInRange("dice", dice, 1, 6);
}

@Test
public void isInRangeShouldFailAboveUpperBound() {
@Tag("isInRange")
void isInRangeShouldFailAboveUpperBound() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
long dice = 7;
Guard.isInRange("dice", dice, 1, 6);
});
assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage(),
"should provide an understandable description");
}

@Test
public void isNotNullShouldNotFailForInitializedObjekt() {
@Tag("isNotNull")
void isNotNullShouldNotFailForInitializedObjekt() {
Object object = "Hello world!";
Guard.isNotNull("object", object);
}

@Test
public void isNotNullShouldFailForNullObjekt() {
@Tag("isNotNull")
void isNotNullShouldFailForNullObjekt() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
Object object = null;
Guard.isNotNull("object", object);
});
assertEquals("'object' must not be null", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'object' must not be null", e.getMessage(), "should provide an understandable description");
}

@Test
public void isPositiveDoubleShouldNotFailForPositiveValue() {
@Tag("isPositive(double)")
void isPositiveDoubleShouldNotFailForPositiveValue() {
double value = 42;
Guard.isPositive("value", value);
}

@Test
public void isPositiveDoubleShouldNotFailForPositiveBoundary() {
@Tag("isPositive(double)")
void isPositiveDoubleShouldNotFailForPositiveBoundary() {
double value = Double.MIN_VALUE;
Guard.isPositive("value", value);
}

@Test
public void isPositiveDoubleShouldFailForZero() {
@Tag("isPositive(double)")
void isPositiveDoubleShouldFailForZero() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
double value = 0;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: 0.0 not > 0", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'value' out of range: 0.0 not > 0", e.getMessage(),
"should provide an understandable description");
}

@Test
public void isPositiveDoubleShouldFailForNegative() {
@Tag("isPositive(double)")
void isPositiveDoubleShouldFailForNegative() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
double value = -299_792_458;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage(),
"should provide an understandable description");
}

@Test
public void isPositiveLongShouldNotFailForPositiveUpperBound() {
@Tag("isPositive(long)")
void isPositiveLongShouldNotFailForPositiveUpperBound() {
long value = Long.MAX_VALUE;
Guard.isPositive("value", value);
}

@Test
public void isPositiveLongShouldNotFailForPositiveLowerBound() {
@Tag("isPositive(long)")
void isPositiveLongShouldNotFailForPositiveLowerBound() {
long value = 1;
Guard.isPositive("value", value);
}

@Test
public void isPositiveLongShouldFailForZero() {
@Tag("isPositive(long)")
void isPositiveLongShouldFailForZero() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
long value = 0;
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: 0 not > 0", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'value' out of range: 0 not > 0", e.getMessage(), "should provide an understandable description");
}

@Test
public void isPositiveLongShouldFailForNegative() {
@Tag("isPositive(long)")
void isPositiveLongShouldFailForNegative() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
long value = Long.MAX_VALUE + 1; // negative due to integer overflow
Guard.isPositive("value", value);
});
assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage());
}, "should throw IllegalArgumentException");
assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage(),
"should provide an understandable description");
}
}

0 comments on commit 766f485

Please sign in to comment.