Create proper assertFileContains()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / FileSearchUtil.java
1 /*
2  * Copyright (c) 2020 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
9
10 import java.io.File;
11 import java.io.FileNotFoundException;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Scanner;
15
16 final class FileSearchUtil {
17     private FileSearchUtil() {
18         // Hidden on purpose
19     }
20
21     static void assertFileContains(final File file, final String searchText) throws FileNotFoundException {
22         try (Scanner scanner = new Scanner(file)) {
23             while (scanner.hasNextLine()) {
24                 if (scanner.nextLine().contains(searchText)) {
25                     return;
26                 }
27             }
28         }
29         throw new AssertionError("File " + file + " does not contain '" + searchText + "'");
30     }
31
32     static Map<String, File> getFiles(final File path) {
33         final Map<String, File> ret = new HashMap<>();
34         getFiles(path, ret);
35         return ret;
36     }
37
38     private static void getFiles(final File path, final Map<String, File> files) {
39         final File [] dirFiles = path.listFiles();
40         for (File file : dirFiles) {
41             if (file.isDirectory()) {
42                 getFiles(file, files);
43             }
44
45             files.put(file.getName(), file);
46         }
47     }
48 }