1 22 package org.objectweb.petals.engine.csv; 23 24 import static org.easymock.EasyMock.expect; 25 import static org.easymock.classextension.EasyMock.createMock; 26 import static org.easymock.classextension.EasyMock.replay; 27 import static org.easymock.classextension.EasyMock.verify; 28 29 import java.net.URI ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 import java.util.Set ; 35 import java.util.logging.Logger ; 36 37 import javax.activation.DataHandler ; 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.jbi.servicedesc.ServiceEndpoint; 43 import javax.xml.namespace.QName ; 44 import javax.xml.transform.Source ; 45 46 import junit.framework.TestCase; 47 48 import org.easymock.IArgumentMatcher; 49 import org.easymock.classextension.EasyMock; 50 51 import org.objectweb.petals.component.common.MEPConstants; 52 import org.objectweb.petals.component.common.util.SourceHelper; 53 import org.objectweb.petals.component.common.util.StringHelper; 54 import org.objectweb.petals.engine.csv.Mock.MockDeliveryChannel; 55 import org.objectweb.petals.engine.csv.Mock.MockMessageExchange; 56 import org.objectweb.petals.engine.csv.Mock.MockServiceEndpoint; 57 import org.objectweb.petals.engine.csv.exception.CsvException; 58 59 67 public class CsvProcessorTest extends TestCase { 68 69 private String baseDir; 70 71 private Logger logger; 72 73 @Override 74 public void setUp() throws Exception { 75 76 baseDir = this.getClass().getResource(".").toString(); 77 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 78 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 79 this.logger = EasyMock.createMock(Logger .class); 80 super.setUp(); 81 } 82 83 public void testCsvContentSingleValue() { 84 testCsvContent("singlevalue", 85 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlevalue</cell></row></data>"); 86 } 87 88 public void testCsvContentSingleRow() { 89 testCsvContent("singlerow1,singlerow2", 90 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlerow1</cell><cell>singlerow2</cell></row></data>"); 91 } 92 93 public void testCsvContentTwoRows() { 94 testCsvContent( 95 "singlerow1,singlerow2\nsecondrow1,secondrow2", 96 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlerow1</cell><cell>singlerow2</cell></row>" 97 + "<row><cell>secondrow1</cell><cell>secondrow2</cell></row></data>"); 98 } 99 100 public void testCsvContent(String testCsvInput, String testXmlOutput) { 101 MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel(); 102 103 Properties csvProperties = new Properties (); 104 csvProperties.setProperty(CsvProcessor.CSV_INPUT_MODE_PROP, 105 CsvProcessor.CSV_INPUT_MODE_CONTENT); 106 Map <String , Properties > mapEndpointCsv = new HashMap <String , Properties >(); 107 mapEndpointCsv.put("myServiceName", csvProperties); 108 CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel, 109 logger, mapEndpointCsv); 110 111 try { 113 mockDeliveryChannel.createExchangeFactory() 114 .createInOutExchange(); 115 } catch (MessagingException e) { 116 e.printStackTrace(); 117 } 118 final MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class); 119 expect(mockServiceEndpoint.getServiceName()).andReturn( 121 new QName ("myServiceName")); 122 123 final QName myOperation = new QName (CsvProcessor.CSV_TO_XML_OPERATION); 124 125 final NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class); 126 String myContent = testCsvInput; 127 Source contentSource = SourceHelper.createContentSource(myContent); 128 expect(mockInNormalizedMessage.getContent()).andReturn(contentSource); 132 140 final NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class); 141 142 156 MessageExchange mockMessageExchange = new MockMessageExchange() { 157 @Override 159 public NormalizedMessage createMessage() throws MessagingException { 160 return mockOutNormalizedMessage; 161 } 162 163 @Override 164 public ServiceEndpoint getEndpoint() { 165 return mockServiceEndpoint; 166 } 167 168 @Override 169 public NormalizedMessage getMessage(String name) { 170 return mockInNormalizedMessage; 171 } 172 173 @Override 174 public QName getOperation() { 175 return myOperation; 176 } 177 178 @Override 179 public URI getPattern() { 180 return MEPConstants.IN_OUT_PATTERN; 181 } 182 183 @Override 184 public Role getRole() { 185 return MessageExchange.Role.PROVIDER; 186 } 187 188 @Override 189 public ExchangeStatus getStatus() { 190 return ExchangeStatus.ACTIVE; 191 } 192 }; 193 194 try { 195 mockOutNormalizedMessage.setContent(eqSource(SourceHelper 196 .createSource(testXmlOutput))); mockMessageExchange.setMessage(mockOutNormalizedMessage, "out"); 199 } catch (MessagingException e) { 200 e.printStackTrace(); 201 } 202 203 replay(mockServiceEndpoint); 204 replay(mockInNormalizedMessage); 205 replay(mockOutNormalizedMessage); 206 208 try { 209 csvProcessor.process(mockMessageExchange); 211 } catch (CsvException e) { 212 e.printStackTrace(); 213 } catch (Exception e) { 214 e.printStackTrace(); 215 } 216 217 verify(mockServiceEndpoint); 218 verify(mockInNormalizedMessage); 219 verify(mockOutNormalizedMessage); 220 } 222 223 public void testCsvAttachmentSingleValue() { 224 testCsvAttachment("singlevalue", 225 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlevalue</cell></row></data>"); 226 } 227 228 public void testCsvAttachmentSingleRow() { 229 testCsvAttachment("singlerow1,singlerow2", 230 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlerow1</cell><cell>singlerow2</cell></row></data>"); 231 } 232 233 public void testCsvAttachmentTwoRows() { 234 testCsvAttachment( 235 "singlerow1,singlerow2\nsecondrow1,secondrow2", 236 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data xmlns=\"http://petals.objectweb.org/components/csv/csvxml\"><row><cell>singlerow1</cell><cell>singlerow2</cell></row>" 237 + "<row><cell>secondrow1</cell><cell>secondrow2</cell></row></data>"); 238 } 239 240 public void testCsvAttachment(String testCsvInput, String testXmlOutput) { 241 MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel(); 242 243 Properties csvProperties = new Properties (); 244 Map <String , Properties > mapEndpointCsv = new HashMap <String , Properties >(); 246 mapEndpointCsv.put("myServiceName", csvProperties); 247 CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel, 248 logger, mapEndpointCsv); 249 250 try { 252 mockDeliveryChannel.createExchangeFactory() 253 .createInOutExchange(); 254 } catch (MessagingException e) { 255 e.printStackTrace(); 256 } 257 258 MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class); 259 expect(mockServiceEndpoint.getServiceName()).andReturn( 261 new QName ("myServiceName")); 262 263 QName myOperation = new QName (CsvProcessor.CSV_TO_XML_OPERATION); 264 265 NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class); 266 Set <String > attachmentNames = new HashSet <String >(); 271 String attachmentName1 = "myFirstAttachment"; 272 attachmentNames.add(attachmentName1); 273 expect(mockInNormalizedMessage.getAttachmentNames()).andReturn( 274 attachmentNames).times(2); expect(mockInNormalizedMessage.getAttachment(attachmentName1)) 276 .andReturn(new DataHandler (new ByteArrayDataSource(testCsvInput))); 278 NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class); 279 280 MessageExchange mockMessageExchange = createMock(MessageExchange.class); 281 expect(mockMessageExchange.getStatus()) 282 .andReturn(ExchangeStatus.ACTIVE); 283 expect(mockMessageExchange.getRole()).andReturn( 284 MessageExchange.Role.PROVIDER); 285 expect(mockMessageExchange.getPattern()).andReturn( 286 MEPConstants.IN_OUT_PATTERN).atLeastOnce(); 287 expect(mockMessageExchange.getFault()).andReturn(null); 288 try { 289 expect(mockMessageExchange.createMessage()).andReturn( 290 mockOutNormalizedMessage); 291 } catch (Exception e) { 292 e.printStackTrace(); 293 } 294 expect(mockMessageExchange.getEndpoint()) 295 .andReturn(mockServiceEndpoint); 296 expect(mockMessageExchange.getOperation()).andReturn(myOperation); 297 expect(mockMessageExchange.getMessage("in")).andReturn( 298 mockInNormalizedMessage); 299 300 try { 301 mockOutNormalizedMessage.setContent(eqSource(SourceHelper 302 .createSource(testXmlOutput))); 303 mockMessageExchange.setMessage(mockOutNormalizedMessage, "out"); 305 } catch (MessagingException e) { 306 e.printStackTrace(); 307 } 308 309 replay(mockServiceEndpoint); 310 replay(mockInNormalizedMessage); 311 replay(mockOutNormalizedMessage); 312 replay(mockMessageExchange); 313 314 try { 315 csvProcessor.process(mockMessageExchange); 317 } catch (CsvException e) { 318 e.printStackTrace(); 319 } catch (Exception e) { 320 e.printStackTrace(); 321 } 322 323 verify(mockServiceEndpoint); 324 verify(mockInNormalizedMessage); 325 verify(mockOutNormalizedMessage); 326 verify(mockMessageExchange); 327 } 328 329 335 public static class SourceArgumentMatcher implements IArgumentMatcher { 336 private Source expectedSource; 337 338 public SourceArgumentMatcher(Source expectedSource) { 339 this.expectedSource = expectedSource; 340 } 341 342 public boolean matches(Object found) { 343 if (!(found instanceof Source )) { 344 return false; 345 } 346 Source foundSource = (Source ) found; 347 try { 348 String foundSourceContent = SourceHelper 349 .createString(foundSource); 350 String expectedSourceContent = SourceHelper 351 .createString(expectedSource); 352 return StringHelper.equal(foundSourceContent, 353 expectedSourceContent); 354 } catch (Exception e) { 355 return false; 356 } 357 } 358 359 public void appendTo(StringBuffer sbuf) { 360 String expectedSourceContent; 361 try { 362 expectedSourceContent = SourceHelper 363 .createString(expectedSource); 364 } catch (Exception e) { 365 expectedSourceContent = "<error creating representation>"; 366 } 367 sbuf.append("eqSource(with content\""); 368 sbuf.append(expectedSourceContent); 369 sbuf.append("\")"); 370 } 371 372 } 373 374 380 public static Source eqSource(Source in) { 381 EasyMock.reportMatcher(new SourceArgumentMatcher(in)); 382 return null; 383 } 384 385 386 public void testXmlToCsvContentSingleValue() { 387 testXmlToCsvContent("<myTable><row><aCell>singlevalue</aCell></row></myTable>", 388 "<content>singlevalue\n</content>"); 389 } 390 391 public void testXmlToCsvContentSingleRow() { 392 testXmlToCsvContent("<data><row><cell>singlerow1</cell><cell>singlerow2</cell></row></data>", 393 "<content>singlerow1,singlerow2\n</content>"); 394 } 395 396 public void testXmlToCsvContentTwoRows() { 397 testXmlToCsvContent( 398 "<data><row><cell>singlerow1</cell><cell>singlerow2</cell></row>" 399 + "<anotherRow><cell>secondrow1</cell><cell>secondrow2</cell></anotherRow></data>", 400 "<content>singlerow1,singlerow2\r\nsecondrow1,secondrow2\n</content>"); 401 } 402 403 public void testXmlToCsvContent(String testCsvInput, String testXmlOutput) { 404 MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel(); 405 406 Properties csvProperties = new Properties (); 407 csvProperties.setProperty(CsvProcessor.CSV_INPUT_MODE_PROP, 408 CsvProcessor.CSV_INPUT_MODE_CONTENT); 409 Map <String , Properties > mapEndpointCsv = new HashMap <String , Properties >(); 410 mapEndpointCsv.put("myServiceName", csvProperties); 411 CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel, 412 logger, mapEndpointCsv); 413 414 try { 416 mockDeliveryChannel.createExchangeFactory() 417 .createInOutExchange(); 418 } catch (MessagingException e) { 419 e.printStackTrace(); 420 } 421 final MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class); 422 expect(mockServiceEndpoint.getServiceName()).andReturn( 424 new QName ("myServiceName")); 425 426 final QName myOperation = new QName (CsvProcessor.XML_TO_CSV_OPERATION); 427 428 final NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class); 429 String myContent = testCsvInput; 430 Source contentSource = SourceHelper.createSource(myContent); 431 expect(mockInNormalizedMessage.getContent()).andReturn(contentSource); 435 443 final NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class); 444 445 459 MessageExchange mockMessageExchange = new MockMessageExchange() { 460 @Override 462 public NormalizedMessage createMessage() throws MessagingException { 463 return mockOutNormalizedMessage; 464 } 465 466 @Override 467 public ServiceEndpoint getEndpoint() { 468 return mockServiceEndpoint; 469 } 470 471 @Override 472 public NormalizedMessage getMessage(String name) { 473 return mockInNormalizedMessage; 474 } 475 476 @Override 477 public QName getOperation() { 478 return myOperation; 479 } 480 481 @Override 482 public URI getPattern() { 483 return MEPConstants.IN_OUT_PATTERN; 484 } 485 486 @Override 487 public Role getRole() { 488 return MessageExchange.Role.PROVIDER; 489 } 490 491 @Override 492 public ExchangeStatus getStatus() { 493 return ExchangeStatus.ACTIVE; 494 } 495 }; 496 497 try { 498 mockOutNormalizedMessage.setContent(eqSource(SourceHelper 499 .createSource(testXmlOutput))); mockMessageExchange.setMessage(mockOutNormalizedMessage, "out"); 502 } catch (MessagingException e) { 503 e.printStackTrace(); 504 } 505 506 replay(mockServiceEndpoint); 507 replay(mockInNormalizedMessage); 508 replay(mockOutNormalizedMessage); 509 511 try { 512 csvProcessor.process(mockMessageExchange); 514 } catch (CsvException e) { 515 e.printStackTrace(); 516 } catch (Exception e) { 517 e.printStackTrace(); 518 } 519 520 verify(mockServiceEndpoint); 521 verify(mockInNormalizedMessage); 522 verify(mockOutNormalizedMessage); 523 } 525 526 } 527 | Popular Tags |