<commit_before>/*
 * Copyright (C) 2010-2014 <NAME> and the contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aludratest.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import org.aludratest.exception.AludraTestException;
import org.aludratest.impl.log4testing.AttachParameter;
import org.aludratest.impl.log4testing.AttachResult;
import org.aludratest.service.Action;
import org.aludratest.service.AludraService;
import org.aludratest.service.ComponentId;
import org.aludratest.service.SystemConnector;
import org.aludratest.testcase.AludraTestContext;
import org.aludratest.testcase.TestStatus;
import org.aludratest.testcase.event.ErrorReport;
import org.aludratest.testcase.event.SystemErrorReporter;
import org.aludratest.testcase.event.attachment.Attachment;
import org.aludratest.testcase.event.impl.TestStepInfoBean;
import org.aludratest.util.retry.AutoRetry;
import org.databene.commons.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wraps an object and intercepts method invocations:
 * By default, the calls are forwarded to the wrapped object,
 * but when an exception occurred, the {@link ControlFlowHandler}
 * will ignore further calls if the property 'stopOnException'
 * is 'true'.
 * @author <NAME>
 */
public class ControlFlowHandler implements InvocationHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ControlFlowHandler.class);

    /** The wrapped object to forward invocations to. */
    private Object target;

    private ComponentId<? extends AludraService> serviceId;

    /** The current {@link SystemConnector}. */
    private SystemConnector systemConnector;

    private Throwable exceptionUnderErrorChecking;

    private TestStepInfoBean stepUnderErrorChecking;

    private AludraTestContext testContext;

    /** If set to true, the instance will ignore further invocations after
     *  the occurrence of an exception. */
    private boolean stopOnException;

    private boolean logTestSteps;

    /** Constructor which takes the initialization values for all attributes.
     * @param target the target object to forward calls to
     * @param serviceId the {@link ComponentId} related to the target object
     * @param systemConnector an optional {@link SystemConnector} to to provide SUT information
     * @param testContext the test context to log to
     * @param stopOnException a flag that indicates whether to stop on exceptions
     * @param logTestSteps If <code>true</code>, method invocations will be fired to the test context as new test step. */
    public ControlFlowHandler(Object target, ComponentId<? extends AludraService> serviceId, SystemConnector systemConnector,
            AludraTestContext testContext, boolean stopOnException, boolean logTestSteps) {
        // check preconditions
        Assert.notNull(target, "target");
        Assert.notNull(testContext, "testContext");

        // assign values
        this.target = target;
        this.serviceId = serviceId;
        this.systemConnector = systemConnector;
        this.testContext = testContext;
        this.stopOnException = stopOnException;
        this.logTestSteps = logTestSteps;
    }


    // InvocationHandler interface implementation ------------------------------

    /**
     * Handles the actual invocation. First it checks if calls shall be ignored.
     * If yes, then an according log entry is written, otherwise the method
     * {@link #forwardAndHandleException(Method, Object[])} is called to call the wrapped object.
     * @see InvocationHandler#invoke(Object, Method, Object[])
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //NOSONAR
        if (shallContinueTestCaseExecution()) {
            if (method.getName().equals("setSystemConnector")) {
                this.systemConnector = (SystemConnector) args[0];
                return null;
            }
            else {
                return forwardAndHandleException(method, args);
            }
        } else {
            if (logTestSteps) {
                TestStepInfoBean testStep = new TestStepInfoBean();
                testStep.setServiceId(serviceId);
                testStep.setCommandNameAndArguments(method, args);
                testStep.setTestStatus(TestStatus.IGNORED);
                testContext.fireTestStep(testStep);
            }<commit_msg>Better handling for setSystemConnector()<commit_after>/*
 * Copyright (C) 2010-2014 <NAME> and the contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aludratest.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import org.aludratest.exception.AludraTestException;
import org.aludratest.impl.log4testing.AttachParameter;
import org.aludratest.impl.log4testing.AttachResult;
import org.aludratest.service.Action;
import org.aludratest.service.AludraService;
import org.aludratest.service.ComponentId;
import org.aludratest.service.SystemConnector;
import org.aludratest.testcase.AludraTestContext;
import org.aludratest.testcase.TestStatus;
import org.aludratest.testcase.event.ErrorReport;
import org.aludratest.testcase.event.SystemErrorReporter;
import org.aludratest.testcase.event.attachment.Attachment;
import org.aludratest.testcase.event.impl.TestStepInfoBean;
import org.aludratest.util.retry.AutoRetry;
import org.databene.commons.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wraps an object and intercepts method invocations:
 * By default, the calls are forwarded to the wrapped object,
 * but when an exception occurred, the {@link ControlFlowHandler}
 * will ignore further calls if the property 'stopOnException'
 * is 'true'.
 * @author <NAME>
 */
public class ControlFlowHandler implements InvocationHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ControlFlowHandler.class);

    /** The wrapped object to forward invocations to. */
    private Object target;

    private ComponentId<? extends AludraService> serviceId;

    /** The current {@link SystemConnector}. */
    private SystemConnector systemConnector;

    private Throwable exceptionUnderErrorChecking;

    private TestStepInfoBean stepUnderErrorChecking;

    private AludraTestContext testContext;

    /** If set to true, the instance will ignore further invocations after
     *  the occurrence of an exception. */
    private boolean stopOnException;

    private boolean logTestSteps;

    /** Constructor which takes the initialization values for all attributes.
     * @param target the target object to forward calls to
     * @param serviceId the {@link ComponentId} related to the target object
     * @param systemConnector an optional {@link SystemConnector} to to provide SUT information
     * @param testContext the test context to log to
     * @param stopOnException a flag that indicates whether to stop on exceptions
     * @param logTestSteps If <code>true</code>, method invocations will be fired to the test context as new test step. */
    public ControlFlowHandler(Object target, ComponentId<? extends AludraService> serviceId, SystemConnector systemConnector,
            AludraTestContext testContext, boolean stopOnException, boolean logTestSteps) {
        // check preconditions
        Assert.notNull(target, "target");
        Assert.notNull(testContext, "testContext");

        // assign values
        this.target = target;
        this.serviceId = serviceId;
        this.systemConnector = systemConnector;
        this.testContext = testContext;
        this.stopOnException = stopOnException;
        this.logTestSteps = logTestSteps;
    }


    // InvocationHandler interface implementation ------------------------------

    /**
     * Handles the actual invocation. First it checks if calls shall be ignored.
     * If yes, then an according log entry is written, otherwise the method
     * {@link #forwardAndHandleException(Method, Object[])} is called to call the wrapped object.
     * @see InvocationHandler#invoke(Object, Method, Object[])
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //NOSONAR
        if (shallContinueTestCaseExecution()) {
            if (method.getName().equals("setSystemConnector")) {
                this.systemConnector = (SystemConnector) args[0];
                boolean oldLogFlag = logTestSteps;
                logTestSteps = false;
                try {
                    return forwardAndHandleException(method, args);
                }
                finally {
                    logTestSteps = oldLogFlag;
                }
            }
            else {
                return forwardAndHandleException(method, args);
            }
        } else {
            if (logTestSteps) {
                TestStepInfoBean testStep = new TestStepInfoBean();
                testStep.setServiceId(serviceId);
                testStep.setCommandNameAndArguments(method, args);
                testStep.setTestStatus(TestStatus.IGNORED);
                testContext.fireTestStep(testStep);
            }
{'fraction_non_alphanumeric': 0.08288201160541586, 'fraction_numerical': 0.0032882011605415862, 'mean_word_length': 3.2890916632102862, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5997689', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2642, 'n_words': 851}