View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2012 The JUpload Team
7   //
8   // Created: 5 nov. 2012
9   // Creator: etienne_sf
10  // Last modified: $Date$
11  //
12  // This program is free software; you can redistribute it and/or modify
13  // it under the terms of the GNU General Public License as published by
14  // the Free Software Foundation; either version 2 of the License, or
15  // (at your option) any later version.
16  //
17  // This program is distributed in the hope that it will be useful,
18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  // GNU General Public License for more details.
21  //
22  // You should have received a copy of the GNU General Public License
23  // along with this program; if not, write to the Free Software
24  // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  
26  package wjhk.jupload2.upload;
27  
28  import java.util.concurrent.BlockingQueue;
29  
30  import static org.mockito.Mockito.*;
31  import static org.junit.Assert.*;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  import wjhk.jupload2.exception.JUploadException;
36  import wjhk.jupload2.exception.JUploadInterrupted;
37  
38  /**
39   * @author etienne_sf
40   */
41  public class DefaultFileUploadThreadTest extends AbstractJUploadTestHelper {
42  
43      private static String DEFAULT_THREAD_NAME = "DefaultFileUploadThread thread name";
44  
45      private static long DEFAULT_MAX_CHUNK_SIZE = 100;
46  
47      /** The object to tests */
48      DefaultFileUploadThread defaultFileUploadThread = null;
49  
50      /** Mock object */
51      BlockingQueue<UploadFilePacket> packetQueue = null;
52  
53      /** Mock object */
54      FileUploadManagerThread fileUploadManagerThread = null;
55  
56      /**
57       * 
58       */
59      public DefaultFileUploadThreadTest() {
60          // TODO Auto-generated constructor stub
61      }
62  
63      @SuppressWarnings("unchecked")
64      @Before
65      public void before() {
66          // Creation of the Mock objects, which will be used in this test.
67          packetQueue = mock(BlockingQueue.class);
68          fileUploadManagerThread = mock(FileUploadManagerThread.class);
69  
70          // Creation of the object we'll test.
71          defaultFileUploadThread = new DefaultFileUploadThreadTestHelper(
72                  DEFAULT_THREAD_NAME, packetQueue, uploadPolicy,
73                  fileUploadManagerThread);
74          defaultFileUploadThread.maxChunkSize = DEFAULT_MAX_CHUNK_SIZE;
75      }
76  
77      @Test
78      public void testConstructor() {
79          assertEquals("Check of thread name", DEFAULT_THREAD_NAME,
80                  defaultFileUploadThread.getName());
81          assertEquals("Check of packetQueue", packetQueue,
82                  defaultFileUploadThread.packetQueue);
83          assertEquals("Check of uploadPolicy", uploadPolicy,
84                  defaultFileUploadThread.uploadPolicy);
85          assertEquals("Check of fileUploadManagerThread",
86                  fileUploadManagerThread,
87                  defaultFileUploadThread.fileUploadManagerThread);
88      }
89  
90      @Test
91      public void testDoChunkedUpload_2chunks() throws JUploadException,
92              JUploadInterrupted {
93          final long UPLOAD_LENGHT = 148;
94          // Test preparation
95          UploadFilePacket packet = new UploadFilePacket(uploadPolicy);
96          UploadFileData mockerUploadFileData = mock(UploadFileData.class);
97          when(mockerUploadFileData.getUploadLength()).thenReturn(UPLOAD_LENGHT);
98          when(mockerUploadFileData.getRemainingLength())
99                  .thenReturn(UPLOAD_LENGHT).thenReturn(UPLOAD_LENGHT - 100)
100                 .thenReturn(UPLOAD_LENGHT - 100).thenReturn((long) 0);
101         packet.add(mockerUploadFileData);
102 
103         // For verifications:
104         DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
105 
106         // Test execution
107         spyDefaultFileUploadThread.doUpload(packet);
108 
109         // Checks (final methods can't be verified through the 'verify' method
110         // of Mockito ... too bad, let's find workarounds).
111         verify(spyDefaultFileUploadThread).doUpload(packet);
112         verify(spyDefaultFileUploadThread).doChunkedUpload(packet);
113         verify(mockerUploadFileData).uploadFile(
114                 spyDefaultFileUploadThread.getOutputStream(), 100);
115         verify(mockerUploadFileData).uploadFile(
116                 spyDefaultFileUploadThread.getOutputStream(), 48);
117     }
118 
119     @Test
120     public void testDoChunkedUpload_2chunks_lastChunkFull()
121             throws JUploadException, JUploadInterrupted {
122         final long UPLOAD_LENGHT = 200;
123         // Test preparation
124         UploadFilePacket packet = new UploadFilePacket(uploadPolicy);
125         UploadFileData mockerUploadFileData = mock(UploadFileData.class);
126         when(mockerUploadFileData.getUploadLength()).thenReturn(UPLOAD_LENGHT);
127         when(mockerUploadFileData.getRemainingLength())
128                 .thenReturn(UPLOAD_LENGHT).thenReturn(UPLOAD_LENGHT - 100)
129                 .thenReturn(UPLOAD_LENGHT - 100).thenReturn((long) 0);
130         packet.add(mockerUploadFileData);
131 
132         // For verifications:
133         DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
134 
135         // Test execution
136         spyDefaultFileUploadThread.doUpload(packet);
137 
138         // Checks (final methods can't be verified through the 'verify' method
139         // of Mockito ... too bad, let's find workarounds).
140         verify(spyDefaultFileUploadThread).doUpload(packet);
141         verify(spyDefaultFileUploadThread).doChunkedUpload(packet);
142         verify(mockerUploadFileData, times(2)).uploadFile(
143                 spyDefaultFileUploadThread.getOutputStream(), 100);
144     }
145 
146     @Test
147     public void testDoChunkedUpload_4chunks() throws JUploadException,
148             JUploadInterrupted {
149         final long UPLOAD_LENGHT = 349;
150         // Test preparation
151         UploadFilePacket packet = new UploadFilePacket(uploadPolicy);
152         UploadFileData mockerUploadFileData = mock(UploadFileData.class);
153         when(mockerUploadFileData.getUploadLength()).thenReturn(UPLOAD_LENGHT);
154         when(mockerUploadFileData.getRemainingLength())
155                 .thenReturn(UPLOAD_LENGHT).thenReturn(UPLOAD_LENGHT - 100)
156                 .thenReturn(UPLOAD_LENGHT - 200)
157                 .thenReturn(UPLOAD_LENGHT - 300)
158                 .thenReturn(UPLOAD_LENGHT - 300).thenReturn((long) 0);
159         packet.add(mockerUploadFileData);
160 
161         // For verifications:
162         DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
163 
164         // Test execution
165         spyDefaultFileUploadThread.doUpload(packet);
166 
167         // Checks (final methods can't be verified through the 'verify' method
168         // of Mockito ... too bad, let's find workarounds).
169         verify(spyDefaultFileUploadThread).doUpload(packet);
170         verify(spyDefaultFileUploadThread).doChunkedUpload(packet);
171         verify(mockerUploadFileData, times(3)).uploadFile(
172                 spyDefaultFileUploadThread.getOutputStream(), 100);
173         verify(mockerUploadFileData).uploadFile(
174                 spyDefaultFileUploadThread.getOutputStream(), 49);
175     }
176 }