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