Skip to content

Commit 7a3b96f

Browse files
authored
Merge pull request #215 from strangelookingnerd/improve_assertions
Replace try-catch constructs in tests with assertThrows
2 parents 51ce0f8 + 1c92047 commit 7a3b96f

13 files changed

+71
-175
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.jxpath.issues;
1818

1919
import java.io.StringReader;
20+
import java.util.List;
2021

2122
import javax.xml.parsers.DocumentBuilder;
2223
import javax.xml.parsers.DocumentBuilderFactory;
@@ -28,6 +29,9 @@
2829
import org.w3c.dom.Document;
2930
import org.xml.sax.InputSource;
3031

32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
3135
public class JXPath113Test extends AbstractJXPathTest
3236
{
3337

@@ -36,7 +40,10 @@ public void testIssue113() throws Exception
3640
{
3741
final Document doc = JAXP.getDocument("<xml/>");
3842
final JXPathContext context = JXPathContext.newContext(doc);
39-
context.selectNodes("//following-sibling::node()");
43+
44+
List result = context.selectNodes("//following-sibling::node()");
45+
assertNotNull(result);
46+
assertTrue(result.isEmpty());
4047
}
4148

4249
static class JAXP
@@ -54,21 +61,12 @@ public static Document getDocument(final InputSource is) throws Exception
5461
return builder.parse(is);
5562
}
5663

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-
64+
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
65+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
66+
factory.setValidating(false);
67+
factory.setNamespaceAware(true);
68+
factory.setExpandEntityReferences(false);
69+
return factory.newDocumentBuilder();
7270
}
7371
}
7472

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testInit() {
3333
JXPathContextReferenceImpl.addNodePointerFactory(factory);
3434
} finally {
3535
while (JXPathContextReferenceImpl.removeNodePointerFactory(factory)) {
36-
36+
// NOP
3737
}
3838
}
3939
}

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(

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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.junit.jupiter.api.Test;
3737

3838
import static org.junit.jupiter.api.Assertions.assertEquals;
39-
import static org.junit.jupiter.api.Assertions.assertTrue;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
4040

4141
/**
4242
* Tests JXPath with mixed model: beans, maps, DOM etc.
@@ -453,14 +453,8 @@ public void testErrorProperty() {
453453
"e",
454454
new ExceptionPropertyTestBean());
455455

456-
boolean ex = false;
457-
try {
458-
assertXPathValue(context, "$e/errorString", null);
459-
}
460-
catch (final Throwable t) {
461-
ex = true;
462-
}
463-
assertTrue(ex, "Legitimate exception accessing property");
456+
assertThrows(Throwable.class, () -> assertXPathValue(context, "$e/errorString", null),
457+
"Legitimate exception accessing property");
464458

465459
assertXPathPointer(context, "$e/errorString", "$e/errorString");
466460

@@ -510,23 +504,11 @@ public void testMatrix() {
510504
Integer.valueOf(2),
511505
"$wholebean/matrix[1]/.[1]");
512506

513-
boolean ex = false;
514-
try {
515-
context.setValue("$wholebean/matrix[1]/.[2]", "4");
516-
}
517-
catch (final Exception e) {
518-
ex = true;
519-
}
520-
assertTrue(ex, "Exception setting value of non-existent element");
507+
assertThrows(Exception.class, () -> context.setValue("$wholebean/matrix[1]/.[2]", "4"),
508+
"Exception setting value of non-existent element");
521509

522-
ex = false;
523-
try {
524-
context.setValue("$wholebean/matrix[2]/.[1]", "4");
525-
}
526-
catch (final Exception e) {
527-
ex = true;
528-
}
529-
assertTrue(ex, "Exception setting value of non-existent element");
510+
assertThrows(Exception.class, () -> context.setValue("$wholebean/matrix[2]/.[1]", "4"),
511+
"Exception setting value of non-existent element");
530512
}
531513

532514
@Test

src/test/java/org/apache/commons/jxpath/ri/model/beans/BadlyImplementedFactoryTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030

3131
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
32-
import static org.junit.jupiter.api.Assertions.fail;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
3333

3434
/**
3535
* Badly-implemented Factory test. From JIRA JXPATH-68.
@@ -52,12 +52,9 @@ public boolean createObject(final JXPathContext context, final Pointer pointer,
5252

5353
@Test
5454
public void testBadFactoryImplementation() {
55-
try {
56-
context.createPath("foo/bar");
57-
fail("should fail with JXPathException caused by JXPathAbstractFactoryException");
58-
} catch (final JXPathException e) {
59-
assertInstanceOf(JXPathAbstractFactoryException.class, e.getCause());
60-
}
55+
JXPathException e = assertThrows(JXPathException.class, () -> context.createPath("foo/bar"),
56+
"should fail with JXPathException caused by JXPathAbstractFactoryException");
57+
assertInstanceOf(JXPathAbstractFactoryException.class, e.getCause());
6158
}
6259

6360
}

0 commit comments

Comments
 (0)