View Javadoc

1   /*
2    * $Id: JdbcNamespaceHandlerTestCase.java 23022 2011-09-25 14:49:23Z dfeist $
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  package org.mule.transport.jdbc.config;
11  
12  import org.mule.api.endpoint.ImmutableEndpoint;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.transaction.MuleTransactionConfig;
15  import org.mule.transaction.XaTransactionFactory;
16  import org.mule.transport.jdbc.ExtendedQueryRunner;
17  import org.mule.transport.jdbc.JdbcConnector;
18  import org.mule.transport.jdbc.JdbcTransactionFactory;
19  import org.mule.transport.jdbc.sqlstrategy.DefaultSqlStatementStrategyFactory;
20  import org.mule.transport.jdbc.test.TestDataSource;
21  
22  import org.apache.commons.dbutils.QueryRunner;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class JdbcNamespaceHandlerTestCase extends FunctionalTestCase
31  {
32  
33      @Override
34      protected String getConfigResources()
35      {
36          return "jdbc-namespace-config.xml";
37      }
38  
39      @Test
40      public void testWithDataSource() throws Exception
41      {
42          JdbcConnector c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector1");
43          assertNotNull(c);
44  
45          assertTrue(c.getDataSource() instanceof TestDataSource);
46          assertNull(c.getQueries());
47          assertEquals(-1, c.getQueryTimeout());
48      }
49  
50      @Test
51      public void testWithDataSourceViaJndi() throws Exception
52      {
53          JdbcConnector c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector2");
54          assertNotNull(c);
55  
56          assertTrue(c.getDataSource() instanceof TestDataSource);
57          assertNull(c.getQueries());
58          assertTrue(c.isConnected());
59          assertTrue(c.isStarted());
60          assertEquals(3, c.getQueryTimeout());
61      }
62  
63      @Test
64      public void testFullyConfigured() throws Exception
65      {
66          JdbcConnector c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector3");
67          assertNotNull(c);
68  
69          assertTrue(c.getDataSource() instanceof TestDataSource);
70  
71          assertNotNull(c.getQueries());
72          assertEquals(3, c.getQueries().size());
73  
74          assertTrue(c.isConnected());
75          assertTrue(c.isStarted());
76          // Test a abstract connector property (MULE-5776)
77          assertTrue(c.isValidateConnections());
78      }
79  
80      @Test
81      public void testEndpointQueryOverride() throws Exception
82      {
83          JdbcConnector c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector3");
84          ImmutableEndpoint testJdbcEndpoint = muleContext.getEndpointFactory()
85              .getInboundEndpoint("testJdbcEndpoint");
86  
87          //On connector, not overridden
88          assertNotNull(c.getQuery(testJdbcEndpoint, "getTest"));
89  
90          //On connector, overridden on endpoint
91          assertNotNull(c.getQuery(testJdbcEndpoint, "getTest2"));
92          assertEquals("OVERRIDDEN VALUE", c.getQuery(testJdbcEndpoint, "getTest2"));
93  
94          //Only on endpoint
95          assertNotNull(c.getQuery(testJdbcEndpoint, "getTest3"));
96  
97          //Does not exist on either
98          assertNull(c.getQuery(testJdbcEndpoint, "getTest4"));
99          assertEquals("3", testJdbcEndpoint.getProperty("queryTimeout"));
100 
101         QueryRunner queryRunner = c.getQueryRunnerFor(testJdbcEndpoint);
102         assertEquals(ExtendedQueryRunner.class, queryRunner.getClass());
103         assertEquals(3, ((ExtendedQueryRunner) queryRunner).getQueryTimeout());
104     }
105 
106     @Test
107     public void testEndpointWithTransaction() throws Exception
108     {
109         ImmutableEndpoint endpoint = muleContext.getRegistry().
110             lookupEndpointBuilder("endpointWithTransaction").buildInboundEndpoint();
111         assertNotNull(endpoint);
112         assertEquals(JdbcTransactionFactory.class,
113             endpoint.getTransactionConfig().getFactory().getClass());
114         assertEquals(MuleTransactionConfig.ACTION_NONE,
115             endpoint.getTransactionConfig().getAction());
116         assertEquals("-1", endpoint.getProperty("queryTimeout"));
117     }
118 
119     @Test
120     public void testEndpointWithXaTransaction() throws Exception
121     {
122         ImmutableEndpoint endpoint = muleContext.getRegistry().
123             lookupEndpointBuilder("endpointWithXaTransaction").buildInboundEndpoint();
124         assertNotNull(endpoint);
125         assertEquals(XaTransactionFactory.class,
126             endpoint.getTransactionConfig().getFactory().getClass());
127         assertEquals(MuleTransactionConfig.ACTION_ALWAYS_BEGIN,
128             endpoint.getTransactionConfig().getAction());
129     }
130 
131     @Test
132     public void testSqlStatementStrategyFactoryOverride() throws Exception
133     {
134         // class config
135         JdbcConnector c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector4");
136         assertNotNull(c.getSqlStatementStrategyFactory());
137         assertTrue(c.getSqlStatementStrategyFactory() instanceof TestSqlStatementStrategyFactory);
138 
139         // ref config
140         c = (JdbcConnector) muleContext.getRegistry().lookupConnector("jdbcConnector5");
141         assertNotNull(c.getSqlStatementStrategyFactory());
142         assertTrue(c.getSqlStatementStrategyFactory() instanceof TestSqlStatementStrategyFactory);
143     }
144 
145     public static class TestSqlStatementStrategyFactory extends DefaultSqlStatementStrategyFactory
146     {
147         // no custom methods
148     }
149 }