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.http.multipart;
8   
9   import static junit.framework.Assert.assertEquals;
10  import static junit.framework.Assert.assertTrue;
11  
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  import org.mule.util.FileUtils;
14  import org.mule.util.IOUtils;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.File;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  
28  /**
29   * Test class for the {@link MultiPartInputStream}.
30   */
31  public class MultiPartInputStreamTestCase extends AbstractMuleTestCase
32  {
33      private static final String MULTIPART_BOUNDARY = "----------------------------299df9f9431b";
34      private static final int NUMBER_OF_PARTS = 3;
35      private static final String TMP_DIR = "./multipartTmpDir";
36  
37      private static File tmpDir;
38      private static Set<String> partContents;
39      private static String multipartMessage;
40  
41      @BeforeClass
42      public static void setUp() throws IOException
43      {
44          // Create temp. dir. for the MultiPartInputStream to store intermediate part files.
45          tmpDir = createTempDirectory(TMP_DIR);
46  
47          // Create part contents and build multipart message.
48          partContents = new HashSet<String>();
49          StringBuilder multipartMessageBuilder = new StringBuilder();
50          for (int i = 0; i < NUMBER_OF_PARTS; i++)
51          {
52              String partContent = "part " + i;
53              partContents.add(partContent);
54  
55              multipartMessageBuilder.append("--").append(MULTIPART_BOUNDARY).append("\r\n");
56              multipartMessageBuilder.append("Content-Disposition: form-data; name=\"").append(partContent).
57                      append("\"; filename=\"").append(partContent).append("\"\r\n");
58              multipartMessageBuilder.append("Content-Type: application/octet-stream\r\n\r\n");
59              multipartMessageBuilder.append(partContent).append("\r\n");
60          }
61          multipartMessage = multipartMessageBuilder.toString();
62      }
63  
64      @AfterClass
65      public static void tearDown() throws IOException
66      {
67          FileUtils.deleteDirectory(tmpDir);
68      }
69  
70      @Test
71      public void buildMultiPartInputStream() throws Exception
72      {
73          ByteArrayInputStream bis = new ByteArrayInputStream(multipartMessage.getBytes("UTF-8"));
74          MultiPartInputStream mpis = new MultiPartInputStream(bis, "multipart/form-data; boundary=" + MULTIPART_BOUNDARY,
75                                                               new MultipartConfiguration(TMP_DIR));
76  
77          Collection<Part> parts = mpis.getParts();
78          assertEquals(NUMBER_OF_PARTS, parts.size());
79  
80          // Read parsed part contents and close input stream.
81          Set<String> parsedPartContents = new HashSet<String>();
82          for (Part part : parts)
83          {
84              InputStream pis = part.getInputStream();
85              parsedPartContents.add(IOUtils.toString(pis));
86              // No need to close the input stream as it is closed automatically when end of input has been reached.
87          }
88  
89          // Assert that the part contents were all parsed correctly.
90          assertEquals(partContents, parsedPartContents);
91  
92          // Assert that no temp files remain in place.
93          assertTrue("Temporary directory should be empty", FileUtils.listFiles(tmpDir, null, false).isEmpty());
94      }
95  
96      /**
97       * Creates an empty temporary directory.
98       *
99       * @param tmpDirName The name of the temporary directory.
100      * @return The created temporary directory.
101      * @throws java.io.IOException If there is an error creating the file
102      */
103     private static File createTempDirectory(String tmpDirName) throws IOException
104     {
105         File tmpDir = FileUtils.openDirectory(tmpDirName);
106         FileUtils.deleteTree(tmpDir);
107         return tmpDir;
108     }
109 }