1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
48 DefaultFileUploadThread defaultFileUploadThread = null;
49
50
51 BlockingQueue<UploadFilePacket> packetQueue = null;
52
53
54 FileUploadManagerThread fileUploadManagerThread = null;
55
56
57
58
59 public DefaultFileUploadThreadTest() {
60
61 }
62
63 @SuppressWarnings("unchecked")
64 @Before
65 public void before() {
66
67 packetQueue = mock(BlockingQueue.class);
68 fileUploadManagerThread = mock(FileUploadManagerThread.class);
69
70
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
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
104 DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
105
106
107 spyDefaultFileUploadThread.doUpload(packet);
108
109
110
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
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
133 DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
134
135
136 spyDefaultFileUploadThread.doUpload(packet);
137
138
139
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
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
162 DefaultFileUploadThread spyDefaultFileUploadThread = spy(defaultFileUploadThread);
163
164
165 spyDefaultFileUploadThread.doUpload(packet);
166
167
168
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 }