View Javadoc

1   /*
2    * $Id: FileToStringTestCase.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.transport.file.transformers;
12  
13  import org.mule.api.transformer.Transformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractTransformerTestCase;
16  import org.mule.util.FileUtils;
17  import org.mule.util.SystemUtils;
18  
19  import java.io.File;
20  import java.io.FileWriter;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  /**
28   * Test case for FileToString transformer
29   */
30  public class FileToStringTestCase extends AbstractTransformerTestCase
31  {
32      FileToString _fts;
33      File _testData = null;
34      final String _resultData = "The dog is on the table, where's the dog?";
35  
36      @Override
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      @Override
47      protected void doTearDown() throws Exception
48      {
49          assertTrue(_testData.delete());
50          super.doTearDown();
51      }
52  
53      @Override
54      public Object getResultData()
55      {
56          return _resultData;
57      }
58  
59      @Override
60      public Transformer getRoundTripTransformer() throws Exception
61      {
62          return null;
63      }
64  
65      @Override
66      public Object getTestData()
67      {
68          return _testData;
69      }
70  
71      @Override
72      public Transformer getTransformer() throws Exception
73      {
74          return new FileToString();
75      }
76  
77      /**
78       * Transform with a wrong encoding should result in an Exception to be thrown
79       */
80      @Test
81      public void testTransformExcEnc() throws Exception
82      {
83          try
84          {
85              FileToString fts = (FileToString)getTransformer();
86              fts.doTransform(getTestData(), "NO-SUCH_ENCODING");
87              fail("Should fail when the specified encoding is not supported");
88          }
89          catch (TransformerException tfe)
90          {
91              // Expected
92          }
93      }
94  
95  }