/*
 * Copyright (C) 2021 Ignite Realtime Foundation. All rights reserved.
 *
 * Licensed 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.jivesoftware.util;

import org.dom4j.Document;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Verifies the implementation of {@link SAXReaderUtil}
 *
 * @author Guus der Kinderen, guus.der.kinderen@gmail.com
 */
public class SAXReaderUtilTest
{
    /**
     * Asserts that XML text can be parsed when provided as a String.
     */
    @Test
    public void testString() throws Exception
    {
        // Setup test fixture.
        final String input = "<foo><bar>test</bar></foo>";

        // Execute system under test.
        final Document output = SAXReaderUtil.readDocument(input);

        // Verify result.
        assertNotNull(output);
        assertEquals("foo", output.getRootElement().getName());
        assertNotNull(output.getRootElement().element("bar"));
        assertEquals("test", output.getRootElement().elementText("bar"));
    }

    /**
     * Asserts that XML text can be parsed when provided as a Reader.
     */
    @Test
    public void testReader() throws Exception
    {
        // Setup test fixture.
        final Reader input = new StringReader("<foo><bar>test</bar></foo>");

        // Execute system under test.
        final Document output = SAXReaderUtil.readDocument(input);

        // Verify result.
        assertNotNull(output);
        assertEquals("foo", output.getRootElement().getName());
        assertNotNull(output.getRootElement().element("bar"));
        assertEquals("test", output.getRootElement().elementText("bar"));
    }

    /**
     * Asserts that XML text can be parsed when provided as an InputStream.
     */
    @Test
    public void testInputStream() throws Exception
    {
        // Setup test fixture.
        final InputStream input =  new ByteArrayInputStream("<foo><bar>test</bar></foo>".getBytes(StandardCharsets.UTF_8));

        // Execute system under test.
        final Document output = SAXReaderUtil.readDocument(input);

        // Verify result.
        assertNotNull(output);
        assertEquals("foo", output.getRootElement().getName());
        assertNotNull(output.getRootElement().element("bar"));
        assertEquals("test", output.getRootElement().elementText("bar"));
    }
}

@@ -688,16 +688,52 @@
 cument;%0A
+import org.dom4j.DocumentException;%0A
 import o
@@ -911,53 +911,56 @@
 ts;%0A
-%0A
 import 
-static org.junit.Assert.assertEquals;
+java.util.concurrent.ExecutionException;%0A
 %0Aimp
@@ -987,29 +987,17 @@
 .Assert.
-assertNotNull
+*
 ;%0A%0A/**%0A 
@@ -2610,17 +2610,16 @@
  input =
- 
  new Byt
@@ -3070,11 +3070,1115 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Without a ExecutorService, problems during parsing/reading data would cause a DocumentException. By executing%0A     * this implementation through a Callable, these Exceptions are wrapped in a ExecutorException. This test verifies%0A     * that a DocumentException, caused by a problem parsing XML, remains accessible through the ExecutorException that%0A     * is thrown.%0A     */%0A    @Test%0A    public void testDocumentException() throws Exception%0A    %7B%0A        // Setup test fixture.%0A        final InputStream input = new ByteArrayInputStream(%22this is not valid XML%22.getBytes(StandardCharsets.UTF_8));%0A%0A        final ExecutionException result;%0A        try %7B%0A            // Execute system under test.%0A            SAXReaderUtil.readDocument(input);%0A            fail(%22An ExecutionException should have been thrown.%22);%0A            return;%0A        %7D catch (ExecutionException e) %7B%0A            result = e;%0A        %7D%0A%0A        // Verify result.%0A        assertNotNull(result);%0A        assertNotNull(result.getCause());%0A        assertEquals(DocumentException.class, result.getCause().getClass());%0A    %7D
 %0A%7D%0A

Add test that verifies exception handling