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.Item;
12  import org.mule.module.xml.util.XMLUtils;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.transformer.types.DataTypeFactory;
15  
16  import org.junit.Test;
17  import org.w3c.dom.Document;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class JaxbAutoTransformerTestCase extends AbstractMuleContextTestCase
24  {
25      public static final String ITEM_XML = "<item><code>1234</code><description>Vacuum Cleaner</description><in-stock>true</in-stock></item>";
26  
27      @Test
28      public void testCustomTransform() throws Exception
29      {
30          MuleMessage message = new DefaultMuleMessage(ITEM_XML, muleContext);
31          Item item = (Item) message.getPayload(DataTypeFactory.create(Item.class));
32  
33          assertNotNull(item);
34          assertEquals("1234", item.getCode());
35          assertEquals("Vacuum Cleaner", item.getDescription());
36          assertTrue(item.isInStock());
37  
38          //and back again
39          Document doc = (Document) message.getPayload(DataTypeFactory.create(Document.class));
40          
41          assertNotNull(doc);
42          assertEquals("1234", XMLUtils.selectValue("/item/code", doc));
43          assertEquals("Vacuum Cleaner", XMLUtils.selectValue("/item/description", doc));
44          assertEquals("true", XMLUtils.selectValue("/item/in-stock", doc));
45      }
46  }