KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > csv > CsvProcessorTest


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: CsvProcessorTest.java 1202 2006-11-06 20:20:41Z dutoo $
20  * -------------------------------------------------------------------------
21  */

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 JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 import javax.activation.DataHandler JavaDoc;
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 JavaDoc;
44 import javax.xml.transform.Source JavaDoc;
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 /**
60  * Tests the Csv processor
61  *
62  * @version $Rev: 1069 $Date: ${date}
63  * @since Petals 1.0
64  * @author Marc Dutoo - Open Wide
65  *
66  */

67 public class CsvProcessorTest extends TestCase {
68
69     private String JavaDoc baseDir;
70
71     private Logger JavaDoc logger;
72
73     @Override JavaDoc
74     public void setUp() throws Exception JavaDoc {
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 JavaDoc.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 JavaDoc testCsvInput, String JavaDoc testXmlOutput) {
101         MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel();
102
103         Properties JavaDoc csvProperties = new Properties JavaDoc();
104         csvProperties.setProperty(CsvProcessor.CSV_INPUT_MODE_PROP,
105             CsvProcessor.CSV_INPUT_MODE_CONTENT);
106         Map JavaDoc<String JavaDoc, Properties JavaDoc> mapEndpointCsv = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
107         mapEndpointCsv.put("myServiceName", csvProperties);
108         CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel,
109             logger, mapEndpointCsv);
110
111         // now test :
112
try {
113             mockDeliveryChannel.createExchangeFactory()
114                 .createInOutExchange();
115         } catch (MessagingException e) {
116             e.printStackTrace();
117         }
118         final MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class);
119         // expect(mockServiceEndpoint.getEndpointName()).andReturn("myEndpointName");
120
expect(mockServiceEndpoint.getServiceName()).andReturn(
121             new QName JavaDoc("myServiceName"));
122
123         final QName JavaDoc myOperation = new QName JavaDoc(CsvProcessor.CSV_TO_XML_OPERATION);
124
125         final NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class);
126         String JavaDoc myContent = testCsvInput;
127         Source JavaDoc contentSource = SourceHelper.createContentSource(myContent);
128         // NB. we can't merely use createSource(), since its transformation to
129
// string
130
// would fail without it being actual XML
131
expect(mockInNormalizedMessage.getContent()).andReturn(contentSource);
132         // NB. content transformation mode, so no attachment
133
// Set<String> attachmentNames = new HashSet<String>();
134
// String attachmentName1 = "myFirstAttachment";
135
// attachmentNames.add(attachmentName1);
136
// expect(mockInNormalizedMessage.getAttachmentNames()).andReturn(attachmentNames);
137
// expect(mockInNormalizedMessage.getAttachment(attachmentName1)).andReturn(
138
// new DataHandler(new ByteArrayDataSource(testXmlOutput)));
139

140         final NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class);
141
142         /*
143          * MessageExchange mockMessageExchange =
144          * createMock(MessageExchange.class);
145          * expect(mockMessageExchange.getStatus()).andReturn(ExchangeStatus.ACTIVE);
146          * expect(mockMessageExchange.getRole()).andReturn(MessageExchange.Role.PROVIDER);
147          * expect(mockMessageExchange.getPattern()).andReturn(MEPConstants.IN_OUT_PATTERN).atLeastOnce(); //
148          * asked for more than once
149          * expect(mockMessageExchange.getFault()).andReturn(null); try {
150          * expect(mockMessageExchange.createMessage()).andReturn(mockOutNormalizedMessage); }
151          * catch (Exception e) { e.printStackTrace(); }
152          * expect(mockMessageExchange.getEndpoint()).andReturn(mockServiceEndpoint);
153          * expect(mockMessageExchange.getOperation()).andReturn(myOperation);
154          * expect(mockMessageExchange.getMessage("in")).andReturn(mockInNormalizedMessage);
155          */

156         MessageExchange mockMessageExchange = new MockMessageExchange() {
157             // NB. Fault already returns null;
158
@Override JavaDoc
159             public NormalizedMessage createMessage() throws MessagingException {
160                 return mockOutNormalizedMessage;
161             }
162
163             @Override JavaDoc
164             public ServiceEndpoint getEndpoint() {
165                 return mockServiceEndpoint;
166             }
167
168             @Override JavaDoc
169             public NormalizedMessage getMessage(String JavaDoc name) {
170                 return mockInNormalizedMessage;
171             }
172
173             @Override JavaDoc
174             public QName JavaDoc getOperation() {
175                 return myOperation;
176             }
177
178             @Override JavaDoc
179             public URI JavaDoc getPattern() {
180                 return MEPConstants.IN_OUT_PATTERN;
181             }
182
183             @Override JavaDoc
184             public Role getRole() {
185                 return MessageExchange.Role.PROVIDER;
186             }
187
188             @Override JavaDoc
189             public ExchangeStatus getStatus() {
190                 return ExchangeStatus.ACTIVE;
191             }
192         };
193
194         try {
195             mockOutNormalizedMessage.setContent(eqSource(SourceHelper
196                 .createSource(testXmlOutput)));// TODO check
197
// expectLastCall().andAnswer(new IAnswer<void>() {
198
mockMessageExchange.setMessage(mockOutNormalizedMessage, "out");
199         } catch (MessagingException e) {
200             e.printStackTrace();
201         }
202
203         replay(mockServiceEndpoint);
204         replay(mockInNormalizedMessage);
205         replay(mockOutNormalizedMessage);
206         // replay(mockMessageExchange);
207

208         try {
209             // Function to test
210
csvProcessor.process(mockMessageExchange);
211         } catch (CsvException e) {
212             e.printStackTrace();
213         } catch (Exception JavaDoc e) {
214             e.printStackTrace();
215         }
216
217         verify(mockServiceEndpoint);
218         verify(mockInNormalizedMessage);
219         verify(mockOutNormalizedMessage);
220         // verify(mockMessageExchange);
221
}
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 JavaDoc testCsvInput, String JavaDoc testXmlOutput) {
241         MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel();
242
243         Properties JavaDoc csvProperties = new Properties JavaDoc();
244         // NB. default CSV input mode is guessing
245
Map JavaDoc<String JavaDoc, Properties JavaDoc> mapEndpointCsv = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
246         mapEndpointCsv.put("myServiceName", csvProperties);
247         CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel,
248             logger, mapEndpointCsv);
249
250         // now test :
251
try {
252             mockDeliveryChannel.createExchangeFactory()
253                 .createInOutExchange();
254         } catch (MessagingException e) {
255             e.printStackTrace();
256         }
257
258         MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class);
259         // expect(mockServiceEndpoint.getEndpointName()).andReturn("myEndpointName");
260
expect(mockServiceEndpoint.getServiceName()).andReturn(
261             new QName JavaDoc("myServiceName"));
262
263         QName JavaDoc myOperation = new QName JavaDoc(CsvProcessor.CSV_TO_XML_OPERATION);
264
265         NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class);
266         // NB. attachment transformation mode, so no content
267
// String myContent = "youpi";
268
// Source contentSource = SourceHelper.createContentSource(myContent);
269
// expect(mockInNormalizedMessage.getContent()).andReturn(contentSource);
270
Set JavaDoc<String JavaDoc> attachmentNames = new HashSet JavaDoc<String JavaDoc>();
271         String JavaDoc attachmentName1 = "myFirstAttachment";
272         attachmentNames.add(attachmentName1);
273         expect(mockInNormalizedMessage.getAttachmentNames()).andReturn(
274             attachmentNames).times(2); // twice because of "GUESS" mode
275
expect(mockInNormalizedMessage.getAttachment(attachmentName1))
276             .andReturn(new DataHandler JavaDoc(new ByteArrayDataSource(testCsvInput)));// TODO
277

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 JavaDoc 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             // expectLastCall().andAnswer(new IAnswer<void>() {
304
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             // Function to test
316
csvProcessor.process(mockMessageExchange);
317         } catch (CsvException e) {
318             e.printStackTrace();
319         } catch (Exception JavaDoc e) {
320             e.printStackTrace();
321         }
322
323         verify(mockServiceEndpoint);
324         verify(mockInNormalizedMessage);
325         verify(mockOutNormalizedMessage);
326         verify(mockMessageExchange);
327     }
328
329     /**
330      * Custom argument matcher for Source
331      *
332      * @author mdutoo
333      *
334      */

335     public static class SourceArgumentMatcher implements IArgumentMatcher {
336         private Source JavaDoc expectedSource;
337
338         public SourceArgumentMatcher(Source JavaDoc expectedSource) {
339             this.expectedSource = expectedSource;
340         }
341
342         public boolean matches(Object JavaDoc found) {
343             if (!(found instanceof Source JavaDoc)) {
344                 return false;
345             }
346             Source JavaDoc foundSource = (Source JavaDoc) found;
347             try {
348                 String JavaDoc foundSourceContent = SourceHelper
349                     .createString(foundSource);
350                 String JavaDoc expectedSourceContent = SourceHelper
351                     .createString(expectedSource);
352                 return StringHelper.equal(foundSourceContent,
353                     expectedSourceContent);
354             } catch (Exception JavaDoc e) {
355                 return false;
356             }
357         }
358
359         public void appendTo(StringBuffer JavaDoc sbuf) {
360             String JavaDoc expectedSourceContent;
361             try {
362                 expectedSourceContent = SourceHelper
363                     .createString(expectedSource);
364             } catch (Exception JavaDoc 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     /**
375      * Helps using the source argument matcher
376      *
377      * @param in
378      * @return
379      */

380     public static Source JavaDoc eqSource(Source JavaDoc 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 JavaDoc testCsvInput, String JavaDoc testXmlOutput) {
404         MockDeliveryChannel mockDeliveryChannel = new MockDeliveryChannel();
405
406         Properties JavaDoc csvProperties = new Properties JavaDoc();
407         csvProperties.setProperty(CsvProcessor.CSV_INPUT_MODE_PROP,
408             CsvProcessor.CSV_INPUT_MODE_CONTENT);
409         Map JavaDoc<String JavaDoc, Properties JavaDoc> mapEndpointCsv = new HashMap JavaDoc<String JavaDoc, Properties JavaDoc>();
410         mapEndpointCsv.put("myServiceName", csvProperties);
411         CsvProcessor csvProcessor = new CsvProcessor(mockDeliveryChannel,
412             logger, mapEndpointCsv);
413
414         // now test :
415
try {
416             mockDeliveryChannel.createExchangeFactory()
417                 .createInOutExchange();
418         } catch (MessagingException e) {
419             e.printStackTrace();
420         }
421         final MockServiceEndpoint mockServiceEndpoint = createMock(MockServiceEndpoint.class);
422         // expect(mockServiceEndpoint.getEndpointName()).andReturn("myEndpointName");
423
expect(mockServiceEndpoint.getServiceName()).andReturn(
424             new QName JavaDoc("myServiceName"));
425
426         final QName JavaDoc myOperation = new QName JavaDoc(CsvProcessor.XML_TO_CSV_OPERATION);
427
428         final NormalizedMessage mockInNormalizedMessage = createMock(NormalizedMessage.class);
429         String JavaDoc myContent = testCsvInput;
430         Source JavaDoc contentSource = SourceHelper.createSource(myContent);
431         // NB. we can't merely use createSource(), since its transformation to
432
// string
433
// would fail without it being actual XML
434
expect(mockInNormalizedMessage.getContent()).andReturn(contentSource);
435         // NB. content transformation mode, so no attachment
436
// Set<String> attachmentNames = new HashSet<String>();
437
// String attachmentName1 = "myFirstAttachment";
438
// attachmentNames.add(attachmentName1);
439
// expect(mockInNormalizedMessage.getAttachmentNames()).andReturn(attachmentNames);
440
// expect(mockInNormalizedMessage.getAttachment(attachmentName1)).andReturn(
441
// new DataHandler(new ByteArrayDataSource(testXmlOutput)));
442

443         final NormalizedMessage mockOutNormalizedMessage = createMock(NormalizedMessage.class);
444
445         /*
446          * MessageExchange mockMessageExchange =
447          * createMock(MessageExchange.class);
448          * expect(mockMessageExchange.getStatus()).andReturn(ExchangeStatus.ACTIVE);
449          * expect(mockMessageExchange.getRole()).andReturn(MessageExchange.Role.PROVIDER);
450          * expect(mockMessageExchange.getPattern()).andReturn(MEPConstants.IN_OUT_PATTERN).atLeastOnce(); //
451          * asked for more than once
452          * expect(mockMessageExchange.getFault()).andReturn(null); try {
453          * expect(mockMessageExchange.createMessage()).andReturn(mockOutNormalizedMessage); }
454          * catch (Exception e) { e.printStackTrace(); }
455          * expect(mockMessageExchange.getEndpoint()).andReturn(mockServiceEndpoint);
456          * expect(mockMessageExchange.getOperation()).andReturn(myOperation);
457          * expect(mockMessageExchange.getMessage("in")).andReturn(mockInNormalizedMessage);
458          */

459         MessageExchange mockMessageExchange = new MockMessageExchange() {
460             // NB. Fault already returns null;
461
@Override JavaDoc
462             public NormalizedMessage createMessage() throws MessagingException {
463                 return mockOutNormalizedMessage;
464             }
465
466             @Override JavaDoc
467             public ServiceEndpoint getEndpoint() {
468                 return mockServiceEndpoint;
469             }
470
471             @Override JavaDoc
472             public NormalizedMessage getMessage(String JavaDoc name) {
473                 return mockInNormalizedMessage;
474             }
475
476             @Override JavaDoc
477             public QName JavaDoc getOperation() {
478                 return myOperation;
479             }
480
481             @Override JavaDoc
482             public URI JavaDoc getPattern() {
483                 return MEPConstants.IN_OUT_PATTERN;
484             }
485
486             @Override JavaDoc
487             public Role getRole() {
488                 return MessageExchange.Role.PROVIDER;
489             }
490
491             @Override JavaDoc
492             public ExchangeStatus getStatus() {
493                 return ExchangeStatus.ACTIVE;
494             }
495         };
496
497         try {
498             mockOutNormalizedMessage.setContent(eqSource(SourceHelper
499                 .createSource(testXmlOutput)));// TODO check
500
// expectLastCall().andAnswer(new IAnswer<void>() {
501
mockMessageExchange.setMessage(mockOutNormalizedMessage, "out");
502         } catch (MessagingException e) {
503             e.printStackTrace();
504         }
505
506         replay(mockServiceEndpoint);
507         replay(mockInNormalizedMessage);
508         replay(mockOutNormalizedMessage);
509         // replay(mockMessageExchange);
510

511         try {
512             // Function to test
513
csvProcessor.process(mockMessageExchange);
514         } catch (CsvException e) {
515             e.printStackTrace();
516         } catch (Exception JavaDoc e) {
517             e.printStackTrace();
518         }
519
520         verify(mockServiceEndpoint);
521         verify(mockInNormalizedMessage);
522         verify(mockOutNormalizedMessage);
523         // verify(mockMessageExchange);
524
}
525
526 }
527
Popular Tags