View Javadoc

1   /*
2    * $Id: BasicTypeAutoTransformationTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.simple;
12  
13  import org.mule.api.transformer.Transformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  
17  import java.math.BigDecimal;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  public class BasicTypeAutoTransformationTestCase extends AbstractMuleContextTestCase
24  {
25  
26      @Test
27      public void testTypes() throws TransformerException
28      {
29          testType("1", Integer.class, Integer.TYPE, Integer.valueOf(1));
30          testType("1", Long.class, Long.TYPE, Long.valueOf(1));
31          testType("1", Short.class, Short.TYPE, Short.valueOf((short) 1));
32          testType("1.1", Double.class, Double.TYPE, Double.valueOf(1.1));
33          testType("1.1", Float.class, Float.TYPE, Float.valueOf((float) 1.1));
34          testType("1.1", BigDecimal.class, null, BigDecimal.valueOf(1.1));
35          testType("true", Boolean.class, Boolean.TYPE, Boolean.TRUE);
36      }
37  
38      protected void testType(String string, Class type, Class primitive, Object value)
39          throws TransformerException
40      {
41          assertEquals(value, lookupFromStringTransformer(type).transform(string));
42          assertEquals(string, lookupToStringTransformer(type).transform(value));
43          if (primitive != null)
44          {
45              assertEquals(value, lookupFromStringTransformer(primitive).transform(string));
46              assertEquals(string, lookupToStringTransformer(primitive).transform(value));
47          }
48      }
49  
50      private Transformer lookupFromStringTransformer(Class to) throws TransformerException
51      {
52          return muleContext.getRegistry().lookupTransformer(String.class, to);
53      }
54  
55      private Transformer lookupToStringTransformer(Class from) throws TransformerException
56      {
57          return muleContext.getRegistry().lookupTransformer(from, String.class);
58      }
59  
60  }