use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class CamelConverterTest method testToProcessorPredicate.
public void testToProcessorPredicate() throws Exception {
Predicate pred = PredicateBuilder.isEqualTo(headerExpression("foo"), constant("bar"));
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setHeader("foo", "bar");
exchange.getIn().setBody("Hello World");
Processor pro = CamelConverter.toProcessor(pred);
pro.process(exchange);
assertEquals(true, exchange.getOut().getBody());
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class ConverterTest method testMandatoryConvertTo.
public void testMandatoryConvertTo() {
CamelContext camel = new DefaultCamelContext();
Exchange e = new DefaultExchange(camel);
try {
converter.mandatoryConvertTo(InputStream.class, e);
fail("Expect exception here");
} catch (Exception ex) {
assertTrue("Expect to get a NoTypeConversionAvailableException here", ex instanceof NoTypeConversionAvailableException);
}
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class ConverterTest method testInstanceMethodConversionWithExchange.
public void testInstanceMethodConversionWithExchange() throws Exception {
String[] values = new String[] { "5", "bar" };
CamelContext camel = new DefaultCamelContext();
Exchange e = new DefaultExchange(camel);
e.setProperty("prefix", "foo-");
MyBean bean = converter.convertTo(MyBean.class, e, values);
assertEquals("converted using exchange", 5, bean.getFoo(), 5);
assertEquals("converted using exchange", "foo-bar", bean.getBar());
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class StaxConverterTest method testEncodingXmlStreamReader.
public void testEncodingXmlStreamReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLStreamReader reader = null;
XMLStreamWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.name());
writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, output);
// copy to writer
while (reader.hasNext()) {
reader.next();
switch(reader.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
writer.writeStartDocument();
break;
case XMLStreamConstants.END_DOCUMENT:
writer.writeEndDocument();
break;
case XMLStreamConstants.START_ELEMENT:
writer.writeStartElement(reader.getName().getLocalPart());
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
break;
default:
break;
}
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
assertEquals(TEST_XML, result);
}
use of org.apache.camel.impl.DefaultExchange in project camel by apache.
the class StaxConverterTest method testEncodingXmlEventReader.
public void testEncodingXmlEventReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLEventReader reader = null;
XMLEventWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
while (reader.hasNext()) {
writer.add(reader.nextEvent());
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
// normalize the auotation mark
if (result.indexOf('\'') > 0) {
result = result.replace('\'', '"');
}
boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
assertTrue("Should match header", equals);
}
Aggregations