View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.BufferedReader;
19  import java.io.FileReader;
20  import java.io.IOException;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  public class ResilienceUtil {
25  
26      static public void verify(String logfile, String regexp, long totalSteps, double successRatioLowerBound) throws NumberFormatException, IOException {
27          FileReader fr = new FileReader(logfile);
28          BufferedReader br = new BufferedReader(fr);
29          Pattern p = Pattern.compile(regexp);
30          String line;
31  
32          int totalLines = 0;
33          int oldNum = -1;
34          int gaps = 0;
35          while ((line = br.readLine()) != null) {
36              Matcher m = p.matcher(line);
37              if (m.matches()) {
38                  totalLines++;
39                  String g = m.group(1);
40                  int num = Integer.parseInt(g);
41                  if (oldNum != -1 && num != oldNum + 1) {
42                      gaps++;
43                  }
44                  oldNum = num;
45              }
46          }
47          fr.close();
48          br.close();
49  
50          int lowerLimit = (int) (totalSteps * successRatioLowerBound);
51          assertTrue("totalLines=" + totalLines + " less than " + lowerLimit, totalLines > lowerLimit);
52  
53          // we want at least one gap indicating recuperation
54          assertTrue("gaps=" + gaps + " less than 1", gaps >= 1);
55  
56      }
57  }