Search in sources :

Example 86 with DefaultExchange

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());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Processor(org.apache.camel.Processor) Predicate(org.apache.camel.Predicate)

Example 87 with DefaultExchange

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);
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) TypeConversionException(org.apache.camel.TypeConversionException)

Example 88 with DefaultExchange

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());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 89 with DefaultExchange

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 90 with DefaultExchange

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Exchange (org.apache.camel.Exchange)381 Test (org.junit.Test)254 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)127 CamelContext (org.apache.camel.CamelContext)54 RegisteredDelivery (org.jsmpp.bean.RegisteredDelivery)39 HashMap (java.util.HashMap)33 Message (org.apache.camel.Message)32 Before (org.junit.Before)32 Tx (org.nhindirect.common.tx.model.Tx)31 ESMClass (org.jsmpp.bean.ESMClass)30 Processor (org.apache.camel.Processor)22 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)22 Expression (org.apache.camel.Expression)21 File (java.io.File)20 DefaultMessage (org.apache.camel.impl.DefaultMessage)20 ArrayList (java.util.ArrayList)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 URL (java.net.URL)16 Date (java.util.Date)16