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 
17 package com.android.libcore.timezone.testing;
18 
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.charset.StandardCharsets;
23 import java.nio.file.FileVisitResult;
24 import java.nio.file.FileVisitor;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.SimpleFileVisitor;
28 import java.nio.file.attribute.BasicFileAttributes;
29 import java.util.Arrays;
30 
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertTrue;
33 
34 /**
35  * Arbitrary static utility methods to help with testing.
36  */
37 public final class TestUtils {
38 
TestUtils()39     private TestUtils() {}
40 
createFile(Path dir, String... lines)41     public static String createFile(Path dir, String... lines) throws IOException {
42         Path tempFile = Files.createTempFile(dir, "tmp", null /* suffix */);
43         Files.write(tempFile, Arrays.asList(lines), StandardCharsets.US_ASCII);
44         return tempFile.toString();
45     }
46 
deleteDir(Path tempDir)47     public static void deleteDir(Path tempDir) throws IOException {
48         FileVisitor<? super Path> deleter = new SimpleFileVisitor<Path>() {
49             @Override
50             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
51                     throws IOException {
52                 return delete(file);
53             }
54 
55             @Override
56             public FileVisitResult postVisitDirectory(Path dir, IOException exc)
57                     throws IOException {
58                 return delete(dir);
59             }
60 
61             private FileVisitResult delete(Path file) throws IOException {
62                 Files.delete(file);
63                 return FileVisitResult.CONTINUE;
64             }
65         };
66         Files.walkFileTree(tempDir, deleter);
67     }
68 
assertAbsent(String s, String... absents)69     public static void assertAbsent(String s, String... absents) {
70         for (String absent : absents) {
71             assertFalse(s + " must not contain " + absent, s.contains(absent));
72         }
73     }
74 
assertContains(String s, String... expecteds)75     public static void assertContains(String s, String... expecteds) {
76         for (String expected : expecteds) {
77             assertTrue(s + " must contain " + expected, s.contains(expected));
78         }
79     }
80 
readFully(InputStream is)81     public static byte[] readFully(InputStream is) throws IOException {
82         int read;
83         byte[] buffer = new byte[1024];
84         ByteArrayOutputStream baos = new ByteArrayOutputStream();
85         while ((read = is.read(buffer)) != -1) {
86             baos.write(buffer, 0, read);
87         }
88 
89         return baos.toByteArray();
90     }
91 }
92