1 /*
2  * Copyright (C) 2018 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 package com.android.compatibility.common.tradefed.result.suite;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
21 import com.android.tradefed.build.BuildInfo;
22 import com.android.tradefed.build.IBuildInfo;
23 import com.android.tradefed.config.ConfigurationDef;
24 import com.android.tradefed.invoker.IInvocationContext;
25 import com.android.tradefed.invoker.InvocationContext;
26 import com.android.tradefed.util.FileUtil;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 
37 /** Unit tests for {@link PreviousSessionFileCopier}. */
38 @RunWith(JUnit4.class)
39 public class PreviousSessionFileCopierTest {
40 
41     private PreviousSessionFileCopier mCopier;
42     private File mPreviousDir;
43     private File mCurrentDir;
44     private IInvocationContext mContext;
45 
46     @Before
setUp()47     public void setUp() throws Exception {
48         mCurrentDir = FileUtil.createTempDir("current-copier-tests");
49         mCopier =
50                 new PreviousSessionFileCopier() {
51                     @Override
52                     protected CompatibilityBuildHelper createCompatibilityHelper(IBuildInfo info) {
53                         return new CompatibilityBuildHelper(info) {
54                             @Override
55                             public File getResultDir() throws FileNotFoundException {
56                                 return mCurrentDir;
57                             }
58                         };
59                     }
60                 };
61         mPreviousDir = FileUtil.createTempDir("previous-copier-tests");
62         mContext = new InvocationContext();
63         mContext.addDeviceBuildInfo(ConfigurationDef.DEFAULT_DEVICE_NAME, new BuildInfo());
64         mCopier.setPreviousSessionDir(mPreviousDir);
65     }
66 
67     @After
tearDown()68     public void tearDown() {
69         FileUtil.recursiveDelete(mPreviousDir);
70     }
71 
72     @Test
testCopy()73     public void testCopy() throws Exception {
74         new File(mPreviousDir, "newFile").createNewFile();
75         assertEquals(0, mCurrentDir.listFiles().length);
76         mCopier.invocationStarted(mContext);
77         mCopier.invocationEnded(500L);
78         assertEquals(1, mCurrentDir.listFiles().length);
79     }
80 
81     @Test
testCopy_fileExists()82     public void testCopy_fileExists() throws Exception {
83         File original = new File(mCurrentDir, "newFile");
84         original.createNewFile();
85         FileUtil.writeToFile("CURRENT", original);
86 
87         File previous = new File(mPreviousDir, "newFile");
88         previous.createNewFile();
89         FileUtil.writeToFile("PREVIOUS", previous);
90 
91         assertEquals(1, mCurrentDir.listFiles().length);
92         mCopier.invocationStarted(mContext);
93         mCopier.invocationEnded(500L);
94         assertEquals(1, mCurrentDir.listFiles().length);
95         // File are not overriden
96         assertEquals("CURRENT", FileUtil.readStringFromFile(original));
97     }
98 
99     @Test
testCopy_fileExcluded()100     public void testCopy_fileExcluded() throws Exception {
101         new File(mPreviousDir, "proto").mkdir();
102         mCopier.invocationStarted(mContext);
103         mCopier.invocationEnded(500L);
104         // Nothing was copied
105         assertEquals(0, mCurrentDir.listFiles().length);
106     }
107 }
108