blob: 3cdeba607d4157d15241f79129691cafd9216071 [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;
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
24
25import org.junit.Before;
26import org.junit.Rule;
27import org.junit.Test;
28import org.junit.rules.TemporaryFolder;
29import org.junit.runner.RunWith;
30import org.mockito.InOrder;
31import org.mockito.Mock;
32import org.mockito.Mockito;
33import org.mockito.MockitoAnnotations;
34
35import java.io.DataInputStream;
36import java.io.File;
37import java.io.FileInputStream;
38import java.util.ArrayList;
Bernardo Rufinofd1d7292018-07-10 12:54:05 +010039import java.util.function.Consumer;
Robert Berryc31a8392017-07-14 14:19:21 +010040
41@SmallTest
42@Presubmit
43@RunWith(AndroidJUnit4.class)
44public class DataChangedJournalTest {
45 private static final String GMAIL = "com.google.gmail";
46 private static final String DOCS = "com.google.docs";
47 private static final String GOOGLE_PLUS = "com.google.plus";
48
49 @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
50
Bernardo Rufinofd1d7292018-07-10 12:54:05 +010051 @Mock private Consumer<String> mConsumer;
Robert Berryc31a8392017-07-14 14:19:21 +010052
53 private File mFile;
54 private DataChangedJournal mJournal;
55
56 @Before
57 public void setUp() throws Exception {
58 MockitoAnnotations.initMocks(this);
59 mFile = mTemporaryFolder.newFile();
60 mJournal = new DataChangedJournal(mFile);
61 }
62
63 @Test
64 public void addPackage_addsPackagesToEndOfFile() throws Exception {
65 mJournal.addPackage(GMAIL);
66 mJournal.addPackage(DOCS);
67 mJournal.addPackage(GOOGLE_PLUS);
68
69 FileInputStream fos = new FileInputStream(mFile);
70 DataInputStream dos = new DataInputStream(fos);
71 assertThat(dos.readUTF()).isEqualTo(GMAIL);
72 assertThat(dos.readUTF()).isEqualTo(DOCS);
73 assertThat(dos.readUTF()).isEqualTo(GOOGLE_PLUS);
74 assertThat(dos.available()).isEqualTo(0);
75 }
76
77 @Test
78 public void delete_deletesTheFile() throws Exception {
79 mJournal.addPackage(GMAIL);
80
81 mJournal.delete();
82
83 assertThat(mFile.exists()).isFalse();
84 }
85
86 @Test
87 public void equals_isTrueForTheSameFile() throws Exception {
88 assertThat(mJournal.equals(new DataChangedJournal(mFile))).isTrue();
89 }
90
91 @Test
92 public void equals_isFalseForDifferentFiles() throws Exception {
93 assertThat(mJournal.equals(new DataChangedJournal(mTemporaryFolder.newFile()))).isFalse();
94 }
95
96 @Test
97 public void forEach_iteratesThroughPackagesInFileInOrder() throws Exception {
98 mJournal.addPackage(GMAIL);
99 mJournal.addPackage(DOCS);
100
101 mJournal.forEach(mConsumer);
102
103 InOrder inOrder = Mockito.inOrder(mConsumer);
104 inOrder.verify(mConsumer).accept(GMAIL);
105 inOrder.verify(mConsumer).accept(DOCS);
106 inOrder.verifyNoMoreInteractions();
107 }
108
109 @Test
110 public void listJournals_returnsJournalsForEveryFileInDirectory() throws Exception {
111 File folder = mTemporaryFolder.newFolder();
112 DataChangedJournal.newJournal(folder);
113 DataChangedJournal.newJournal(folder);
114
115 ArrayList<DataChangedJournal> journals = DataChangedJournal.listJournals(folder);
116
117 assertThat(journals).hasSize(2);
118 }
119
120 @Test
121 public void newJournal_createsANewTemporaryFile() throws Exception {
122 File folder = mTemporaryFolder.newFolder();
123
124 DataChangedJournal.newJournal(folder);
125
126 assertThat(folder.listFiles()).hasLength(1);
127 }
128
129 @Test
130 public void toString_isSameAsFileToString() throws Exception {
131 assertThat(mJournal.toString()).isEqualTo(mFile.toString());
132 }
133}