2222package com.owncloud.android.lib.resources.files.webdav
2323
2424import com.owncloud.android.AbstractIT
25+ import com.owncloud.android.lib.common.network.WebdavEntry
26+ import com.owncloud.android.lib.common.network.WebdavUtils
2527import com.owncloud.android.lib.common.operations.RemoteOperationResult
2628import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
2729import com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation
2830import junit.framework.TestCase
2931import junit.framework.TestCase.assertNotNull
30- import junit.framework.TestCase.assertTrue
32+ import org.apache.jackrabbit.webdav.client.methods.PropFindMethod
3133import org.junit.Test
34+ import java.io.File
3235
3336class ChunkedFileUploadRemoteOperationIT : AbstractIT () {
3437 @Test
35- fun upload () {
36- // create file
37- val filePath = createFile(" chunkedFile.txt" , BIG_FILE_ITERATION )
38- val remotePath = " /bigFile.md"
39-
40- val sut = ChunkedFileUploadRemoteOperation (
41- filePath,
42- remotePath,
43- " text/markdown" ,
44- " " ,
45- RANDOM_MTIME ,
46- System .currentTimeMillis() / MILLI_TO_SECOND ,
47- true ,
48- true
49- )
38+ fun uploadWifi () {
39+ val sut = genLargeUpload(true )
40+ val uploadResult = sut.execute(client)
41+ assert (uploadResult.isSuccess)
42+ }
5043
44+ @Test
45+ fun uploadMobile () {
46+ val sut = genLargeUpload(false )
5147 val uploadResult = sut.execute(client)
52- assertTrue (uploadResult.isSuccess)
48+ assert (uploadResult.isSuccess)
5349 }
5450
5551 @Test
5652 fun cancel () {
57- // create file
58- val filePath = createFile(" chunkedFile.txt" , BIG_FILE_ITERATION )
59- val remotePath = " /cancelFile.md"
53+ val sut = genLargeUpload(false )
54+
55+ var uploadResult: RemoteOperationResult <String >? = null
56+ Thread {
57+ uploadResult = sut.execute(client)
58+ }.start()
59+
60+ shortSleep()
61+ sut.cancel(ResultCode .CANCELLED )
62+
63+ for (i in 1 .. MAX_TRIES ) {
64+ shortSleep()
65+
66+ if (uploadResult != null ) {
67+ break
68+ }
69+ }
70+
71+ assertNotNull(uploadResult)
72+ TestCase .assertFalse(uploadResult?.isSuccess == true )
73+ TestCase .assertSame(ResultCode .CANCELLED , uploadResult?.code)
74+ }
6075
61- val sut = ChunkedFileUploadRemoteOperation (
76+ @Test
77+ fun resume () {
78+ val filePath = createFile(" chunkedFile.txt" , BIG_FILE_ITERATION * 2 )
79+ val timestamp = System .currentTimeMillis() / MILLI_TO_SECOND
80+ val remotePath = " /bigFile.md"
81+
82+ // set up first upload
83+ var sut = ChunkedFileUploadRemoteOperation (
6284 filePath,
6385 remotePath,
6486 " text/markdown" ,
6587 " " ,
6688 RANDOM_MTIME ,
67- System .currentTimeMillis() / MILLI_TO_SECOND ,
89+ timestamp ,
6890 false ,
6991 true
7092 )
7193
94+ // start first upload
7295 var uploadResult: RemoteOperationResult <String >? = null
7396 Thread {
7497 uploadResult = sut.execute(client)
7598 }.start()
7699
100+ // delay and cancel upload
101+ shortSleep()
77102 shortSleep()
78103 sut.cancel(ResultCode .CANCELLED )
79104
@@ -85,13 +110,56 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
85110 }
86111 }
87112
88- assertNotNull(uploadResult)
89- TestCase .assertFalse(uploadResult?.isSuccess == true )
90- TestCase .assertSame(ResultCode .CANCELLED , uploadResult?.code)
113+ // start second upload of same file
114+ sut = ChunkedFileUploadRemoteOperation (
115+ filePath,
116+ remotePath,
117+ " text/markdown" ,
118+ " " ,
119+ RANDOM_MTIME ,
120+ timestamp,
121+ false ,
122+ true
123+ )
124+
125+ // reset result; start second upload
126+ uploadResult = null
127+ uploadResult = sut.execute(client)
128+
129+ // second upload should succeed
130+ assert (uploadResult?.isSuccess == true )
131+
132+ assert (File (filePath).length() == getRemoteSize(remotePath))
133+ }
134+
135+ private fun genLargeUpload (onWifiConnection : Boolean ): ChunkedFileUploadRemoteOperation {
136+ // create file
137+ val filePath = createFile(" chunkedFile.txt" , BIG_FILE_ITERATION )
138+ val remotePath = " /bigFile.md"
139+
140+ return ChunkedFileUploadRemoteOperation (
141+ filePath,
142+ remotePath,
143+ " text/markdown" ,
144+ " " ,
145+ RANDOM_MTIME ,
146+ System .currentTimeMillis() / MILLI_TO_SECOND ,
147+ onWifiConnection,
148+ true
149+ )
150+ }
151+
152+ private fun getRemoteSize (remotePath : String ): Long {
153+ val davPath = client.filesDavUri.toString() + " /" + WebdavUtils .encodePath(remotePath)
154+ val propFindMethod = PropFindMethod (davPath, WebdavUtils .getFilePropSet(), 0 )
155+ client.executeMethod(propFindMethod)
156+ assert (propFindMethod.succeeded())
157+
158+ return WebdavEntry (propFindMethod.responseBodyAsMultiStatus.responses[0 ], remotePath).contentLength
91159 }
92160
93161 companion object {
94- val BIG_FILE_ITERATION = 500000
95- val MAX_TRIES = 30
162+ const val BIG_FILE_ITERATION = 1000000
163+ const val MAX_TRIES = 30
96164 }
97165}
0 commit comments