1 /*
2  * Copyright (C) 2016 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.tools.build.apkzlib.zip;
18 
19 import static org.junit.Assert.assertFalse;
20 
21 import com.android.tools.build.apkzlib.utils.ApkZFileTestUtils;
22 import com.google.common.io.Files;
23 import java.io.File;
24 import java.io.IOException;
25 import javax.annotation.Nonnull;
26 import org.junit.rules.TemporaryFolder;
27 
28 /**
29  * Utility method for zip tests.
30  */
31 class ZipTestUtils {
32 
33     /**
34      * Obtains the data of a resource with the given name.
35      *
36      * @param rsrcName the resource name inside packaging resource folder
37      * @return the resource data
38      * @throws IOException I/O failed
39      */
40     @Nonnull
rsrcBytes(@onnull String rsrcName)41     static byte[] rsrcBytes(@Nonnull String rsrcName) throws IOException {
42         return ApkZFileTestUtils.getResourceBytes("/testData/packaging/" + rsrcName).read();
43     }
44 
45     /**
46      * Clones a resource to a temporary folder. Generally, resources do not need to be cloned to
47      * be used. However, in code where there is danger of changing resource files and corrupting
48      * the source directory, cloning should be done before accessing the resources.
49      *
50      * @param rsrcName the resource name
51      * @param folder the temporary folder
52      * @return the file that was created with the resource
53      * @throws IOException failed to clone the resource
54      */
cloneRsrc(@onnull String rsrcName, @Nonnull TemporaryFolder folder)55     static File cloneRsrc(@Nonnull String rsrcName, @Nonnull TemporaryFolder folder)
56             throws IOException {
57         String cloneName;
58         if (rsrcName.contains("/")) {
59             cloneName = rsrcName.substring(rsrcName.lastIndexOf('/') + 1);
60         } else {
61             cloneName = rsrcName;
62         }
63 
64         return cloneRsrc(rsrcName, folder, cloneName);
65     }
66 
67     /**
68      * Clones a resource to a temporary folder. Generally, resources do not need to be cloned to
69      * be used. However, in code where there is danger of changing resource files and corrupting
70      * the source directory, cloning should be done before accessing the resources.
71      *
72      * @param rsrcName the resource name
73      * @param folder the temporary folder
74      * @param cloneName the name of the cloned resource that will be created inside the temporary
75      * folder
76      * @return the file that was created with the resource
77      * @throws IOException failed to clone the resource
78      */
cloneRsrc( @onnull String rsrcName, @Nonnull TemporaryFolder folder, @Nonnull String cloneName)79     static File cloneRsrc(
80             @Nonnull String rsrcName,
81             @Nonnull TemporaryFolder folder,
82             @Nonnull String cloneName)
83             throws IOException {
84         File result = new File(folder.getRoot(), cloneName);
85         assertFalse(result.exists());
86 
87         Files.write(rsrcBytes(rsrcName), result);
88         return result;
89     }
90 }
91