/*
 * Copyright 2002-2018 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 org.springframework.http.codec.xml;

import java.util.Collections;
import javax.xml.stream.events.XMLEvent;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;

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

/**
 * @author Arjen Poutsma
 */
public class XmlEventDecoderTests extends AbstractDataBufferAllocatingTestCase {

	private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
			"<pojo>" +
			"<foo>foofoo</foo>" +
			"<bar>barbar</bar>" +
			"</pojo>";

	private XmlEventDecoder decoder = new XmlEventDecoder();

	@Test
	public void toXMLEventsAalto() {

		Flux<XMLEvent> events =
				this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());

		StepVerifier.create(events)
				.consumeNextWith(e -> assertTrue(e.isStartDocument()))
				.consumeNextWith(e -> assertStartElement(e, "pojo"))
				.consumeNextWith(e -> assertStartElement(e, "foo"))
				.consumeNextWith(e -> assertCharacters(e, "foofoo"))
				.consumeNextWith(e -> assertEndElement(e, "foo"))
				.consumeNextWith(e -> assertStartElement(e, "bar"))
				.consumeNextWith(e -> assertCharacters(e, "barbar"))
				.consumeNextWith(e -> assertEndElement(e, "bar"))
				.consumeNextWith(e -> assertEndElement(e, "pojo"))
				.expectComplete()
				.verify();
	}

	@Test
	public void toXMLEventsNonAalto() {
		decoder.useAalto = false;

		Flux<XMLEvent> events =
				this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());

		StepVerifier.create(events)
				.consumeNextWith(e -> assertTrue(e.isStartDocument()))
				.consumeNextWith(e -> assertStartElement(e, "pojo"))
				.consumeNextWith(e -> assertStartElement(e, "foo"))
				.consumeNextWith(e -> assertCharacters(e, "foofoo"))
				.consumeNextWith(e -> assertEndElement(e, "foo"))
				.consumeNextWith(e -> assertStartElement(e, "bar"))
				.consumeNextWith(e -> assertCharacters(e, "barbar"))
				.consumeNextWith(e -> assertEndElement(e, "bar"))
				.consumeNextWith(e -> assertEndElement(e, "pojo"))
				.consumeNextWith(e -> assertTrue(e.isEndDocument()))
				.expectComplete()
				.verify();
	}

	private static void assertStartElement(XMLEvent event, String expectedLocalName) {
		assertTrue(event.isStartElement());
		assertEquals(expectedLocalName, event.asStartElement().getName().getLocalPart());
	}

	private static void assertEndElement(XMLEvent event, String expectedLocalName) {
		assertTrue(event + " is no end element", event.isEndElement());
		assertEquals(expectedLocalName, event.asEndElement().getName().getLocalPart());
	}

	private static void assertCharacters(XMLEvent event, String expectedData) {
		assertTrue(event.isCharacters());
		assertEquals(expectedData, event.asCharacters().getData());
	}

}

@@ -910,53 +910,62 @@
 se;%0A
-%0A
 import 
-static org.junit.Assert.assertEquals;
+org.springframework.core.io.buffer.DataBuffer;%0A
 %0Aimp
@@ -992,26 +992,17 @@
 .Assert.
-assertTrue
+*
 ;%0A%0A/**%0A 
@@ -2887,24 +2887,858 @@
 rify();%0A%09%7D%0A%0A
+%09@Test%0A%09public void decodeErrorAalto() %7B%0A%09%09Flux%3CDataBuffer%3E source = Flux.just(stringBuffer(%22%3Cpojo%3E%22))%0A%09%09%09%09.concatWith(Flux.error(new RuntimeException()));%0A%0A%09%09Flux%3CXMLEvent%3E events =%0A%09%09%09%09this.decoder.decode(source, null, null, Collections.emptyMap());%0A%0A%09%09StepVerifier.create(events)%0A%09%09%09%09.consumeNextWith(e -%3E assertTrue(e.isStartDocument()))%0A%09%09%09%09.consumeNextWith(e -%3E assertStartElement(e, %22pojo%22))%0A%09%09%09%09.expectError(RuntimeException.class)%0A%09%09%09%09.verify();%0A%09%7D%0A%0A%09@Test%0A%09public void decodeErrorNonAalto() %7B%0A%09%09decoder.useAalto = false;%0A%0A%09%09Flux%3CDataBuffer%3E source = Flux.just(stringBuffer(%22%3Cpojo%3E%22))%0A%09%09%09%09.concatWith(Flux.error(new RuntimeException()));%0A%0A%09%09Flux%3CXMLEvent%3E events =%0A%09%09%09%09this.decoder.decode(source, null, null, Collections.emptyMap());%0A%0A%09%09StepVerifier.create(events)%0A%09%09%09%09.expectError(RuntimeException.class)%0A%09%09%09%09.verify();%0A%09%7D%0A%0A
 %09private sta

Add error stream tests for XmlEventDecoder