View Javadoc

1   /*
2    * $Id: EndpointFactoryTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.endpoint;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleException;
15  import org.mule.api.endpoint.EndpointBuilder;
16  import org.mule.api.endpoint.EndpointFactory;
17  import org.mule.api.endpoint.ImmutableEndpoint;
18  import org.mule.api.endpoint.InboundEndpoint;
19  import org.mule.api.endpoint.OutboundEndpoint;
20  import org.mule.api.registry.Registry;
21  import org.mule.tck.AbstractMuleTestCase;
22  import org.mule.tck.testmodels.mule.TestConnector;
23  
24  public class EndpointFactoryTestCase extends AbstractMuleTestCase
25  {
26  
27      public void testCreateInboundEndpoint() throws Exception
28      {
29          String uri = "test://address";
30          EndpointFactory endpointFactory = new DefaultEndpointFactory();
31          endpointFactory.setMuleContext(muleContext);
32          ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
33          assertEquals(DefaultInboundEndpoint.class, ep.getClass());
34          assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
35          assertTrue(ep instanceof InboundEndpoint);
36      }
37  
38      public void testCreateInboundEndpointFromGlobalEndpoint() throws Exception
39      {
40          muleContext.getRegistry().registerEndpointBuilder("myGlobalEndpoint",
41                  new EndpointURIEndpointBuilder("test://address", muleContext));
42          String uri = "myGlobalEndpoint";
43          EndpointFactory endpointFactory = new DefaultEndpointFactory();
44          endpointFactory.setMuleContext(muleContext);
45          try
46          {
47              ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
48              assertEquals(DefaultInboundEndpoint.class, ep.getClass());
49              assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
50              assertTrue(ep instanceof InboundEndpoint);
51          }
52          catch (Exception e)
53          {
54              fail("Unexpected exception: " + e.getMessage());
55          }
56      }
57  
58      public void testCreateInboundEndpointFromNamedConcreteEndpoint() throws Exception
59      {
60          muleContext.getRegistry().registerEndpointBuilder("&myNamedConcreateEndpoint",
61                  new EndpointURIEndpointBuilder("test://address", muleContext));
62          String uri = "&myNamedConcreateEndpoint";
63          EndpointFactory endpointFactory = new DefaultEndpointFactory();
64          endpointFactory.setMuleContext(muleContext);
65          ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
66          assertEquals(DefaultInboundEndpoint.class, ep.getClass());
67          assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
68          assertTrue(ep instanceof InboundEndpoint);
69      }
70  
71      public void testCreateOutboundEndpoint() throws Exception
72      {
73          String uri = "test://address";
74          EndpointFactory endpointFactory = new DefaultEndpointFactory();
75          endpointFactory.setMuleContext(muleContext);
76          ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
77          assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
78          assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
79          assertTrue(ep instanceof OutboundEndpoint);
80      }
81  
82      public void testCreateoutboundEndpointFromGlobalEndpoint() throws Exception
83      {
84          muleContext.getRegistry().registerEndpointBuilder("myGlobalEndpoint",
85                  new EndpointURIEndpointBuilder("test://address", muleContext));
86          String uri = "myGlobalEndpoint";
87          EndpointFactory endpointFactory = new DefaultEndpointFactory();
88          endpointFactory.setMuleContext(muleContext);
89          ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
90          assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
91          assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
92          assertTrue(ep instanceof OutboundEndpoint);
93      }
94  
95      public void testCreateoutboundEndpointFromNamedConcreteEndpoint() throws Exception
96      {
97          muleContext.getRegistry().registerEndpointBuilder("&myNamedConcreateEndpoint",
98                  new EndpointURIEndpointBuilder("test://address", muleContext));
99          String uri = "&myNamedConcreateEndpoint";
100         EndpointFactory endpointFactory = new DefaultEndpointFactory();
101         endpointFactory.setMuleContext(muleContext);
102         ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
103         assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
104         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
105         assertTrue(ep instanceof OutboundEndpoint);
106     }
107 
108     public void testCreateInboundEndpointWithBuilder() throws Exception
109     {
110         EndpointBuilder builder = new EndpointURIEndpointBuilder("test://address", muleContext);
111         EndpointFactory endpointFactory = new DefaultEndpointFactory();
112         endpointFactory.setMuleContext(muleContext);
113         ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(builder);
114         assertEquals(DefaultInboundEndpoint.class, ep.getClass());
115         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
116         assertTrue(ep instanceof InboundEndpoint);
117     }
118 
119     public void testCreateOutboundEndpointWithBuilder() throws Exception
120     {
121         EndpointBuilder builder = new EndpointURIEndpointBuilder("test://address", muleContext);
122         EndpointFactory endpointFactory = new DefaultEndpointFactory();
123         endpointFactory.setMuleContext(muleContext);
124         ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(builder);
125         assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
126         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
127         assertTrue(ep instanceof OutboundEndpoint);
128     }
129 
130     public void testCreateEndpoint() throws MuleException
131     {
132         String uri = "test://address";
133         EndpointFactory endpointFactory = new DefaultEndpointFactory();
134         endpointFactory.setMuleContext(muleContext);
135         ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
136         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
137     }
138 
139     public void testCreateEndpointFromGlobalEndpoint() throws MuleException
140     {
141         Registry r = muleContext.getRegistry();
142         r.registerObject("myGlobalEndpoint", new EndpointURIEndpointBuilder("test://address", muleContext), muleContext);
143         String uri = "myGlobalEndpoint";
144         EndpointFactory endpointFactory = new DefaultEndpointFactory();
145         endpointFactory.setMuleContext(muleContext);
146         try
147         {
148             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
149             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
150         }
151         catch (Exception e)
152         {
153             fail("Unexpected exception: " + e.getMessage());
154         }
155     }
156 
157     public void testCreateEndpointFromNamedConcreteEndpoint() throws MuleException
158     {
159         Registry r = muleContext.getRegistry();
160         r.registerObject("&myNamedConcreteEndpoint", new EndpointURIEndpointBuilder("test://address", muleContext));
161         String uri = "&myNamedConcreteEndpoint";
162         EndpointFactory endpointFactory = new DefaultEndpointFactory();
163         endpointFactory.setMuleContext(muleContext);
164         ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
165         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
166     }
167 
168     public void testCreateEndpointByCustomizingEndpointBuilder() throws MuleException
169     {
170         // Create and register two connectors
171         TestConnector testConnector1 = new TestConnector(muleContext);
172         testConnector1.setName("testConnector1");
173         TestConnector testConnector2 = new TestConnector(muleContext);
174         testConnector2.setName("testConnector2");
175         muleContext.getRegistry().registerConnector(testConnector1);
176         muleContext.getRegistry().registerConnector(testConnector2);
177 
178         String globalEndpointName = "concreteEndpoint";
179 
180         // Create and register a endpoint builder (global endpoint) with connector1
181         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test://address", muleContext);
182         endpointBuilder.setConnector(testConnector1);
183         muleContext.getRegistry().registerObject(globalEndpointName, endpointBuilder);
184         EndpointFactory endpointFactory = new DefaultEndpointFactory();
185         endpointFactory.setMuleContext(muleContext);
186         // Test that DefaultEndpointFactory.getEndpointBuilder() returns a new
187         // EndpointBuilder instance equal to
188         // the one we registered earlier
189         EndpointBuilder endpointBuilder1 = endpointFactory.getEndpointBuilder(globalEndpointName);
190         assertNotSame(endpointBuilder1, endpointBuilder);
191         assertTrue(endpointBuilder1.equals(endpointBuilder));
192 
193         // Test that DefaultEndpointFactory.getEndpointBuilder() returns a new
194         // EndpointBuilder instance equal to
195         // the one we registered earlier
196         EndpointBuilder endpointBuilder2 = endpointFactory.getEndpointBuilder(globalEndpointName);
197         assertNotSame(endpointBuilder2, endpointBuilder);
198         assertTrue(endpointBuilder2.equals(endpointBuilder));
199 
200         // Check that all EndpointBuilder's returned are unique but equal
201         assertNotSame(endpointBuilder1, endpointBuilder2);
202         assertTrue(endpointBuilder1.equals(endpointBuilder2));
203         assertEquals(endpointBuilder1.hashCode(), endpointBuilder2.hashCode());
204 
205         // Test creating an endpoint from endpointBuilder1
206         endpointBuilder1.setExchangePattern(MessageExchangePattern.REQUEST_RESPONSE);
207         endpointBuilder1.setResponseTimeout(99);
208         ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(endpointBuilder1);
209         assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
210         assertEquals(MessageExchangePattern.REQUEST_RESPONSE, ep.getExchangePattern());
211         assertEquals(99, ep.getResponseTimeout());
212         assertNotNull(ep.getConnector());
213         assertEquals(testConnector1, ep.getConnector());
214 
215         // Test creating an endpoint from endpointBuilder2
216         endpointBuilder2.setExchangePattern(MessageExchangePattern.ONE_WAY);
217         endpointBuilder2.setResponseTimeout(0);
218         endpointBuilder2.setConnector(testConnector2);
219         ImmutableEndpoint ep2 = endpointFactory.getInboundEndpoint(endpointBuilder2);
220         assertEquals(ep2.getEndpointURI().getUri().toString(), "test://address");
221         assertEquals(MessageExchangePattern.ONE_WAY, ep2.getExchangePattern());
222         assertEquals(0, ep2.getResponseTimeout());
223         assertNotNull(ep.getConnector());
224         assertEquals(testConnector2, ep2.getConnector());
225 
226         // Test creating a new endpoint from endpointBuilder1
227         ImmutableEndpoint ep3 = endpointFactory.getInboundEndpoint(endpointBuilder1);
228         assertEquals(ep3.getEndpointURI().getUri().toString(), "test://address");
229         assertTrue(ep3.getResponseTimeout() != 0);
230         assertEquals(MessageExchangePattern.REQUEST_RESPONSE, ep3.getExchangePattern());
231         assertNotNull(ep.getConnector());
232         assertEquals(testConnector1, ep3.getConnector());
233     }
234 
235 }