Skip to content

Commit

Permalink
Remove usage of assertDoesNotThrow()
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd committed Jan 10, 2025
1 parent 423ca0e commit 1c92047
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class JXPathContextReferenceImplTestCase {

/**
Expand All @@ -31,10 +29,12 @@ public class JXPathContextReferenceImplTestCase {
@Test
public void testInit() {
final ContainerPointerFactory factory = new ContainerPointerFactory();
assertDoesNotThrow(() -> JXPathContextReferenceImpl.addNodePointerFactory(factory));

while (JXPathContextReferenceImpl.removeNodePointerFactory(factory)) {
// NOP
try {
JXPathContextReferenceImpl.addNodePointerFactory(factory);
} finally {
while (JXPathContextReferenceImpl.removeNodePointerFactory(factory)) {
// NOP
}
}
}
}
23 changes: 18 additions & 5 deletions src/test/java/org/apache/commons/jxpath/ri/StressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Test thread safety.
Expand All @@ -32,6 +32,7 @@ public class StressTest {
private static final int THREAD_DURATION = 1000;
private static JXPathContext context;
private static int count;
private static Throwable exception;

@Test
public void testThreads() throws Throwable {
Expand All @@ -46,16 +47,25 @@ public void testThreads() throws Throwable {
}

for (final Thread element : threadArray) {
assertDoesNotThrow(() -> element.join(), "Interrupted");
try {
element.join();
}
catch (final InterruptedException e) {
fail("Interrupted");
}
}

if (exception != null) {
throw exception;
}
assertEquals(THREAD_COUNT * THREAD_DURATION, count, "Test count");
}

private static final class StressRunnable implements Runnable {
@Override
public void run() {
for (int j = 0; j < THREAD_DURATION; j++) {
assertDoesNotThrow(() -> {
for (int j = 0; j < THREAD_DURATION && exception == null; j++) {
try {
final double random = 1 + Math.random();
final double sum =
((Double) context.getValue("/ + " + random))
Expand All @@ -64,7 +74,10 @@ public void run() {
synchronized (context) {
count++;
}
});
}
catch (final Throwable t) {
exception = t;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -125,8 +124,13 @@ public static void callExampleMessageMethodAndAssertClassNotFoundJXPathException
*/
public static void callExampleMessageMethodAndAssertSuccess() {
final JXPathContext context = JXPathContext.newContext(new Object());
Object value = assertDoesNotThrow(() -> context.selectSingleNode(EXAMPLE_CLASS_NAME+".getMessage()"));
assertEquals("an example class", value);
Object value;
try {
value = context.selectSingleNode(EXAMPLE_CLASS_NAME+".getMessage()");
assertEquals("an example class", value);
} catch ( final Exception e ) {
fail(e.getMessage());
}
}

/**
Expand All @@ -138,8 +142,18 @@ public static void callExampleMessageMethodAndAssertSuccess() {
* to invoke.
*/
private void executeTestMethodUnderClassLoader(final ClassLoader cl, final String methodName) {
Class testClass = assertDoesNotThrow(() -> cl.loadClass(TEST_CASE_CLASS_NAME));
Method testMethod = assertDoesNotThrow(() -> testClass.getMethod(methodName, null));
Class testClass = null;
try {
testClass = cl.loadClass(TEST_CASE_CLASS_NAME);
} catch (final ClassNotFoundException e) {
fail(e.getMessage());
}
Method testMethod = null;
try {
testMethod = testClass.getMethod(methodName, null);
} catch (final SecurityException | NoSuchMethodException e) {
fail(e.getMessage());
}

try {
testMethod.invoke(null, null);
Expand Down

0 comments on commit 1c92047

Please sign in to comment.