/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.builder.xml;

import java.io.FileNotFoundException;

import org.xml.sax.SAXParseException;

import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.TypeConversionException;
import org.apache.camel.converter.jaxp.XmlConverter;
import org.junit.Test;

import static org.apache.camel.builder.xml.XPathBuilder.xpath;

public class XPathFeatureTest extends ContextTestSupport {
    public static final String DOM_BUILDER_FACTORY_FEATURE = XmlConverter.DOCUMENT_BUILDER_FACTORY_FEATURE;
    
    public static final String XML_DATA = " <!DOCTYPE foo [ " 
        + " <!ELEMENT foo ANY > <!ENTITY xxe SYSTEM \"file:///bin/test.sh\" >]> <test> &xxe; </test>";
    public static final String XML_DATA_INVALID = " <!DOCTYPE foo [ " 
            + " <!ELEMENT foo ANY > <!ENTITY xxe SYSTEM \"file:///bin/test.sh\" >]> <test> &xxe; </test><notwellformed>";

    @Override
    public boolean isUseRouteBuilder() {
        return false;
    }

    @Test
    public void testXPathResult() throws Exception {
        String result = (String)xpath("/").stringResult().evaluate(createExchange(XML_DATA));
        assertEquals("Get a wrong result", "  ", result);
    }

    @Test
    public void testXPath() throws Exception {
        // Set this feature will enable the external general entities
        System.setProperty(DOM_BUILDER_FACTORY_FEATURE + ":"
            + "http://xml.org/sax/features/external-general-entities", "true");
        try {
            xpath("/").stringResult().evaluate(createExchange(XML_DATA));
            fail("Expect an Exception here");
        } catch (TypeConversionException ex) {
            assertTrue("Get a wrong exception cause: " + ex.getCause().getClass() + " instead of " + FileNotFoundException.class, ex.getCause() instanceof FileNotFoundException);
        } finally {
            System.clearProperty(DOM_BUILDER_FACTORY_FEATURE + ":"
                + "http://xml.org/sax/features/external-general-entities");
        }
    }

    @Test
    public void testXPathNoTypeConverter() throws Exception {
        try {
            // define a class without type converter as document type
            xpath("/").documentType(Exchange.class).stringResult().evaluate(createExchange(XML_DATA));
            fail("Expect an Exception here");
        } catch (RuntimeCamelException ex) {
            assertTrue("Get a wrong exception cause: " + ex.getCause().getClass() + " instead of " + NoTypeConversionAvailableException.class, ex.getCause() instanceof NoTypeConversionAvailableException);
        }
    }

    @Test
    public void testXPathResultOnInvalidData() throws Exception {
        try {
            xpath("/").stringResult().evaluate(createExchange(XML_DATA_INVALID));
            fail("Expect an Exception here");
        } catch (TypeConversionException ex) {
            assertTrue("Get a wrong exception cause: " + ex.getCause().getClass() + " instead of " + SAXParseException.class, ex.getCause() instanceof SAXParseException);
        }
    }

    protected Exchange createExchange(Object xml) {
        Exchange exchange = createExchangeWithBody(context, xml);
        return exchange;
    }

}

@@ -865,32 +865,137 @@
 tFoundException;
+%0Aimport java.lang.reflect.Constructor;%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;
 %0A%0Aimport org.xml
@@ -1306,16 +1306,86 @@
 verter;%0A
+import org.apache.camel.impl.converter.CoreStaticTypeConverterLoader;%0A
 import o
@@ -1865,32 +1865,32 @@
 OCTYPE foo %5B %22 %0A
-
             + %22 
@@ -1996,16 +1996,686 @@
 med%3E%22;%0A%0A
+    @Override%0A    public void setUp() throws Exception %7B%0A        resetCoreConverters();%0A        super.setUp();%0A    %7D%0A%0A    private void resetCoreConverters() throws Exception %7B%0A        Field field = CoreStaticTypeConverterLoader.class.getDeclaredField(%22INSTANCE%22);%0A        field.setAccessible(true);%0A        Field modifiersField = Field.class.getDeclaredField(%22modifiers%22);%0A        modifiersField.setAccessible(true);%0A        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);%0A        Constructor%3C?%3E cns = CoreStaticTypeConverterLoader.class.getDeclaredConstructor();%0A        cns.setAccessible(true);%0A        field.set(null, cns.newInstance());%0A    %7D%0A%0A
     @Ove

Fix failing test caused by using a static list of converters