Fix NPE while reading leftover journals in BMS
Thisis the NPE bug we saw a while ago but it has been coming up in test runs recently (b/). Let's fix the NPE for now and we can work on the underlying issue later. Bug: 136730045 Test: atest DataChangedJournalTest Change-Id: I7267f507be4c797491d86ebb8bc5e6c3c17652a3
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package com.android.server.backup;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.util.Slog;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
@@ -36,6 +37,7 @@ import java.util.function.Consumer;
|
||||
* reboot.
|
||||
*/
|
||||
public class DataChangedJournal {
|
||||
private static final String TAG = "DataChangedJournal";
|
||||
private static final String FILE_NAME_PREFIX = "journal";
|
||||
|
||||
/**
|
||||
@@ -139,7 +141,12 @@ public class DataChangedJournal {
|
||||
*/
|
||||
static ArrayList<DataChangedJournal> listJournals(File journalDirectory) {
|
||||
ArrayList<DataChangedJournal> journals = new ArrayList<>();
|
||||
for (File file : journalDirectory.listFiles()) {
|
||||
File[] journalFiles = journalDirectory.listFiles();
|
||||
if (journalFiles == null) {
|
||||
Slog.w(TAG, "Failed to read journal files");
|
||||
return journals;
|
||||
}
|
||||
for (File file : journalFiles) {
|
||||
journals.add(new DataChangedJournal(file));
|
||||
}
|
||||
return journals;
|
||||
|
||||
@@ -18,6 +18,9 @@ package com.android.server.backup;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
@@ -50,6 +53,7 @@ public class DataChangedJournalTest {
|
||||
@Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock private Consumer<String> mConsumer;
|
||||
@Mock private File invalidFile;
|
||||
|
||||
private File mFile;
|
||||
private DataChangedJournal mJournal;
|
||||
@@ -131,4 +135,10 @@ public class DataChangedJournalTest {
|
||||
public void toString_isSameAsFileToString() throws Exception {
|
||||
assertThat(mJournal.toString()).isEqualTo(mFile.toString());
|
||||
}
|
||||
|
||||
public void listJournals_invalidJournalFile_returnsEmptyList() throws Exception {
|
||||
when(invalidFile.listFiles()).thenReturn(null);
|
||||
|
||||
assertEquals(0, DataChangedJournal.listJournals(invalidFile).size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user