1   /*
2    * $Id: EndpointFactoryTestCase.java 11343 2008-03-13 10:58:26Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.api.MuleException;
14  import org.mule.api.endpoint.EndpointBuilder;
15  import org.mule.api.endpoint.EndpointFactory;
16  import org.mule.api.endpoint.ImmutableEndpoint;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.registry.Registry;
20  import org.mule.tck.AbstractMuleTestCase;
21  import org.mule.tck.testmodels.mule.TestConnector;
22  
23  public class EndpointFactoryTestCase extends AbstractMuleTestCase
24  {
25  
26      public void testCreateInboundEndpoint() throws MuleException
27      {
28          String uri = "test://address";
29          EndpointFactory endpointFactory = new DefaultEndpointFactory();
30          endpointFactory.setMuleContext(muleContext);
31          try
32          {
33              ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
34              assertEquals(DefaultInboundEndpoint.class, ep.getClass());
35              assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
36              assertTrue(ep instanceof InboundEndpoint);
37          }
38          catch (Exception e)
39          {
40              fail("Unexpected exception: " + e.getMessage());
41          }
42      }
43  
44      public void testCreateInboundEndpointFromGlobalEndpoint() throws MuleException
45      {
46          muleContext.getRegistry().registerEndpointBuilder("myGlobalEndpoint",
47              new EndpointURIEndpointBuilder("test://address", muleContext));
48          String uri = "myGlobalEndpoint";
49          EndpointFactory endpointFactory = new DefaultEndpointFactory();
50          endpointFactory.setMuleContext(muleContext);
51          try
52          {
53              ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
54              assertEquals(DefaultInboundEndpoint.class, ep.getClass());
55              assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
56              assertTrue(ep instanceof InboundEndpoint);
57          }
58          catch (Exception e)
59          {
60              fail("Unexpected exception: " + e.getMessage());
61          }
62      }
63  
64      public void testCreateInboundEndpointFromNamedConcreteEndpoint() throws MuleException
65      {
66          muleContext.getRegistry().registerEndpointBuilder("&myNamedConcreateEndpoint",
67              new EndpointURIEndpointBuilder("test://address", muleContext));
68          String uri = "&myNamedConcreateEndpoint";
69          EndpointFactory endpointFactory = new DefaultEndpointFactory();
70          endpointFactory.setMuleContext(muleContext);
71          try
72          {
73              ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
74              assertEquals(DefaultInboundEndpoint.class, ep.getClass());
75              assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
76              assertTrue(ep instanceof InboundEndpoint);
77          }
78          catch (Exception e)
79          {
80              fail("Unexpected exception: " + e.getMessage());
81          }
82      }
83  
84      public void testCreateOutboundEndpoint() throws MuleException
85      {
86          String uri = "test://address";
87          EndpointFactory endpointFactory = new DefaultEndpointFactory();
88          endpointFactory.setMuleContext(muleContext);
89          try
90          {
91              ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
92              assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
93              assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
94              assertTrue(ep instanceof OutboundEndpoint);
95          }
96          catch (Exception e)
97          {
98              fail("Unexpected exception: " + e.getMessage());
99          }
100     }
101 
102     public void testCreateoutboundEndpointFromGlobalEndpoint() throws MuleException
103     {
104         muleContext.getRegistry().registerEndpointBuilder("myGlobalEndpoint",
105             new EndpointURIEndpointBuilder("test://address", muleContext));
106         String uri = "myGlobalEndpoint";
107         EndpointFactory endpointFactory = new DefaultEndpointFactory();
108         endpointFactory.setMuleContext(muleContext);
109         try
110         {
111             ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
112             assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
113             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
114             assertTrue(ep instanceof OutboundEndpoint);
115         }
116         catch (Exception e)
117         {
118             fail("Unexpected exception: " + e.getMessage());
119         }
120     }
121 
122     public void testCreateoutboundEndpointFromNamedConcreteEndpoint() throws MuleException
123     {
124         muleContext.getRegistry().registerEndpointBuilder("&myNamedConcreateEndpoint",
125             new EndpointURIEndpointBuilder("test://address", muleContext));
126         String uri = "&myNamedConcreateEndpoint";
127         EndpointFactory endpointFactory = new DefaultEndpointFactory();
128         endpointFactory.setMuleContext(muleContext);
129         try
130         {
131             ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(uri);
132             assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
133             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
134             assertTrue(ep instanceof OutboundEndpoint);
135         }
136         catch (Exception e)
137         {
138             fail("Unexpected exception: " + e.getMessage());
139         }
140     }
141 
142     public void testCreateInboundEndpointWithBuilder() throws MuleException
143     {
144         EndpointBuilder builder = new EndpointURIEndpointBuilder("test://address", muleContext);
145         EndpointFactory endpointFactory = new DefaultEndpointFactory();
146         endpointFactory.setMuleContext(muleContext);
147         try
148         {
149             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(builder);
150             assertEquals(DefaultInboundEndpoint.class, ep.getClass());
151             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
152             assertTrue(ep instanceof InboundEndpoint);
153         }
154         catch (Exception e)
155         {
156             fail("Unexpected exception: " + e.getMessage());
157         }
158     }
159 
160     public void testCreateOutboundEndpointWithBuilder() throws MuleException
161     {
162         EndpointBuilder builder = new EndpointURIEndpointBuilder("test://address", muleContext);
163         EndpointFactory endpointFactory = new DefaultEndpointFactory();
164         endpointFactory.setMuleContext(muleContext);
165         try
166         {
167             ImmutableEndpoint ep = endpointFactory.getOutboundEndpoint(builder);
168             assertEquals(DefaultOutboundEndpoint.class, ep.getClass());
169             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
170             assertTrue(ep instanceof OutboundEndpoint);
171         }
172         catch (Exception e)
173         {
174             fail("Unexpected exception: " + e.getMessage());
175         }
176     }
177 
178     public void testCreateEndpoint() throws MuleException
179     {
180         String uri = "test://address";
181         EndpointFactory endpointFactory = new DefaultEndpointFactory();
182         endpointFactory.setMuleContext(muleContext);
183         try
184         {
185             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
186             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
187         }
188         catch (Exception e)
189         {
190             fail("Unexpected exception: " + e.getMessage());
191         }
192     }
193 
194     public void testCreateEndpointFromGlobalEndpoint() throws MuleException
195     {
196         Registry r = muleContext.getRegistry();
197         r.registerObject("myGlobalEndpoint", new EndpointURIEndpointBuilder("test://address", muleContext), muleContext);
198         String uri = "myGlobalEndpoint";
199         EndpointFactory endpointFactory = new DefaultEndpointFactory();
200         endpointFactory.setMuleContext(muleContext);
201         try
202         {
203             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
204             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
205         }
206         catch (Exception e)
207         {
208             fail("Unexpected exception: " + e.getMessage());
209         }
210     }
211 
212     public void testCreateEndpointFromNamedConcreteEndpoint() throws MuleException
213     {
214         Registry r = muleContext.getRegistry();
215         r.registerObject("&myNamedConcreateEndpoint", new EndpointURIEndpointBuilder("test://address", muleContext));
216         String uri = "&myNamedConcreateEndpoint";
217         EndpointFactory endpointFactory = new DefaultEndpointFactory();
218         endpointFactory.setMuleContext(muleContext);
219         try
220         {
221             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(uri);
222             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
223         }
224         catch (Exception e)
225         {
226             fail("Unexpected exception: " + e.getMessage());
227         }
228     }
229 
230     public void testCreateEndpointByCustomizingEndpointBuilder() throws MuleException
231     {
232         // Create and register two connectors
233         TestConnector testConnector1 = new TestConnector();
234         testConnector1.setName("testConnector1");
235         TestConnector testConnector2 = new TestConnector();
236         testConnector2.setName("testConnector2");
237         muleContext.getRegistry().registerConnector(testConnector1);
238         muleContext.getRegistry().registerConnector(testConnector2);
239 
240         String globalEndpointName = "concreteEndpoint";
241 
242         // Create and register a endpoint builder (global endpoint) with connector1
243         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test://address", muleContext);
244         endpointBuilder.setConnector(testConnector1);
245         muleContext.getRegistry().registerObject(globalEndpointName, endpointBuilder);
246         EndpointFactory endpointFactory = new DefaultEndpointFactory();
247         endpointFactory.setMuleContext(muleContext);
248         try
249         {
250             // Test that DefaultEndpointFactory.getEndpointBuilder() returns a new
251             // EndpointBuilder instance equal to
252             // the one we registered earlier
253             EndpointBuilder endpointBuilder1 = endpointFactory.getEndpointBuilder(globalEndpointName);
254             assertNotSame(endpointBuilder1, endpointBuilder);
255             assertTrue(endpointBuilder1.equals(endpointBuilder));
256 
257             // Test that DefaultEndpointFactory.getEndpointBuilder() returns a new
258             // EndpointBuilder instance equal to
259             // the one we registered earlier
260             EndpointBuilder endpointBuilder2 = endpointFactory.getEndpointBuilder(globalEndpointName);
261             assertNotSame(endpointBuilder2, endpointBuilder);
262             assertTrue(endpointBuilder2.equals(endpointBuilder));
263 
264             // Check that all EndpointBuilder's returned are unique but equal
265             assertNotSame(endpointBuilder1, endpointBuilder2);
266             assertTrue(endpointBuilder1.equals(endpointBuilder2));
267             assertEquals(endpointBuilder1.hashCode(), endpointBuilder2.hashCode());
268 
269             // Test creating an endpoint from endpointBuilder1
270             endpointBuilder1.setSynchronous(true);
271             endpointBuilder1.setRemoteSyncTimeout(99);
272             ImmutableEndpoint ep = endpointFactory.getInboundEndpoint(endpointBuilder1);
273             assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
274             assertTrue(ep.isSynchronous());
275             assertEquals(99, ep.getRemoteSyncTimeout());
276             assertNotNull(ep.getConnector());
277             assertEquals(testConnector1, ep.getConnector());
278 
279             // Test creating an endpoint from endpointBuilder2
280             endpointBuilder2.setSynchronous(false);
281             endpointBuilder2.setRemoteSyncTimeout(0);
282             endpointBuilder2.setConnector(testConnector2);
283             ImmutableEndpoint ep2 = endpointFactory.getInboundEndpoint(endpointBuilder2);
284             assertEquals(ep2.getEndpointURI().getUri().toString(), "test://address");
285             assertFalse(ep2.isSynchronous());
286             assertEquals(0, ep2.getRemoteSyncTimeout());
287             assertNotNull(ep.getConnector());
288             assertEquals(testConnector2, ep2.getConnector());
289 
290             // Test creating a new endpoint from endpointBuilder1
291             ImmutableEndpoint ep3 = endpointFactory.getInboundEndpoint(endpointBuilder1);
292             assertEquals(ep3.getEndpointURI().getUri().toString(), "test://address");
293             assertTrue(ep3.getRemoteSyncTimeout() != 0);
294             assertTrue(ep3.isSynchronous());
295             assertNotNull(ep.getConnector());
296             assertEquals(testConnector1, ep3.getConnector());
297         }
298         catch (Exception e)
299         {
300             fail("Unexpected exception: " + e.getMessage());
301         }
302     }
303 
304 }