1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Merge branch 'move_files_and_folders' into develop

This commit is contained in:
David A. Velasco 2014-09-12 13:37:36 +02:00
commit 79d586637e
8 changed files with 785 additions and 39 deletions

View File

@ -32,7 +32,6 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
@ -79,7 +78,7 @@ public class MainActivity extends Activity implements OnRemoteOperationListener,
mHandler = new Handler(); mHandler = new Handler();
Uri serverUri = Uri.parse(getString(R.string.server_base_url) + AccountUtils.WEBDAV_PATH_4_0); Uri serverUri = Uri.parse(getString(R.string.server_base_url));
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true); mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
mClient.setCredentials( mClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials( OwnCloudCredentialsFactory.newBasicCredentials(

View File

@ -225,6 +225,26 @@ public class OwnCloudClient extends HttpClient {
Log.d(TAG + " #" + mInstanceNumber, Log.d(TAG + " #" + mInstanceNumber,
"Location to redirect: " + location.getValue()); "Location to redirect: " + location.getValue());
method.setURI(new URI(location.getValue(), true)); method.setURI(new URI(location.getValue(), true));
Header destination = method.getRequestHeader("Destination");
if (destination == null) {
destination = method.getRequestHeader("destination");
}
if (destination != null) {
String locationStr = location.getValue();
int suffixIndex = locationStr.lastIndexOf(
(mCredentials instanceof OwnCloudBearerCredentials) ?
AccountUtils.ODAV_PATH :
AccountUtils.WEBDAV_PATH_4_0
);
String redirectionBase = locationStr.substring(0, suffixIndex);
String destinationStr = destination.getValue();
String destinationPath = destinationStr.substring(mBaseUri.toString().length());
String redirectedDestination = redirectionBase + destinationPath;
destination.setValue(redirectedDestination);
method.setRequestHeader(destination);
}
status = super.executeMethod(method); status = super.executeMethod(method);
redirectionsCount++; redirectionsCount++;

View File

@ -101,7 +101,9 @@ public class RemoteOperationResult implements Serializable {
LOCAL_STORAGE_NOT_REMOVED, LOCAL_STORAGE_NOT_REMOVED,
FORBIDDEN, FORBIDDEN,
SHARE_FORBIDDEN, SHARE_FORBIDDEN,
OK_REDIRECT_TO_NON_SECURE_CONNECTION OK_REDIRECT_TO_NON_SECURE_CONNECTION,
INVALID_MOVE_INTO_DESCENDANT,
PARTIAL_MOVE_DONE
} }
private boolean mSuccess = false; private boolean mSuccess = false;

View File

@ -0,0 +1,213 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2014 ownCloud Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.resources.files;
import java.io.IOException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.Status;
import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
/**
* Remote operation moving a remote file or folder in the ownCloud server to a different folder
* in the same account.
*
* Allows renaming the moving file/folder at the same time.
*
* @author David A. Velasco
*/
public class MoveRemoteFileOperation extends RemoteOperation {
private static final String TAG = MoveRemoteFileOperation.class.getSimpleName();
private static final int MOVE_READ_TIMEOUT = 600000;
private static final int MOVE_CONNECTION_TIMEOUT = 5000;
private String mSrcRemotePath;
private String mTargetRemotePath;
private boolean mOverwrite;
/**
* Constructor.
*
* TODO Paths should finish in "/" in the case of folders. ?
*
* @param srcRemotePath Remote path of the file/folder to move.
* @param targetRemotePath Remove path desired for the file/folder after moving it.
*/
public MoveRemoteFileOperation(
String srcRemotePath, String targetRemotePath, boolean overwrite
) {
mSrcRemotePath = srcRemotePath;
mTargetRemotePath = targetRemotePath;
mOverwrite = overwrite;
}
/**
* Performs the rename operation.
*
* @param client Client object to communicate with the remote ownCloud server.
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
/// check parameters
if (!FileUtils.isValidPath(mTargetRemotePath)) {
return new RemoteOperationResult(ResultCode.INVALID_CHARACTER_IN_NAME);
}
if (mTargetRemotePath.equals(mSrcRemotePath)) {
// nothing to do!
return new RemoteOperationResult(ResultCode.OK);
}
if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
}
/// perform remote operation
//LocalMoveMethod move = null;
MoveMethod move = null;
RemoteOperationResult result = null;
try {
move = new MoveMethod(
client.getWebdavUri() + WebdavUtils.encodePath(mSrcRemotePath),
client.getWebdavUri() + WebdavUtils.encodePath(mTargetRemotePath),
mOverwrite
);
int status = client.executeMethod(move, MOVE_READ_TIMEOUT, MOVE_CONNECTION_TIMEOUT);
/// process response
if (status == HttpStatus.SC_MULTI_STATUS) {
result = processPartialError(move);
} else if (status == HttpStatus.SC_PRECONDITION_FAILED && !mOverwrite) {
result = new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
client.exhaustResponse(move.getResponseBodyAsStream());
/// for other errors that could be explicitly handled, check first:
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
} else {
result = new RemoteOperationResult(
isSuccess(status), // move.succeeded()? trustful?
status,
move.getResponseHeaders()
);
client.exhaustResponse(move.getResponseBodyAsStream());
}
Log.i(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " +
result.getLogMessage());
} catch (Exception e) {
result = new RemoteOperationResult(e);
Log.e(TAG, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " +
result.getLogMessage(), e);
} finally {
if (move != null)
move.releaseConnection();
}
return result;
}
/**
* Analyzes a multistatus response from the OC server to generate an appropriate result.
*
* In WebDAV, a MOVE request on collections (folders) can be PARTIALLY successful: some
* children are moved, some other aren't.
*
* According to the WebDAV specification, a multistatus response SHOULD NOT include partial
* successes (201, 204) nor for descendants of already failed children (424) in the response
* entity. But SHOULD NOT != MUST NOT, so take carefully.
*
* @param move Move operation just finished with a multistatus response
* @return A result for the {@link MoveRemoteFileOperation} caller
*
* @throws IOException If the response body could not be parsed
* @throws DavException If the status code is other than MultiStatus or if obtaining
* the response XML document fails
*/
private RemoteOperationResult processPartialError(MoveMethod move)
throws IOException, DavException {
// Adding a list of failed descendants to the result could be interesting; or maybe not.
// For the moment, let's take the easy way.
/// check that some error really occurred
MultiStatusResponse[] responses = move.getResponseBodyAsMultiStatus().getResponses();
Status[] status = null;
boolean failFound = false;
for (int i = 0; i < responses.length && !failFound; i++ ) {
status = responses[i].getStatus();
failFound = (
status != null &&
status.length > 0 &&
status[0].getStatusCode() > 299
);
}
RemoteOperationResult result;
if (failFound) {
result = new RemoteOperationResult(ResultCode.PARTIAL_MOVE_DONE);
} else {
result = new RemoteOperationResult(
true,
HttpStatus.SC_MULTI_STATUS,
move.getResponseHeaders()
);
}
return result;
}
protected boolean isSuccess(int status) {
return status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT;
}
}

View File

@ -47,7 +47,7 @@ public class RenameRemoteFileOperation extends RemoteOperation {
private static final String TAG = RenameRemoteFileOperation.class.getSimpleName(); private static final String TAG = RenameRemoteFileOperation.class.getSimpleName();
private static final int RENAME_READ_TIMEOUT = 10000; private static final int RENAME_READ_TIMEOUT = 600000;
private static final int RENAME_CONNECTION_TIMEOUT = 5000; private static final int RENAME_CONNECTION_TIMEOUT = 5000;
private String mOldName; private String mOldName;

View File

@ -24,9 +24,7 @@
--> -->
<resources> <resources>
<string name="server_base_url"></string> <!-- the server url, without webdav path --> <string name="server_base_url"></string>
<string name="webdav_path">/remote.php/webdav</string> <!-- default value for webdav path (owncloud version > = 4)-->
<string name="username"></string> <string name="username"></string>
<string name="password"></string> <string name="password"></string>
<bool name="chunked">true</bool>
</resources> </resources>

View File

@ -33,16 +33,24 @@ import java.security.GeneralSecurityException;
import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import com.owncloud.android.lib.common.OwnCloudClientFactory; import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory; import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.resources.files.RemoteFile;
import com.owncloud.android.lib.common.network.NetworkUtils; import com.owncloud.android.lib.common.network.NetworkUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation; import com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation; import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation;
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation; import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation;
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
@ -50,14 +58,6 @@ import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
import com.owncloud.android.lib.resources.shares.GetRemoteSharesOperation; import com.owncloud.android.lib.resources.shares.GetRemoteSharesOperation;
import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation; import com.owncloud.android.lib.resources.shares.RemoveRemoteShareOperation;
import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.resources.shares.ShareType;
import com.owncloud.android.lib.test_project.R;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
/** /**
* Activity to test OC framework * Activity to test OC framework
@ -72,7 +72,6 @@ public class TestActivity extends Activity {
private String mServerUri; private String mServerUri;
private String mUser; private String mUser;
private String mPass; private String mPass;
private boolean mChunked;
private static final int BUFFER_SIZE = 1024; private static final int BUFFER_SIZE = 1024;
@ -91,7 +90,6 @@ public class TestActivity extends Activity {
mServerUri = getString(R.string.server_base_url); mServerUri = getString(R.string.server_base_url);
mUser = getString(R.string.username); mUser = getString(R.string.username);
mPass = getString(R.string.password); mPass = getString(R.string.password);
mChunked = getResources().getBoolean(R.bool.chunked);
Protocol pr = Protocol.getProtocol("https"); Protocol pr = Protocol.getProtocol("https");
if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) { if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) {
@ -133,14 +131,32 @@ public class TestActivity extends Activity {
/** /**
* Access to the library method to Create a Folder * Access to the library method to Create a Folder
* @param remotePath Full path to the new directory to create in the remote server. * @param remotePath Full path to the new directory to create in the remote server.
* @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet. * @param createFullPath 'True' means that all the ancestor folders should be created if
* don't exist yet.
* *
* @return * @return
*/ */
public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) { public RemoteOperationResult createFolder(String remotePath, boolean createFullPath) {
CreateRemoteFolderOperation createOperation = new CreateRemoteFolderOperation(remotePath, createFullPath); return TestActivity.createFolder(remotePath, createFullPath, mClient);
RemoteOperationResult result = createOperation.execute(mClient); }
/**
* Access to the library method to Create a Folder
* @param remotePath Full path to the new directory to create in the remote server.
* @param createFullPath 'True' means that all the ancestor folders should be created if
* don't exist yet.
* @param client Client instance configured to access the target OC server.
*
* @return Result of the operation
*/
public static RemoteOperationResult createFolder(
String remotePath, boolean createFullPath, OwnCloudClient client
) {
CreateRemoteFolderOperation createOperation =
new CreateRemoteFolderOperation(remotePath, createFullPath);
RemoteOperationResult result = createOperation.execute(client);
return result; return result;
} }
@ -170,13 +186,24 @@ public class TestActivity extends Activity {
* @return * @return
*/ */
public RemoteOperationResult removeFile(String remotePath) { public RemoteOperationResult removeFile(String remotePath) {
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath); RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
RemoteOperationResult result = removeOperation.execute(mClient); RemoteOperationResult result = removeOperation.execute(mClient);
return TestActivity.removeFile(remotePath, mClient);
}
/**
* Access to the library method to Remove a File or Folder
*
* @param remotePath Remote path of the file or folder in the server.
* @return
*/
public static RemoteOperationResult removeFile(String remotePath, OwnCloudClient client) {
RemoveRemoteFileOperation removeOperation = new RemoveRemoteFileOperation(remotePath);
RemoteOperationResult result = removeOperation.execute(client);
return result; return result;
} }
/** /**
* Access to the library method to Read a Folder (PROPFIND DEPTH 1) * Access to the library method to Read a Folder (PROPFIND DEPTH 1)
* @param remotePath * @param remotePath
@ -217,21 +244,39 @@ public class TestActivity extends Activity {
* *
* @return * @return
*/ */
public RemoteOperationResult uploadFile(String storagePath, String remotePath, String mimeType) { public RemoteOperationResult uploadFile(
String storagePath, String remotePath, String mimeType
UploadRemoteFileOperation uploadOperation; ) {
if ( mChunked && (new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) { return TestActivity.uploadFile(storagePath, remotePath, mimeType, mClient);
uploadOperation = new ChunkedUploadRemoteFileOperation(storagePath, remotePath, mimeType);
} else {
uploadOperation = new UploadRemoteFileOperation(storagePath, remotePath, mimeType);
}
RemoteOperationResult result = uploadOperation.execute(mClient);
return result;
} }
/** Access to the library method to Upload a File
* @param storagePath
* @param remotePath
* @param mimeType
* @param client Client instance configured to access the target OC server.
*
* @return
*/
public static RemoteOperationResult uploadFile(
String storagePath, String remotePath, String mimeType, OwnCloudClient client
) {
UploadRemoteFileOperation uploadOperation;
if ((new File(storagePath)).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) {
uploadOperation = new ChunkedUploadRemoteFileOperation(
storagePath, remotePath, mimeType
);
} else {
uploadOperation = new UploadRemoteFileOperation(
storagePath, remotePath, mimeType
);
}
RemoteOperationResult result = uploadOperation.execute(client);
return result;
}
/** Access to the library method to Get Shares /** Access to the library method to Get Shares
* *
* @return * @return
@ -295,11 +340,22 @@ public class TestActivity extends Activity {
* @return File instance of the extracted file. * @return File instance of the extracted file.
*/ */
public File extractAsset(String fileName) throws IOException { public File extractAsset(String fileName) throws IOException {
File extractedFile = new File(getCacheDir() + File.separator + fileName); return TestActivity.extractAsset(fileName, this);
}
/**
* Extracts file from AssetManager to cache folder.
*
* @param fileName Name of the asset file to extract.
* @param context Android context to access assets and file system.
* @return File instance of the extracted file.
*/
public static File extractAsset(String fileName, Context context) throws IOException {
File extractedFile = new File(context.getCacheDir() + File.separator + fileName);
if (!extractedFile.exists()) { if (!extractedFile.exists()) {
InputStream in = null; InputStream in = null;
FileOutputStream out = null; FileOutputStream out = null;
in = getAssets().open(fileName); in = context.getAssets().open(fileName);
out = new FileOutputStream(extractedFile); out = new FileOutputStream(extractedFile);
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
int readCount; int readCount;
@ -312,7 +368,6 @@ public class TestActivity extends Activity {
} }
return extractedFile; return extractedFile;
} }
} }

View File

@ -0,0 +1,459 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2014 ownCloud Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.test_project.test;
import java.io.File;
import java.security.GeneralSecurityException;
import junit.framework.AssertionFailedError;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
import com.owncloud.android.lib.common.network.NetworkUtils;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation;
import com.owncloud.android.lib.test_project.R;
import com.owncloud.android.lib.test_project.SelfSignedConfidentSslSocketFactory;
import com.owncloud.android.lib.test_project.TestActivity;
import android.content.Context;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
//import android.test.AndroidTestCase;
import android.util.Log;
/**
* Class to test MoveRemoteFileOperation
*
* With this TestCase we are experimenting a bit to improve the test suite design, in two aspects:
*
* - Reduce the dependency from the set of test cases on the "test project" needed to
* have an instrumented APK to install in the device, as required by the testing framework
* provided by Android. To get there, this class avoids calling TestActivity methods in the test
* method.
*
* - Reduce the impact of creating a remote fixture over the Internet, while the structure of the
* TestCase is kept easy to maintain. To get this, all the tests are done in a single test method,
* granting this way that setUp and tearDown are run only once.
*
*
* @author David A. Velasco
*/
//public class MoveFileTest extends AndroidTestCase {
public class MoveFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
private static final String LOG_TAG = MoveFileTest.class.getCanonicalName();
/// Paths to files and folders in fixture
private static final String SRC_BASE_FOLDER = "/src/";
private static final String TARGET_BASE_FOLDER = "/target/";
private static final String NO_FILE = "nofile.txt";
private static final String FILE1 = "file1.txt";
private static final String FILE2 = "file2.txt";
private static final String FILE3 = "file3.txt";
private static final String FILE4 = "file4.txt";
private static final String FILE5 = "file5.txt";
private static final String FILE6 = "file6.txt";
private static final String FILE7 = "file7.txt";
private static final String EMPTY = "empty/";
private static final String NO_FOLDER = "nofolder/";
private static final String FOLDER1 = "folder1/";
private static final String FOLDER2 = "folder2/";
private static final String FOLDER3 = "folder3/";
private static final String FOLDER4 = "folder4/";
private static final String SRC_PATH_TO_FILE_1 = SRC_BASE_FOLDER + FILE1;
private static final String TARGET_PATH_TO_FILE_1 = TARGET_BASE_FOLDER + FILE1;
private static final String SRC_PATH_TO_FILE_2 = SRC_BASE_FOLDER + FILE2;
private static final String TARGET_PATH_TO_FILE_2_RENAMED =
TARGET_BASE_FOLDER + "renamed_" + FILE2;
private static final String SRC_PATH_TO_FILE_3 = SRC_BASE_FOLDER + FILE3;
private static final String SRC_PATH_TO_FILE_3_RENAMED = SRC_BASE_FOLDER + "renamed_" + FILE3;
private static final String SRC_PATH_TO_FILE_4 = SRC_BASE_FOLDER + FILE4;
private static final String SRC_PATH_TO_FILE_5 = SRC_BASE_FOLDER + FILE5;
private static final String SRC_PATH_TO_FILE_6 = SRC_BASE_FOLDER + FILE6;
private static final String SRC_PATH_TO_FILE_7 = SRC_BASE_FOLDER + FILE7;
private static final String SRC_PATH_TO_NON_EXISTENT_FILE = SRC_BASE_FOLDER + NO_FILE;
private static final String SRC_PATH_TO_EMPTY_FOLDER = SRC_BASE_FOLDER + EMPTY;
private static final String TARGET_PATH_TO_EMPTY_FOLDER = TARGET_BASE_FOLDER + EMPTY;
private static final String SRC_PATH_TO_FULL_FOLDER_1 = SRC_BASE_FOLDER + FOLDER1;
private static final String TARGET_PATH_TO_FULL_FOLDER_1 = TARGET_BASE_FOLDER + FOLDER1;
private static final String SRC_PATH_TO_FULL_FOLDER_2 = SRC_BASE_FOLDER + FOLDER2;
private static final String TARGET_PATH_TO_FULL_FOLDER_2_RENAMED =
TARGET_BASE_FOLDER + "renamed_" + FOLDER2;
private static final String SRC_PATH_TO_FULL_FOLDER_3 = SRC_BASE_FOLDER + FOLDER3;
private static final String SRC_PATH_TO_FULL_FOLDER_4 = SRC_BASE_FOLDER + FOLDER4;
private static final String SRC_PATH_TO_FULL_FOLDER_3_RENAMED =
SRC_BASE_FOLDER + "renamed_" + FOLDER3;
private static final String TARGET_PATH_RENAMED_WITH_INVALID_CHARS =
SRC_BASE_FOLDER + "renamed:??_" + FILE6;
private static final String TARGET_PATH_TO_ALREADY_EXISTENT_EMPTY_FOLDER_4 = TARGET_BASE_FOLDER
+ FOLDER4;
private static final String TARGET_PATH_TO_NON_EXISTENT_FILE = TARGET_BASE_FOLDER + NO_FILE;
private static final String TARGET_PATH_TO_FILE_5_INTO_NON_EXISTENT_FOLDER =
TARGET_BASE_FOLDER + NO_FOLDER + FILE5;
private static final String TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7 = TARGET_BASE_FOLDER + FILE7;
private static final String[] FOLDERS_IN_FIXTURE = {
SRC_PATH_TO_EMPTY_FOLDER,
SRC_PATH_TO_FULL_FOLDER_1,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_2,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_3,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_4,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2 + FOLDER1,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2 + FOLDER2,
TARGET_BASE_FOLDER,
TARGET_PATH_TO_ALREADY_EXISTENT_EMPTY_FOLDER_4
};
private static final String[] FILES_IN_FIXTURE = {
SRC_PATH_TO_FILE_1,
SRC_PATH_TO_FILE_2,
SRC_PATH_TO_FILE_3,
SRC_PATH_TO_FILE_4,
SRC_PATH_TO_FILE_5,
SRC_PATH_TO_FULL_FOLDER_1 + FILE1,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2 + FILE1,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_1 + FOLDER2 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_2 + FILE1,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2 + FILE1,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_2 + FOLDER2 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_3 + FILE1,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2 + FILE1,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_3 + FOLDER2 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_4 + FILE1,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2 + FILE1,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2 + FILE2,
SRC_PATH_TO_FULL_FOLDER_4 + FOLDER2 + FOLDER2 + FILE2,
TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7
};
String mServerUri, mUser, mPass;
OwnCloudClient mClient = null;
public MoveFileTest() {
super(TestActivity.class);
Protocol pr = Protocol.getProtocol("https");
if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) {
try {
ProtocolSocketFactory psf = new SelfSignedConfidentSslSocketFactory();
Protocol.registerProtocol(
"https",
new Protocol("https", psf, 443));
} catch (GeneralSecurityException e) {
throw new AssertionFailedError(
"Self-signed confident SSL context could not be loaded");
}
}
}
protected Context getContext() {
return getActivity();
}
@Override
protected void setUp() throws Exception {
super.setUp();
// Next initialization cannot be done in the constructor because getContext() is not
// ready yet, returns NULL.
initAccessToServer(getContext());
Log.v(LOG_TAG, "Setting up the remote fixture...");
RemoteOperationResult result = null;
for (String folderPath : FOLDERS_IN_FIXTURE) {
result = TestActivity.createFolder(folderPath, true, mClient);
if (!result.isSuccess()) {
Utils.logAndThrow(LOG_TAG, result);
}
}
File txtFile = TestActivity.extractAsset(
TestActivity.ASSETS__TEXT_FILE_NAME, getContext()
);
for (String filePath : FILES_IN_FIXTURE) {
result = TestActivity.uploadFile(
txtFile.getAbsolutePath(), filePath, "txt/plain", mClient
);
if (!result.isSuccess()) {
Utils.logAndThrow(LOG_TAG, result);
}
}
Log.v(LOG_TAG, "Remote fixture created.");
}
/**
* Test move folder
*/
public void testMoveRemoteFileOperation() {
Log.v(LOG_TAG, "testMoveFolder in");
/// successful cases
// move file
MoveRemoteFileOperation moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_1,
TARGET_PATH_TO_FILE_1,
false
);
RemoteOperationResult result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move & rename file, different location
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_2,
TARGET_PATH_TO_FILE_2_RENAMED,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move & rename file, same location (rename file)
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_3,
SRC_PATH_TO_FILE_3_RENAMED,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move empty folder
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_EMPTY_FOLDER,
TARGET_PATH_TO_EMPTY_FOLDER,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move non-empty folder
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FULL_FOLDER_1,
TARGET_PATH_TO_FULL_FOLDER_1,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move & rename folder, different location
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FULL_FOLDER_2,
TARGET_PATH_TO_FULL_FOLDER_2_RENAMED,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move & rename folder, same location (rename folder)
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FULL_FOLDER_3,
SRC_PATH_TO_FULL_FOLDER_3_RENAMED,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move for nothing (success, but no interaction with network)
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_4,
SRC_PATH_TO_FILE_4,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
// move overwriting
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FULL_FOLDER_4,
TARGET_PATH_TO_ALREADY_EXISTENT_EMPTY_FOLDER_4,
true
);
result = moveOperation.execute(mClient);
assertTrue(result.isSuccess());
/// Failed cases
// file to move does not exist
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_NON_EXISTENT_FILE,
TARGET_PATH_TO_NON_EXISTENT_FILE,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.getCode() == ResultCode.FILE_NOT_FOUND);
// folder to move into does no exist
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_5,
TARGET_PATH_TO_FILE_5_INTO_NON_EXISTENT_FOLDER,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.getHttpCode() == HttpStatus.SC_CONFLICT);
// target location (renaming) has invalid characters
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_6,
TARGET_PATH_RENAMED_WITH_INVALID_CHARS,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME);
// name collision
moveOperation = new MoveRemoteFileOperation(
SRC_PATH_TO_FILE_7,
TARGET_PATH_TO_ALREADY_EXISTENT_FILE_7,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.getCode() == ResultCode.INVALID_OVERWRITE);
// move a folder into a descendant
moveOperation = new MoveRemoteFileOperation(
SRC_BASE_FOLDER,
SRC_PATH_TO_EMPTY_FOLDER,
false
);
result = moveOperation.execute(mClient);
assertTrue(result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT);
}
@Override
protected void tearDown() throws Exception {
Log.v(LOG_TAG, "Deleting remote fixture...");
String[] mPathsToCleanUp = {
SRC_BASE_FOLDER,
TARGET_BASE_FOLDER
};
for (String path : mPathsToCleanUp) {
RemoteOperationResult removeResult =
TestActivity.removeFile(path, mClient);
if (!removeResult.isSuccess() && removeResult.getCode() != ResultCode.TIMEOUT ) {
Utils.logAndThrow(LOG_TAG, removeResult);
}
}
super.tearDown();
Log.v(LOG_TAG, "Remote fixture delete.");
}
private void initAccessToServer(Context context) {
Log.v(LOG_TAG, "Setting up client instance to access OC server...");
mServerUri = context.getString(R.string.server_base_url);
mUser = context.getString(R.string.username);
mPass = context.getString(R.string.password);
mClient = new OwnCloudClient(
Uri.parse(mServerUri),
NetworkUtils.getMultiThreadedConnManager()
);
mClient.setDefaultTimeouts(
OwnCloudClientFactory.DEFAULT_DATA_TIMEOUT,
OwnCloudClientFactory.DEFAULT_CONNECTION_TIMEOUT);
mClient.setFollowRedirects(true);
mClient.setCredentials(
OwnCloudCredentialsFactory.newBasicCredentials(
mUser,
mPass
)
);
Log.v(LOG_TAG, "Client instance set up.");
}
}