View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2015 The JUpload Team
7   //
8   // Created: 3 févr. 2015
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 static org.junit.Assert.*;
29  
30  import java.util.concurrent.ArrayBlockingQueue;
31  import java.util.concurrent.BlockingQueue;
32  
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import wjhk.jupload2.testhelpers.FileDataTestHelper;
37  import wjhk.jupload2.testhelpers.FilePanelTestHelper;
38  
39  /**
40   * @author etienne_sf
41   */
42  public class FilePreparationThreadTest extends AbstractJUploadTestHelper {
43  
44      /** The number of files to put in the file list */
45      public final static int NB_FILES_TO_UPLOAD = 5;
46  
47      /** The if of the file, which will have the uploadFlag set to false */
48      public final static int NUM_FILE_WITH_UPLOADFLAG_TO_FALSE = 3;
49  
50      /** The instance to be tested */
51      // public FilePreparationThread filePreparationThread = null;
52  
53      /* The mock objects, to allow testing */
54      BlockingQueue<UploadFileData> preparedFileQueue;
55  
56      /**
57       * @throws java.lang.Exception
58       */
59      @Before
60      public void setUp() throws Exception {
61          // Let's create our own set of files to upload, for this test case.
62          setupFileList(NB_FILES_TO_UPLOAD);
63  
64          preparedFileQueue = new ArrayBlockingQueue<UploadFileData>(20);
65  
66          // The FilePanel.removeFileNotToUpload() must be called once, during FilePreparationThread construction
67          assertEquals("FilePanel.removeFileNotToUpload() not called before constructor", 0,
68                  ((FilePanelTestHelper) this.filePanel).removeFileNotToUpload_NbCalls);
69          filePreparationThread = new FilePreparationThread(preparedFileQueue, fileUploadManagerThread, uploadPolicy);
70          assertEquals("FilePanel.removeFileNotToUpload() called once during constructor", 1,
71                  ((FilePanelTestHelper) this.filePanel).removeFileNotToUpload_NbCalls);
72      }
73  
74      /**
75       * Test method for {@link wjhk.jupload2.upload.FilePreparationThread#run()}.
76       * 
77       * @throws InterruptedException
78       */
79      @Test
80      public void testRun() throws InterruptedException {
81          // Checks, before run
82          assertEquals("getNbTotalNumberOfPreparedBytes", 0, filePreparationThread.getNbTotalNumberOfPreparedBytes(), 0);
83          assertEquals("getTotalFileBytesToSend", 0, filePreparationThread.getTotalFileBytesToSend(), 0);
84          assertEquals("nbFilesToSend", NB_FILES_TO_UPLOAD, filePreparationThread.getNbFilesToSend());
85          assertEquals("getNbPreparedFiles", 0, filePreparationThread.getNbPreparedFiles());
86  
87          // go, go, go
88          filePreparationThread.run();
89  
90          // Checks, after run
91          assertEquals("nbFilesToSend", NB_FILES_TO_UPLOAD, filePreparationThread.getNbFilesToSend());
92          assertEquals("getNbPreparedFiles", NB_FILES_TO_UPLOAD, filePreparationThread.getNbPreparedFiles());
93          assertEquals("getNbTotalNumberOfPreparedBytes",
94                  (NB_FILES_TO_UPLOAD) * (FileDataTestHelper.FILE_CONTENT_PREFIX.length() + 1),
95                  filePreparationThread.getNbTotalNumberOfPreparedBytes(), 0);
96          assertEquals("getTotalFileBytesToSend", (NB_FILES_TO_UPLOAD)
97                  * (FileDataTestHelper.FILE_CONTENT_PREFIX.length() + 1),
98                  filePreparationThread.getTotalFileBytesToSend(), 0);
99          // Checks the file names
100         for (int i = 0; i < NB_FILES_TO_UPLOAD; i += 1) {
101             assertEquals("File name " + i, i + ".txt", preparedFileQueue.take().getFileName());
102         }
103     }
104 }