1 22 package org.objectweb.petals.binding.xquarebc.listeners; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.lang.reflect.Method ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import java.util.logging.Logger ; 31 32 import javax.jbi.JBIException; 33 import javax.jbi.component.ComponentContext; 34 import javax.jbi.servicedesc.ServiceEndpoint; 35 import javax.xml.namespace.QName ; 36 import javax.xml.transform.Source ; 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 ; 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 59 public class XQuareBCListenerTest extends XQuareBCTestTemplate { 60 61 private ComponentContext context; 62 63 private MockDeliveryChannel channel; 64 65 @Override 66 public void setUp() throws Exception { 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 .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 { 85 cleanDatabase(); 86 String testContent = XQuareBCTestTemplate.getStringFromInputStream(new FileInputStream (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 { 94 Method method1 = null; 95 Method method2 = null; 96 try { 97 method1 = XQuareBCListener.class.getDeclaredMethod( 98 "handleInOnlyMessage", new Class [] {Source .class, QName .class, 99 String .class}); 100 method2 = XQuareBCListener.class.getDeclaredMethod( 101 "handleInOutMessage", new Class [] {Source .class, QName .class, 102 String .class}); 103 } catch (SecurityException e1) { 104 e1.printStackTrace(); 105 } catch (NoSuchMethodException e1) { 106 e1.printStackTrace(); 107 } 108 Method [] mockedMethods = new Method [] {method1, method2}; 109 XQuareBCListener xquareListener = createMock( 110 XQuareBCListener.class, mockedMethods); 111 112 HashMap <String , Properties > serviceToPropertiesMap = new HashMap <String , Properties >(); 113 serviceToPropertiesMap.put("anyService", this.testXquareAuctionProperties); 114 xquareListener.processor = new XQuareBCJBIProcessor(context, channel, logger, serviceToPropertiesMap); 115 116 insertOneUser(); 117 118 String testContent = XQuareBCTestTemplate.getStringFromInputStream(new FileInputStream (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 xquareListener.handleNewData(inOut, QName.valueOf("service"), 134 "operation", this.testXquareAuctionProperties); 135 136 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 wsdlFile = new File (workDir + "HelloworldService.wsdl"); 148 Document 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(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 <String , Properties > mapDirConfig = new HashMap <String , Properties >(); 168 Properties props = new Properties (); 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 service, 176 String operation, Properties serviceProps) throws Exception { 177 178 } 179 }; 180 181 184 xquareListener.process(this.testXquareAuctionProperties); 185 EasyMock.reset(context); 186 EasyMock.reset(serviceEndpoint); 187 mapDirConfig.clear(); 188 190 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 |