-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14180 from Bensikrac/master
fix: FilesSyncHelper: Shuffle array after null check, else NullPointerException is thrown
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/test/java/com/owncloud/android/utils/FilesSyncHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Nextcloud - Android Client | ||
* | ||
* SPDX-FileCopyrightText: 2024 Benedek Major <[email protected]> | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
package com.owncloud.android.utils; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import org.lukhnos.nnio.file.FileVisitResult; | ||
import org.lukhnos.nnio.file.Path; | ||
import org.lukhnos.nnio.file.Paths; | ||
import org.lukhnos.nnio.file.SimpleFileVisitor; | ||
import org.lukhnos.nnio.file.attribute.BasicFileAttributes; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
|
||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
|
||
import static com.owncloud.android.utils.FilesSyncHelper.walkFileTreeRandomly; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.mock; | ||
|
||
|
||
|
||
public class FilesSyncHelperTest { | ||
@Mock | ||
private File mockFile; | ||
|
||
@Mock | ||
private Path mockPath; | ||
|
||
@Test | ||
public void testWalkFileTreeNullHandling(){ | ||
mockFile = mock(File.class); | ||
mockPath = mock(Path.class); | ||
Mockito.when(mockFile.isDirectory()).thenReturn(true); | ||
Mockito.when(mockPath.toFile()).thenReturn(mockFile); | ||
Mockito.when(mockFile.listFiles()).thenReturn(null); | ||
Mockito.when(mockFile.canRead()).thenReturn(true); | ||
|
||
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}; | ||
try{ | ||
walkFileTreeRandomly(mockPath,visitor); | ||
} catch (Exception e){ | ||
StringWriter writer = new StringWriter(); | ||
PrintWriter printWriter = new PrintWriter( writer ); | ||
e.printStackTrace( printWriter ); | ||
printWriter.flush(); | ||
|
||
String stackTrace = writer.toString(); | ||
fail("walkFileTree throws an exception: \n" + stackTrace); | ||
} | ||
} | ||
|
||
} |