View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2009 The JUpload Team
7   //
8   // Created: 28 mai 2009
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.After;
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import wjhk.jupload2.exception.JUploadException;
34  import wjhk.jupload2.testhelpers.JUploadPanelTestHelper;
35  
36  /**
37   * @author etienne_sf
38   */
39  public class FileUploadManagerThreadImplTest extends AbstractJUploadTestHelper {
40  
41  	/**
42  	 * @throws JUploadException
43  	 */
44  	@Before
45  	public void setUp() throws JUploadException {
46  		// FIXME Create a real JUploadContext, and set it to this UploadPolicy
47  		// juploadContext = new JUploadContextExecutable(null, propertiesURL);
48  
49  		this.fileUploadManagerThread = new FileUploadManagerThreadImpl(
50  				this.uploadPolicy, this.fileUploadThread);
51  		((JUploadPanelTestHelper) this.juploadPanel).fileUploadManagerThread = this.fileUploadManagerThread;
52  	}
53  
54  	/**
55  	 * Starts the upload, and wait for the {@link FileUploadThreadManager} to
56  	 * finish.
57  	 */
58  	private void executeUpload() throws Exception {
59  		this.fileUploadManagerThread.start();
60  		waitForThreadToFinish((Thread) this.fileUploadManagerThread,
61  				"fileUploadManagerThread");
62  	}
63  
64  	/**
65  	 * @throws java.lang.Exception
66  	 */
67  	@After
68  	public void tearDown() throws Exception {
69  	}
70  
71  	/**
72  	 * Test method for
73  	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#run()}.
74  	 * 
75  	 * @throws Exception
76  	 */
77  	@Test
78  	public void testRun() throws Exception {
79  		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).uploadException = null;
80  		Assert.assertNull("Check uploadException getter",
81  				this.fileUploadManagerThread.getUploadException());
82  		executeUpload();
83  		Assert.assertNull("There should be no exception during upload",
84  				this.fileUploadManagerThread.getUploadException());
85  	}
86  
87  	/**
88  	 * Test method for
89  	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#setUploadException(wjhk.jupload2.exception.JUploadException)}
90  	 * .
91  	 */
92  	@Test
93  	public void testSetUploadException() {
94  		JUploadException jue = new JUploadException(
95  				"A test exception, to test FileUploadManagerThreadImpl.setUploadException()");
96  		this.fileUploadManagerThread.setUploadException(jue);
97  		Assert.assertTrue(jue == this.fileUploadManagerThread
98  				.getUploadException());
99  	}
100 
101 	/**
102 	 * Test method for
103 	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#getUploadException()}
104 	 * .
105 	 */
106 	@Test
107 	public void testGetUploadException() {
108 		testSetUploadException();
109 	}
110 
111 	/**
112 	 * Test method for
113 	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#isUploadFinished()}
114 	 * .
115 	 * 
116 	 * @throws Exception
117 	 */
118 	@Test
119 	public void testIsUploadFinished() throws Exception {
120 		Assert.assertFalse("Before starting, the upload is not finished!",
121 				this.fileUploadManagerThread.isUploadFinished());
122 
123 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).stop = true;
124 		Assert.assertTrue("If the upload is stopped, the upload is finished!",
125 				this.fileUploadManagerThread.isUploadFinished());
126 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).stop = false;
127 
128 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).uploadFinished = true;
129 		Assert.assertTrue(
130 				"If the upload is finished... the upload is finished!",
131 				this.fileUploadManagerThread.isUploadFinished());
132 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).uploadFinished = false;
133 
134 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).uploadException = new JUploadException(
135 				"bla bla bla");
136 		Assert.assertTrue("If there is an exception, the upload is finished!",
137 				this.fileUploadManagerThread.isUploadFinished());
138 		((FileUploadManagerThreadImpl) this.fileUploadManagerThread).uploadException = null;
139 	}
140 
141 	/**
142 	 * Test method for
143 	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#stopUpload()}.
144 	 * 
145 	 * @throws Exception
146 	 */
147 	@Test
148 	public void testStopUpload() throws Exception {
149 		this.fileUploadManagerThread.start();
150 		this.fileUploadManagerThread.stopUpload();
151 		/*
152 		 * Assert.assertTrue("Uploade should be stopped",
153 		 * this.fileUploadManagerThread.uploadStopped);
154 		 * Assert.assertTrue("Uploade should be finished",
155 		 * this.fileUploadManagerThread.uploadFinished);
156 		 */
157 		Assert.assertTrue("Uploade should be finished",
158 				this.fileUploadManagerThread.isUploadFinished());
159 	}
160 
161 	/**
162 	 * Test method for
163 	 * {@link wjhk.jupload2.upload.FileUploadManagerThreadImpl#currentRequestIsFinished(UploadFilePacket)}
164 	 * .
165 	 * 
166 	 * @throws Exception
167 	 */
168 	@Test
169 	public void testCurrentRequestIsFinishedWithOneFile() throws Exception {
170 		// Test with one file.
171 		this.fileUploadManagerThread.start();
172 		Thread.sleep(20);
173 		Assert.assertFalse("Before starting, the upload is not finished!",
174 				this.fileUploadManagerThread.isUploadFinished());
175 
176 		// Let's simulate the call to currentRequestIsFinished, indicating that
177 		// the file to upload has been successfully uploaded
178 		UploadFilePacket uploadFilePacket = new UploadFilePacket(
179 				this.uploadPolicy);
180 		uploadFilePacket.add(new UploadFileData(this.filesToUpload.get(0), 1,
181 				this.fileUploadManagerThread, this.uploadPolicy));
182 
183 		this.fileUploadManagerThread.currentRequestIsFinished(uploadFilePacket);
184 		Assert.assertNull("There should be no upload error",
185 				this.fileUploadManagerThread.getUploadException());
186 		Assert.assertTrue("After upload, the upload should be finished.",
187 				this.fileUploadManagerThread.isUploadFinished());
188 	}
189 }