View Javadoc

1   /*
2    * $Id: FileEncodingFunctionalTestCase.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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.util.FileUtils;
16  
17  import java.io.File;
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      public void testReadingFileWithEucJpEncodingGetsTheRightText() throws Exception
35      {
36          tmpDir = createFolder(".mule/mule-file-test-EUC-JP");
37          createDataFile(tmpDir, ENCODING, TEST_MESSAGE_EUC_JP_ENCODED);
38  
39          MuleClient client = new MuleClient(muleContext);
40          MuleMessage message = client.request("vm://receive", FIVE_SECONDS_TIMEOUT);
41  
42          assertNotNull(message);
43          assertEquals(ENCODING, message.getEncoding());
44          assertEquals(TEST_MESSAGE_EUC_JP_ENCODED, message.getPayloadAsString());
45      }
46  
47      private File createDataFile(File folder, String encoding, final String testMessage) throws Exception
48      {
49          File target = File.createTempFile("mule-file-test-", ".txt", folder);
50          target.deleteOnExit();
51          FileUtils.writeStringToFile(target, testMessage, encoding);
52  
53          return target;
54      }
55  
56      private File createFolder(String name)
57      {
58          File result = FileUtils.newFile(name);
59          result.delete();
60          result.mkdir();
61          result.deleteOnExit();
62  
63          return result;
64      }
65  
66      @Override
67      protected void doTearDown() throws Exception
68      {
69          super.doTearDown();
70          FileUtils.deleteTree(tmpDir);
71      }
72  }