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 org.junit.Assert;
29  import org.junit.Test;
30  
31  import wjhk.jupload2.exception.JUploadException;
32  import wjhk.jupload2.filedata.FileData;
33  import wjhk.jupload2.policies.UploadPolicy;
34  import wjhk.jupload2.testhelpers.FileDataTestHelper;
35  
36  /**
37   * @author etienne_sf
38   */
39  public class PacketConstructionThreadTest extends AbstractJUploadTestHelper {
40  
41      /**
42       * @param nbFilesInFilePanel
43       * @throws Exception
44       * @see wjhk.jupload2.upload.AbstractJUploadTestHelper#setupFullUploadEnvironment()
45       */
46      private void prepareUpload(int nbFilesInFilePanel) {
47          // We need nbFilesPerRequest to be set to 2.
48          System.setProperty(UploadPolicy.PROP_NB_FILES_PER_REQUEST, "2");
49  
50          // We want to test the behavior, when putting the first file, while the
51          // total upload contains more files.
52          setupFileList(nbFilesInFilePanel);
53  
54          this.packetConstructionThread = new PacketConstructionThread(this.preparedFileQueue, this.packetQueue,
55                  this.fileUploadManagerThread, this.uploadPolicy);
56      }
57  
58      /**
59       * Tests the constructor.
60       * 
61       * @throws Exception
62       */
63      @Test
64      public synchronized void packetConstructionThreadTest() throws Exception {
65          prepareUpload(1);
66  
67          this.logger.info("Start of packetConstructionThreadTest()");
68          Assert.assertEquals("Check of the preparedFileQueue", this.preparedFileQueue,
69                  this.packetConstructionThread.preparedFileQueue);
70          Assert.assertEquals("Check of the packetQueue", this.packetQueue, this.packetConstructionThread.packetQueue);
71          Assert.assertEquals("Check of the fileUploadManagerThread", this.fileUploadManagerThread,
72                  this.packetConstructionThread.fileUploadManagerThread);
73          Assert.assertEquals("Check of the upload policy", this.uploadPolicy, this.packetConstructionThread.uploadPolicy);
74          this.logger.info("End of packetConstructionThreadTest()");
75      }
76  
77      /**
78       * @throws InterruptedException
79       * @throws JUploadException
80       */
81      @Test
82      public synchronized void runOneFile() throws InterruptedException, JUploadException {
83          this.logger.info("Start of runOneFile()");
84          prepareUpload(10);
85  
86          FileData fileData = new FileDataTestHelper(1, uploadPolicy);
87          fileData.beforeUpload(null);
88  
89          Assert.assertEquals("This test case assumes that the nbFilesPerRequest is 2", 2,
90                  this.uploadPolicy.getNbFilesPerRequest());
91  
92          // Check the prepared file is posted on the packetQueue.
93          Assert.assertTrue("The preparedFileQueue should be empty before starting the test",
94                  this.preparedFileQueue.isEmpty());
95          Assert.assertTrue("The packetQueue should be empty before starting the test", this.packetQueue.isEmpty());
96          int numOfFileInCurrentRequest = 0;
97          this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest,
98                  this.fileUploadManagerThread, this.uploadPolicy));
99          Assert.assertFalse("The queue should contain this file", this.preparedFileQueue.isEmpty());
100         this.packetConstructionThread.start();
101         // The packetConstructionThread should take the file. We wait for that.
102         waitForQueueToBeEmpty(this.preparedFileQueue, "preparedFileQueue");
103         Assert.assertTrue("The queue should be empty once the file is prepared", this.preparedFileQueue.isEmpty());
104         Assert.assertTrue("The packet is not finished (only one file: no packet is ready)", this.packetQueue.isEmpty());
105         Assert.assertTrue("The PacketConstructionThread should still be alive (poisonned file not received)",
106                 this.packetConstructionThread.isAlive());
107 
108         // Putting the poisonned file should finish the thread.
109         this.preparedFileQueue.put(new UploadFileDataPoisonned());
110         waitForThreadToFinish(this.packetConstructionThread, "packetConstructionThread");
111         Assert.assertTrue("The queue should be empty once the file is prepared", this.preparedFileQueue.isEmpty());
112         Assert.assertFalse("One packet should be in the queue", this.packetQueue.isEmpty());
113         UploadFilePacket packet = this.packetQueue.take();
114         Assert.assertEquals("The packet should be one file long", 1, packet.size());
115         Assert.assertFalse("The PacketConstructionThread should have finished", this.packetConstructionThread.isAlive());
116         this.logger.info("End of runOneFile()");
117     }
118 
119     /**
120      * @throws InterruptedException
121      * @throws JUploadException
122      */
123     @Test
124     public synchronized void runTwoFiles() throws InterruptedException, JUploadException {
125         this.logger.info("Start of runTwoFiles()");
126         prepareUpload(10);
127 
128         FileData fileData = new FileDataTestHelper(1, uploadPolicy);
129         fileData.beforeUpload(null);
130 
131         Assert.assertEquals("This test case assumes that the nbFilesPerRequest is 2", 2,
132                 this.uploadPolicy.getNbFilesPerRequest());
133 
134         // Assumes that runOneFile() was Ok
135         int numOfFileInCurrentRequest = 0;
136         this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest++,
137                 this.fileUploadManagerThread, this.uploadPolicy));
138         this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest++,
139                 this.fileUploadManagerThread, this.uploadPolicy));
140 
141         this.packetConstructionThread.start();
142         waitForQueueToBeEmpty(this.preparedFileQueue, "preparedFileQueue");
143         Assert.assertTrue("The files should have been taken", this.preparedFileQueue.isEmpty());
144         Assert.assertFalse("The packetQueue should contain one packet", this.packetQueue.isEmpty());
145         UploadFilePacket packet = this.packetQueue.take();
146         Assert.assertEquals("The packet should be two files long", 2, packet.size());
147         Assert.assertTrue("The PacketConstructionThread should still be alive (poisonned file not received)",
148                 this.packetConstructionThread.isAlive());
149     }
150 
151     /**
152      * @throws InterruptedException
153      * @throws JUploadException
154      */
155     @Test
156     public synchronized void runThreeFiles() throws InterruptedException, JUploadException {
157         this.logger.info("Start of runThreeFiles()");
158         prepareUpload(10);
159 
160         FileData fileData = new FileDataTestHelper(1, uploadPolicy);
161 
162         Assert.assertEquals("This test case assumes that the nbFilesPerRequest is 2", 2,
163                 this.uploadPolicy.getNbFilesPerRequest());
164 
165         // Assumes that runOneFile() was Ok
166         int numOfFileInCurrentRequest = 0;
167         this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest++,
168                 this.fileUploadManagerThread, this.uploadPolicy));
169         this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest++,
170                 this.fileUploadManagerThread, this.uploadPolicy));
171         this.preparedFileQueue.put(new UploadFileData(fileData, numOfFileInCurrentRequest++,
172                 this.fileUploadManagerThread, this.uploadPolicy));
173 
174         this.packetConstructionThread.start();
175         waitForQueueToBeEmpty(this.preparedFileQueue, "preparedFileQueue");
176         Assert.assertTrue("The files should have been taken", this.preparedFileQueue.isEmpty());
177         Assert.assertFalse("The packetQueue should contain one packets", this.packetQueue.isEmpty());
178         UploadFilePacket packet = this.packetQueue.take();
179         Assert.assertTrue("The packetQueue should have contained one packet", this.packetQueue.isEmpty());
180         Assert.assertEquals("The packet should be two files long", 2, packet.size());
181         Assert.assertTrue("The PacketConstructionThread should still be alive (poisonned file not received)",
182                 this.packetConstructionThread.isAlive());
183 
184         // Let's send the poisoned
185         // Putting the poisonned file should finish the thread.
186         this.preparedFileQueue.put(new UploadFileDataPoisonned());
187         waitForThreadToFinish(this.packetConstructionThread, "packetConstructionThread");
188         Assert.assertTrue("The preparedFileQueue should be empty once the file is prepared",
189                 this.preparedFileQueue.isEmpty());
190         Assert.assertFalse("One packet should be in the queue", this.packetQueue.isEmpty());
191         packet = this.packetQueue.take();
192         Assert.assertEquals("The packet should be one file long", 1, packet.size());
193         Assert.assertFalse("The PacketConstructionThread should have finished", this.packetConstructionThread.isAlive());
194         this.logger.info("End of runThreeFiless()");
195     }
196 }