View Javadoc

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