1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.polling;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16
17 import org.mule.DefaultMuleEvent;
18 import org.mule.DefaultMuleMessage;
19 import org.mule.api.MuleEvent;
20 import org.mule.api.MuleException;
21 import org.mule.api.construct.FlowConstruct;
22 import org.mule.api.endpoint.InboundEndpoint;
23 import org.mule.api.lifecycle.InitialisationException;
24 import org.mule.api.processor.MessageProcessor;
25 import org.mule.endpoint.EndpointURIEndpointBuilder;
26 import org.mule.tck.SensingNullMessageProcessor;
27 import org.mule.tck.junit4.AbstractMuleContextTestCase;
28 import org.mule.transport.NullPayload;
29
30 import org.junit.Test;
31 import org.mockito.Mockito;
32
33 public class MessageProcessorPollingMessageReceiverTestCase extends AbstractMuleContextTestCase
34 {
35
36 @Test
37 public void testNullResponseFromNestedMP() throws Exception
38 {
39
40 MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
41 {
42 public MuleEvent process(MuleEvent event) throws MuleException
43 {
44 return null;
45 }
46 });
47
48 SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
49 receiver.setListener(flow);
50
51 receiver.poll();
52
53 assertNull(flow.event);
54 }
55
56 @Test
57 public void testNullPayloadResponseFromNestedMP() throws Exception
58 {
59
60 MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
61 {
62 public MuleEvent process(MuleEvent event) throws MuleException
63 {
64 return new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(), muleContext),
65 event);
66 }
67 });
68
69 SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
70 receiver.setListener(flow);
71
72 receiver.poll();
73
74 assertNull(flow.event);
75 }
76
77 @Test
78 public void testEmptyStringResponseFromNestedMP() throws Exception
79 {
80
81 MessageProcessorPollingMessageReceiver receiver = createReceiver(new MessageProcessor()
82 {
83 public MuleEvent process(MuleEvent event) throws MuleException
84 {
85 return new DefaultMuleEvent(new DefaultMuleMessage("", muleContext), event);
86 }
87 });
88
89 SensingNullMessageProcessor flow = getSensingNullMessageProcessor();
90 receiver.setListener(flow);
91
92 receiver.poll();
93
94 assertNotNull(flow.event);
95 }
96
97 @Test
98 public void testNestedOneWayEndpoint() throws Exception
99 {
100
101 try
102 {
103 createReceiver(muleContext.getEndpointFactory().getOutboundEndpoint("test://test2"));
104
105 org.junit.Assert.fail("Exception expected");
106 }
107 catch (Exception e)
108 {
109
110 assertEquals(InitialisationException.class, e.getClass());
111 }
112
113 }
114
115 private MessageProcessorPollingMessageReceiver createReceiver(MessageProcessor processor)
116 throws MuleException
117 {
118 EndpointURIEndpointBuilder builder = new EndpointURIEndpointBuilder("test://test", muleContext);
119 builder.setProperty(MessageProcessorPollingMessageReceiver.SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME,
120 processor);
121
122 InboundEndpoint inboundEndpoint = muleContext.getEndpointFactory().getInboundEndpoint(builder);
123
124 MessageProcessorPollingMessageReceiver receiver = new MessageProcessorPollingMessageReceiver(
125 inboundEndpoint.getConnector(), Mockito.mock(FlowConstruct.class), inboundEndpoint);
126
127 receiver.initialise();
128 return receiver;
129 }
130
131 }