Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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.cts;
     17 
     18 import java.io.BufferedWriter;
     19 import java.io.File;
     20 import java.io.FileWriter;
     21 import java.io.IOException;
     22 
     23 import junit.framework.TestCase;
     24 
     25 /**
     26  * Set up the test environment and offer the utility APIs.
     27  */
     28 public abstract class CtsTestBase extends TestCase {
     29     public static final String APK_SUFFIX = ".apk";
     30     public static final String DESCRITION_SUFFIX = ".xml";
     31 
     32     protected static final String ROOT = "tmp";
     33     protected static final String CONFIG_PATH = ROOT + File.separator + "host_config.xml";
     34 
     35     private static final String CASE_REPOSITORY = "case_rep_demo";
     36     private static final String RESULT_REPOSITORY = "result_rep_demo";
     37     private static final String PLAN_REPOSITORY = "plan_rep_demo";
     38 
     39     /** {@inheritDoc} */
     40     @Override
     41     public void setUp() {
     42         // create root direcoty for the test
     43         new File(ROOT).mkdirs();
     44 
     45         initConfig();
     46         Log.initLog(ROOT);
     47     }
     48 
     49     /** {@inheritDoc} */
     50     @Override
     51     public void tearDown() {
     52         Log.closeLog();
     53         clearDirectory(ROOT);
     54     }
     55 
     56     /**
     57      * Initialize the configuration for tests.
     58      */
     59     private void initConfig() {
     60         StringBuilder buf = new StringBuilder();
     61 
     62         buf.append("<HostConfiguration>");
     63         buf.append("\t<Repository root=\"" + ROOT + "\" >");
     64         buf.append("\t\t<TestPlan path=\"" + PLAN_REPOSITORY + "\" />");
     65         buf.append("\t\t<TestCase path=\"" + CASE_REPOSITORY + "\" />");
     66         buf.append("\t\t<TestResult path=\"" + RESULT_REPOSITORY + "\" />");
     67         buf.append("\t</Repository>");
     68         buf.append("</HostConfiguration>");
     69         try {
     70             new File(ROOT + File.separator + PLAN_REPOSITORY).mkdirs();
     71             new File(ROOT + File.separator + CASE_REPOSITORY).mkdirs();
     72             new File(ROOT + File.separator + RESULT_REPOSITORY).mkdirs();
     73             createFile(buf.toString(), CONFIG_PATH);
     74 
     75         } catch (IOException e1) {
     76             fail("Can't create config file");
     77         }
     78 
     79         try {
     80             TestHost.loadConfig(CONFIG_PATH);
     81         } catch (Exception e) {
     82             e.printStackTrace();
     83             fail("Can't initiate config");
     84         }
     85     }
     86 
     87     /**
     88      * Create test package with the package name and the xml message as the content.
     89      *
     90      * @param xmlMsg The message as the content of the package.
     91      * @param packageName The package name.
     92      */
     93     protected void createTestPackage(String xmlMsg, String packageName) throws IOException {
     94         String caseRoot = HostConfig.getInstance().getCaseRepository()
     95                 .getRoot();
     96 
     97         String apkPath = caseRoot + File.separator + packageName + APK_SUFFIX;
     98         String xmlPath = caseRoot + File.separator + packageName
     99                 + DESCRITION_SUFFIX;
    100 
    101         createFile(null, apkPath);
    102         createFile(xmlMsg, xmlPath);
    103     }
    104 
    105     /**
    106      * Delete the test package.
    107      */
    108     protected void deleteTestPackage(String path) {
    109         String apkPath = path + File.separator + APK_SUFFIX;
    110         String desPath = path + File.separator + DESCRITION_SUFFIX;
    111 
    112         deleteFile(apkPath);
    113         deleteFile(desPath);
    114     }
    115 
    116     /**
    117      * Create the specified file with the specified content.
    118      *
    119      * @param content The content written into the file.
    120      * @param filePath The file to be created.
    121      */
    122     protected void createFile(String content, String filePath)
    123             throws IOException {
    124         BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
    125         if (content != null) {
    126             out.write(content);
    127         }
    128 
    129         out.close();
    130     }
    131 
    132     /**
    133      * Delete the specified file.
    134      *
    135      * @param path The file to be deleted.
    136      */
    137     protected void deleteFile(String path) {
    138         File f = new File(path);
    139 
    140         if (f.exists() && f.canWrite()) {
    141             f.delete();
    142         }
    143     }
    144 
    145     /**
    146      * Clear the directory by deleting the files and directories under it.
    147      *
    148      * @param path The directory to be cleared.
    149      */
    150     private void clearDirectory(String path) {
    151         File root = new File(path);
    152         for (File f : root.listFiles()) {
    153             if (f.isFile()) {
    154                 f.delete();
    155             } else {
    156                 deleteDirectory(f);
    157             }
    158         }
    159 
    160         root.delete();
    161     }
    162 
    163     /**
    164      * Deleted the directory, including the files and sub-directories under it.
    165      *
    166      * @param path The directory to be deleted.
    167      */
    168     private void deleteDirectory(File path) {
    169         if (path.exists()) {
    170             File[] files = path.listFiles();
    171             for (int i = 0; i < files.length; i++) {
    172                 if (files[i].isDirectory()) {
    173                     deleteDirectory(files[i]);
    174                 } else {
    175                     files[i].delete();
    176                 }
    177             }
    178 
    179             path.delete();
    180         }
    181     }
    182 }
    183