1   /*
2    * $Id: FileToStringTestCase.java 10787 2008-02-12 18:51:50Z dfeist $
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.transport.file.transformers;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.api.transformer.Transformer;
15  import org.mule.transformer.AbstractTransformerTestCase;
16  import org.mule.transport.file.transformers.FileToString;
17  import org.mule.util.FileUtils;
18  import org.mule.util.SystemUtils;
19  
20  import java.io.File;
21  import java.io.FileWriter;
22  
23  /**
24   * Test case for FileToString transformer
25   */
26  public class FileToStringTestCase extends AbstractTransformerTestCase
27  {
28      FileToString _fts;
29      File _testData = null;
30      final String _resultData = "The dog is on the table, where's the dog?";
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see org.mule.tck.AbstractTransformerTestCase#doSetUp()
36       */
37      protected void doSetUp() throws Exception
38      {
39          super.doSetUp();
40          _testData = FileUtils.newFile(SystemUtils.JAVA_IO_TMPDIR, "FileToStringTestData");
41          FileWriter fw = new FileWriter(_testData);
42          fw.write(_resultData);
43          fw.close();
44      }
45  
46      /*
47       * (non-Javadoc)
48       * 
49       * @see org.mule.tck.AbstractTransformerTestCase#doTearDown()
50       */
51      protected void doTearDown() throws Exception
52      {
53          assertTrue(_testData.delete());
54          super.doTearDown();
55      }
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see org.mule.tck.AbstractTransformerTestCase#getResultData()
61       */
62      public Object getResultData()
63      {
64          return _resultData;
65      }
66  
67      /*
68       * (non-Javadoc)
69       * 
70       * @see org.mule.tck.AbstractTransformerTestCase#getRoundTripTransformer()
71       */
72      public Transformer getRoundTripTransformer() throws Exception
73      {
74          return null;
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see org.mule.tck.AbstractTransformerTestCase#getTestData()
81       */
82      public Object getTestData()
83      {
84          return _testData;
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see org.mule.tck.AbstractTransformerTestCase#getTransformer()
91       */
92      public Transformer getTransformer() throws Exception
93      {
94          return new FileToString();
95      }
96  
97      /**
98       * Transform with a wrong encoding should result in an Exception to be thrown
99       */
100     public void testTransformExcEnc() throws Exception
101     {
102         try
103         {
104             FileToString fts = (FileToString)getTransformer();
105             fts.doTransform(getTestData(), "NO-SUCH_ENCODING");
106             fail("Should fail when the specified encoding is not supported");
107         }
108         catch (TransformerException tfe)
109         {
110             // Expected
111         }
112     }
113 
114 }