view src/org/thermostat/qa2/framework/utils/CommonUtilities.java @ 184:40990362e151

Improved reporter - for compatibility report thermostat versions are now diplayed in heading and title of pages. - fixed bug in replacePatterns method in CommonUtilities class, when there is more then one pattern on single line.
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 19:09:02 +0200
parents e37e710e45df
children 5d335809cd51
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.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

    public static boolean findInLineList(List<String> lines, String pattern, boolean exact) {
        for (String line : lines) {
            if (exact) {
                if (line.equals(pattern)) {
                    return true;
                }
            } else {
                if (line.contains(pattern)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static List<String> addLinesToList(List<String> list, BufferedReader br) throws IOException {
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        return list;
    }

    public static void sleep(long milis) {
        try {
            Thread.sleep(milis);
        } catch (InterruptedException ex) {
        }
    }

    public static void printHeading(String text) {
        System.out.println("INFO: ------------------------------------------------------------");
        System.out.println("INFO: " + text);
        System.out.println("INFO: ------------------------------------------------------------");
    }

    public static List getLineList(BufferedReader br) throws IOException {
        ArrayList<String> list = new ArrayList();
        return CommonUtilities.addLinesToList(list, br);
    }

    public static void replacePatterns(List<String> template, List<String> output, Map<String, List<String>> patterns) {
        int patternsCount = patterns.size();
        Map.Entry<String, List<String>>[] entries = new Map.Entry[patternsCount];
        patterns.entrySet().toArray(entries);
        for (String line : template) {
            String restOfLine = line;
            String toPrint = "";
            Map.Entry<String, List<String>> entryFound;
            do {
                entryFound = null;
                int index = -1;
                for (int i = 0; i < patternsCount; ++i) {
                    Map.Entry<String, List<String>> entry = entries[i];
                    String pattern = entry.getKey();
                    int foundIndex = restOfLine.indexOf(pattern);
                    if (foundIndex >= 0 && (entryFound == null || foundIndex < index)) {
                        index = foundIndex;
                        entryFound = entry;
                    }
                }
                if (entryFound != null) {
                    String pattern = entryFound.getKey();
                    List<String> replacement = entryFound.getValue();
                    int replacementSize = replacement.size();
                    toPrint += restOfLine.substring(0, index);
                    for (int j = 0; j < replacementSize - 1; ++j) {
                        String replacementLine = replacement.get(j);
                        output.add(toPrint += replacementLine);
                        toPrint = "";
                    }
                    String lastReplacementLine = replacementSize > 0 ? replacement.get(replacementSize - 1) : "";
                    toPrint += lastReplacementLine;
                    restOfLine = restOfLine.substring(index + pattern.length());
                }
            } while (entryFound != null);
            output.add(toPrint + restOfLine);
        }
    }

    public static void addReplacementToMap(Map<String, List<String>> map, String pattern, String replacement) {
        List<String> replacementText = new ArrayList();
        replacementText.add(replacement);
        map.put(pattern, replacementText);
    }

}