1 /*
2  * Copyright (C) 2012 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.tradefed.util;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Map;
22 
23 /**
24  * A testing fixture that creates a fake unzipped tests folder based on a list of content.
25  *
26  * The folder structure is configured based on a list of file names or folder names, as provided
27  * to the constructor. {@link FakeTestsZipFolder#cleanUp()} should be called after the folder is
28  * no longer needed.
29  *
30  */
31 public class FakeTestsZipFolder {
32 
33     public enum ItemType {
34         FILE, DIRECTORY,
35     }
36 
37     private Map<String, ItemType> mItems;
38     private File mBase;
39     private File mData;
40 
41     /**
42      * Create a fake unzipped tests folder backed by empty files
43      *
44      * @param items list of items to include in the fake unzipped folder. key of
45      *            the map shall be the relative path of the item, value of the
46      *            entry shall indicate if the entry should be backed by an empty
47      *            file or a folder
48      */
FakeTestsZipFolder(Map<String, ItemType> items)49     public FakeTestsZipFolder(Map<String, ItemType> items) {
50         mItems = items;
51     }
52 
53     /**
54      * Create fake unzipped tests folder as indicated by the manifest of items
55      *
56      * @return false if failed to create any item
57      * @throws IOException
58      */
createItems()59     public boolean createItems() throws IOException {
60         mBase = File.createTempFile("tf_", "_test_zip");
61         mBase.delete();
62         mData = new File(mBase, "DATA");
63         if (!mData.mkdirs()) {
64             return false;
65         }
66         boolean failed = false;
67         for (String fileName : mItems.keySet()) {
68             File file = new File(mData, fileName);
69             ItemType type = mItems.get(fileName);
70             if (ItemType.DIRECTORY.equals(type)) {
71                 if (!file.mkdirs()) {
72                     failed = true;
73                     break;
74                 }
75             } else {
76                 File p = file.getParentFile();
77                 if (!p.exists()) {
78                     if (!p.mkdirs()) {
79                         failed = true;
80                         break;
81                     }
82                 }
83                 if (!file.createNewFile()) {
84                     failed = true;
85                     break;
86                 }
87             }
88         }
89         if (failed) {
90             // attempt to clean up
91             cleanUp();
92         }
93         return !failed;
94     }
95 
96     /**
97      * Delete the entire fake unzipped test folder
98      */
cleanUp()99     public void cleanUp() {
100         FileUtil.recursiveDelete(mBase);
101     }
102 
getDataFolder()103     protected File getDataFolder() {
104         return mData;
105     }
106 
107     /**
108      * Returns the base of the fake unzipped folder This would be a replacement
109      * of root folder where a real tests zip is expanded
110      */
getBasePath()111     public File getBasePath() {
112         return mBase;
113     }
114 }
115