View Javadoc

1   /*
2    * $Id: JaxbRoundtripNonRootElementTestCase.java 22556 2011-07-25 21:01:33Z mike.schilling $
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.custommonkey.xmlunit.XMLUnit;
13  import org.mule.api.transformer.Transformer;
14  import org.mule.jaxb.model.EmailAddress;
15  import org.mule.jaxb.model.Person;
16  import org.mule.module.xml.transformer.jaxb.JAXBMarshallerTransformer;
17  import org.mule.module.xml.transformer.jaxb.JAXBUnmarshallerTransformer;
18  import org.mule.module.xml.util.XMLUtils;
19  import org.mule.transformer.AbstractTransformerTestCase;
20  import org.mule.transformer.types.DataTypeFactory;
21  import org.w3c.dom.Document;
22  
23  import javax.xml.bind.JAXBContext;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  public class JaxbRoundtripNonRootElementTestCase extends JaxbRoundtripTestCase
28  {
29  
30      @Override
31      public Transformer getTransformer() throws Exception
32      {
33          JAXBUnmarshallerTransformer t = new JAXBUnmarshallerTransformer(ctx, DataTypeFactory.create(String.class));
34          initialiseObject(t);
35          return t;
36      }
37  
38      @Override
39      public Transformer getRoundTripTransformer() throws Exception
40      {
41          // Since we're transforming to a non-JAXB type, we can't round-trip
42          return null;
43      }
44  
45      @Override
46      public Object getTestData()
47      {
48          try
49          {
50              Document doc = XMLUtils.toW3cDocument(super.getTestData());
51              return doc.getDocumentElement().getFirstChild();
52          }
53          catch (Exception e)
54          {
55              throw new RuntimeException(e);
56          }
57      }
58  
59      @Override
60      public Object getResultData()
61      {
62          return ((Person)super.getResultData()).getName();
63      }
64  }