View Javadoc

1   /*
2    * $Id: NullResultTestCase.java 22377 2011-07-11 12:41:42Z 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  
11  package org.mule.transformer;
12  
13  import org.mule.api.transformer.Transformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.transport.NullPayload;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.fail;
21  
22  public class NullResultTestCase extends AbstractTransformerTestCase
23  {
24      private final NullResultTransformer transformer = new NullResultTransformer();
25  
26      @Override
27      public Object getTestData()
28      {
29          return new Object();
30      }
31  
32      @Override
33      public Object getResultData()
34      {
35          return NullPayload.getInstance();
36      }
37  
38      @Override
39      public Transformer getTransformer() throws Exception
40      {
41          return transformer;
42      }
43  
44      @Override
45      public Transformer getRoundTripTransformer() throws Exception
46      {
47          return null;
48      }
49  
50      @Test
51      public void testNullNotExpected() throws Exception
52      {
53          transformer.setReturnDataType(DataTypeFactory.STRING);
54          try
55          {
56              testTransform();
57              fail("Transformer should have thrown an exception because the return class doesn't match the result.");
58          }
59          catch (TransformerException e)
60          {
61              // expected
62          }
63      }
64  
65      public static final class NullResultTransformer extends AbstractTransformer
66      {
67          public NullResultTransformer()
68          {
69              super();
70              this.registerSourceType(DataTypeFactory.OBJECT);
71              this.setReturnDataType(DataTypeFactory.create(NullPayload.class));
72          }
73  
74          @Override
75          public Object doTransform(Object src, String encoding) throws TransformerException
76          {
77              return null;
78          }
79      }
80  }