View Javadoc

1   /*
2    * $Id: XStreamAdditionalConvertersTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.xml.functional;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.AbstractServiceAndFlowTestCase;
16  import org.mule.tck.functional.EventCallback;
17  import org.mule.tck.functional.FunctionalTestComponent;
18  
19  import com.thoughtworks.xstream.converters.extended.ISO8601DateConverter;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Date;
24  import java.util.concurrent.CountDownLatch;
25  import java.util.concurrent.TimeUnit;
26  
27  import org.junit.Test;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertTrue;
32  
33  public class XStreamAdditionalConvertersTestCase extends AbstractServiceAndFlowTestCase
34  {
35      private CountDownLatch latch = new CountDownLatch(1);
36  
37      @Parameters
38      public static Collection<Object[]> parameters()
39      {
40          return Arrays.asList(new Object[][]{
41              {ConfigVariant.SERVICE, "org/mule/module/xml/xstream-additional-converters-service.xml"},
42              {ConfigVariant.FLOW, "org/mule/module/xml/xstream-additional-converters-flow.xml"}});
43      }
44  
45      public XStreamAdditionalConvertersTestCase(ConfigVariant variant, String configResources)
46      {
47          super(variant, configResources);
48      }
49  
50      @Override
51      protected void doSetUp() throws Exception
52      {
53          super.doSetUp();
54  
55          FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("ObjectToXml");
56          assertNotNull(testComponent);
57          testComponent.setEventCallback(new Callback(latch));
58      }
59  
60      @Test
61      public void testAdditionalConverters() throws Exception
62      {
63          ISO8601DateConverter converter = new ISO8601DateConverter();
64          String timestamp = converter.toString(new Date(System.currentTimeMillis()));
65          String input = "<test-bean><createDate>" + timestamp + "</createDate></test-bean>";
66  
67          MuleClient client = new MuleClient(muleContext);
68          client.dispatch("vm://FromTest", input, null);
69  
70          assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
71      }
72  
73      private static class Callback implements EventCallback
74      {
75          private CountDownLatch testLatch;
76  
77          public Callback(CountDownLatch latch)
78          {
79              testLatch = latch;
80          }
81  
82          @Override
83          public void eventReceived(MuleEventContext context, Object component) throws Exception
84          {
85              Object payload = context.getMessage().getPayload();
86              assertTrue(payload instanceof TestBean);
87              assertNotNull(((TestBean) payload).getCreateDate());
88  
89              testLatch.countDown();
90          }
91      }
92  
93      public static class TestBean
94      {
95          private Date createDate = null;
96  
97          public Date getCreateDate()
98          {
99              return createDate;
100         }
101 
102         public void setCreateDate(Date createDate)
103         {
104             this.createDate = createDate;
105         }
106     }
107 }