001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static org.junit.Assert.assertTrue;
017
018import java.io.BufferedReader;
019import java.io.FileReader;
020import java.io.IOException;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024public class ResilienceUtil {
025
026    static public void verify(String logfile, String regexp, long totalSteps, double successRatioLowerBound) throws NumberFormatException, IOException {
027        FileReader fr = new FileReader(logfile);
028        BufferedReader br = new BufferedReader(fr);
029        Pattern p = Pattern.compile(regexp);
030        String line;
031
032        int totalLines = 0;
033        int oldNum = -1;
034        int gaps = 0;
035        while ((line = br.readLine()) != null) {
036            Matcher m = p.matcher(line);
037            if (m.matches()) {
038                totalLines++;
039                String g = m.group(1);
040                int num = Integer.parseInt(g);
041                if (oldNum != -1 && num != oldNum + 1) {
042                    gaps++;
043                }
044                oldNum = num;
045            }
046        }
047        fr.close();
048        br.close();
049
050        int lowerLimit = (int) (totalSteps * successRatioLowerBound);
051        assertTrue("totalLines=" + totalLines + " less than " + lowerLimit, totalLines > lowerLimit);
052
053        // we want at least one gap indicating recuperation
054        assertTrue("gaps=" + gaps + " less than 1", gaps >= 1);
055
056    }
057}