KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > audit > jdbc > JdbcAuditorTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.audit.jdbc;
18
19 import org.apache.servicemix.jbi.audit.jdbc.JdbcAuditor;
20 import org.apache.servicemix.jbi.container.JBIContainer;
21 import org.apache.servicemix.jbi.jaxp.StringSource;
22 import org.apache.servicemix.tck.ReceiverComponent;
23 import org.apache.servicemix.tck.SenderComponent;
24 import org.hsqldb.jdbc.jdbcDataSource;
25
26 import javax.jbi.messaging.ExchangeStatus;
27 import javax.jbi.messaging.InOnly;
28 import javax.jbi.messaging.MessageExchange;
29 import javax.sql.DataSource JavaDoc;
30
31 import java.sql.Connection JavaDoc;
32
33 import junit.framework.TestCase;
34
35
36 public class JdbcAuditorTest extends TestCase {
37
38     private DataSource dataSource;
39     private Connection JavaDoc connection;
40     private JBIContainer jbi;
41     
42     
43     protected void setUp() throws Exception JavaDoc {
44         jdbcDataSource ds = new jdbcDataSource();
45         ds.setDatabase("jdbc:hsqldb:mem:aname");
46         ds.setUser("sa");
47         dataSource = ds;
48         connection = dataSource.getConnection();
49         jbi = new JBIContainer();
50     }
51     
52     protected void tearDown() throws Exception JavaDoc {
53         if (jbi != null) {
54             jbi.shutDown();
55         }
56         if (connection != null) {
57             connection.close();
58         }
59     }
60     
61     
62     public void testInsertUpdate() throws Exception JavaDoc {
63         jbi.setFlowName("st");
64         jbi.setEmbedded(true);
65         jbi.init();
66         jbi.start();
67         SenderComponent sender = new SenderComponent();
68         ReceiverComponent receiver = new ReceiverComponent();
69         jbi.activateComponent(sender, "sender");
70         jbi.activateComponent(receiver, "receiver");
71         
72         JdbcAuditor auditor = new JdbcAuditor();
73         auditor.setContainer(jbi);
74         auditor.setDataSource(dataSource);
75         auditor.afterPropertiesSet();
76         
77         InOnly inonly = sender.createInOnlyExchange(ReceiverComponent.SERVICE, null, null);
78         inonly.setInMessage(inonly.createMessage());
79         inonly.getInMessage().setContent(new StringSource("<hello>world</hello>"));
80         sender.send(inonly);
81         
82         int nbMessages = auditor.getExchangeCount();
83         assertEquals(1, nbMessages);
84         MessageExchange[] exchanges = auditor.getExchanges(0, 1);
85         assertNotNull(exchanges);
86         assertEquals(1, exchanges.length);
87         assertEquals(ExchangeStatus.DONE, exchanges[0].getStatus());
88         
89         auditor.resendExchange(exchanges[0]);
90
91         nbMessages = auditor.getExchangeCount();
92         assertEquals(2, nbMessages);
93         MessageExchange exchange = auditor.getExchange(1);
94         assertNotNull(exchange);
95         assertEquals(ExchangeStatus.DONE, exchange.getStatus());
96         
97         /*
98         PreparedStatement st = connection.prepareStatement("SELECT EXCHANGE FROM SM_AUDIT WHERE ID = ?");
99         try {
100             st.setString(1, inonly.getExchangeId());
101             ResultSet rs = st.executeQuery();
102             assertTrue(rs.next());
103             byte[] data = rs.getBytes(1);
104             ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
105             Object obj = ois.readObject();
106             assertNotNull(obj);
107             assertTrue(obj instanceof ExchangePacket);
108             assertEquals(ExchangeStatus.DONE, ((ExchangePacket) obj).getStatus());
109         } finally {
110             st.close();
111         }
112         */

113         
114     }
115     
116 }
117
Popular Tags