<commit_before>/**
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual 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.jboss.arquillian.warp.impl.client.eventbus;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext;
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.container.test.impl.execution.RemoteTestExecuter;
import org.jboss.arquillian.container.test.impl.execution.event.LocalExecutionEvent;
import org.jboss.arquillian.container.test.spi.command.Command;
import org.jboss.arquillian.container.test.spi.command.CommandCallback;
import org.jboss.arquillian.core.api.Event;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.spi.context.ApplicationContext;
import org.jboss.arquillian.test.spi.TestMethodExecutor;
import org.jboss.arquillian.test.spi.context.ClassContext;
import org.jboss.arquillian.test.spi.context.SuiteContext;
import org.jboss.arquillian.test.spi.context.TestContext;
import org.jboss.arquillian.test.spi.event.suite.After;
import org.jboss.arquillian.warp.impl.server.command.WarpEventBusServlet;

/**
 * <p>
 * Provides an event bus during test execution to listen for incoming
 * {@link Command} events.
 * </p>
 * <p>
 * Event Bus functionality is similar to ServletProtocol
 * </p>
 *
 * @author <NAME>
 */
public class WarpEventBus {
    @Inject
    private Instance<ProtocolMetaData> protocolMetadata;

    @Inject
    private Event<Object> remoteEvent;

    @Inject
    private Instance<ApplicationContext> applicationContextInst;

    @Inject
    private Instance<SuiteContext> suiteContextInst;

    @Inject
    private Instance<ClassContext> classContextInst;

    @Inject
    private Instance<TestContext> testContextInst;

    private static Timer eventBusTimer;

    /**
     * Starts the Event Bus as soon as a {@link LocalExecutionEvent} is fired.
     *
     * @param event
     *            the {@link LocalExecutionEvent} that triggered this method
     *            execution
     * @throws Exception
     *
     * @see RemoteTestExecuter
     * @see ServletMethodExecutor
     */
    void startEventBus(@Observes(precedence = 100) LocalExecutionEvent event)
            throws Exception {
        // Calculate eventUrl
        Collection<HTTPContext> contexts = protocolMetadata.get().getContexts(
                HTTPContext.class);
        TestMethodExecutor testMethodExecutor = event.getExecutor();
        Class<?> testClass = testMethodExecutor.getInstance().getClass();
        HTTPContext context = locateHTTPContext(testMethodExecutor.getMethod(),
                contexts);
        URI servletURI = context.getServletByName(
                WarpEventBusServlet.WARP_EVENT_BUS_SERVLET_NAME).getFullURI();

        final String eventUrl = servletURI.toASCIIString() + "?className="
                + testClass.getName() + "&methodName="
                + testMethodExecutor.getMethod().getName();

        // Prepare CommandCallback
        final ApplicationContext applicationContext = applicationContextInst
                .get();<commit_msg>Enable EventBus only if @WarpTest is present<commit_after>/**
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual 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.jboss.arquillian.warp.impl.client.eventbus;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext;
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.container.test.impl.execution.RemoteTestExecuter;
import org.jboss.arquillian.container.test.impl.execution.event.LocalExecutionEvent;
import org.jboss.arquillian.container.test.spi.command.Command;
import org.jboss.arquillian.container.test.spi.command.CommandCallback;
import org.jboss.arquillian.core.api.Event;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.core.spi.context.ApplicationContext;
import org.jboss.arquillian.test.spi.TestMethodExecutor;
import org.jboss.arquillian.test.spi.context.ClassContext;
import org.jboss.arquillian.test.spi.context.SuiteContext;
import org.jboss.arquillian.test.spi.context.TestContext;
import org.jboss.arquillian.test.spi.event.suite.After;
import org.jboss.arquillian.warp.WarpTest;
import org.jboss.arquillian.warp.impl.server.command.WarpEventBusServlet;

/**
 * <p>
 * Provides an event bus during test execution to listen for incoming
 * {@link Command} events.
 * </p>
 * <p>
 * Event Bus functionality is similar to ServletProtocol
 * </p>
 *
 * @author <NAME>
 */
public class WarpEventBus {
    @Inject
    private Instance<ProtocolMetaData> protocolMetadata;

    @Inject
    private Event<Object> remoteEvent;

    @Inject
    private Instance<ApplicationContext> applicationContextInst;

    @Inject
    private Instance<SuiteContext> suiteContextInst;

    @Inject
    private Instance<ClassContext> classContextInst;

    @Inject
    private Instance<TestContext> testContextInst;

    private static Timer eventBusTimer;

    /**
     * Starts the Event Bus as soon as a {@link LocalExecutionEvent} is fired.
     *
     * @param event
     *            the {@link LocalExecutionEvent} that triggered this method
     *            execution
     * @throws Exception
     *
     * @see RemoteTestExecuter
     * @see ServletMethodExecutor
     */
    void startEventBus(@Observes(precedence = 100) LocalExecutionEvent event)
            throws Exception {

        // Calculate eventUrl
        Collection<HTTPContext> contexts = protocolMetadata.get().getContexts(
                HTTPContext.class);
        TestMethodExecutor testMethodExecutor = event.getExecutor();
        Class<?> testClass = testMethodExecutor.getInstance().getClass();
        if (!testClass.isAnnotationPresent(WarpTest.class)) {
            return;
        }
        HTTPContext context = locateHTTPContext(testMethodExecutor.getMethod(),
                contexts);
        URI servletURI = context.getServletByName(
                WarpEventBusServlet.WARP_EVENT_BUS_SERVLET_NAME).getFullURI();

        final String eventUrl = servletURI.toASCIIString() + "?className="
                + testClass.getName() + "&methodName="
                + testMethodExecutor.getMethod().getName();

        // Prepare CommandCallback
        final ApplicationContext applicationContext = applicationContextInst
                .get();
{'fraction_non_alphanumeric': 0.09170355237770968, 'fraction_numerical': 0.0024968789013732834, 'mean_word_length': 3.823207443897099, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3960379', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2411, 'n_words': 608}