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.io.File;
29
30 import org.junit.Assert;
31
32 import org.junit.Before;
33 import org.junit.Test;
34
35 import wjhk.jupload2.exception.JUploadException;
36 import wjhk.jupload2.filedata.DefaultFileData;
37 import wjhk.jupload2.filedata.FileData;
38 import wjhk.jupload2.policies.UploadPolicy;
39 import wjhk.jupload2.testhelpers.FileDataTestHelper;
40
41
42
43
44 public class UploadFilePacketTest extends AbstractJUploadTestHelper {
45
46
47 public final static int MAX_CHUNK_SIZE = 1000;
48
49
50 public final static int NB_FILES_IN_FILE_PANEL = 999;
51
52
53 public final static int NB_FILES_PER_REQUEST = 2;
54
55 UploadFilePacket uploadFilePacket;
56
57 File root = new File("./tests");
58
59 File f = new File("./tests/files/fichier_1.txt");
60
61 FileData fileData;
62
63 UploadFileData uploadFileData;
64
65
66 @Before
67 public void setUp() {
68 prepareUpload(10);
69 }
70
71
72
73
74
75
76 private void prepareUpload(int nbFilesInFilePanel) {
77 System.setProperty("maxChunkSize", String.valueOf(MAX_CHUNK_SIZE));
78 System.setProperty("nbFilesPerRequest", String.valueOf(NB_FILES_PER_REQUEST));
79
80
81
82 setupFileList(NB_FILES_IN_FILE_PANEL);
83
84 createTestData();
85 this.packetConstructionThread = new PacketConstructionThread(this.preparedFileQueue, this.packetQueue,
86 this.fileUploadManagerThread, this.uploadPolicy);
87 }
88
89
90
91
92
93
94 private void createTestData() {
95 this.uploadFilePacket = new UploadFilePacket(this.uploadPolicy);
96
97 this.fileData = new FileDataTestHelper(1, uploadPolicy);
98 Assert.assertTrue("The file must be readable", this.fileData.canRead());
99 this.uploadFileData = new UploadFileData(this.fileData, 0, this.fileUploadManagerThread, this.uploadPolicy);
100 }
101
102
103 @Test
104 public void testUploadFilePacket() {
105 Assert.assertEquals("The good upload policy", this.uploadPolicy, this.uploadFilePacket.uploadPolicy);
106 Assert.assertEquals("The correct maxChunkSize", MAX_CHUNK_SIZE, this.uploadFilePacket.maxNbBytes);
107
108 Assert.assertEquals("The correct maxNbFiles", NB_FILES_PER_REQUEST, this.uploadFilePacket.maxNbFiles);
109
110 Assert.assertFalse("This class should never be poisonned", this.uploadFilePacket.isPoisonned());
111 }
112
113
114
115
116 @Test
117 public void testIsFullOfFiles() throws JUploadException {
118 Assert.assertFalse("This packet is not full yet (0 files)", this.uploadFilePacket.isFull());
119 for (int i = 1; i < NB_FILES_PER_REQUEST; i += 1) {
120 this.fileData = new DefaultFileData(this.f, this.uploadPolicy);
121 this.fileData.beforeUpload(null);
122 Assert.assertTrue("The file must be accepted", this.uploadFilePacket.add(new UploadFileData(this.fileData,
123 0, this.fileUploadManagerThread, this.uploadPolicy)));
124 Assert.assertFalse("This packet is not full yet (" + i + " files)", this.uploadFilePacket.isFull());
125 }
126 this.uploadFilePacket
127 .add(new UploadFileData(this.fileData, 0, this.fileUploadManagerThread, this.uploadPolicy));
128 Assert.assertTrue("This packet is now full", this.uploadFilePacket.isFull());
129 }
130
131
132
133
134 @Test
135 public void testIsFullOfBytes() throws JUploadException {
136 Assert.assertFalse("This packet is not full yet (0 files)", this.uploadFilePacket.isFull());
137
138 int maxNbFiles = 1000;
139 int realMaxNbFiles = Math.min(maxNbFiles, NB_FILES_IN_FILE_PANEL);
140 int maxNbBytes = 100;
141
142 System.setProperty(UploadPolicy.PROP_NB_FILES_PER_REQUEST, String.valueOf(maxNbFiles));
143 System.setProperty(UploadPolicy.PROP_MAX_CHUNK_SIZE, String.valueOf(maxNbBytes));
144
145 createTestData();
146 Assert.assertEquals("The maximal number of files", realMaxNbFiles, this.uploadFilePacket.maxNbFiles);
147 Assert.assertEquals("The maximal number of bytes", maxNbBytes, this.uploadFilePacket.maxNbBytes);
148
149
150 String fileContent = "a";
151 for (int i = 1; i < maxNbBytes; i += 1) {
152 this.fileData = new FileDataTestHelper(0, uploadPolicy);
153 ((FileDataTestHelper) this.fileData).fileContent = fileContent;
154 this.fileData.beforeUpload(null);
155 Assert.assertTrue("The file must be accepted", this.uploadFilePacket.add(new UploadFileData(this.fileData,
156 0, this.fileUploadManagerThread, this.uploadPolicy)));
157 Assert.assertFalse("This packet is not full yet (" + i + " files)", this.uploadFilePacket.isFull());
158 }
159
160 this.fileData = new FileDataTestHelper(0, uploadPolicy);
161 ((FileDataTestHelper) this.fileData).fileContent = fileContent;
162 this.uploadFilePacket
163 .add(new UploadFileData(this.fileData, 0, this.fileUploadManagerThread, this.uploadPolicy));
164 Assert.assertTrue("This packet is now full", this.uploadFilePacket.isFull());
165 }
166
167
168
169
170 @Test
171 public void testCanAdd_checkBytes() throws JUploadException {
172 FileData fileData = new FileDataTestHelper(0, uploadPolicy);
173 int maxNbFiles = 1000;
174 int maxNbBytes = 100;
175
176 System.setProperty(UploadPolicy.PROP_NB_FILES_PER_REQUEST, String.valueOf(maxNbFiles));
177 System.setProperty(UploadPolicy.PROP_MAX_CHUNK_SIZE, String.valueOf(maxNbBytes));
178
179 String fileContent = ((FileDataTestHelper) fileData).fileContent;
180 int nbLoop = maxNbBytes / fileContent.length();
181 for (int i = 0; i < nbLoop; i += 1) {
182 fileData = new FileDataTestHelper(0, uploadPolicy);
183 fileData.beforeUpload(null);
184 Assert.assertTrue("The file must be accepted", this.uploadFilePacket.canAdd(new UploadFileData(fileData, 0,
185 this.fileUploadManagerThread, this.uploadPolicy)));
186 Assert.assertTrue("The file must be accepted", this.uploadFilePacket.add(new UploadFileData(fileData, 0,
187 this.fileUploadManagerThread, this.uploadPolicy)));
188 }
189
190 fileData = new FileDataTestHelper(0, uploadPolicy);
191 Assert.assertFalse("The file must not be accepted", this.uploadFilePacket.canAdd(new UploadFileData(fileData,
192 0, this.fileUploadManagerThread, this.uploadPolicy)));
193 Assert.assertFalse("The file must not be accepted", this.uploadFilePacket.add(new UploadFileData(fileData, 0,
194 this.fileUploadManagerThread, this.uploadPolicy)));
195 }
196
197
198 @Test
199 public void testIsPoisonned() {
200 Assert.assertFalse("This class is not poisonned", this.uploadFilePacket.isPoisonned());
201 }
202 }