View Javadoc

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