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

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.lang.reflect.Method;

import org.xwiki.component.descriptor.ComponentDescriptor;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.manager.ComponentManager;

/**
 * JUnit Test Setup that initializes the Component Manager. 
 * Test suite that want to initialize the Component manager only once should use this class instead of 
 * {@link AbstractXWikiComponentTestCase}.
 * 
 * @version $Id$
 * @since 2.0M1
 */
public class ComponentManagerTestSetup extends TestSetup
{
    private XWikiComponentInitializer initializer = new XWikiComponentInitializer();

    private List<ComponentDescriptor< ? >> componentDescriptors = new ArrayList<ComponentDescriptor< ? >>();
    
    public ComponentManagerTestSetup(TestSuite suite)
    {
        super(suite);
    }

    public ComponentManagerTestSetup(TestSuite suite, List<ComponentDescriptor< ? >> componentDescriptors) throws Exception
    {
        super(suite);
        this.componentDescriptors.addAll(componentDescriptors);
    }

    /**
     * {@inheritDoc}
     * 
     * @see junit.extensions.TestDecorator#getTest()
     */
    @Override
    public TestSuite getTest()
    {
        return (TestSuite) super.getTest();
    }

    /**
     * @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.
     * @since 1.7M3
     */
    public EmbeddableComponentManager getComponentManager() throws Exception
    {
        return this.initializer.getComponentManager();
    }

    /**
     * {@inheritDoc}
     * 
     * @see junit.extensions.TestSetup#setUp()
     */
    @Override
    protected void setUp() throws Exception
    {
        this.initializer.initializeContainer();
        this.initializer.initializeConfigurationSource();
        
        // Register defined component descriptors as components 
        for (ComponentDescriptor< ? > descriptor : this.componentDescriptors) {
            getComponentManager().registerComponent(descriptor);
        }

        this.initializer.initializeExecution();
        
        for (Enumeration<Test> tests = getTest().tests(); tests.hasMoreElements();) {
            Test test = (Test) tests.nextElement();
            try {
                Method method = test.getClass().getMethod("setComponentManager", ComponentManager.class);
                method.invoke(test, this.initializer.getComponentManager());
            } catch (Exception e) {
                // Apparently the test doesn't have a setComponentManager method, don't do anything then.
            }
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see junit.extensions.TestSetup#tearDown()
     */
    @Override
    protected void tearDown() throws Exception
    {
        this.initializer.shutdown();
    }
}

@@ -1367,20 +1367,16 @@
 anager. 
-%0A * 
 Test sui
@@ -1423,16 +1423,19 @@
  manager
+%0A *
  only on
@@ -1470,20 +1470,16 @@
 tead of 
-%0A * 
 %7B@link A
@@ -1805,21 +1805,17 @@
 ? %3E%3E();%0A
-    
 %0A
+
     publ
@@ -2001,16 +2001,24 @@
 riptors)
+%0A       
  throws 
@@ -2947,32 +2947,24 @@
 onSource();%0A
-        
 %0A        // 
@@ -3015,17 +3015,16 @@
 mponents
- 
 %0A       
@@ -3224,17 +3224,9 @@
 ();%0A
-        
 %0A
+
     
@@ -3303,24 +3303,24 @@
 ments();) %7B%0A
+
             
@@ -3334,15 +3334,8 @@
 st =
- (Test)
  tes

Apply codestyle