/*
 * 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.jmock;

import org.xwiki.component.descriptor.DefaultComponentDescriptor;
import org.xwiki.component.internal.StackingComponentEventManager;
import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.xwiki.context.ExecutionContextManager;
import org.xwiki.test.internal.MockConfigurationSource;

/**
 * Initialize a component manager used in unit tests.
 *
 * @version $Id$
 * @deprecated starting with 4.3.1 use {@link org.xwiki.test.ComponentManagerRule} instead
 */
@Deprecated
public class XWikiComponentInitializer
{
    private MockingComponentManager componentManager;

    private MockConfigurationSource configurationSource;

    public void initializeConfigurationSource() throws Exception
    {
        // Register the mock configuration source for different roles so that tests always use the mock
        this.configurationSource = new MockConfigurationSource();

        DefaultComponentDescriptor<ConfigurationSource> descriptor;

        descriptor = new DefaultComponentDescriptor<ConfigurationSource>();
        descriptor.setRole(ConfigurationSource.class);
        getComponentManager().registerComponent(descriptor, this.configurationSource);

        descriptor = new DefaultComponentDescriptor<ConfigurationSource>();
        descriptor.setRole(ConfigurationSource.class);
        descriptor.setRoleHint("xwikiproperties");
        getComponentManager().registerComponent(descriptor, this.configurationSource);
    }

    public void initializeExecution() throws Exception
    {
        // Initialize the Execution Context
        ExecutionContextManager ecm = getComponentManager().getInstance(ExecutionContextManager.class);

        ExecutionContext ec = new ExecutionContext();

        ecm.initialize(ec);
    }

    public void shutdown() throws Exception
    {
        Execution execution = getComponentManager().getInstance(Execution.class);
        execution.removeContext();

        // Clean possible resources some components might hold
        this.componentManager.dispose();

        // 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
     */
    public MockingComponentManager getComponentManager() throws Exception
    {
        if (this.componentManager == null) {
            MockingComponentManager ecm = new MockingComponentManager();
            ecm.initialize(this.getClass().getClassLoader());
            ecm.setComponentEventManager(new StackingComponentEventManager());
            this.componentManager = ecm;
        }

        return this.componentManager;
    }

    /**
     * @return a modifiable mock configuration source
     */
    public MockConfigurationSource getConfigurationSource()
    {
        return this.configurationSource;
    }
}

@@ -949,164 +949,46 @@
 ent.
-descriptor.DefaultComponentDescriptor;%0Aimport org.xwiki.component.internal.StackingComponentEventManager;%0Aimport org.xwiki.configuration.ConfigurationSource
+internal.StackingComponentEventManager
 ;%0Aim
@@ -1760,22 +1760,18 @@
         
-Defaul
+ge
 tCompone
@@ -1768,35 +1768,56 @@
 getComponent
-Descriptor%3C
+Manager().registerComponent(Mock
 Configuratio
@@ -1827,11 +1827,13 @@
 urce
-%3E d
+.getD
 escr
@@ -1841,115 +1841,155 @@
 ptor
-;%0A%0A        descriptor = new DefaultComponentDescriptor%3CConfigurationSource%3E();%0A        descriptor.setRole(C
+(null), this.configurationSource);%0A        getComponentManager().registerComponent(MockConfigurationSource.getDescriptor(%22xwikiproperties%22), this.c
 onfi
@@ -1994,38 +1994,32 @@
 figurationSource
-.class
 );%0A        getCo
@@ -2045,33 +2045,60 @@
 gisterComponent(
-d
+MockConfigurationSource.getD
 escriptor, this.
@@ -2082,32 +2082,40 @@
 ce.getDescriptor
+(%22wiki%22)
 , this.configura
@@ -2127,25 +2127,24 @@
 ource);%0A
-%0A
         
 descript
@@ -2139,53 +2139,102 @@
     
-descriptor = new DefaultComponentDescriptor%3CC
+getComponentManager().registerComponent(MockConfigurationSource.getDescriptor(%22space%22), this.c
 onfi
@@ -2251,10 +2251,8 @@
 urce
-%3E(
 );%0A 
@@ -2262,103 +2262,119 @@
     
-descriptor.setRole(ConfigurationSource.class);%0A        descriptor.setRoleHint(%22xwikiproperties%22
+getComponentManager().registerComponent(MockConfigurationSource.getDescriptor(%22user%22), this.configurationSource
 );%0A 
@@ -2420,17 +2420,44 @@
 mponent(
-d
+MockConfigurationSource.getD
 escripto
@@ -2457,16 +2457,23 @@
 scriptor
+(%22all%22)
 , this.c

Add a few more mocked configuration by default