View Javadoc
1   package wjhk.jupload2.testhelpers;
2   
3   import java.util.concurrent.BlockingQueue;
4   
5   import wjhk.jupload2.exception.JUploadException;
6   import wjhk.jupload2.policies.UploadPolicy;
7   import wjhk.jupload2.upload.FileUploadManagerThread;
8   import wjhk.jupload2.upload.FileUploadManagerThreadImpl;
9   import wjhk.jupload2.upload.FileUploadThread;
10  import wjhk.jupload2.upload.UploadFileData;
11  import wjhk.jupload2.upload.UploadFilePacket;
12  
13  /**
14   * This class allows easy construction of non-active instances of
15   * FileUploadThread. It is used to execute unit tests on
16   * {@link FileUploadManagerThreadImpl}
17   * 
18   * @author etienne_sf
19   */
20  public class FileUploadThreadSuccessTestHelper extends Thread implements
21  		FileUploadThread {
22  
23  	UploadPolicy uploadPolicy = null;
24  
25  	FileUploadManagerThread fileUploadManagerThread;
26  
27  	BlockingQueue<UploadFilePacket> packetQueue = null;
28  
29  	/**
30  	 * @param packetQueue
31  	 * @param uploadPolicy
32  	 * @param fileUploadManagerThread
33  	 */
34  	public FileUploadThreadSuccessTestHelper(
35  			BlockingQueue<UploadFilePacket> packetQueue,
36  			UploadPolicy uploadPolicy,
37  			FileUploadManagerThread fileUploadManagerThread) {
38  		super("FileUploadThreadSuccessTestHelper");
39  
40  		this.packetQueue = packetQueue;
41  		this.uploadPolicy = uploadPolicy;
42  		this.fileUploadManagerThread = fileUploadManagerThread;
43  	}
44  
45  	/**
46  	 * @see java.lang.Thread#run()
47  	 */
48  	/**
49  	 * This method loops on the packetQueue. When a packet is received, it calls
50  	 * the doUpload() method, to send these files to the server.
51  	 */
52  	@Override
53  	final public void run() {
54  		try {
55  			while (!this.fileUploadManagerThread.isUploadFinished()) {
56  				UploadFilePacket uploadFilePacket = this.packetQueue.take();
57  
58  				// Loop through the files in this packet.
59  				for (UploadFileData uploadFileData : uploadFilePacket) {
60  					// Let's simulate the upload.
61  					this.fileUploadManagerThread.nbBytesUploaded(uploadFileData
62  							.getFileLength(), uploadFileData);
63  					// Ok, the file has been sent (hum, almost!)
64  					this.fileUploadManagerThread.anotherFileHasBeenSent(
65  							uploadFilePacket, uploadFileData);
66  				}
67  				this.fileUploadManagerThread
68  						.currentRequestIsFinished(uploadFilePacket);
69  			}// while
70  		} catch (JUploadException e) {
71  			this.fileUploadManagerThread.setUploadException(e);
72  		} catch (InterruptedException e) {
73  			// No action, in this test class
74  		}
75  	}// run
76  
77  	/** {@inheritDoc} */
78  	public void close() {
79  		// No action
80  	}
81  
82  	/** {@inheritDoc} */
83  	public String getResponseMsg() {
84  		return this.uploadPolicy.getStringUploadSuccess();
85  	}
86  
87  	/** {@inheritDoc} */
88  	public void setFileUploadThreadManager(
89  			FileUploadManagerThread fileUploadManagerThread) {
90  		this.fileUploadManagerThread = fileUploadManagerThread;
91  	}
92  
93  	/** {@inheritDoc} */
94  	public BlockingQueue<UploadFilePacket> getPacketQueue() {
95  		return this.packetQueue;
96  	}
97  
98  }