001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.plugins.javascript;
019
020import java.io.PrintStream;
021import java.util.List;
022
023import static io.stallion.utils.Literals.*;
024import static io.stallion.Context.*;
025
026
027public class TestResults {
028    private String file = "";
029    private String name = "";
030    private List<TestResult> results = list();
031    private int erroredCount = 0;
032    private int succeededCount = 0;
033    private int failedCount = 0;
034
035    public TestResults(String name) {
036        this.name = name;
037    }
038
039    public boolean hasAnyErrors() {
040        return (erroredCount > 0) || (failedCount > 0);
041    }
042
043    public void printResults() {
044        PrintStream err = System.err;
045        err.println("\n\n\n---------------------------\n");
046        err.println("Test results for file: '" + file + "' suite: '" + name + "'");
047        err.printf("%s succeeded, %s errored, %s failed\n", succeededCount, erroredCount, failedCount);
048        for (TestResult result: results) {
049            if (result.errored) {
050                err.printf("Exception: %s\t\tReason: %s\n", result.getName(), result.getMessage());
051            } else if (!result.isSucceeded()) {
052                err.printf("Failed:    %s\t\tReason: %s\n", result.getName(), result.getMessage());
053            } else {
054                err.printf("Succeeded: %s\n", result.getName());
055            }
056        }
057        err.println("\n---------------------------\n\n\n");
058    }
059
060    public TestResults addResult(String name, boolean succeeded, boolean errored, Throwable e) {
061        String msg = "";
062        if (e != null) {
063            msg = e.getClass().getSimpleName() + ": " + e.getMessage();
064        }
065        results.add(
066                new TestResult()
067                        .setName(name)
068                        .setSucceeded(succeeded)
069                        .setErrored(errored)
070                        .setMessage(msg)
071        );
072        if (succeeded) {
073            succeededCount++;
074        } else if (errored) {
075            erroredCount++;
076        } else {
077            failedCount++;
078        }
079        return this;
080    }
081
082
083    public List<TestResult> getResults() {
084        return results;
085    }
086
087    public int getErroredCount() {
088        return erroredCount;
089    }
090
091    public int getSucceededCount() {
092        return succeededCount;
093    }
094
095    public int getFailedCount() {
096        return failedCount;
097    }
098
099    public String getFile() {
100        return file;
101    }
102
103    public TestResults setFile(String file) {
104        this.file = file;
105        return this;
106    }
107
108    public String getName() {
109        return name;
110    }
111
112    public TestResults setName(String name) {
113        this.name = name;
114        return this;
115    }
116
117    public static class TestResult {
118        private String name = "";
119        private boolean succeeded = true;
120        private boolean errored = false;
121        private String message = "";
122
123        public String getName() {
124            return name;
125        }
126
127        public TestResult setName(String name) {
128            this.name = name;
129            return this;
130        }
131
132        public boolean isSucceeded() {
133            return succeeded;
134        }
135
136        public TestResult setSucceeded(boolean succeeded) {
137            this.succeeded = succeeded;
138            return this;
139        }
140
141        public boolean isErrored() {
142            return errored;
143        }
144
145        public TestResult setErrored(boolean errored) {
146            this.errored = errored;
147            return this;
148        }
149
150        public String getMessage() {
151            return message;
152        }
153
154        public TestResult setMessage(String message) {
155            this.message = message;
156            return this;
157        }
158    }
159}