blob: f588c4fdb060e2fa91ad7ced6d62e437628d852c [file] [log] [blame]
Robert Berryc31a8392017-07-14 14:19:21 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.backup;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.platform.test.annotations.Presubmit;
Brett Chabota26eda92018-07-23 13:08:30 -070022
23import androidx.test.filters.SmallTest;
24import androidx.test.runner.AndroidJUnit4;
Robert Berryc31a8392017-07-14 14:19:21 +010025
26import org.junit.Before;
27import org.junit.Rule;
28import org.junit.Test;
29import org.junit.rules.TemporaryFolder;
30import org.junit.runner.RunWith;
31import org.mockito.InOrder;
32import org.mockito.Mock;
33import org.mockito.Mockito;
34import org.mockito.MockitoAnnotations;
35
36import java.io.DataInputStream;
37import java.io.File;
38import java.io.FileInputStream;
39import java.util.ArrayList;
Bernardo Rufinofd1d7292018-07-10 12:54:05 +010040import java.util.function.Consumer;
Robert Berryc31a8392017-07-14 14:19:21 +010041
42@SmallTest
43@Presubmit
44@RunWith(AndroidJUnit4.class)
45public class DataChangedJournalTest {
46 private static final String GMAIL = "com.google.gmail";
47 private static final String DOCS = "com.google.docs";
48 private static final String GOOGLE_PLUS = "com.google.plus";
49
50 @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
51
Bernardo Rufinofd1d7292018-07-10 12:54:05 +010052 @Mock private Consumer<String> mConsumer;
Robert Berryc31a8392017-07-14 14:19:21 +010053
54 private File mFile;
55 private DataChangedJournal mJournal;
56
57 @Before
58 public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 mFile = mTemporaryFolder.newFile();
61 mJournal = new DataChangedJournal(mFile);
62 }
63
64 @Test
65 public void addPackage_addsPackagesToEndOfFile() throws Exception {
66 mJournal.addPackage(GMAIL);
67 mJournal.addPackage(DOCS);
68 mJournal.addPackage(GOOGLE_PLUS);
69
70 FileInputStream fos = new FileInputStream(mFile);
71 DataInputStream dos = new DataInputStream(fos);
72 assertThat(dos.readUTF()).isEqualTo(GMAIL);
73 assertThat(dos.readUTF()).isEqualTo(DOCS);
74 assertThat(dos.readUTF()).isEqualTo(GOOGLE_PLUS);
75 assertThat(dos.available()).isEqualTo(0);
76 }
77
78 @Test
79 public void delete_deletesTheFile() throws Exception {
80 mJournal.addPackage(GMAIL);
81
82 mJournal.delete();
83
84 assertThat(mFile.exists()).isFalse();
85 }
86
87 @Test
88 public void equals_isTrueForTheSameFile() throws Exception {
89 assertThat(mJournal.equals(new DataChangedJournal(mFile))).isTrue();
90 }
91
92 @Test
93 public void equals_isFalseForDifferentFiles() throws Exception {
94 assertThat(mJournal.equals(new DataChangedJournal(mTemporaryFolder.newFile()))).isFalse();
95 }
96
97 @Test
98 public void forEach_iteratesThroughPackagesInFileInOrder() throws Exception {
99 mJournal.addPackage(GMAIL);
100 mJournal.addPackage(DOCS);
101
102 mJournal.forEach(mConsumer);
103
104 InOrder inOrder = Mockito.inOrder(mConsumer);
105 inOrder.verify(mConsumer).accept(GMAIL);
106 inOrder.verify(mConsumer).accept(DOCS);
107 inOrder.verifyNoMoreInteractions();
108 }
109
110 @Test
111 public void listJournals_returnsJournalsForEveryFileInDirectory() throws Exception {
112 File folder = mTemporaryFolder.newFolder();
113 DataChangedJournal.newJournal(folder);
114 DataChangedJournal.newJournal(folder);
115
116 ArrayList<DataChangedJournal> journals = DataChangedJournal.listJournals(folder);
117
118 assertThat(journals).hasSize(2);
119 }
120
121 @Test
122 public void newJournal_createsANewTemporaryFile() throws Exception {
123 File folder = mTemporaryFolder.newFolder();
124
125 DataChangedJournal.newJournal(folder);
126
127 assertThat(folder.listFiles()).hasLength(1);
128 }
129
130 @Test
131 public void toString_isSameAsFileToString() throws Exception {
132 assertThat(mJournal.toString()).isEqualTo(mFile.toString());
133 }
134}