feat: uploaded versions for jack
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2021 Exercism
|
||||
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
||||
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ErrorHandlingTest {
|
||||
|
||||
private static final int MAX_POINTS = 100;
|
||||
private static final int TEST_CASES = 11;
|
||||
private static final int SUCCESS_POINTS = MAX_POINTS / TEST_CASES;
|
||||
private static final int FAILURE_POINTS = 0;
|
||||
|
||||
private final java.util.List<Integer> testPoints = new java.util.ArrayList<>();
|
||||
|
||||
@Test(name = "Throws IllegalArgumentException")
|
||||
public void testThrowIllegalArgumentException() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(Exception.class,
|
||||
() -> errorHandling.handleErrorByThrowingIllegalArgumentException(), false,
|
||||
null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws IllegalArgumentException with provided detail message")
|
||||
public void testThrowIllegalArgumentExceptionWithDetailMessage() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> errorHandling
|
||||
.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(
|
||||
"This is the detail message."),
|
||||
false, "This is the detail message.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws any checked exception")
|
||||
public void testThrowAnyCheckedException() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(Exception.class, () -> errorHandling.handleErrorByThrowingAnyCheckedException(),
|
||||
true, null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws any checked exception with provided detail message")
|
||||
public void testThrowAnyCheckedExceptionWithDetailMessage() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(Exception.class,
|
||||
() -> errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(
|
||||
"This is the detail message."),
|
||||
true, "This is the detail message.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws any unchecked exception")
|
||||
public void testThrowAnyUncheckedException() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> errorHandling.handleErrorByThrowingAnyUncheckedException(), false, null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws any unchecked exception with provided detail message")
|
||||
public void testThrowAnyUncheckedExceptionWithDetailMessage() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(RuntimeException.class,
|
||||
() -> errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(
|
||||
"This is the detail message."),
|
||||
false, "This is the detail message.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws custom checked exception")
|
||||
public void testThrowCustomCheckedException() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(CustomCheckedException.class,
|
||||
() -> errorHandling.handleErrorByThrowingCustomCheckedException(), false, null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws custom checked exception with provided detail message")
|
||||
public void testThrowCustomCheckedExceptionWithDetailMessage() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(CustomCheckedException.class,
|
||||
() -> errorHandling
|
||||
.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(
|
||||
"This is the detail message."),
|
||||
false, "This is the detail message.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws custom unchecked exception")
|
||||
public void testThrowCustomUncheckedException() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(CustomUncheckedException.class,
|
||||
() -> errorHandling.handleErrorByThrowingCustomUncheckedException(), false,
|
||||
null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Throws custom unchecked exception with provided detail message")
|
||||
public void testThrowCustomUncheckedExceptionWithDetailMessage() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
assertThrows(CustomUncheckedException.class,
|
||||
() -> errorHandling
|
||||
.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(
|
||||
"This is the detail message."),
|
||||
false, "This is the detail message.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test(name = "Handles error by throwing Optional instance")
|
||||
public void testReturnOptionalInstance() {
|
||||
runTest(() -> {
|
||||
ErrorHandling errorHandling = new ErrorHandling();
|
||||
Optional<Integer> successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1");
|
||||
assertOptionalPresentWithValue(successfulResult, 1);
|
||||
|
||||
Optional<Integer> failureResult = errorHandling.handleErrorByReturningOptionalInstance("a");
|
||||
assertOptionalNotPresent(failureResult);
|
||||
});
|
||||
}
|
||||
|
||||
private void runTest(Runnable body) {
|
||||
try {
|
||||
body.run();
|
||||
testPoints.add(SUCCESS_POINTS);
|
||||
} catch (Exception | AssertionError t) {
|
||||
TracingFramework.printError(t.getMessage());
|
||||
testPoints.add(FAILURE_POINTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertThrows(Class<? extends Throwable> expectedType, Runnable body, boolean notRuntime,
|
||||
String expectedMessage) {
|
||||
try {
|
||||
body.run();
|
||||
throw new AssertionError("Expected exception of type " + expectedType.getName()
|
||||
+ " but nothing was thrown");
|
||||
} catch (Exception t) {
|
||||
if (!expectedType.isAssignableFrom(t.getClass())) {
|
||||
throw new AssertionError("Expected exception of type " + expectedType.getName()
|
||||
+ " but was " + t.getClass().getName());
|
||||
}
|
||||
if (notRuntime && RuntimeException.class.isAssignableFrom(t.getClass())) {
|
||||
throw new AssertionError("Expected a checked exception but got runtime exception "
|
||||
+ t.getClass().getName());
|
||||
}
|
||||
if (expectedMessage != null
|
||||
&& (t.getMessage() == null || !t.getMessage().equals(expectedMessage))) {
|
||||
throw new AssertionError("Expected exception message '" + expectedMessage
|
||||
+ "' but was '" + t.getMessage() + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void assertOptionalPresentWithValue(Optional<Integer> opt, int expected) {
|
||||
if (!opt.isPresent())
|
||||
throw new AssertionError("Expected Optional to be present");
|
||||
if (!Integer.valueOf(expected).equals(opt.get()))
|
||||
throw new AssertionError("Expected Optional value " + expected + " but was " + opt.get());
|
||||
}
|
||||
|
||||
private void assertOptionalNotPresent(Optional<?> opt) {
|
||||
if (opt.isPresent())
|
||||
throw new AssertionError("Expected Optional to be empty");
|
||||
}
|
||||
|
||||
public int getResult() {
|
||||
int sum = testPoints.stream().mapToInt(Integer::intValue).sum();
|
||||
return sum / SUCCESS_POINTS == TEST_CASES ? MAX_POINTS : sum;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user