Skip to content

Commit 24bf0e9

Browse files
Replace try-catch constructs in tests with assertThrows and assertDoesNotThrow
1 parent c72661e commit 24bf0e9

14 files changed

+81
-216
lines changed

src/test/java/org/apache/commons/jxpath/issues/JXPath113Test.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.w3c.dom.Document;
2929
import org.xml.sax.InputSource;
3030

31+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
32+
3133
public class JXPath113Test extends AbstractJXPathTest
3234
{
3335

@@ -36,7 +38,8 @@ public void testIssue113() throws Exception
3638
{
3739
final Document doc = JAXP.getDocument("<xml/>");
3840
final JXPathContext context = JXPathContext.newContext(doc);
39-
context.selectNodes("//following-sibling::node()");
41+
42+
assertDoesNotThrow(() -> context.selectNodes("//following-sibling::node()"));
4043
}
4144

4245
static class JAXP
@@ -54,21 +57,12 @@ public static Document getDocument(final InputSource is) throws Exception
5457
return builder.parse(is);
5558
}
5659

57-
private static DocumentBuilder getDocumentBuilder()
58-
{
59-
try
60-
{
61-
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62-
factory.setValidating(false);
63-
factory.setNamespaceAware(true);
64-
factory.setExpandEntityReferences(false);
65-
return factory.newDocumentBuilder();
66-
}
67-
catch (final ParserConfigurationException e)
68-
{
69-
throw new Error("JAXP config error:" + e.getMessage(), e);
70-
}
71-
60+
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
61+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62+
factory.setValidating(false);
63+
factory.setNamespaceAware(true);
64+
factory.setExpandEntityReferences(false);
65+
return factory.newDocumentBuilder();
7266
}
7367
}
7468

src/test/java/org/apache/commons/jxpath/issues/JXPath172Test.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertNotNull;
3030
import static org.junit.jupiter.api.Assertions.assertNull;
31-
import static org.junit.jupiter.api.Assertions.fail;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3232

3333
public class JXPath172Test extends AbstractJXPathTest
3434
{
@@ -90,36 +90,10 @@ public void testIssue172_NestedPropertyUnexisting()
9090
public void testIssue172_propertyDoesNotExist_NotLenient()
9191
{
9292
final JXPathContext context = getContext(null, false);
93-
try
94-
{
95-
final Object bRet = context.selectSingleNode("unexisting");
96-
fail("" + bRet);
97-
}
98-
catch (final JXPathNotFoundException e)
99-
{
100-
101-
}
102-
103-
try
104-
{
105-
final Pointer pointer = context.getPointer("unexisting");
106-
fail(" " + pointer);
107-
}
108-
catch (final JXPathNotFoundException e)
109-
{
110-
111-
}
112-
113-
try
114-
{
115-
final Pointer pointer = context.getPointer("value.unexisting");
116-
fail(" " + pointer);
117-
}
118-
catch (final JXPathNotFoundException e)
119-
{
120-
121-
}
12293

94+
assertThrows(JXPathNotFoundException.class, () -> context.selectSingleNode("unexisting"));
95+
assertThrows(JXPathNotFoundException.class, () -> context.getPointer("unexisting"));
96+
assertThrows(JXPathNotFoundException.class, () -> context.getPointer("value.unexisting"));
12397
}
12498

12599
/**

src/test/java/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323

24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.api.Assertions.fail;
2526

2627
/**
@@ -57,34 +58,30 @@ public Object getFoo() {
5758

5859
@Test
5960
public void testHandleFoo() throws Exception {
60-
try {
61-
context.getValue("foo");
62-
fail("expected Throwable");
63-
} catch (Throwable t) {
64-
while (t != null) {
65-
if ("foo unavailable".equals(t.getMessage())) {
66-
return;
67-
}
68-
t = t.getCause();
61+
Throwable t = assertThrows(Throwable.class, () -> context.getValue("foo"),
62+
"expected Throwable");
63+
64+
while (t != null) {
65+
if ("foo unavailable".equals(t.getMessage())) {
66+
return;
6967
}
70-
fail("expected \"foo unavailable\" in throwable chain");
68+
t = t.getCause();
7169
}
70+
fail("expected \"foo unavailable\" in throwable chain");
7271
}
7372

7473
@Test
7574
public void testHandleBarBaz() throws Exception {
76-
try {
77-
context.getValue("bar/baz");
78-
fail("expected Throwable");
79-
} catch (Throwable t) {
80-
while (t != null) {
81-
if ("baz unavailable".equals(t.getMessage())) {
82-
return;
83-
}
84-
t = t.getCause();
75+
Throwable t = assertThrows(Throwable.class, () -> context.getValue("bar/baz"),
76+
"expected Throwable");
77+
78+
while (t != null) {
79+
if ("baz unavailable".equals(t.getMessage())) {
80+
return;
8581
}
86-
fail("expected \"baz unavailable\" in throwable chain");
82+
t = t.getCause();
8783
}
84+
fail("expected \"baz unavailable\" in throwable chain");
8885
}
8986

9087
public Bar getBar() {

src/test/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImplTestCase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25+
2426
public class JXPathContextReferenceImplTestCase {
2527

2628
/**
@@ -29,12 +31,10 @@ public class JXPathContextReferenceImplTestCase {
2931
@Test
3032
public void testInit() {
3133
final ContainerPointerFactory factory = new ContainerPointerFactory();
32-
try {
33-
JXPathContextReferenceImpl.addNodePointerFactory(factory);
34-
} finally {
35-
while (JXPathContextReferenceImpl.removeNodePointerFactory(factory)) {
34+
assertDoesNotThrow(() -> JXPathContextReferenceImpl.addNodePointerFactory(factory));
3635

37-
}
36+
while (JXPathContextReferenceImpl.removeNodePointerFactory(factory)) {
37+
// NOP
3838
}
3939
}
4040
}

src/test/java/org/apache/commons/jxpath/ri/StressTest.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2324
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.fail;
2525

2626
/**
2727
* Test thread safety.
@@ -32,7 +32,6 @@ public class StressTest {
3232
private static final int THREAD_DURATION = 1000;
3333
private static JXPathContext context;
3434
private static int count;
35-
private static Throwable exception;
3635

3736
@Test
3837
public void testThreads() throws Throwable {
@@ -47,25 +46,16 @@ public void testThreads() throws Throwable {
4746
}
4847

4948
for (final Thread element : threadArray) {
50-
try {
51-
element.join();
52-
}
53-
catch (final InterruptedException e) {
54-
fail("Interrupted");
55-
}
56-
}
57-
58-
if (exception != null) {
59-
throw exception;
49+
assertDoesNotThrow(() -> element.join(), "Interrupted");
6050
}
6151
assertEquals(THREAD_COUNT * THREAD_DURATION, count, "Test count");
6252
}
6353

6454
private static final class StressRunnable implements Runnable {
6555
@Override
6656
public void run() {
67-
for (int j = 0; j < THREAD_DURATION && exception == null; j++) {
68-
try {
57+
for (int j = 0; j < THREAD_DURATION; j++) {
58+
assertDoesNotThrow(() -> {
6959
final double random = 1 + Math.random();
7060
final double sum =
7161
((Double) context.getValue("/ + " + random))
@@ -74,10 +64,7 @@ public void run() {
7464
synchronized (context) {
7565
count++;
7666
}
77-
}
78-
catch (final Throwable t) {
79-
exception = t;
80-
}
67+
});
8168
}
8269
}
8370
}

src/test/java/org/apache/commons/jxpath/ri/compiler/CoreFunctionTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,8 @@ public void testCoreFunctions() {
6363
assertXPathValue(context, "ends-with('xabc', 'ab')", Boolean.FALSE);
6464
assertXPathValue(context, "contains('xabc', 'ab')", Boolean.TRUE);
6565
assertXPathValue(context, "contains('xabc', 'ba')", Boolean.FALSE);
66-
assertXPathValue(
67-
context,
68-
"substring-before('1999/04/01', '/')",
69-
"1999");
70-
assertXPathValue(
71-
context,
72-
"substring-after('1999/04/01', '/')",
73-
"04/01");
66+
assertXPathValue(context,"substring-before('1999/04/01', '/')","1999");
67+
assertXPathValue(context,"substring-after('1999/04/01', '/')","04/01");
7468
assertXPathValue(context, "substring('12345', 2, 3)", "234");
7569
assertXPathValue(context, "substring('12345', 2)", "2345");
7670
assertXPathValue(context, "substring('12345', 1.5, 2.6)", "234");

src/test/java/org/apache/commons/jxpath/ri/compiler/VariableTest.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import static org.junit.jupiter.api.Assertions.assertEquals;
2727
import static org.junit.jupiter.api.Assertions.assertFalse;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
2929

3030
/**
3131
* Test basic functionality of JXPath - infoset types,
@@ -73,26 +73,10 @@ public void testVariablesInExpressions() {
7373

7474
@Test
7575
public void testInvalidVariableName() {
76-
boolean exception = false;
77-
try {
78-
context.getValue("$none");
79-
}
80-
catch (final Exception ex) {
81-
exception = true;
82-
}
83-
assertTrue(
84-
exception,
76+
assertThrows(Exception.class, () -> context.getValue("$none"),
8577
"Evaluating '$none', expected exception - did not get it");
8678

87-
exception = false;
88-
try {
89-
context.setValue("$none", Integer.valueOf(1));
90-
}
91-
catch (final Exception ex) {
92-
exception = true;
93-
}
94-
assertTrue(
95-
exception,
79+
assertThrows(Exception.class, () -> context.setValue("$none", Integer.valueOf(1)),
9680
"Setting '$none = 1', expected exception - did not get it");
9781
}
9882

src/test/java/org/apache/commons/jxpath/ri/model/AbstractBeanModelTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
import static org.junit.jupiter.api.Assertions.assertEquals;
4141
import static org.junit.jupiter.api.Assertions.assertNull;
42-
import static org.junit.jupiter.api.Assertions.assertTrue;
42+
import static org.junit.jupiter.api.Assertions.assertThrows;
4343

4444
/**
4545
* Abstract superclass for Bean access with JXPath.
@@ -827,19 +827,13 @@ public void testCreatePath() {
827827
Integer.valueOf(1),
828828
"/nestedBean/int");
829829

830-
boolean ex = false;
831-
try {
830+
assertThrows(Exception.class, () ->
832831
assertXPathCreatePath(
833832
context,
834833
"/nestedBean/beans[last() + 1]",
835834
Integer.valueOf(1),
836-
"/nestedBean/beans[last() + 1]");
837-
}
838-
catch (final Exception e) {
839-
ex = true;
840-
}
841-
assertTrue(ex, "Exception thrown on invalid path for creation");
842-
835+
"/nestedBean/beans[last() + 1]"),
836+
"Exception thrown on invalid path for creation");
843837
}
844838

845839
@Test

src/test/java/org/apache/commons/jxpath/ri/model/AbstractXMLModelTest.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
import static org.junit.jupiter.api.Assertions.assertEquals;
3030
import static org.junit.jupiter.api.Assertions.assertNotEquals;
31-
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
3232

3333
/**
3434
* Abstract superclass for pure XPath 1.0. Subclasses
@@ -340,25 +340,15 @@ public void testAxisChild() {
340340

341341
assertXPathValue(context, "/vendor/contact[@name='jim']", "Jim");
342342

343-
boolean nsv = false;
344-
try {
343+
assertThrows(JXPathException.class, () -> {
345344
context.setLenient(false);
346345
context.getValue("/vendor/contact[@name='jane']");
347-
}
348-
catch (final JXPathException ex) {
349-
nsv = true;
350-
}
351-
assertTrue(nsv, "No such value: /vendor/contact[@name='jim']");
346+
}, "No such value: /vendor/contact[@name='jim']");
352347

353-
nsv = false;
354-
try {
348+
assertThrows(JXPathException.class, () -> {
355349
context.setLenient(false);
356350
context.getValue("/vendor/contact[@name='jane']/*");
357-
}
358-
catch (final JXPathException ex) {
359-
nsv = true;
360-
}
361-
assertTrue(nsv, "No such value: /vendor/contact[@name='jane']/*");
351+
}, "No such value: /vendor/contact[@name='jane']/*");
362352

363353
// child:: with a wildcard
364354
assertXPathValue(

0 commit comments

Comments
 (0)