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;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertNotNull;
11  import org.mule.api.MuleMessage;
12  import org.mule.module.client.MuleClient;
13  import org.mule.util.FileUtils;
14  
15  import java.io.File;
16  
17  import org.junit.Test;
18  
19  public class FileEncodingFunctionalTestCase extends AbstractFileFunctionalTestCase
20  {
21  
22      private static final String TEST_MESSAGE_EUC_JP_ENCODED = "\u3053";
23      private static final int FIVE_SECONDS_TIMEOUT = 5000;
24      private static final String ENCODING = "EUC-JP";
25  
26      private File tmpDir;
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "file-encoding-test.xml";
32      }
33  
34      @Test
35      public void testReadingFileWithEucJpEncodingGetsTheRightText() throws Exception
36      {
37          tmpDir = createFolder(".mule/mule-file-test-EUC-JP");
38          createDataFile(tmpDir, ENCODING, TEST_MESSAGE_EUC_JP_ENCODED);
39  
40          MuleClient client = new MuleClient(muleContext);
41          MuleMessage message = client.request("vm://receive", FIVE_SECONDS_TIMEOUT);
42  
43          assertNotNull(message);
44          assertEquals(ENCODING, message.getEncoding());
45          assertEquals(TEST_MESSAGE_EUC_JP_ENCODED, message.getPayloadAsString());
46      }
47  
48      private File createDataFile(File folder, String encoding, final String testMessage) throws Exception
49      {
50          // Creates a temp file with the required data
51          File temp = File.createTempFile("mule-file-test-", ".txt");
52          FileUtils.writeStringToFile(temp, testMessage, encoding);
53  
54          // Copies temp file to target
55          File target = new File(folder, temp.getName());
56          target.deleteOnExit();
57          FileUtils.renameFile(temp, target);
58  
59          return target;
60      }
61  
62      private File createFolder(String name)
63      {
64          File result = FileUtils.newFile(name);
65          result.delete();
66          result.mkdir();
67          result.deleteOnExit();
68  
69          return result;
70      }
71  
72      @Override
73      protected void doTearDown() throws Exception
74      {
75          super.doTearDown();
76          FileUtils.deleteTree(tmpDir);
77      }
78  }