View Javadoc

1   /*
2    * $Id: JaxbTransformerTestCase.java 22387 2011-07-12 03:53:36Z 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  package org.mule.transformers.jaxb;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleMessage;
14  import org.mule.jaxb.model.EmailAddress;
15  import org.mule.jaxb.model.Person;
16  import org.mule.tck.junit4.AbstractMuleContextTestCase;
17  import org.mule.transformer.types.DataTypeFactory;
18  import org.mule.transformer.types.ListDataType;
19  
20  import java.io.ByteArrayInputStream;
21  import java.util.List;
22  
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  
28  public class JaxbTransformerTestCase extends AbstractMuleContextTestCase
29  {
30      public static final String PERSON_XML = "<person><name>John Doe</name><dob>01/01/1970</dob><emailAddresses><emailAddress><type>home</type><address>john.doe@gmail.com</address></emailAddress><emailAddress><type>work</type><address>jdoe@bigco.com</address></emailAddress></emailAddresses></person>";
31  
32      @Override
33      public void doSetUp() throws Exception
34      {
35          muleContext.getRegistry().registerObject("trans", new JAXBTestTransformers());
36      }
37  
38      @Test
39      public void testCustomTransform() throws Exception
40      {
41          MuleMessage message = new DefaultMuleMessage(PERSON_XML, muleContext);
42          Person person = message.getPayload(DataTypeFactory.create(Person.class));
43          assertNotNull(person);
44          assertEquals("John Doe", person.getName());
45          assertEquals("01/01/1970", person.getDob());
46          assertEquals(2, person.getEmailAddresses().size());
47          assertEquals("home", person.getEmailAddresses().get(0).getType());
48          assertEquals("john.doe@gmail.com", person.getEmailAddresses().get(0).getAddress());
49          assertEquals("work", person.getEmailAddresses().get(1).getType());
50          assertEquals("jdoe@bigco.com", person.getEmailAddresses().get(1).getAddress());
51      }
52  
53      @Test
54      public void testCustomTransformWithMuleMessage() throws Exception
55      {
56          ByteArrayInputStream in = new ByteArrayInputStream(PERSON_XML.getBytes());
57          DefaultMuleMessage msg = new DefaultMuleMessage(in, muleContext);
58          msg.setInboundProperty("foo", "fooValue");
59          List<EmailAddress> emailAddresses = msg.getPayload(new ListDataType<List<EmailAddress>>(EmailAddress.class));
60          assertNotNull(emailAddresses);
61          assertEquals(2, emailAddresses.size());
62          assertEquals("home", emailAddresses.get(0).getType());
63          assertEquals("john.doe@gmail.com", emailAddresses.get(0).getAddress());
64          assertEquals("work", emailAddresses.get(1).getType());
65          assertEquals("jdoe@bigco.com", emailAddresses.get(1).getAddress());
66      }
67  }