View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformers.jaxb;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.jaxb.model.EmailAddress;
12  import org.mule.jaxb.model.Person;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.transformer.types.DataTypeFactory;
15  import org.mule.transformer.types.ListDataType;
16  
17  import java.io.ByteArrayInputStream;
18  import java.util.List;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  public class JaxbTransformerTestCase extends AbstractMuleContextTestCase
26  {
27      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>";
28  
29      @Override
30      public void doSetUp() throws Exception
31      {
32          muleContext.getRegistry().registerObject("trans", new JAXBTestTransformers());
33      }
34  
35      @Test
36      public void testCustomTransform() throws Exception
37      {
38          MuleMessage message = new DefaultMuleMessage(PERSON_XML, muleContext);
39          Person person = (Person) message.getPayload(DataTypeFactory.create(Person.class));
40          assertNotNull(person);
41          assertEquals("John Doe", person.getName());
42          assertEquals("01/01/1970", person.getDob());
43          assertEquals(2, person.getEmailAddresses().size());
44          assertEquals("home", person.getEmailAddresses().get(0).getType());
45          assertEquals("john.doe@gmail.com", person.getEmailAddresses().get(0).getAddress());
46          assertEquals("work", person.getEmailAddresses().get(1).getType());
47          assertEquals("jdoe@bigco.com", person.getEmailAddresses().get(1).getAddress());
48      }
49  
50      @Test
51      public void testCustomTransformWithMuleMessage() throws Exception
52      {
53          ByteArrayInputStream in = new ByteArrayInputStream(PERSON_XML.getBytes());
54          DefaultMuleMessage msg = new DefaultMuleMessage(in, muleContext);
55          msg.setInboundProperty("foo", "fooValue");
56          //List<EmailAddress> emailAddresses = iBeansContext.transform(msg, new ListDataType<List<EmailAddress>>(EmailAddress.class));
57          List<EmailAddress> emailAddresses = msg.getPayload(new ListDataType<List<EmailAddress>>(EmailAddress.class));
58          assertNotNull(emailAddresses);
59          assertEquals(2, emailAddresses.size());
60          assertEquals("home", emailAddresses.get(0).getType());
61          assertEquals("john.doe@gmail.com", emailAddresses.get(0).getAddress());
62          assertEquals("work", emailAddresses.get(1).getType());
63          assertEquals("jdoe@bigco.com", emailAddresses.get(1).getAddress());
64      }
65  
66      
67  }