/*
 * 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 com.xpn.xwiki.test;

import org.xwiki.component.manager.ComponentManager;
import org.xwiki.plexus.manager.PlexusComponentManager;
import org.xwiki.context.ExecutionContextInitializerManager;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.DefaultPlexusContainer;

public class XWikiComponentInitializer
{
    private ComponentManager componentManager;

    public void initialize() throws Exception
    {
        // Initialize the Execution Context
        ExecutionContextInitializerManager ecim =
            (ExecutionContextInitializerManager) getComponentManager().lookup(ExecutionContextInitializerManager.ROLE);
        Execution execution = (Execution) getComponentManager().lookup(Execution.ROLE);

        ExecutionContext ec = new ExecutionContext();

        ecim.initialize(ec);
        execution.setContext(ec);
    }

    public void shutdown() throws Exception
    {
        Execution execution = (Execution) getComponentManager().lookup(Execution.ROLE);
        execution.removeContext();

        // Make sure we mark the component manager for garbage collection as otherwise each JUnit test will
        // have an instance of the Component Manager (will all the components it's holding), leading to
        // out of memory errors when there are lots of tests...
        this.componentManager = null;
    }

    /**
     * @return a configured Component Manager (which uses the plexus.xml file in the test resources directory)
     *         which can then be put in the XWiki Context for testing.
     */
    public ComponentManager getComponentManager() throws Exception
    {
        if (this.componentManager == null) {
            DefaultContainerConfiguration configuration = new DefaultContainerConfiguration();
            configuration.setContainerConfiguration("/plexus.xml");
            DefaultPlexusContainer container = new DefaultPlexusContainer(configuration);
            this.componentManager = new PlexusComponentManager(container);
        }

        return this.componentManager;
    }
}

@@ -1063,33 +1063,8 @@
 tion
-ContextInitializerManager
 ;%0Aim
@@ -1087,32 +1087,39 @@
 ontext.Execution
+Context
 ;%0Aimport org.xwi
@@ -1137,32 +1137,39 @@
 ExecutionContext
+Manager
 ;%0Aimport org.cod
@@ -1469,35 +1469,24 @@
 utionContext
-Initializer
 Manager ecim
@@ -1483,17 +1483,16 @@
 nager ec
-i
 m =%0A    
@@ -1512,35 +1512,24 @@
 utionContext
-Initializer
 Manager) get
@@ -1570,27 +1570,16 @@
 nContext
-Initializer
 Manager.
@@ -1741,57 +1741,477 @@
     
-ecim.initialize(ec);%0A        execution.setContext
+// Make sure we push this empty context in the Execution component before we call the initialization%0A        // so that we don't get any NPE if some initializer code asks to get the Execution Context. This %0A        // happens for example with the Velocity Execution Context initializer which in turns calls the Velocity%0A        // Context initializers and some of them look inside the Execution Context.%0A        execution.setContext(ec);%0A        %0A        ecm.initialize
 (ec)

Include Macro fails with an NPE when the page is called in a new context and the included page contains a Velocity script