/*
 * Copyright 2013 the original author or authors.
 *
 * 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 com.developerb.nmxmlp;

import com.google.common.io.ByteSource;
import org.junit.Test;

import static com.google.common.base.Charsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;

public class XmlLoadingTest extends AbstractNXTest {

    @Test
    public void loadInvalidXML() throws Exception {
        try {
            parse("<root><unclosedTag></root>");

            fail("Should not have accepted invalid xml");
        }
        catch (NX.Ex ex) {
            assertThat(ex)
                    .as("Expected exception")
                    .hasMessage("Failed to initialize xml cursor");
        }
    }

    @Test
    public void loadInvalidXMLFromBytes() throws Exception {
        try {
            parse(ByteSource.wrap("<root><unclosedTag></root>".getBytes(UTF_8)));

            fail("Should not have accepted invalid xml");
        }
        catch (NX.Ex ex) {
            assertThat(ex)
                    .as("Expected exception")
                    .hasMessage("Failed to initialize xml cursor");
        }
    }

    @Test
    public void readingNodeWithDuplicateAttributeTriggersException() {
        try {
            parse("<people><person name='First' name='Second' /></people>");
        }
        catch (NX.Ex ex) {
            assertThat(ex.getCause())
                    .as("Expected exception")
                    .hasMessageContaining("Attribute \"name\" was already specified for element \"person\"");
        }
    }

    @Test
    public void loadValidXML() throws Exception {
        NX.Cursor cursor = parse("<root><a /></root>");

        assertNotNull(cursor);
        assertEquals("root", cursor.name());
    }

}
@@ -690,22 +690,178 @@
 ort 
-org.junit.Test
+com.google.common.io.Resources;%0Aimport org.junit.Test;%0A%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL
 ;%0A%0Ai
@@ -2479,9 +2479,742 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void failingToReadFromByteSource() throws MalformedURLException %7B%0A        try %7B%0A            parse(new ByteSource() %7B%0A%0A                @Override%0A                public InputStream openStream() throws IOException %7B%0A                    throw new IllegalStateException(%22Oups...%22);%0A                %7D%0A%0A            %7D);%0A        %7D%0A        catch (NX.Ex ex) %7B%0A            assertThat(ex)%0A                    .as(%22Expected exception%22)%0A                    .hasMessage(%22Failed to initialize xml cursor%22);%0A%0A            assertThat(ex.getCause())%0A                    .as(%22Cause of the expected exception%22)%0A                    .isInstanceOf(IllegalStateException.class)%0A                    .hasMessage(%22Oups...%22);%0A        %7D%0A    %7D%0A%0A
 %7D

Test - Failing to read from ByteSource