View Javadoc

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