1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.buildcache.util;
20
21 import java.util.Arrays;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import org.apache.maven.it.VerificationException;
27 import org.apache.maven.it.Verifier;
28
29 /**
30 * Utils to inspect the generated log file
31 */
32 public final class LogFileUtils {
33
34 private LogFileUtils() {
35 // Nothing to do
36 }
37
38 /**
39 * Find the first line matching all the strings given as parameter in the log file attached to a verifier
40 * @param verifier the maven verifier instance
41 * @param texts all the matching strings to find
42 * @return the first matching string or null
43 * @throws VerificationException
44 */
45 public static String findFirstLineContainingTextsInLogs(final Verifier verifier, final String... texts)
46 throws VerificationException {
47 List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
48
49 for (String s : lines) {
50 String line = Verifier.stripAnsi(s);
51 boolean matches = true;
52 Iterator<String> toMatchIterator = Arrays.stream(texts).iterator();
53 while (matches && toMatchIterator.hasNext()) {
54 matches = line.contains(toMatchIterator.next());
55 }
56 if (matches) {
57 return line;
58 }
59 }
60
61 return null;
62 }
63
64 /**
65 * Find lines matching all the strings given as parameter in the log file attached to a verifier
66 * @param verifier the maven verifier instance
67 * @param texts all the matching strings to find
68 * @return a list of matching strings
69 * @throws VerificationException
70 */
71 public static List<String> findLinesContainingTextsInLogs(final Verifier verifier, final String... texts)
72 throws VerificationException {
73 List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
74 return lines.stream()
75 .map(s -> Verifier.stripAnsi(s))
76 .filter(s -> Arrays.stream(texts).allMatch(s::contains))
77 .collect(Collectors.toList());
78 }
79 }