View Javadoc

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