/*******************************************************************************
 * Copyright (c) 2012 - 2018 Signal Iduna Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Signal Iduna Corporation - initial API and implementation
 * akquinet AG
 * itemis AG
 *******************************************************************************/

package org.testeditor.fixture.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.testeditor.fixture.core.TestRunReporter.SemanticUnit;
import org.testeditor.fixture.core.TestRunReporter.Status;

/**
 * Class from which all generated unit tests are (transitively) derived
 * 
 * Every generated unit test class inherits (transitively in case of configs)
 * from this abstract test class! Every generated class makes use of the
 * interface of this class:
 * 
 * <pre>
 * - calls to the reporter are generated (to report current test status)
 * - newVarId s are generated to hold call tree ids used for enter/leave 
 *   reporting 
 * - initializsation/cleanup of junit 
 * - finishTestWith to inform the test of the final status
 * </pre>
 */
public class AbstractTestCase {
    // reporter for current test status, register with this reported for addition listeners
    protected final TestRunReporter reporter; 
    // a number that is used to generate variables, holding call tree ids (used for enter/leave reporting)
    private long runningNumber; 
    // (probably an) assertion error if test is not finished as expected
    private Status finalStatus = Status.ERROR; 

    /**
     * default ctor
     */
    public AbstractTestCase() {
        // initialization is done in ctor to allow other ctors to access reporter
        // to allow registration before the first event is reported (ENTER TEST)
        reporter = createTestRunReporter();
        List<TestRunListener> listeners = additionalListeners();
        if (listeners != null) {
            additionalListeners().stream().forEach(listener -> {
                reporter.addListener(listener);
            });
        }
        runningNumber = 0;
    }

    protected String newVarId() {
        runningNumber++;
        return "ID" + Long.toString(runningNumber);
    }

    @Before
    public void initTestLaunch() {
        reporter.enter(SemanticUnit.TEST, getClass().getName(), "IDROOT", Status.STARTED, null);
    }

    @After
    public void finishtestLaunch() {
        reporter.leave(SemanticUnit.TEST, getClass().getName(), "IDROOT", this.finalStatus, null);
    }

    /**
     * Call this method with the real test result status, before finishing the
     * execute test method!
     * 
     * @param status
     */
    protected void finishedTestWith(Status status) {
        this.finalStatus = status;
    }

    // may be overridden to provide alternate implementations of the test run
    // reporter
    protected TestRunReporter createTestRunReporter() {
        return new DefaultTestRunReporter();
    }

    // may be override to add aditional test run listeners to the reporter
    protected List<TestRunListener> additionalListeners() {
        return Collections.emptyList();
    }

    /**
     * utility builder function to pass varable-names and values as hashmap
     * 
     * @param strings make sure there is an even number of parameters used, since
     *            only the first even numbered parameters are packed into the map
     * @return
     */
    public static Map<String, String> variables(String... strings) {
        HashMap<String, String> result = new HashMap<>();

        for (int i = 0; i < strings.length / 2; i++) {
            result.put(strings[i * 2], strings[i * 2 + 1]);
        }

        return result;
    }

}

@@ -970,17 +970,16 @@
 rived%0A *
- 
 %0A * Ever
@@ -1152,17 +1152,16 @@
 lass:%0A *
- 
 %0A * %3Cpre
@@ -1307,17 +1307,16 @@
 er/leave
- 
 %0A *   re
@@ -1322,17 +1322,16 @@
 eporting
- 
 %0A * - in
@@ -1360,17 +1360,16 @@
 of junit
- 
 %0A * - fi
@@ -1603,17 +1603,16 @@
 eporter;
- 
 %0A    // 
@@ -1742,17 +1742,16 @@
 gNumber;
- 
 %0A    // 
@@ -1862,17 +1862,16 @@
 s.ERROR;
- 
 %0A%0A    /*
@@ -1887,16 +1887,23 @@
 default 
+constru
 ctor%0A   
@@ -2957,25 +2957,24 @@
 thod!%0A     *
- 
 %0A     * @par
@@ -3560,17 +3560,16 @@
 p%0A     *
- 
 %0A     * 

fix ctor to constructor