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 Iterator it = lines.iterator();
49
50 while (it.hasNext()) {
51 String line = verifier.stripAnsi((String) it.next());
52 boolean matches = true;
53 Iterator<String> toMatchIterator = Arrays.stream(texts).iterator();
54 while (matches && toMatchIterator.hasNext()) {
55 matches = line.contains(toMatchIterator.next());
56 }
57 if (matches) {
58 return line;
59 }
60 }
61
62 return null;
63 }
64
65 /**
66 * Find lines matching all the strings given as parameter in the log file attached to a verifier
67 * @param verifier the maven verifier instance
68 * @param texts all the matching strings to find
69 * @return a list of matching strings
70 * @throws VerificationException
71 */
72 public static List<String> findLinesContainingTextsInLogs(final Verifier verifier, final String... texts)
73 throws VerificationException {
74 List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
75 return lines.stream()
76 .map(s -> verifier.stripAnsi(s))
77 .filter(s -> Arrays.stream(texts).allMatch(text -> s.contains(text)))
78 .collect(Collectors.toList());
79 }
80 }