camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Matèrne (jhm) <apa...@materne.de>
Subject AW: Splitting up XML into 2 XMLs
Date Tue, 19 Nov 2013 16:49:55 GMT
The trick is using the right xpath. Here use a wildcard "/Route/*".


Jan


package de.materne.camel.sandbox.split;

import static org.apache.camel.builder.PredicateBuilder.or;

import java.io.IOException;

import org.apache.camel.EndpointInject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class SplitTest extends CamelTestSupport {

	@EndpointInject(uri="mock:end")
	MockEndpoint mock;
	
	@Test
	public void split() throws InterruptedException, IOException {
		mock.expectedMessageCount(2);
		mock.expectedMessagesMatches(
		    or(
				bodyAs(String.class).contains("42"),
				bodyAs(String.class).contains("Jan")
			)
		);
		String xml = createXml();
		template.sendBody("direct:split", xml);
		mock.assertIsSatisfied();
	}
	
	private String createXml() {
		StringBuilder xml = new StringBuilder();
		xml.append("<Route>");
		xml.append("  <Account><number>42</number></Account>");
		xml.append("  <Person><name>Jan</name></Person>");
		xml.append("</Route>");
		return xml.toString();
	}

	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("direct:split")
					.split(xpath("/Route/*"))
					.to("mock:end");
			}
		};
	}
}


Mime
View raw message