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.transformer.AbstractTransformerTestCase;
11  import org.mule.util.FileUtils;
12  import org.mule.util.SystemUtils;
13  
14  import java.io.File;
15  import java.io.FileInputStream;
16  import java.io.FileWriter;
17  
18  import edu.emory.mathcs.backport.java.util.Arrays;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  public class FileToByteArrayTestCase extends AbstractTransformerTestCase
24  {
25      private static final String TEST_STRING = "The dog is on the table, where's the dog?";
26  
27      private File testFile;
28      private byte[] resultData;
29  
30      @Override
31      protected void doSetUp() throws Exception
32      {
33          super.doSetUp();
34          resultData = TEST_STRING.getBytes(muleContext.getConfiguration().getDefaultEncoding());
35          testFile = FileUtils.newFile(SystemUtils.JAVA_IO_TMPDIR, "FileToStringTestData");
36          FileWriter fw = new FileWriter(testFile);
37          try
38          {
39              fw.write(TEST_STRING);
40          }
41          finally
42          {
43              fw.close();
44          }
45      }
46  
47      @Override
48      protected void doTearDown() throws Exception
49      {
50          assertTrue(testFile.delete());
51          super.doTearDown();
52      }
53  
54      @Override
55      public Transformer getTransformer() throws Exception
56      {
57          return new FileToByteArray();
58      }
59  
60      @Override
61      public Object getResultData()
62      {
63          return resultData;
64      }
65  
66      @Override
67      public Transformer getRoundTripTransformer() throws Exception
68      {
69          return null;
70      }
71  
72      @Override
73      public Object getTestData()
74      {
75          return testFile;
76      }
77  
78      @Test
79      public void testTransformInputStream() throws Exception
80      {
81          FileInputStream fis = new FileInputStream(testFile);
82          try
83          {
84              assertTrue(Arrays.equals(resultData, (byte[]) getTransformer().transform(fis)));
85          }
86          finally
87          {
88              fis.close();
89          }
90      }
91  
92      @Test
93      public void testTransformByteArray() throws Exception
94      {
95          FileInputStream fis = new FileInputStream(testFile);
96          byte[] bytes = new byte[(int) testFile.length()];
97          try
98          {
99              int count;
100             while ((count = fis.read(bytes)) != -1)
101             {
102                 // read fully
103             }
104             assertTrue(Arrays.equals(resultData, (byte[]) getTransformer().transform(bytes)));
105         }
106         finally
107         {
108             fis.close();
109         }
110     }
111 
112     @Test
113     public void testTransformString() throws Exception
114     {
115         assertTrue(Arrays.equals(resultData, (byte[]) getTransformer().transform(TEST_STRING)));
116     }
117 
118 }