Home | History | Annotate | Download | only in util
      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 package com.android.tradefed.util;
     17 import static org.junit.Assert.assertEquals;
     18 import static org.junit.Assert.assertTrue;
     19 
     20 import com.android.tradefed.build.IBuildInfo;
     21 
     22 import org.junit.Test;
     23 import org.junit.runner.RunWith;
     24 import org.junit.runners.JUnit4;
     25 import org.mockito.Mockito;
     26 
     27 import java.io.File;
     28 import java.io.IOException;
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /** Unit tests for {@link SystemUtil} */
     33 @RunWith(JUnit4.class)
     34 public class SystemUtilTest {
     35 
     36     /**
     37      * test {@link SystemUtil#getTestCasesDirs(IBuildInfo)} to make sure it gets directories from
     38      * environment variables.
     39      */
     40     @Test
     41     public void testGetExternalTestCasesDirs() throws IOException {
     42         File targetOutDir = null;
     43         File hostOutDir = null;
     44         try {
     45             targetOutDir = FileUtil.createTempDir("target_out_dir");
     46             hostOutDir = FileUtil.createTempDir("host_out_dir");
     47 
     48             SystemUtil.singleton = Mockito.mock(SystemUtil.class);
     49             Mockito.when(SystemUtil.singleton.getEnv(SystemUtil.ENV_ANDROID_TARGET_OUT_TESTCASES))
     50                     .thenReturn(targetOutDir.getAbsolutePath());
     51             Mockito.when(SystemUtil.singleton.getEnv(SystemUtil.ENV_ANDROID_HOST_OUT_TESTCASES))
     52                     .thenReturn(hostOutDir.getAbsolutePath());
     53 
     54             List<File> testCasesDirs = new ArrayList<File>(SystemUtil.getExternalTestCasesDirs());
     55             assertTrue(testCasesDirs.get(0).equals(targetOutDir));
     56             assertTrue(testCasesDirs.get(1).equals(hostOutDir));
     57         } finally {
     58             FileUtil.recursiveDelete(targetOutDir);
     59             FileUtil.recursiveDelete(hostOutDir);
     60         }
     61     }
     62 
     63     /**
     64      * test {@link SystemUtil#getTestCasesDirs(IBuildInfo)} to make sure no exception thrown if no
     65      * environment variable is set or the directory does not exist.
     66      */
     67     @Test
     68     public void testGetExternalTestCasesDirsNoDir() {
     69         File targetOutDir = new File("/path/not/exist_1");
     70 
     71         SystemUtil.singleton = Mockito.mock(SystemUtil.class);
     72         Mockito.when(SystemUtil.singleton.getEnv(SystemUtil.ENV_ANDROID_TARGET_OUT_TESTCASES))
     73                 .thenReturn(targetOutDir.getAbsolutePath());
     74         Mockito.when(SystemUtil.singleton.getEnv(SystemUtil.ENV_ANDROID_HOST_OUT_TESTCASES))
     75                 .thenReturn(null);
     76         List<File> testCasesDirs = new ArrayList<File>(SystemUtil.getExternalTestCasesDirs());
     77         assertEquals(0, testCasesDirs.size());
     78     }
     79 }
     80