view src/org/thermostat/qa2/framework/Assert.java @ 185:3ec1dcfecb16

Adds new test class ShellCommandsTest, which tests commands in thermostat shell.
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 19:12:02 +0200
parents 0d34a379de82
children b4fc34288ffd
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.framework;

import java.util.List;
import org.thermostat.qa2.framework.services.Service;
import org.thermostat.qa2.framework.utils.CommonUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public class Assert {

    static final int runTimeout = 5;
    static final int failTimeout = 10;

    public static void assertFailsToStartAndRun(Service service, String message) {
        Assert.assertFailsToStartAndRun(service, failTimeout, message);
    }

    public static void assertFailsToStartAndRun(Service service, long timeoutInSec, String message) {
        try {
            service.start();
        } catch (Exception e) {
            return;
        }
        boolean error = false;
        for (int i = 0; i < timeoutInSec; ++i) {
            if (!service.isRunning()) {
                return;
            }
            CommonUtilities.sleep(1000);
        }
        throw new AssertionError(message);
    }

    public static void assertRuns(Service service, String message) throws Exception {
        assertRuns(service, runTimeout, message);
    }

    public static void assertRuns(Service service, long timeoutInSec, String message) throws Exception {
        for (int i = 0; i < timeoutInSec; ++i) {
            if (!service.isRunning()) {
                throw new AssertionError(message);
            }
            CommonUtilities.sleep(1000);
        }
    }

    public static void assertContainsPattern(List<String> lines, String patterns) {
        assertContainsPatterns(lines, new String[]{patterns});
    }

    public static void assertContainsPatterns(List<String> lines, String[] patterns) {
        for (String pattern : patterns) {
            boolean b = CommonUtilities.findInLineList(lines, pattern, false);
            if (!b) {
                throw new AssertionError("Pattern not found: " + pattern);
            }
        }
    }

    public static void assertNotContainsPattern(List<String> lines, String patterns) {
        assertNotContainsPatterns(lines, new String[]{patterns});
    }

    public static void assertNotContainsPatterns(List<String> lines, String[] patterns) {
        for (String pattern : patterns) {
            boolean b = CommonUtilities.findInLineList(lines, pattern, false);
            if (b) {
                throw new AssertionError("Unwanted pattern found: " + pattern);
            }
        }
    }

    public static void assertEquals(List<String> lines1, List<String> lines2) {
        if (lines1.size() != lines2.size()) {
            throw new AssertionError("Line lists are not equal in length");
        }
        for (int i = 0; i < lines1.size(); ++i) {
            String line1 = lines1.get(i);
            String line2 = lines2.get(i);
            if (!line1.equals(line2)) {
                throw new AssertionError("Lines are not equal: " + line1 + " != " + line2);
            }
        }
    }

    public static void assertEquals(Object o1, Object o2, String message) {
        assertTrue(o1.equals(o2), message);
    }

    public static void assertTrue(boolean value, String message) {
        if (!value) {
            throw new AssertionError(message);
        }
    }

    public static void assertFalse(boolean value, String message) {
        if (value) {
            throw new AssertionError(message);
        }
    }

    public static void assertNull(Object object, String message) {
        assertTrue(object == null, message);
    }

    public static void assertNotNull(Object object, String message) {
        assertTrue(object != null, message);
    }

}