KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > xquarebc > listeners > XQuareBCListenerTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id : $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.xquarebc.listeners;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 import javax.jbi.JBIException;
33 import javax.jbi.component.ComponentContext;
34 import javax.jbi.servicedesc.ServiceEndpoint;
35 import javax.xml.namespace.QName JavaDoc;
36 import javax.xml.transform.Source JavaDoc;
37
38 import org.easymock.classextension.EasyMock;
39 import org.objectweb.petals.binding.xquarebc.XQuareBCException;
40 import org.objectweb.petals.binding.xquarebc.mock.MockDeliveryChannel;
41 import org.objectweb.petals.component.common.util.SourceHelper;
42 import org.objectweb.petals.component.common.util.WSDLHelper;
43 import org.w3c.dom.Document JavaDoc;
44
45 import static org.easymock.EasyMock.expect;
46 import static org.easymock.classextension.EasyMock.createMock;
47 import static org.easymock.classextension.EasyMock.replay;
48 import static org.easymock.classextension.EasyMock.verify;
49
50 /**
51  *
52  * Tests the XQuareBCListener
53  *
54  * @version $Rev: 250 $Date: {date}
55  * @since Petals 1.0
56  * @author Marc Dutoo - Open Wide
57  *
58  */

59 public class XQuareBCListenerTest extends XQuareBCTestTemplate {
60
61     private ComponentContext context;
62
63     private MockDeliveryChannel channel;
64
65     @Override JavaDoc
66     public void setUp() throws Exception JavaDoc {
67         super.setUp();
68         baseDir = this.getClass().getResource(".").toString();
69         baseDir = baseDir.substring(0, baseDir.indexOf("target"));
70         baseDir = baseDir.substring(baseDir.indexOf(":") + 1);
71         workDir = baseDir + "src" + File.separator + "test-data"
72             + File.separator;
73         this.logger = EasyMock.createMock(Logger JavaDoc.class);
74
75         channel = new MockDeliveryChannel();
76
77         context = createMock(ComponentContext.class);
78         expect(context.getDeliveryChannel()).andReturn(channel).anyTimes();
79         replay(context);
80     }
81
82
83
84     public void insertOneUser() throws Exception JavaDoc {
85         cleanDatabase();
86         String JavaDoc testContent = XQuareBCTestTemplate.getStringFromInputStream(new FileInputStream JavaDoc(auctionDir + "auction_test_insert_one_user-message.xml"));
87         testOperation("insert", testContent, "<success/>", this.testXquareAuctionProperties);
88         int userNb = getSelectRowNb("select * from users");
89         assertEquals(1, userNb);
90     }
91     
92     
93     public void testHandleNewData() throws Exception JavaDoc {
94         Method JavaDoc method1 = null;
95         Method JavaDoc method2 = null;
96         try {
97             method1 = XQuareBCListener.class.getDeclaredMethod(
98                 "handleInOnlyMessage", new Class JavaDoc[] {Source JavaDoc.class, QName JavaDoc.class,
99                     String JavaDoc.class});
100             method2 = XQuareBCListener.class.getDeclaredMethod(
101                 "handleInOutMessage", new Class JavaDoc[] {Source JavaDoc.class, QName JavaDoc.class,
102                     String JavaDoc.class});
103         } catch (SecurityException JavaDoc e1) {
104             e1.printStackTrace();
105         } catch (NoSuchMethodException JavaDoc e1) {
106             e1.printStackTrace();
107         }
108         Method JavaDoc[] mockedMethods = new Method JavaDoc[] {method1, method2};
109         XQuareBCListener xquareListener = createMock(
110             XQuareBCListener.class, mockedMethods);
111         
112         HashMap JavaDoc<String JavaDoc, Properties JavaDoc> serviceToPropertiesMap = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
113         serviceToPropertiesMap.put("anyService", this.testXquareAuctionProperties);
114         xquareListener.processor = new XQuareBCJBIProcessor(context, channel, logger, serviceToPropertiesMap);
115         
116         insertOneUser();
117
118         String JavaDoc testContent = XQuareBCTestTemplate.getStringFromInputStream(new FileInputStream JavaDoc(auctionDir + "auction_test_listen_one_user-results.xml"));
119         try {
120             expect(
121                 xquareListener.handleInOutMessage(eqSource(SourceHelper.createSource(testContent)),
122                     EasyMock.eq(QName.valueOf("service")), EasyMock.eq("operation"))).andReturn(true);
123             expect(
124                 xquareListener.handleInOnlyMessage(eqSource(SourceHelper.createSource(testContent)),
125                     EasyMock.eq(QName.valueOf("service")), EasyMock.eq("operation"))).andReturn(true);
126         } catch (XQuareBCException e) {
127             e.printStackTrace();
128         }
129         replay(xquareListener);
130
131         boolean inOut = true;
132         // it is an inOut operation
133
xquareListener.handleNewData(inOut, QName.valueOf("service"),
134             "operation", this.testXquareAuctionProperties);
135
136         // it is an inOnly operation
137
inOut = false;
138         xquareListener.handleNewData(inOut, QName.valueOf("service"),
139             "operation", this.testXquareAuctionProperties);
140     }
141
142
143     public void testProcess() {
144
145         XQuareBCListener xquareListener = null;
146
147         File JavaDoc wsdlFile = new File JavaDoc(workDir + "HelloworldService.wsdl");
148         Document JavaDoc wsdlDoc = WSDLHelper.createDocumentFromWSDL(wsdlFile);
149
150         ServiceEndpoint serviceEndpoint = createMock(ServiceEndpoint.class);
151         ServiceEndpoint[] serviceEndpoints = {serviceEndpoint};
152
153         ComponentContext context = createMock(ComponentContext.class);
154         try {
155             //expect(serviceEndpoint.getServiceName()).andReturn(
156
// new QName("{http://petals.objectweb.org/}HelloworldService"));
157
expect(context.getEndpointsForService(EasyMock.eq(QName.valueOf("{http://petals.objectweb.org/}HelloworldService"))))
158                 .andReturn(serviceEndpoints);
159             expect(context.getEndpointDescriptor(serviceEndpoint)).andReturn(wsdlDoc)
160                 .anyTimes();
161         } catch (JBIException e) {
162             e.printStackTrace();
163         }
164         replay(serviceEndpoint);
165         replay(context);
166
167         Map JavaDoc<String JavaDoc, Properties JavaDoc> mapDirConfig = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
168         Properties JavaDoc props = new Properties JavaDoc();
169         props.put("service.name", "MockService");
170         props.put("operation.name", "inOnlyMessage");
171         props.put("directory.destination", "destinationDir");
172         mapDirConfig.put("MockService", props);
173         xquareListener = new XQuareBCListener(
174             context, channel, this.logger, null, null) {
175             public void handleNewData(boolean inOut, QName JavaDoc service,
176                 String JavaDoc operation, Properties JavaDoc serviceProps) throws Exception JavaDoc {
177
178             }
179         };
180
181         /*
182          * Test case 1: operation inOnly
183          */

184         xquareListener.process(this.testXquareAuctionProperties);
185         EasyMock.reset(context);
186         EasyMock.reset(serviceEndpoint);
187         mapDirConfig.clear();
188         // assert
189

190         /*
191          * Test case 2 :operation inOut
192          */

193
194         try {
195             expect(context.getEndpointsForService(EasyMock.eq(QName.valueOf("{http://petals.objectweb.org/}HelloworldService"))))
196                 .andReturn(serviceEndpoints);
197             expect(context.getEndpointDescriptor(serviceEndpoint)).andReturn(wsdlDoc)
198                 .anyTimes();
199         } catch (JBIException e) {
200             e.printStackTrace();
201         }
202         replay(serviceEndpoint);
203         replay(context);
204
205         props.put("operation.name", "inOutOperation");
206         mapDirConfig.put("MockService", props);
207         xquareListener.process(this.testXquareAuctionProperties);
208
209         verify(context);
210         verify(serviceEndpoint);
211     }
212 }
213
Popular Tags