22
22
package com.owncloud.android.lib.resources.files.webdav
23
23
24
24
import com.owncloud.android.AbstractIT
25
+ import com.owncloud.android.lib.common.network.WebdavEntry
26
+ import com.owncloud.android.lib.common.network.WebdavUtils
25
27
import com.owncloud.android.lib.common.operations.RemoteOperationResult
26
28
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
27
29
import com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation
28
30
import junit.framework.TestCase
29
31
import junit.framework.TestCase.assertNotNull
30
- import junit.framework.TestCase.assertTrue
32
+ import org.apache.jackrabbit.webdav.client.methods.PropFindMethod
31
33
import org.junit.Test
34
+ import java.io.File
32
35
33
36
class ChunkedFileUploadRemoteOperationIT : AbstractIT () {
34
37
@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
+ }
50
43
44
+ @Test
45
+ fun uploadMobile () {
46
+ val sut = genLargeUpload(false )
51
47
val uploadResult = sut.execute(client)
52
- assertTrue (uploadResult.isSuccess)
48
+ assert (uploadResult.isSuccess)
53
49
}
54
50
55
51
@Test
56
52
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
+ }
60
75
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 (
62
84
filePath,
63
85
remotePath,
64
86
" text/markdown" ,
65
87
" " ,
66
88
RANDOM_MTIME ,
67
- System .currentTimeMillis() / MILLI_TO_SECOND ,
89
+ timestamp ,
68
90
false ,
69
91
true
70
92
)
71
93
94
+ // start first upload
72
95
var uploadResult: RemoteOperationResult <String >? = null
73
96
Thread {
74
97
uploadResult = sut.execute(client)
75
98
}.start()
76
99
100
+ // delay and cancel upload
101
+ shortSleep()
77
102
shortSleep()
78
103
sut.cancel(ResultCode .CANCELLED )
79
104
@@ -85,13 +110,56 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
85
110
}
86
111
}
87
112
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
91
159
}
92
160
93
161
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
96
164
}
97
165
}
0 commit comments