1919
2020import java .io .FileNotFoundException ;
2121
22- import javax .xml .xpath .XPathExpressionException ;
23-
2422import org .apache .camel .ContextTestSupport ;
2523import org .apache .camel .Exchange ;
24+ import org .apache .camel .NoTypeConversionAvailableException ;
25+ import org .apache .camel .RuntimeCamelException ;
26+ import org .apache .camel .TypeConversionException ;
2627import org .apache .camel .converter .jaxp .XmlConverter ;
28+ import org .xml .sax .SAXParseException ;
2729
2830import static org .apache .camel .builder .xml .XPathBuilder .xpath ;
2931
@@ -32,18 +34,19 @@ public class XPathFeatureTest extends ContextTestSupport {
3234
3335 public static final String XML_DATA = " <!DOCTYPE foo [ "
3436 + " <!ELEMENT foo ANY > <!ENTITY xxe SYSTEM \" file:///bin/test.sh\" >]> <test> &xxe; </test>" ;
35-
36-
37+ public static final String XML_DATA_INVALID = " <!DOCTYPE foo [ "
38+ + " <!ELEMENT foo ANY > <!ENTITY xxe SYSTEM \" file:///bin/test.sh\" >]> <test> &xxe; </test><notwellformed>" ;
39+
3740 @ Override
3841 public boolean isUseRouteBuilder () {
3942 return false ;
4043 }
41-
44+
4245 public void testXPathResult () throws Exception {
4346 String result = (String )xpath ("/" ).stringResult ().evaluate (createExchange (XML_DATA ));
4447 assertEquals ("Get a wrong result" , " " , result );
4548 }
46-
49+
4750 public void testXPath () throws Exception {
4851
4952 // Set this feature will enable the external general entities
@@ -52,16 +55,35 @@ public void testXPath() throws Exception {
5255 try {
5356 xpath ("/" ).stringResult ().evaluate (createExchange (XML_DATA ));
5457 fail ("Expect an Exception here" );
55- } catch (Exception ex ) {
56- assertTrue ("Get a wrong exception cause." , ex instanceof InvalidXPathExpression );
57- assertTrue ("Get a wrong exception cause." , ex .getCause () instanceof XPathExpressionException );
58+ } catch (TypeConversionException ex ) {
59+ assertTrue ("Get a wrong exception cause." , ex .getCause () instanceof RuntimeCamelException );
5860 assertTrue ("Get a wrong exception cause." , ex .getCause ().getCause () instanceof FileNotFoundException );
5961 } finally {
6062 System .clearProperty (DOM_BUILER_FACTORY_FEATRUE + ":"
6163 + "http://xml.org/sax/features/external-general-entities" );
6264 }
6365 }
64-
66+
67+ public void testXPathNoTypeConverter () throws Exception {
68+ try {
69+ // define a class without type converter as document type
70+ xpath ("/" ).documentType (Exchange .class ).stringResult ().evaluate (createExchange (XML_DATA ));
71+ fail ("Expect an Exception here" );
72+ } catch (RuntimeCamelException ex ) {
73+ assertTrue ("Get a wrong exception cause." , ex .getCause () instanceof NoTypeConversionAvailableException );
74+ }
75+ }
76+
77+ public void testXPathResultOnInvalidData () throws Exception {
78+ try {
79+ xpath ("/" ).stringResult ().evaluate (createExchange (XML_DATA_INVALID ));
80+ fail ("Expect an Exception here" );
81+ } catch (TypeConversionException ex ) {
82+ assertTrue ("Get a wrong exception cause." , ex .getCause () instanceof RuntimeCamelException );
83+ assertTrue ("Get a wrong exception cause." , ex .getCause ().getCause () instanceof SAXParseException );
84+ }
85+ }
86+
6587 protected Exchange createExchange (Object xml ) {
6688 Exchange exchange = createExchangeWithBody (context , xml );
6789 return exchange ;
0 commit comments