@@ -25,26 +25,35 @@ import kotlin.random.Random
2525@Suppress(" TooManyFunctions" , " MagicNumber" )
2626class FileDeletionTests : AbstractIT () {
2727
28- private lateinit var tempDir : File
28+ private val createdFilePaths = mutableListOf< String >()
2929
3030 @Before
3131 fun setup () {
32- val parent = System .getProperty(" java.io.tmpdir" )
33- val childPath = " file_deletion_test_${System .currentTimeMillis()} "
34- tempDir = File (parent, childPath)
35- tempDir.mkdirs()
32+ createdFilePaths.clear()
3633 }
3734
3835 @After
3936 fun cleanup () {
40- tempDir.deleteRecursively()
37+ createdFilePaths.forEach { File (it).delete() }
38+ createdFilePaths.clear()
4139 }
4240
4341 private fun getRandomRemoteId (): String = Random
4442 .nextLong(10_000_000L , 99_999_999L )
4543 .toString()
4644 .padEnd(32 , ' 0' )
4745
46+ private fun createFileAtExpectedPath (ocFile : OCFile , content : String = "Temporary test content"): File {
47+ val expectedPath = FileStorageUtils .getDefaultSavePathFor(user.accountName, ocFile)
48+ val localFile = File (expectedPath).apply {
49+ parentFile?.mkdirs()
50+ createNewFile()
51+ writeText(content)
52+ }
53+ createdFilePaths.add(localFile.absolutePath)
54+ return localFile
55+ }
56+
4857 private fun createAndSaveSingleFileWithLocalCopy (): OCFile {
4958 val now = System .currentTimeMillis()
5059
@@ -59,11 +68,7 @@ class FileDeletionTests : AbstractIT() {
5968 permissions = " RWDNV"
6069 }
6170
62- val localFile = File (tempDir, " TestFile_${file.fileId} .txt" ).apply {
63- parentFile?.mkdirs()
64- createNewFile()
65- writeText(" Temporary test content" )
66- }
71+ val localFile = createFileAtExpectedPath(file)
6772 file.storagePath = localFile.absolutePath
6873
6974 storageManager.saveFile(file)
@@ -117,11 +122,11 @@ class FileDeletionTests : AbstractIT() {
117122
118123 listOf (rootFolder, subFolder, file1, file2).forEach { storageManager.saveFile(it) }
119124
120- val file1Path = File (tempDir, " file1_ ${ file1.fileId} .txt " ). apply { createNewFile() }
121- val file2Path = File (tempDir, " file2_ ${ file2.fileId} .txt " ). apply { createNewFile() }
125+ val localFile1 = createFileAtExpectedPath( file1)
126+ val localFile2 = createFileAtExpectedPath( file2)
122127
123- file1.storagePath = file1Path .absolutePath
124- file2.storagePath = file2Path .absolutePath
128+ file1.storagePath = localFile1 .absolutePath
129+ file2.storagePath = localFile2 .absolutePath
125130
126131 storageManager.saveFile(file1)
127132 storageManager.saveFile(file2)
@@ -132,69 +137,85 @@ class FileDeletionTests : AbstractIT() {
132137 private fun getMixedOcFiles (): List <OCFile > {
133138 val now = System .currentTimeMillis()
134139
135- fun createFolder (id : Long , parentId : Long , path : String ): OCFile = OCFile (path).apply {
136- fileId = id
140+ fun saveFolder (parentId : Long , path : String ): OCFile = OCFile (path).apply {
137141 this .parentId = parentId
138142 remoteId = getRandomRemoteId()
139143 mimeType = MimeType .DIRECTORY
140144 creationTimestamp = now
141145 modificationTimestamp = now
142146 permissions = " RWDNVCK"
143- }
144-
145- fun createFile (id : Long , parentId : Long , path : String , size : Long , mime : String ): OCFile = OCFile (path).apply {
146- fileId = id
147- this .parentId = parentId
148- remoteId = getRandomRemoteId()
149- fileLength = size
150- creationTimestamp = now
151- mimeType = mime
152- modificationTimestamp = now
153- permissions = " RWDNV"
154- }
155-
156- val list = mutableListOf<OCFile >()
157-
158- list.add(createFolder(1 , 0 , " /" ))
159-
160- list.add(createFolder(5 , 2 , " /Documents/Projects" ))
161- list.add(createFile(9 , 5 , " /Documents/Projects/spec.txt" , 12000 , MimeType .TEXT_PLAIN ))
162- list.add(createFolder(2 , 1 , " /Documents" ))
163- list.add(createFile(11 , 7 , " /Photos/Vacation/img2.jpg" , 300000 , MimeType .JPEG ))
164- list.add(createFolder(7 , 3 , " /Photos/Vacation" ))
165- list.add(createFile(4 , 2 , " /Documents/example.pdf" , 150000 , MimeType .PDF ))
166- list.add(createFolder(3 , 1 , " /Photos" ))
167- list.add(createFile(12 , 3 , " /Photos/cover.png" , 80000 , MimeType .PNG ))
168- list.add(createFile(6 , 5 , " /Documents/Projects/readme.txt" , 2000 , MimeType .TEXT_PLAIN ))
169- list.add(createFolder(8 , 5 , " /Documents/Projects/Archive" ))
170- list.add(createFile(13 , 8 , " /Documents/Projects/Archive/old.bmp" , 900000 , MimeType .BMP ))
171- list.add(createFile(10 , 7 , " /Photos/Vacation/img1.jpg" , 250000 , MimeType .JPEG ))
172- list.add(createFolder(14 , 1 , " /Temp" ))
173- list.add(createFile(15 , 14 , " /Temp/tmp_file_1.txt" , 400 , MimeType .TEXT_PLAIN ))
174- list.add(createFile(16 , 14 , " /Temp/tmp_file_2.txt" , 800 , MimeType .TEXT_PLAIN ))
175- list.add(createFolder(17 , 14 , " /Temp/Nested" ))
176- list.add(createFile(18 , 17 , " /Temp/Nested/deep.txt" , 100 , MimeType .TEXT_PLAIN ))
177- list.add(createFile(19 , 2 , " /Documents/notes.txt" , 1500 , MimeType .TEXT_PLAIN ))
178- list.add(createFolder(20 , 3 , " /Photos/EmptyFolder" ))
179-
180- list.forEach { ocFile ->
181- if (! ocFile.isFolder) {
182- val localFile = File (tempDir, ocFile.remoteId).apply {
183- parentFile?.mkdirs()
184- createNewFile()
185- writeText(" test content" )
186- }
187- ocFile.storagePath = localFile.absolutePath
188- storageManager.saveFile(ocFile)
189- } else {
190- // For folders, create the folder in tempDir
191- val localFolder = File (tempDir, ocFile.remoteId).apply { mkdirs() }
192- ocFile.storagePath = localFolder.absolutePath
193- storageManager.saveFile(ocFile)
147+ }.also { storageManager.saveFile(it) }
148+
149+ fun saveFileWithLocalCopy (parentId : Long , path : String , size : Long , mime : String ): OCFile {
150+ val ocFile = OCFile (path).apply {
151+ this .parentId = parentId
152+ remoteId = getRandomRemoteId()
153+ fileLength = size
154+ creationTimestamp = now
155+ mimeType = mime
156+ modificationTimestamp = now
157+ permissions = " RWDNV"
158+ storagePath = FileStorageUtils .getDefaultSavePathFor(user.accountName, this )
159+ }
160+ val localFile = File (ocFile.storagePath).apply {
161+ parentFile?.mkdirs()
162+ createNewFile()
163+ writeText(" test content" )
194164 }
165+ createdFilePaths.add(localFile.absolutePath)
166+ storageManager.saveFile(ocFile)
167+ return ocFile
195168 }
196169
197- return list
170+ val root = saveFolder(0 , " /" )
171+ val documents = saveFolder(root.fileId, " /Documents" )
172+ val photos = saveFolder(root.fileId, " /Photos" )
173+ val temp = saveFolder(root.fileId, " /Temp" )
174+ val projects = saveFolder(documents.fileId, " /Documents/Projects" )
175+ val vacation = saveFolder(photos.fileId, " /Photos/Vacation" )
176+ val archive = saveFolder(projects.fileId, " /Documents/Projects/Archive" )
177+ val nested = saveFolder(temp.fileId, " /Temp/Nested" )
178+ val emptyFolder = saveFolder(photos.fileId, " /Photos/EmptyFolder" )
179+
180+ val allEntries = mutableListOf (root, documents, photos, temp, projects, vacation, archive, nested, emptyFolder)
181+
182+ allEntries.add(
183+ saveFileWithLocalCopy(
184+ projects.fileId,
185+ " /Documents/Projects/spec.txt" ,
186+ 12000 ,
187+ MimeType .TEXT_PLAIN
188+ )
189+ )
190+ allEntries.add(saveFileWithLocalCopy(vacation.fileId, " /Photos/Vacation/img2.jpg" , 300000 , MimeType .JPEG ))
191+ allEntries.add(saveFileWithLocalCopy(documents.fileId, " /Documents/example.pdf" , 150000 , MimeType .PDF ))
192+ allEntries.add(saveFileWithLocalCopy(photos.fileId, " /Photos/cover.png" , 80000 , MimeType .PNG ))
193+ allEntries.add(
194+ saveFileWithLocalCopy(
195+ projects.fileId,
196+ " /Documents/Projects/readme.txt" ,
197+ 2000 ,
198+ MimeType .TEXT_PLAIN
199+ )
200+ )
201+ allEntries.add(
202+ saveFileWithLocalCopy(
203+ archive.fileId,
204+ " /Documents/Projects/Archive/old.bmp" ,
205+ 900000 ,
206+ MimeType .BMP
207+ )
208+ )
209+ allEntries.add(saveFileWithLocalCopy(vacation.fileId, " /Photos/Vacation/img1.jpg" , 250000 , MimeType .JPEG ))
210+ allEntries.add(saveFileWithLocalCopy(temp.fileId, " /Temp/tmp_file_1.txt" , 400 , MimeType .TEXT_PLAIN ))
211+ allEntries.add(saveFileWithLocalCopy(temp.fileId, " /Temp/tmp_file_2.txt" , 800 , MimeType .TEXT_PLAIN ))
212+ allEntries.add(saveFileWithLocalCopy(nested.fileId, " /Temp/Nested/deep.txt" , 100 , MimeType .TEXT_PLAIN ))
213+ allEntries.add(saveFileWithLocalCopy(documents.fileId, " /Documents/notes.txt" , 1500 , MimeType .TEXT_PLAIN ))
214+
215+ return allEntries.sortedWith(
216+ compareBy<OCFile > { it.isFolder }
217+ .thenByDescending { it.remotePath.count { c -> c == ' /' } }
218+ )
198219 }
199220
200221 @Test
0 commit comments