<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.ui;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.xwiki.test.integration.XWikiExecutorSuite;

/**
 * Extends {@link XWikiExecutorSuite} and initialize the {@link PersistentTestContext}.
 * 
 * @version $Id$
 * @since 3.2M3
 */
public class PageObjectSuite extends XWikiExecutorSuite
{
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public static @interface PostStart
    {
    }
    
    private PersistentTestContext context;

    public PageObjectSuite(Class< ? > suiteClass, RunnerBuilder builder) throws InitializationError
    {
        super(suiteClass, builder);
    }

    @Override
    protected void beforeTests()
    {
        // Construct the executors and start the XWiki instances.
        super.beforeTests();

        try {
            initializePersistentTestContext();

            // Callback to setup executors in the suite class after containers have been started
            for (Method method : getTestClass().getJavaClass().getMethods()) {
                PostStart postStartAnnotation = method.getAnnotation(PostStart.class);
                if (postStartAnnotation != null) {
                    // Call it!
                    Object instance = getTestClass().getJavaClass().newInstance();
                    method.invoke(instance, this.context);
                }
            }
        } catch (Exception e) {
            // Make sure to stop the executors if anything goes wrong
            afterTests();

            throw new RuntimeException("Failed to initialize PO suite", e);
        }
    }

    private void initializePersistentTestContext()
    {
        try {
            this.context = new PersistentTestContext(getExecutors().get(0));
            AbstractTest.setContext(this.context.getUnstoppable());

            // Cache the initial CSRF token since that token needs to be passed to all forms (this is done automatically
            // in TestUtils), including the login form. Whenever a new user logs in we need to recache.
            // Note that this requires a running XWiki instance.
            AbstractTest.context.getUtil().recacheSecretToken();
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize PersistentTestContext", e);
        }
    }

    @Override
    protected void afterTests()
    {
        try {
            context.shutdown();
        } catch (Exception e) {
            throw new RuntimeException("Failed to shutdown PersistentTestContext", e);
        }

        // Note: Don't call super.afterTests() since XWiki will be stopped twice otherwise!
    }
}
<commit_msg>[Misc] Avoid NPE which can now happen after the fix for (XWIKI-10976: XE doesn't stop if there's a failure during the PersistentTestContext initialization)<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.ui;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import org.xwiki.test.integration.XWikiExecutorSuite;

/**
 * Extends {@link XWikiExecutorSuite} and initialize the {@link PersistentTestContext}.
 * 
 * @version $Id$
 * @since 3.2M3
 */
public class PageObjectSuite extends XWikiExecutorSuite
{
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public static @interface PostStart
    {
    }
    
    private PersistentTestContext context;

    public PageObjectSuite(Class< ? > suiteClass, RunnerBuilder builder) throws InitializationError
    {
        super(suiteClass, builder);
    }

    @Override
    protected void beforeTests()
    {
        // Construct the executors and start the XWiki instances.
        super.beforeTests();

        try {
            initializePersistentTestContext();

            // Callback to setup executors in the suite class after containers have been started
            for (Method method : getTestClass().getJavaClass().getMethods()) {
                PostStart postStartAnnotation = method.getAnnotation(PostStart.class);
                if (postStartAnnotation != null) {
                    // Call it!
                    Object instance = getTestClass().getJavaClass().newInstance();
                    method.invoke(instance, this.context);
                }
            }
        } catch (Exception e) {
            // Make sure to stop the executors if anything goes wrong
            afterTests();

            throw new RuntimeException("Failed to initialize PO suite", e);
        }
    }

    private void initializePersistentTestContext()
    {
        try {
            this.context = new PersistentTestContext(getExecutors().get(0));
            AbstractTest.setContext(this.context.getUnstoppable());

            // Cache the initial CSRF token since that token needs to be passed to all forms (this is done automatically
            // in TestUtils), including the login form. Whenever a new user logs in we need to recache.
            // Note that this requires a running XWiki instance.
            AbstractTest.context.getUtil().recacheSecretToken();
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize PersistentTestContext", e);
        }
    }

    @Override
    protected void afterTests()
    {
        if (context != null) {
            try {
                context.shutdown();
            } catch (Exception e) {
                throw new RuntimeException("Failed to shutdown PersistentTestContext", e);
            }
        }

        // Note: Don't call super.afterTests() since XWiki will be stopped twice otherwise!
    }
}

{'fraction_non_alphanumeric': 0.0789439958990132, 'fraction_numerical': 0.004998077662437524, 'mean_word_length': 2.681132075471698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5568067', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1901, 'n_words': 715}