View Javadoc

1   /*
2    * $Id: JdbcFunctionalTestCase.java 22552 2011-07-25 07:18:19Z claude.mamo $
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.transport.jdbc.functional;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.DefaultMuleMessage;
17  import org.mule.api.MuleMessage;
18  import org.mule.module.client.MuleClient;
19  
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.dbutils.QueryRunner;
26  import org.apache.commons.dbutils.handlers.ArrayHandler;
27  import org.junit.Test;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  public class JdbcFunctionalTestCase extends AbstractJdbcFunctionalTestCase
31  {
32      public JdbcFunctionalTestCase(ConfigVariant variant, String configResources)
33      {
34          super(variant, configResources);
35          setPopulateTestData(false);
36      }
37      
38      @Parameters
39      public static Collection<Object[]> parameters()
40      {
41          return Arrays.asList(new Object[][]{
42              {ConfigVariant.SERVICE, AbstractJdbcFunctionalTestCase.getConfig() + ",jdbc-functional-config-service.xml"},
43              {ConfigVariant.FLOW, AbstractJdbcFunctionalTestCase.getConfig() + ",jdbc-functional-config-flow.xml"}
44          });
45      }      
46      
47      @Test
48      public void testDirectSql() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage message = client.request("jdbc://SELECT * FROM TEST", 1000);
52          assertResultSetEmpty(message);
53          
54          QueryRunner qr = jdbcConnector.getQueryRunner();
55          int updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA) VALUES (1, '" + TEST_MESSAGE + "')");
56          assertEquals(1, updated);
57          message = client.request("jdbc://SELECT * FROM TEST", 1000);
58          assertResultSetNotEmpty(message);
59      }
60  
61      @Test
62      public void testSend() throws Exception
63      {
64          MuleClient client = new MuleClient(muleContext);
65          client.send("jdbc://writeTest?type=2", new DefaultMuleMessage(TEST_MESSAGE, muleContext));
66  
67          QueryRunner qr = jdbcConnector.getQueryRunner();
68          Object[] obj2 = 
69              (Object[]) qr.query(jdbcConnector.getConnection(), "SELECT DATA FROM TEST WHERE TYPE = 2", new ArrayHandler());
70          assertNotNull(obj2);
71          assertEquals(1, obj2.length);
72          assertEquals(TEST_MESSAGE, obj2[0]);
73      }
74  
75      @Test
76      public void testSendMap() throws Exception
77      {
78          MuleClient client = new MuleClient(muleContext);
79          Map map = new HashMap();
80          map.put("data", TEST_MESSAGE);
81          client.send("jdbc://writeMap?type=2", new DefaultMuleMessage(map, muleContext));
82  
83          QueryRunner qr = jdbcConnector.getQueryRunner();
84          Object[] obj2 = 
85              (Object[]) qr.query(jdbcConnector.getConnection(), "SELECT DATA FROM TEST WHERE TYPE = 2", new ArrayHandler());
86          assertNotNull(obj2);
87          assertEquals(1, obj2.length);
88          assertEquals(TEST_MESSAGE, obj2[0]);
89      }
90  
91      @Test
92      public void testReceive() throws Exception
93      {
94          MuleClient client = new MuleClient(muleContext);
95          MuleMessage message = client.request("jdbc://getTest?type=1", 1000);
96          assertResultSetEmpty(message);
97  
98          QueryRunner qr = jdbcConnector.getQueryRunner();
99          int updated = qr.update(jdbcConnector.getConnection(), "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (1, '" + TEST_MESSAGE
100             + "', NULL, NULL)");
101         assertEquals(1, updated);
102 
103         message = client.request("jdbc://getTest?type=1", 1000);
104         assertResultSetNotEmpty(message);
105     }
106 
107     @Test
108     public void testReceiveAndSend() throws Exception
109     {
110         QueryRunner qr = jdbcConnector.getQueryRunner();
111         qr.update(jdbcConnector.getConnection(), 
112             "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (1, '" + TEST_MESSAGE + "', NULL, NULL)");
113 
114         long t0 = System.currentTimeMillis();
115         while (System.currentTimeMillis() - t0 < 20000)
116         {
117             Object[] rs = 
118                 (Object[]) qr.query(jdbcConnector.getConnection(), "SELECT COUNT(*) FROM TEST WHERE TYPE = 2", new ArrayHandler());
119             assertNotNull(rs);
120             assertEquals(1, rs.length);
121             if (((Number)rs[0]).intValue() > 0)
122             {
123                 break;
124             }
125             Thread.sleep(100);
126         }
127 
128         Object[] obj2 = 
129             (Object[]) qr.query(jdbcConnector.getConnection(), "SELECT DATA FROM TEST WHERE TYPE = 2", new ArrayHandler());
130         assertNotNull(obj2);
131         assertEquals(1, obj2.length);
132         assertEquals(TEST_MESSAGE + " Received", obj2[0]);
133     }
134 }