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