View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2010 The JUpload Team
7   //
8   // Created: 27 janv. 2010
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.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   * @author etienne_sf
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       * @param nbFilesInFilePanel
73       * @throws Exception
74       * @see wjhk.jupload2.upload.AbstractJUploadTestHelper#setupFullUploadEnvironment()
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          // We want to test the behavior, when putting the first file, while the
81          // total upload contains more files.
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       * This method creates or re-creates all objects for the current instance, using the current system properties. If
91       * called by {@link #setUp()}, it uses the default test configuration. If called by any test method, it uses the
92       * overriden properties.
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         // This test expects
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      * @throws JUploadException
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      * @throws JUploadException
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         // We need to re-initialize this test, to use the above setting.
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         // The packet is full only if we have exactly the good number of bytes !
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      * @throws JUploadException
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 }