View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.file.transformers;
8   
9   import org.mule.api.transformer.Transformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.AbstractTransformerTestCase;
12  import org.mule.util.FileUtils;
13  import org.mule.util.SystemUtils;
14  
15  import java.io.File;
16  import java.io.FileWriter;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
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      @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          _testData = FileUtils.newFile(SystemUtils.JAVA_IO_TMPDIR, "FileToStringTestData");
37          FileWriter fw = new FileWriter(_testData);
38          fw.write(_resultData);
39          fw.close();
40      }
41  
42      @Override
43      protected void doTearDown() throws Exception
44      {
45          assertTrue(_testData.delete());
46          super.doTearDown();
47      }
48  
49      @Override
50      public Object getResultData()
51      {
52          return _resultData;
53      }
54  
55      @Override
56      public Transformer getRoundTripTransformer() throws Exception
57      {
58          return null;
59      }
60  
61      @Override
62      public Object getTestData()
63      {
64          return _testData;
65      }
66  
67      @Override
68      public Transformer getTransformer() throws Exception
69      {
70          return new FileToString();
71      }
72  
73      /**
74       * Transform with a wrong encoding should result in an Exception to be thrown
75       */
76      @Test
77      public void testTransformExcEnc() throws Exception
78      {
79          try
80          {
81              FileToString fts = (FileToString)getTransformer();
82              fts.doTransform(getTestData(), "NO-SUCH_ENCODING");
83              fail("Should fail when the specified encoding is not supported");
84          }
85          catch (TransformerException tfe)
86          {
87              // Expected
88          }
89      }
90  
91  }