1   /*
2    * $Id: EndpointURIEndpointBuilderTestCase.java 11371 2008-03-15 03:12:09Z 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.EndpointException;
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.tck.AbstractMuleTestCase;
20  import org.mule.tck.testmodels.mule.TestConnector;
21  import org.mule.tck.testmodels.mule.TestInboundTransformer;
22  import org.mule.tck.testmodels.mule.TestOutboundTransformer;
23  import org.mule.tck.testmodels.mule.TestResponseTransformer;
24  import org.mule.transaction.MuleTransactionConfig;
25  import org.mule.transport.SingleAttemptConnectionStrategy;
26  import org.mule.util.ObjectNameHelper;
27  
28  public class EndpointURIEndpointBuilderTestCase extends AbstractMuleTestCase
29  {
30      public void testBuildInboundEndpoint() throws MuleException
31      {
32          String uri = "test://address";
33          EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
34          try
35          {
36              ImmutableEndpoint ep = endpointBuilder.buildInboundEndpoint();
37              assertTrue(ep instanceof InboundEndpoint);
38              assertFalse(ep instanceof OutboundEndpoint);
39              assertNotNull(ep.getTransformers());
40              assertEquals(1, ep.getTransformers().size());
41              assertTrue(ep.getTransformers().get(0) instanceof TestInboundTransformer);
42              assertNotNull(ep.getResponseTransformers());
43              assertEquals(1, ep.getResponseTransformers().size());
44              assertTrue(ep.getResponseTransformers().get(0) instanceof TestResponseTransformer);
45              testDefaultCommonEndpointAttributes(ep);
46          }
47          catch (Exception e)
48          {
49              fail("Unexpected exception: " + e.getMessage());
50          }
51      }
52  
53      public void testBuildOutboundEndpoint() throws MuleException
54      {
55          String uri = "test://address";
56          EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
57          try
58          {
59              ImmutableEndpoint ep = endpointBuilder.buildOutboundEndpoint();
60              assertFalse(ep instanceof InboundEndpoint);
61              assertTrue(ep instanceof OutboundEndpoint);
62              assertTrue(ep.getTransformers() != null);
63              assertTrue(ep.getTransformers().get(0) instanceof TestOutboundTransformer);
64              assertTrue(ep.getResponseTransformers().isEmpty());
65              testDefaultCommonEndpointAttributes(ep);
66          }
67          catch (Exception e)
68          {
69              fail("Unexpected exception: " + e.getStackTrace());
70          }
71      }
72  
73      // TODO DF: Test more than defaults with tests using builder to set non-default
74      // values
75  
76      protected void testDefaultCommonEndpointAttributes(ImmutableEndpoint ep)
77      {
78          assertEquals(ep.getEndpointURI().getUri().toString(), "test://address");
79          assertEquals(muleContext.getConfiguration().getDefaultSynchronousEventTimeout(), ep.getRemoteSyncTimeout());
80          assertEquals(muleContext.getConfiguration().isDefaultSynchronousEndpoints()
81                       || muleContext.getConfiguration().isDefaultRemoteSync(), ep.isSynchronous());
82          assertEquals(muleContext.getConfiguration().isDefaultRemoteSync(), ep.isRemoteSync());
83          assertTrue(ep.getConnectionStrategy() instanceof SingleAttemptConnectionStrategy);
84          assertTrue(ep.getTransactionConfig() instanceof MuleTransactionConfig);
85          assertTrue(ep.getTransactionConfig() instanceof MuleTransactionConfig);
86          assertEquals(null, ep.getSecurityFilter());
87          assertTrue(ep.getConnector() instanceof TestConnector);
88          assertEquals(ObjectNameHelper.getEndpointName(ep.getEndpointURI()), ep.getName());
89          assertFalse(ep.isDeleteUnacceptedMessages());
90          assertEquals(muleContext.getConfiguration().getDefaultEncoding(), ep.getEncoding());
91          assertEquals(null, ep.getFilter());
92          assertEquals(ImmutableEndpoint.INITIAL_STATE_STARTED, ep.getInitialState());
93      }
94  
95      public void testHasSetEncodingMethod() throws EndpointException, SecurityException, NoSuchMethodException
96      {
97          String uri = "test://address";
98          EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
99          assertNotNull(endpointBuilder.getClass().getMethod("setEncoding", new Class[]{String.class}));
100     }
101 
102 }