FileIT.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2018 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
  6. */
  7. package com.owncloud.android;
  8. import com.owncloud.android.datamodel.OCFile;
  9. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  10. import com.owncloud.android.operations.CreateFolderOperation;
  11. import com.owncloud.android.operations.RemoveFileOperation;
  12. import com.owncloud.android.operations.RenameFileOperation;
  13. import com.owncloud.android.operations.SynchronizeFolderOperation;
  14. import com.owncloud.android.operations.common.SyncOperation;
  15. import org.junit.Test;
  16. import org.junit.runner.RunWith;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import androidx.test.ext.junit.runners.AndroidJUnit4;
  20. import static junit.framework.TestCase.assertTrue;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertFalse;
  23. import static org.junit.Assert.assertNull;
  24. /**
  25. * Tests related to file operations.
  26. */
  27. @RunWith(AndroidJUnit4.class)
  28. public class FileIT extends AbstractOnServerIT {
  29. @Test
  30. public void testCreateFolder() {
  31. String path = "/testFolder/";
  32. // folder does not exist yet
  33. assertNull(getStorageManager().getFileByPath(path));
  34. SyncOperation syncOp = new CreateFolderOperation(path, user, targetContext, getStorageManager());
  35. RemoteOperationResult result = syncOp.execute(client);
  36. assertTrue(result.toString(), result.isSuccess());
  37. // folder exists
  38. OCFile file = getStorageManager().getFileByPath(path);
  39. assertTrue(file.isFolder());
  40. // cleanup
  41. assertTrue(new RemoveFileOperation(file, false, user, false, targetContext, getStorageManager())
  42. .execute(client)
  43. .isSuccess());
  44. }
  45. @Test
  46. public void testCreateNonExistingSubFolder() {
  47. String path = "/subFolder/1/2/3/4/5/";
  48. // folder does not exist yet
  49. assertNull(getStorageManager().getFileByPath(path));
  50. SyncOperation syncOp = new CreateFolderOperation(path, user, targetContext, getStorageManager());
  51. RemoteOperationResult result = syncOp.execute(client);
  52. assertTrue(result.toString(), result.isSuccess());
  53. // folder exists
  54. OCFile file = getStorageManager().getFileByPath(path);
  55. assertTrue(file.isFolder());
  56. // cleanup
  57. new RemoveFileOperation(file,
  58. false,
  59. user,
  60. false,
  61. targetContext,
  62. getStorageManager())
  63. .execute(client);
  64. }
  65. @Test
  66. public void testRemoteIdNull() {
  67. getStorageManager().deleteAllFiles();
  68. assertEquals(0, getStorageManager().getAllFiles().size());
  69. OCFile test = new OCFile("/123.txt");
  70. getStorageManager().saveFile(test);
  71. assertEquals(1, getStorageManager().getAllFiles().size());
  72. getStorageManager().deleteAllFiles();
  73. assertEquals(0, getStorageManager().getAllFiles().size());
  74. }
  75. @Test
  76. public void testRenameFolder() throws IOException {
  77. String folderPath = "/testRenameFolder/";
  78. // create folder
  79. createFolder(folderPath);
  80. // upload file inside it
  81. uploadFile(getDummyFile("nonEmpty.txt"), folderPath + "text.txt");
  82. // sync folder
  83. assertTrue(new SynchronizeFolderOperation(targetContext,
  84. folderPath,
  85. user,
  86. fileDataStorageManager)
  87. .execute(targetContext)
  88. .isSuccess());
  89. // check if file exists
  90. String storagePath1 = fileDataStorageManager.getFileByDecryptedRemotePath(folderPath).getStoragePath();
  91. assertTrue(new File(storagePath1).exists());
  92. String storagePath2 = fileDataStorageManager
  93. .getFileByDecryptedRemotePath(folderPath + "text.txt")
  94. .getStoragePath();
  95. assertTrue(new File(storagePath2).exists());
  96. shortSleep();
  97. // Rename
  98. assertTrue(
  99. new RenameFileOperation(folderPath, "test123", fileDataStorageManager)
  100. .execute(targetContext)
  101. .isSuccess()
  102. );
  103. // after rename check new location
  104. assertTrue(
  105. new File(fileDataStorageManager.getFileByDecryptedRemotePath("/test123/").getStoragePath())
  106. .exists()
  107. );
  108. assertTrue(
  109. new File(fileDataStorageManager.getFileByDecryptedRemotePath("/test123/text.txt").getStoragePath())
  110. .exists()
  111. );
  112. // old files do no exist
  113. assertNull(fileDataStorageManager.getFileByDecryptedRemotePath(folderPath));
  114. assertNull(fileDataStorageManager.getFileByDecryptedRemotePath(folderPath + "text.txt"));
  115. // local files also do not exist
  116. assertFalse(new File(storagePath1).exists());
  117. assertFalse(new File(storagePath2).exists());
  118. }
  119. }