Search in sources :

Example 1 with CSVWriter

use of jp.ossc.nimbus.io.CSVWriter in project nimbus by nimbus-org.

the class CSVTestReporterService method report.

public void report(TestController controller) {
    CSVWriter writer = csvWriter == null ? new CSVWriter() : csvWriter.cloneWriter();
    if (writer.getNullValue() == null) {
        writer.setNullValue("");
    }
    List csv = new ArrayList();
    try {
        BufferedWriter bw = new BufferedWriter(encoding == null ? new FileWriter(outputFile) : new OutputStreamWriter(new FileOutputStream(outputFile), encoding));
        writer.setWriter(bw);
        Set scenarioGroupCategorySet = new TreeSet();
        Set scenarioCategorySet = new TreeSet();
        Set testcaseCategorySet = new TreeSet();
        TestScenarioGroup[] groups = controller.getScenarioGroups();
        for (int i = 0; i < groups.length; i++) {
            TestScenarioGroup.TestScenarioGroupResource sgr = groups[i].getTestScenarioGroupResource();
            if (sgr != null) {
                Map categoryMap = sgr.getCategoryMap();
                if (categoryMap.size() != 0) {
                    scenarioGroupCategorySet.addAll(categoryMap.keySet());
                }
            }
            TestScenario[] scenarios = controller.getScenarios(groups[i].getScenarioGroupId());
            for (int j = 0; j < scenarios.length; j++) {
                TestScenario.TestScenarioResource sr = scenarios[j].getTestScenarioResource();
                if (sr != null) {
                    Map categoryMap = sr.getCategoryMap();
                    if (categoryMap.size() != 0) {
                        scenarioCategorySet.addAll(categoryMap.keySet());
                    }
                }
                TestCase[] testcases = controller.getTestCases(groups[i].getScenarioGroupId(), scenarios[j].getScenarioId());
                for (int k = 0; k < testcases.length; k++) {
                    TestCase.TestCaseResource tr = testcases[k].getTestCaseResource();
                    if (tr != null) {
                        Map categoryMap = tr.getCategoryMap();
                        if (categoryMap.size() != 0) {
                            testcaseCategorySet.addAll(categoryMap.keySet());
                        }
                    }
                }
            }
        }
        csv.add("ScenarioGroupId");
        Iterator categories = scenarioGroupCategorySet.iterator();
        while (categories.hasNext()) {
            String categoryName = (String) categories.next();
            csv.add(categoryName);
        }
        csv.add("Title");
        csv.add("Description");
        csv.add("UserId");
        csv.add("StartTime");
        csv.add("EndTime");
        csv.add("State");
        csv.add("Result");
        csv.add("Exception");
        csv.add("ActionId");
        csv.add("Title");
        csv.add("Description");
        csv.add("ActionType");
        csv.add("Result");
        csv.add("Exception");
        csv.add("ScenarioId");
        categories = scenarioCategorySet.iterator();
        while (categories.hasNext()) {
            String categoryName = (String) categories.next();
            csv.add(categoryName);
        }
        csv.add("Title");
        csv.add("Description");
        csv.add("UserId");
        csv.add("StartTime");
        csv.add("EndTime");
        csv.add("State");
        csv.add("Result");
        csv.add("Exception");
        csv.add("ActionId");
        csv.add("Title");
        csv.add("Description");
        csv.add("ActionType");
        csv.add("Result");
        csv.add("Exception");
        csv.add("TestcaseId");
        categories = testcaseCategorySet.iterator();
        while (categories.hasNext()) {
            String categoryName = (String) categories.next();
            csv.add(categoryName);
        }
        csv.add("Title");
        csv.add("Description");
        csv.add("UserId");
        csv.add("StartTime");
        csv.add("EndTime");
        csv.add("State");
        csv.add("Result");
        csv.add("Exception");
        csv.add("ActionId");
        csv.add("Title");
        csv.add("Description");
        csv.add("ActionType");
        csv.add("Result");
        csv.add("Exception");
        writer.writeCSV(csv);
        for (int i = 0; i < groups.length; i++) {
            TestScenarioGroup.TestScenarioGroupResource sgr = groups[i].getTestScenarioGroupResource();
            csv.clear();
            addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, true);
            addScenarioElements(csv, null, null, scenarioCategorySet, null, null, false);
            addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
            writer.writeCSV(csv);
            if (sgr != null) {
                String[] actionIds = sgr.getBeforeActionIds();
                if (actionIds != null) {
                    for (int j = 0; j < actionIds.length; j++) {
                        csv.clear();
                        addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, actionIds[j], "before", false);
                        addScenarioElements(csv, null, null, scenarioCategorySet, null, null, false);
                        addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                        writer.writeCSV(csv);
                    }
                }
                actionIds = sgr.getFinallyActionIds();
                if (actionIds != null) {
                    for (int j = 0; j < actionIds.length; j++) {
                        csv.clear();
                        addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, actionIds[j], "finally", false);
                        addScenarioElements(csv, null, null, scenarioCategorySet, null, null, false);
                        addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                        writer.writeCSV(csv);
                    }
                }
            }
            TestScenario[] scenarios = controller.getScenarios(groups[i].getScenarioGroupId());
            for (int j = 0; j < scenarios.length; j++) {
                TestScenario.TestScenarioResource sr = scenarios[j].getTestScenarioResource();
                csv.clear();
                addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, true);
                addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                writer.writeCSV(csv);
                if (sr != null) {
                    String[] actionIds = sr.getBeforeActionIds();
                    if (actionIds != null) {
                        for (int k = 0; k < actionIds.length; k++) {
                            csv.clear();
                            addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                            addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, actionIds[k], "before", false);
                            addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                            writer.writeCSV(csv);
                        }
                    }
                    actionIds = sr.getAfterActionIds();
                    if (actionIds != null) {
                        for (int k = 0; k < actionIds.length; k++) {
                            csv.clear();
                            addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                            addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, actionIds[k], "after", false);
                            addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                            writer.writeCSV(csv);
                        }
                    }
                    actionIds = sr.getFinallyActionIds();
                    if (actionIds != null) {
                        for (int k = 0; k < actionIds.length; k++) {
                            csv.clear();
                            addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                            addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, actionIds[k], "finally", false);
                            addTestCaseElements(csv, null, null, testcaseCategorySet, null, null, false);
                            writer.writeCSV(csv);
                        }
                    }
                }
                TestCase[] testcases = controller.getTestCases(groups[i].getScenarioGroupId(), scenarios[j].getScenarioId());
                for (int k = 0; k < testcases.length; k++) {
                    TestCase.TestCaseResource tr = testcases[k].getTestCaseResource();
                    csv.clear();
                    addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                    addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, false);
                    addTestCaseElements(csv, testcases[k], tr, testcaseCategorySet, null, null, true);
                    writer.writeCSV(csv);
                    if (tr != null) {
                        String[] actionIds = tr.getBeforeActionIds();
                        if (actionIds != null) {
                            for (int l = 0; l < actionIds.length; l++) {
                                csv.clear();
                                addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                                addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, false);
                                addTestCaseElements(csv, testcases[k], tr, testcaseCategorySet, actionIds[l], "before", false);
                                writer.writeCSV(csv);
                            }
                        }
                        actionIds = tr.getActionIds();
                        if (actionIds != null) {
                            for (int l = 0; l < actionIds.length; l++) {
                                csv.clear();
                                addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                                addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, false);
                                addTestCaseElements(csv, testcases[k], tr, testcaseCategorySet, actionIds[l], "action", false);
                                writer.writeCSV(csv);
                            }
                        }
                        actionIds = tr.getAfterActionIds();
                        if (actionIds != null) {
                            for (int l = 0; l < actionIds.length; l++) {
                                csv.clear();
                                addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                                addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, false);
                                addTestCaseElements(csv, testcases[k], tr, testcaseCategorySet, actionIds[l], "after", false);
                                writer.writeCSV(csv);
                            }
                        }
                        actionIds = tr.getFinallyActionIds();
                        if (actionIds != null) {
                            for (int l = 0; l < actionIds.length; l++) {
                                csv.clear();
                                addScenarioGroupElements(csv, groups[i], sgr, scenarioGroupCategorySet, null, null, false);
                                addScenarioElements(csv, scenarios[j], sr, scenarioCategorySet, null, null, false);
                                addTestCaseElements(csv, testcases[k], tr, testcaseCategorySet, actionIds[l], "finally", false);
                                writer.writeCSV(csv);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ServiceManagerFactory.getLogger().write("CTR__00001", e);
        return;
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
        }
        try {
            writer.close();
        } catch (IOException e) {
        }
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) TestScenario(jp.ossc.nimbus.service.test.TestScenario) FileWriter(java.io.FileWriter) TestScenarioGroup(jp.ossc.nimbus.service.test.TestScenarioGroup) ArrayList(java.util.ArrayList) CSVWriter(jp.ossc.nimbus.io.CSVWriter) BufferedWriter(java.io.BufferedWriter) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) IOException(java.io.IOException) TestCase(jp.ossc.nimbus.service.test.TestCase) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map)

Example 2 with CSVWriter

use of jp.ossc.nimbus.io.CSVWriter in project nimbus by nimbus-org.

the class CSVCompareEvaluateActionService method execute.

/**
 * 2つのCSVファイルを比較して、内容が等価かどうか評価する。<p>
 * リソースのフォーマットは、以下。<br>
 * <pre>
 * srcFilePath
 * dstFilePath
 * ignoreCSVElements
 * </pre>
 * srcFilePathは、比較元のCSVファイルのパスを指定する。<br>
 * dstFilePathは、比較先のCSVファイルのパスを指定する。ここで指定したファイルが存在しない場合は、比較を行わずにtrueを返す。<br>
 * ignoreCSVElementsは、比較時に無視するCSV要素を要素名または要素インデックスで指定する。要素名を指定する場合は、CSVファイルの一行目にCSV要素名ヘッダ行が必要である。要素インデックスは、0から開始する。複数指定する場合は、カンマ区切りで指定する。<br>
 *
 * @param context コンテキスト
 * @param actionId アクションID
 * @param resource リソース
 * @return 比較結果が等しい場合は、true
 */
public boolean execute(TestContext context, String actionId, Reader resource) throws Exception {
    BufferedReader br = new BufferedReader(resource);
    File srcFile = null;
    File dstFile = null;
    int[] ignoreIndexes = null;
    String[] ignoreNames = null;
    try {
        final String srcFilePath = br.readLine();
        if (srcFilePath == null) {
            throw new Exception("Unexpected EOF on srcFilePath");
        }
        final String dstFilePath = br.readLine();
        if (dstFilePath == null) {
            throw new Exception("Unexpected EOF on dstFilePath");
        }
        srcFile = new File(srcFilePath);
        if (!srcFile.exists()) {
            srcFile = new File(context.getCurrentDirectory(), srcFilePath);
        }
        if (!srcFile.exists()) {
            throw new Exception("File not found. srcFilePath=" + srcFilePath);
        }
        dstFile = new File(dstFilePath);
        if (!dstFile.exists()) {
            dstFile = new File(context.getCurrentDirectory(), dstFilePath);
        }
        if (!dstFile.exists()) {
            return !isResultNGOnNotFoundDestFile;
        }
        String ignoreCSVElementsStr = br.readLine();
        if (ignoreCSVElementsStr != null) {
            final String[] ignoreCSVElements = CSVReader.toArray(ignoreCSVElementsStr, ',', '\\', '"', "null", "#", false, false, true, false);
            if (ignoreCSVElements != null && ignoreCSVElements.length != 0) {
                try {
                    ignoreIndexes = new int[ignoreCSVElements.length];
                    for (int i = 0; i < ignoreCSVElements.length; i++) {
                        ignoreIndexes[i] = Integer.parseInt(ignoreCSVElements[i]);
                    }
                } catch (NumberFormatException e) {
                    ignoreNames = ignoreCSVElements;
                    ignoreIndexes = null;
                }
            }
        }
    } finally {
        br.close();
        br = null;
    }
    InputStreamReader srcisr = null;
    InputStreamReader dstisr = null;
    CSVReader srccsvr = null;
    CSVReader dstcsvr = null;
    OutputStreamWriter srcosw = null;
    OutputStreamWriter dstosw = null;
    CSVWriter srccsvw = null;
    CSVWriter dstcsvw = null;
    boolean result = true;
    try {
        srcisr = fileEncoding == null ? new InputStreamReader(new FileInputStream(srcFile)) : new InputStreamReader(new FileInputStream(srcFile), fileEncoding);
        dstisr = fileEncoding == null ? new InputStreamReader(new FileInputStream(dstFile)) : new InputStreamReader(new FileInputStream(dstFile), fileEncoding);
        srccsvr = csvReader == null ? new CSVReader() : csvReader.cloneReader();
        dstcsvr = csvReader == null ? new CSVReader() : csvReader.cloneReader();
        srccsvr.setReader(srcisr);
        dstcsvr.setReader(dstisr);
        if (isOutputFileAfterEdit) {
            File editSrcFile = new File(srcFile.getPath() + fileAfterEditExtention);
            File editDstFile = new File(dstFile.getPath() + fileAfterEditExtention);
            srcosw = fileEncoding == null ? new OutputStreamWriter(new FileOutputStream(editSrcFile)) : new OutputStreamWriter(new FileOutputStream(editSrcFile), fileEncoding);
            dstosw = fileEncoding == null ? new OutputStreamWriter(new FileOutputStream(editDstFile)) : new OutputStreamWriter(new FileOutputStream(editDstFile), fileEncoding);
            srccsvw = csvWriter == null ? new CSVWriter() : csvWriter.cloneWriter();
            dstcsvw = csvWriter == null ? new CSVWriter() : csvWriter.cloneWriter();
            srccsvw.setWriter(srcosw);
            dstcsvw.setWriter(dstosw);
        }
        List srccsv = new ArrayList();
        List dstcsv = new ArrayList();
        boolean[] ignores = null;
        if (ignoreNames != null && ignoreNames.length != 0) {
            Set ignoreNameSet = new HashSet();
            for (int i = 0; i < ignoreNames.length; i++) {
                ignoreNameSet.add(ignoreNames[i]);
            }
            srccsv = srccsvr.readCSVLineList(srccsv);
            if (srccsv != null && srccsv.size() != 0) {
                ignores = new boolean[srccsv.size()];
                for (int i = 0; i < srccsv.size(); i++) {
                    ignores[i] = ignoreNameSet.contains(srccsv.get(i));
                }
            }
            if (isOutputFileAfterEdit) {
                writeCSV(srccsvw, srccsv, ignores);
            }
            dstcsv = dstcsvr.readCSVLineList(dstcsv);
            if (isOutputFileAfterEdit) {
                writeCSV(dstcsvw, dstcsv, ignores);
            }
            if (!compareCSVList(srccsv, dstcsv, ignores)) {
                return false;
            }
        } else if (ignoreIndexes != null && ignoreIndexes.length != 0) {
            srccsv = srccsvr.readCSVLineList(srccsv);
            if (srccsv != null && srccsv.size() != 0) {
                ignores = new boolean[srccsv.size()];
                for (int i = 0, imax = ignoreIndexes.length; i < imax; i++) {
                    if (ignores.length > ignoreIndexes[i]) {
                        ignores[ignoreIndexes[i]] = true;
                    }
                }
            }
            if (isOutputFileAfterEdit) {
                writeCSV(srccsvw, srccsv, ignores);
            }
            dstcsv = dstcsvr.readCSVLineList(dstcsv);
            if (isOutputFileAfterEdit) {
                writeCSV(dstcsvw, dstcsv, ignores);
            }
            if (!compareCSVList(srccsv, dstcsv, ignores)) {
                return false;
            }
        }
        do {
            if (srccsv != null) {
                srccsv = srccsvr.readCSVLineList(srccsv);
            }
            if (dstcsv != null) {
                dstcsv = dstcsvr.readCSVLineList(dstcsv);
            }
            if (isOutputFileAfterEdit) {
                writeCSV(srccsvw, srccsv, ignores);
                writeCSV(dstcsvw, dstcsv, ignores);
            }
            result &= compareCSVList(srccsv, dstcsv, ignores);
        } while (isOutputFileAfterEdit ? (srccsv != null || dstcsv != null) : result);
    } finally {
        if (srcisr != null) {
            try {
                srcisr.close();
            } catch (IOException e) {
            }
        }
        if (dstisr != null) {
            try {
                dstisr.close();
            } catch (IOException e) {
            }
        }
        if (srccsvr != null) {
            try {
                srccsvr.close();
            } catch (IOException e) {
            }
        }
        if (dstcsvr != null) {
            try {
                dstcsvr.close();
            } catch (IOException e) {
            }
        }
        if (isOutputFileAfterEdit) {
            srccsvw.flush();
            dstcsvw.flush();
        }
        if (srcosw != null) {
            try {
                srcosw.close();
            } catch (IOException e) {
            }
        }
        if (dstosw != null) {
            try {
                dstosw.close();
            } catch (IOException e) {
            }
        }
        if (srccsvw != null) {
            try {
                srccsvw.close();
            } catch (IOException e) {
            }
        }
        if (dstcsvw != null) {
            try {
                dstcsvw.close();
            } catch (IOException e) {
            }
        }
    }
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InputStreamReader(java.io.InputStreamReader) CSVReader(jp.ossc.nimbus.io.CSVReader) ArrayList(java.util.ArrayList) CSVWriter(jp.ossc.nimbus.io.CSVWriter) IOException(java.io.IOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 CSVWriter (jp.ossc.nimbus.io.CSVWriter)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 CSVReader (jp.ossc.nimbus.io.CSVReader)1 TestCase (jp.ossc.nimbus.service.test.TestCase)1 TestScenario (jp.ossc.nimbus.service.test.TestScenario)1