/*
 * 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.jmock.cglib.MockObjectTestCase;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.container.Container;
import org.xwiki.container.daemon.DaemonContainerFactory;
import org.xwiki.plexus.manager.PlexusComponentManager;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.DefaultContainerConfiguration;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.PlexusContainerLocator;

import com.xpn.xwiki.XWikiContext;

/**
 * Tests which needs to have XWiki Components set up should extend this class which makes the Component Manager
 * available.
 */
public abstract class AbstractXWikiComponentTestCase extends MockObjectTestCase
{
    private ComponentManager componentManager;
    
    private XWikiContext context;
    
    protected void setUp() throws Exception
    {
        this.context = new XWikiContext();

        // We need to initialize the Component Manager so that the components can be looked up
        getContext().put(ComponentManager.class.getName(), getComponentManager());

        // Initialize the Container objects
        DaemonContainerFactory dcf = (DaemonContainerFactory) getComponentManager().lookup(DaemonContainerFactory.ROLE);
        Container container = (Container) getComponentManager().lookup(Container.ROLE);
        container.setRequest(dcf.createRequest());

        // This is a bridge that we need for old code to play well with new components.
        // Old code relies on the XWikiContext object whereas new code uses the Container component.
        container.getRequest().setProperty("xwikicontext", getContext());
    }

    protected void tearDown() throws Exception
    {
        Container container = (Container) getComponentManager().lookup(Container.ROLE);
        container.removeRequest();
        container.removeResponse();
        container.removeSession();
    }
    
    public XWikiContext getContext()
    {
        return this.context;
    }

    /**
     * @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);
            PlexusContainerLocator locator = new PlexusContainerLocator(container);
            this.componentManager = new PlexusComponentManager(locator);
        }

        return this.componentManager;
    }
}

@@ -1102,23 +1102,27 @@
 ontainer
-Factory
+Initializer
 ;%0Aimport
@@ -2054,19 +2054,23 @@
 iner
-Factory dcf
+Initializer dci
  = (
@@ -2084,23 +2084,27 @@
 ontainer
-Factory
+Initializer
 ) getCom
@@ -2130,174 +2130,85 @@
 kup(
-DaemonContainerFactory.ROLE);%0A        Container container = (Container) getComponentManager().lookup(Container.ROLE);%0A        container.setRequest(dcf.createRequest()
+%0A            DaemonContainerInitializer.ROLE);%0A        dci.initializeRequest(
 );%0A%0A
@@ -2396,16 +2396,104 @@
 ponent.%0A
+        Container container = (Container) getComponentManager().lookup(Container.ROLE);%0A
         

Introduce new Container component (using ThreadLocal variables) to prevent having to pass request/response/session data to all methods