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 io.stallion.services.Log;
021import io.stallion.testing.TestClient;
022import org.apache.commons.lang3.exception.ExceptionUtils;
023
024import java.util.List;
025import java.util.Map;
026import java.util.function.Function;
027
028import static io.stallion.utils.Literals.*;
029import static io.stallion.Context.*;
030
031
032public class JsTestSuite {
033    private List<Map.Entry<String, Function>> tests = list();
034    private TestClient client = new TestClient();
035    private String name = "";
036    private String file = "";
037    private TestResults results;
038
039    public void setUp(Object self) {};
040    public void setUpSuite(Object self) {};
041    public void tearDown(Object self) {};
042    public void tearDownSuite(Object self) {};
043
044
045
046    public void add(String name, Function test) {
047        Log.info("adding javascript test: {0}", name);
048        tests.add(val(name, test));
049    }
050
051    public void run() {
052        results = new TestResults(getName()).setFile(getFile());
053        tearDownSuite(this);
054        for (Map.Entry<String, Function> entry: tests) {
055            Log.info("Run test {0}", entry.getKey());
056            setUp(this);
057
058            try {
059                entry.getValue().apply(this);
060                results.addResult(entry.getKey(), true, false, null);
061            } catch (AssertionError e) {
062                ExceptionUtils.printRootCauseStackTrace(e);
063                results.addResult(entry.getKey(), false, false, e);
064            } catch (Exception e) {
065                ExceptionUtils.printRootCauseStackTrace(e);
066                results.addResult(entry.getKey(), false, true, e);
067            }
068            tearDown(this);
069        }
070    }
071
072    public TestClient getClient() {
073        return client;
074    }
075
076    public String getName() {
077        return name;
078    }
079
080    public JsTestSuite setName(String name) {
081        this.name = name;
082        return this;
083    }
084
085    public TestResults getResults() {
086        return results;
087    }
088
089    public String getFile() {
090        return file;
091    }
092
093    public JsTestSuite setFile(String file) {
094        this.file = file;
095        return this;
096    }
097}