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

Added success cases to MoveFileTest

This commit is contained in:
David A. Velasco 2014-08-22 15:29:20 +02:00
parent dc26392647
commit 9d286636d5

View File

@ -24,8 +24,8 @@
package com.owncloud.android.lib.test_project.test;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.Vector;
import junit.framework.AssertionFailedError;
@ -42,38 +42,130 @@ 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.AndroidTestCase;
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 AndroidTestCase {
public class MoveFileTest extends ActivityInstrumentationTestCase2<TestActivity> {
private static final String LOG_TAG = MoveFileTest.class.getCanonicalName();
/* Paths for tests on folders */
private static final String SRC_FOLDER_NAME = "folderToMove";
private static final String SRC_FOLDER_PARENT_PATH = "/src/";
private static final String SRC_FOLDER_PATH = SRC_FOLDER_PARENT_PATH + SRC_FOLDER_NAME;
private static final String TARGET_FOLDER_PARENT_PATH = "/target/";
private static final String TARGET_FOLDER_PATH = TARGET_FOLDER_PARENT_PATH + SRC_FOLDER_NAME;
private static boolean mGlobalSetupDone = false;
/// 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 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 EMPTY = "empty/";
private static final String FOLDER1 = "folder1/";
private static final String FOLDER2 = "folder2/";
private static final String FOLDER3 = "folder3/";
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_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_3_RENAMED =
SRC_BASE_FOLDER + "renamed_" + FOLDER3;
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,
TARGET_BASE_FOLDER
};
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_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,
};
String mServerUri, mUser, mPass;
OwnCloudClient mClient = null;
private Vector<String> mToCleanUpInServer = new Vector<String>(2);
public MoveFileTest() {
super();
mGlobalSetupDone = false;
super(TestActivity.class);
Protocol pr = Protocol.getProtocol("https");
if (pr == null || !(pr.getSocketFactory() instanceof SelfSignedConfidentSslSocketFactory)) {
@ -90,14 +182,169 @@ public class MoveFileTest extends AndroidTestCase {
}
}
protected Context getContext() {
return getActivity();
}
@Override
protected void setUp() throws Exception {
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());
mServerUri = getContext().getString(R.string.server_base_url);
mUser = getContext().getString(R.string.username);
mPass = getContext().getString(R.string.password);
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());
/// TODO failed cases
// file to move does not exist
// folder to move into does no exist
// target location (renaming) has invalid characters
// name collision
}
@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()) {
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),
@ -113,55 +360,10 @@ public class MoveFileTest extends AndroidTestCase {
mPass
)
);
if (!mGlobalSetupDone) {
Log.v(LOG_TAG, "Starting global set up");
RemoteOperationResult result =
TestActivity.createFolder(SRC_FOLDER_PATH, true, mClient);
if (!result.isSuccess()) {
Utils.logAndThrow(LOG_TAG, result);
}
result = TestActivity.createFolder(TARGET_FOLDER_PARENT_PATH, true, mClient);
if (!result.isSuccess()) {
Utils.logAndThrow(LOG_TAG, result);
}
Log.v(LOG_TAG, "Global set up done");
mGlobalSetupDone = true;
}
Log.v(LOG_TAG, "Client instance set up.");
}
/**
* Test move folder
*/
public void testMoveFolder() {
mToCleanUpInServer.add(SRC_FOLDER_PARENT_PATH);
mToCleanUpInServer.add(TARGET_FOLDER_PARENT_PATH);
MoveRemoteFileOperation renameOperation = new MoveRemoteFileOperation(
SRC_FOLDER_PATH,
TARGET_FOLDER_PATH,
false
);
RemoteOperationResult result = renameOperation.execute(mClient);
assertTrue(result.isSuccess());
}
@Override
protected void tearDown() throws Exception {
for (String path : mToCleanUpInServer) {
RemoteOperationResult removeResult =
TestActivity.removeFile(path, mClient);
if (!removeResult.isSuccess()) {
Utils.logAndThrow(LOG_TAG, removeResult);
}
}
mToCleanUpInServer.clear();
super.tearDown();
}
}