1   /*
2    * $Id: AbstractConverterTestCase.java 7963 2007-08-21 08:53:15Z 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.test.config;
12  
13  import org.mule.MuleManager;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.umo.manager.UMOManager;
16  
17  import com.mockobjects.dynamic.C;
18  import com.mockobjects.dynamic.Mock;
19  
20  import org.apache.commons.beanutils.Converter;
21  
22  public abstract class AbstractConverterTestCase extends AbstractMuleTestCase
23  {
24  
25      public void testNullConverter()
26      {
27          try
28          {
29              getConverter().convert(getValidConvertedType().getClass(), null);
30              fail("Should thow exception on null");
31          }
32          catch (Exception e)
33          {
34              // expected
35          }
36      }
37  
38      public void testObjectAlreadyConverted()
39      {
40          Object obj = getValidConvertedType();
41          Object result = getConverter().convert(obj.getClass(), obj);
42  
43          assertNotNull(result);
44          assertEquals(obj, result);
45      }
46  
47      public void testValidConversion()
48      {
49          Mock mockManager = new Mock(UMOManager.class);
50          try
51          {
52              MuleManager.setInstance((UMOManager)mockManager.proxy());
53              Object obj = getValidConvertedType();
54              mockManager.expectAndReturn(getLookupMethod(), C.eq("test://Test"), obj);
55              Object result = getConverter().convert(obj.getClass(), "test://Test");
56  
57              assertNotNull(result);
58              mockManager.verify();
59          }
60          finally
61          {
62              MuleManager.setInstance(null);
63          }
64      }
65  
66      public void testInvalidConversion()
67      {
68          Mock mockManager = new Mock(UMOManager.class);
69          MuleManager.setInstance((UMOManager)mockManager.proxy());
70          Object obj = getValidConvertedType();
71          mockManager.expectAndReturn(getLookupMethod(), C.eq("TestBad"), null);
72          try
73          {
74              getConverter().convert(obj.getClass(), "TestBad");
75              fail("should throw an exception if not found");
76          }
77          catch (Exception e)
78          {
79              // exprected
80              mockManager.verify();
81          }
82          finally
83          {
84              MuleManager.setInstance(null);
85          }
86  
87      }
88  
89      public abstract Converter getConverter();
90  
91      public abstract Object getValidConvertedType();
92  
93      public abstract String getLookupMethod();
94  }