1 22 package org.objectweb.petals.binding.xquarebc.listeners; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.sql.Statement ; 33 import java.util.HashMap ; 34 import java.util.Properties ; 35 import java.util.logging.Logger ; 36 37 import javax.jbi.component.ComponentContext; 38 import javax.jbi.messaging.ExchangeStatus; 39 import javax.jbi.messaging.MessageExchange; 40 import javax.jbi.messaging.MessagingException; 41 import javax.jbi.messaging.NormalizedMessage; 42 import javax.xml.namespace.QName ; 43 import javax.xml.transform.Source ; 44 45 import junit.framework.TestCase; 46 47 import org.easymock.IArgumentMatcher; 48 import org.easymock.classextension.EasyMock; 49 import org.objectweb.petals.binding.xquarebc.XQuareBCException; 50 import org.objectweb.petals.binding.xquarebc.XQuareSUHandler; 51 import org.objectweb.petals.binding.xquarebc.mock.MockDeliveryChannel; 52 import org.objectweb.petals.binding.xquarebc.mock.MockServiceEndpoint; 53 import org.objectweb.petals.component.common.MEPConstants; 54 import org.objectweb.petals.component.common.util.SourceHelper; 55 56 import static org.easymock.EasyMock.expect; 57 import static org.easymock.classextension.EasyMock.createMock; 58 import static org.easymock.classextension.EasyMock.replay; 59 import static org.easymock.classextension.EasyMock.verify; 60 61 72 public class XQuareBCTestTemplate extends TestCase { 73 74 protected String baseDir; 75 76 protected String workDir; 77 78 protected String auctionDir; 79 80 protected Logger logger; 81 82 protected Properties testXquareAuctionProperties; 83 84 protected Connection dbConn = null; 85 86 @Override 87 public void setUp() throws Exception { 88 baseDir = this.getClass().getResource(".").toString(); 89 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 90 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 91 workDir = baseDir + "src" + File.separator + "test-data" 92 + File.separator; 93 auctionDir = workDir + "auction" + File.separator; 94 95 this.logger = EasyMock.createMock(Logger .class); 96 97 98 this.testXquareAuctionProperties = new Properties (); 99 String databaseUrl = "jdbc:mysql://localhost/petals_samples_xquare_auction"; 101 String databaseUser = "petals_samples"; 102 String databasePassword = "petals_samples"; 103 String databaseMappingFilepath = auctionDir + "auction.map"; 104 testXquareAuctionProperties.put(XQuareSUHandler.DATABASE_URL, databaseUrl); 105 testXquareAuctionProperties.put(XQuareSUHandler.DATABASE_USER, databaseUser); 106 testXquareAuctionProperties.put(XQuareSUHandler.DATABASE_PASSWORD, databasePassword); 107 testXquareAuctionProperties.put(XQuareSUHandler.DATABASE_MAPPING, databaseMappingFilepath); 108 testXquareAuctionProperties.setProperty(XQuareSUHandler.XML_CONNECTION_BASE_URI, "file://" + auctionDir); 109 testXquareAuctionProperties.setProperty(XQuareSUHandler.WRAP_QUERY_RESULTS_IN_ROOT_TAG, "true"); 110 try { 115 Class.forName("com.mysql.jdbc.Driver"); 116 dbConn = DriverManager.getConnection(databaseUrl, databaseUser, databasePassword); 117 dbConn.setAutoCommit(false); 118 } catch (ClassNotFoundException e) { 119 throw new XQuareBCException("Unable to load database driver", e); 120 } catch (SQLException e) { 121 throw new XQuareBCException("Unable to connect to database", e); 122 } 123 124 String delimiterPrefix = XQuareSUHandler.LISTENER_PROP_PREFIX + DelimiterNewDataStrategy.DELIMITER_PROP_PREFIX; 125 testXquareAuctionProperties.put(XQuareSUHandler.LISTENER_SERVICE_PROP, "{http://petals.objectweb.org/}HelloworldService"); 126 testXquareAuctionProperties.put(XQuareSUHandler.LISTENER_OPERATION_PROP, "sayHello"); 127 testXquareAuctionProperties.put(delimiterPrefix + DelimiterNewDataStrategy.OLD_DATA_DELIMITER_SELECT_PROP, "select max(userid) from users_xqbc"); 128 testXquareAuctionProperties.put(delimiterPrefix + DelimiterNewDataStrategy.NEW_DATA_DELIMITER_SELECT_PROP, "select max(userid) from users"); 129 testXquareAuctionProperties.put(delimiterPrefix + DelimiterNewDataStrategy.INIT_DATA_DELIMITER_PROP, "U00"); 130 testXquareAuctionProperties.put(delimiterPrefix + DelimiterNewDataStrategy.NEW_DATA_DELIMITER_UPDATES_PROP, "delete from users_xqbc;\ninsert into users_xqbc (userid) values (\"##NEW_DATA_DELIMITER##\")"); 131 testXquareAuctionProperties.put(delimiterPrefix + DelimiterNewDataStrategy.NEW_DATA_XQUERY_PROP, 132 "for $i in collection(\"users\")/users \n" + 133 "where $i/userid > \"##OLD_DATA_DELIMITER##\" and $i/userid <= \"##NEW_DATA_DELIMITER##\" return \n" + 134 "<user>{ $i/userid }</user>"); 135 136 super.setUp(); 137 } 138 139 140 @Override 141 protected void tearDown() throws Exception { 142 super.tearDown(); 143 144 try { 145 if (dbConn != null) { 146 dbConn.close(); 147 } 148 } catch (SQLException e) { 149 throw new XQuareBCException("Unable to close connection to database", e); 150 } 151 152 } 153 154 155 protected int getSelectRowNb(String selectStatementString) throws Exception { 156 Statement stmt = dbConn.createStatement(); 157 stmt.execute(selectStatementString); 158 ResultSet rs = stmt.getResultSet(); 159 int rowNb = 0; 160 while (rs.next()) { 161 rowNb++; 162 } 163 stmt.close(); 164 return rowNb; 165 } 166 167 168 protected void cleanDatabase() throws Exception { 169 Statement stmt = dbConn.createStatement(); 170 stmt.executeUpdate("delete from bids;"); 171 stmt.close(); 172 stmt = dbConn.createStatement(); 173 stmt.executeUpdate("delete from items;"); 174 stmt.close(); 175 stmt = dbConn.createStatement(); 176 stmt.executeUpdate("delete from users;"); 177 stmt.close(); 178 stmt = dbConn.createStatement(); 179 stmt.executeUpdate("delete from users_xqbc;"); 180 stmt.close(); 181 assert(getSelectRowNb("select * from bids") == 0); 183 assert(getSelectRowNb("select * from items") == 0); 184 assert(getSelectRowNb("select * from users") == 0); 185 assert(getSelectRowNb("select * from users_xqbc") == 0); 186 } 187 188 189 190 191 public void testOperation(String testOperation, String testInput, 192 String testOutput, Properties testServiceProperties) { 193 MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel(); 194 ComponentContext mockComponentContext = EasyMock.createMock(ComponentContext.class); 196 HashMap <String , Properties > mapEndpointProps = new HashMap <String , Properties >(); 197 mapEndpointProps.put("myServiceName", testServiceProperties); 198 XQuareBCJBIProcessor xquareProcessor = new XQuareBCJBIProcessor( 199 mockComponentContext, mockDeliveryChannel, 200 logger, mapEndpointProps); 201 202 MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class); 203 expect(mockServiceEndpoint.getServiceName()).andReturn( 205 new QName ("myServiceName")); 206 207 QName myOperation = new QName (testOperation); 208 209 NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class); 210 Source contentSource = SourceHelper.createSource(testInput); 211 expect(mockInNormalizedMessage.getContent()).andReturn(contentSource); 212 NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class); 213 214 MessageExchange mockMessageExchange = createMock(MessageExchange.class); 215 expect(mockMessageExchange.getStatus()) 216 .andReturn(ExchangeStatus.ACTIVE); 217 expect(mockMessageExchange.getRole()).andReturn( 218 MessageExchange.Role.PROVIDER); 219 expect(mockMessageExchange.getPattern()).andReturn( 220 MEPConstants.IN_OUT_PATTERN).atLeastOnce(); 221 expect(mockMessageExchange.getFault()).andReturn(null); 222 try { 223 expect(mockMessageExchange.createMessage()).andReturn( 224 mockOutNormalizedMessage); 225 } catch (Exception e) { 226 e.printStackTrace(); 227 } 228 expect(mockMessageExchange.getEndpoint()) 229 .andReturn(mockServiceEndpoint); 230 expect(mockMessageExchange.getOperation()).andReturn(myOperation); 231 expect(mockMessageExchange.getMessage("in")).andReturn( 232 mockInNormalizedMessage); 233 234 try { 235 mockOutNormalizedMessage.setContent(eqSource(SourceHelper 236 .createSource(testOutput))); 237 mockMessageExchange.setMessage(mockOutNormalizedMessage, "out"); 239 } catch (MessagingException e) { 240 e.printStackTrace(); 241 } 242 243 replay(mockServiceEndpoint); 244 replay(mockInNormalizedMessage); 245 replay(mockOutNormalizedMessage); 246 replay(mockMessageExchange); 247 248 try { 249 xquareProcessor.process(mockMessageExchange); 251 } catch (XQuareBCException e) { 252 e.printStackTrace(); 253 } catch (Exception e) { 254 e.printStackTrace(); 255 } 256 257 verify(mockServiceEndpoint); 258 verify(mockInNormalizedMessage); 259 verify(mockOutNormalizedMessage); 260 verify(mockMessageExchange); 261 } 262 263 264 public void testEmitNewData(String testOperation, String testInput, 265 String testOutput, Properties testServiceProperties) { 266 MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel(); 267 ComponentContext mockComponentContext = EasyMock.createMock(ComponentContext.class); 269 HashMap <String , Properties > mapEndpointProps = new HashMap <String , Properties >(); 270 mapEndpointProps.put("myServiceName", testServiceProperties); 271 XQuareBCJBIProcessor xquareProcessor = new XQuareBCJBIProcessor( 272 mockComponentContext, mockDeliveryChannel, 273 logger, mapEndpointProps); 274 275 276 XQuareBCListener xquareListener = new XQuareBCListener( 277 mockComponentContext, mockDeliveryChannel, 278 logger, mapEndpointProps, xquareProcessor); 279 280 xquareListener.process(testServiceProperties); 281 boolean isInOut = true; 282 String tagetServiceName = "targetServiceName"; 283 String targetOperation = "targetOperation"; 284 try { 285 xquareListener.handleNewData(isInOut, QName.valueOf(tagetServiceName), 286 targetOperation, testServiceProperties); 287 } catch (Exception e1) { 288 e1.printStackTrace(); 290 } 291 292 MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class); 293 expect(mockServiceEndpoint.getServiceName()).andReturn( 295 new QName ("myServiceName")); 296 297 QName myOperation = new QName (testOperation); 298 299 NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class); 300 Source contentSource = SourceHelper.createSource(testInput); 301 expect(mockInNormalizedMessage.getContent()).andReturn(contentSource); 302 NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class); 303 304 MessageExchange mockMessageExchange = createMock(MessageExchange.class); 305 expect(mockMessageExchange.getStatus()) 306 .andReturn(ExchangeStatus.ACTIVE); 307 expect(mockMessageExchange.getRole()).andReturn( 308 MessageExchange.Role.PROVIDER); 309 expect(mockMessageExchange.getPattern()).andReturn( 310 MEPConstants.IN_OUT_PATTERN).atLeastOnce(); 311 expect(mockMessageExchange.getFault()).andReturn(null); 312 try { 313 expect(mockMessageExchange.createMessage()).andReturn( 314 mockOutNormalizedMessage); 315 } catch (Exception e) { 316 e.printStackTrace(); 317 } 318 expect(mockMessageExchange.getEndpoint()) 319 .andReturn(mockServiceEndpoint); 320 expect(mockMessageExchange.getOperation()).andReturn(myOperation); 321 expect(mockMessageExchange.getMessage("in")).andReturn( 322 mockInNormalizedMessage); 323 324 try { 325 mockOutNormalizedMessage.setContent(eqSource(SourceHelper 326 .createSource(testOutput))); 327 mockMessageExchange.setMessage(mockOutNormalizedMessage, "out"); 329 } catch (MessagingException e) { 330 e.printStackTrace(); 331 } 332 333 replay(mockServiceEndpoint); 334 replay(mockInNormalizedMessage); 335 replay(mockOutNormalizedMessage); 336 replay(mockMessageExchange); 337 338 try { 339 xquareProcessor.process(mockMessageExchange); 341 } catch (XQuareBCException e) { 342 e.printStackTrace(); 343 } catch (Exception e) { 344 e.printStackTrace(); 345 } 346 347 verify(mockServiceEndpoint); 348 verify(mockInNormalizedMessage); 349 verify(mockOutNormalizedMessage); 350 verify(mockMessageExchange); 351 } 352 353 354 355 361 public static class SourceArgumentMatcher implements IArgumentMatcher { 362 private Source expectedSource; 363 364 public SourceArgumentMatcher(Source expectedSource) { 365 this.expectedSource = expectedSource; 366 } 367 368 public boolean matches(Object found) { 369 if (!(found instanceof Source )) { 370 return false; 371 } 372 Source foundSource = (Source ) found; 373 try { 374 String foundSourceContent = SourceHelper 375 .createString(foundSource); 376 String expectedSourceContent = SourceHelper 377 .createString(expectedSource); 378 return (foundSourceContent == null) ? 379 (expectedSourceContent == null) : 380 foundSourceContent.equals(expectedSourceContent); 381 } catch (Exception e) { 382 return false; 383 } 384 } 385 386 public void appendTo(StringBuffer sbuf) { 387 String expectedSourceContent; 388 try { 389 expectedSourceContent = SourceHelper 390 .createString(expectedSource); 391 } catch (Exception e) { 392 expectedSourceContent = "<error creating representation>"; 393 } 394 sbuf.append("eqSource(with content\""); 395 sbuf.append(expectedSourceContent); 396 sbuf.append("\")"); 397 } 398 399 } 400 401 407 public static Source eqSource(Source in) { 408 EasyMock.reportMatcher(new SourceArgumentMatcher(in)); 409 return in; 410 } 411 412 420 public static final String getStringFromInputStream(InputStream in) 421 throws IOException { 422 byte[] byteBuf = new byte[1024]; 424 int byteReadNb; 425 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 426 while ((byteReadNb = in.read(byteBuf)) != -1) { 427 bos.write(byteBuf, 0, byteReadNb); 428 } 429 String res = bos.toString(); 430 return res; 431 } 432 } 433 | Popular Tags |