/*
 * 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 com.xpn.xwiki.CoreConfiguration;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.util.XWikiStubContextProvider;
import com.xpn.xwiki.web.Utils;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.container.Container;
import org.xwiki.context.Execution;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.test.AbstractComponentTestCase;

/**
 * Same as {@link com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase} but for JUnit 4.x and JMock 2.x.
 *
 * @version $Id$
 * @since 2.2M2
 */
public class AbstractBridgedComponentTestCase extends AbstractComponentTestCase
{
    private XWikiContext context;

    private Mockery mockery = new Mockery()
    {
        {
            // Used to be able to mock class instances (and not only interfaces).
            setImposteriser(ClassImposteriser.INSTANCE);
        }
    };

    @Before
    public void setUp() throws Exception
    {
        super.setUp();

        // Statically store the component manager in {@link Utils} to be able to access it without
        // the context.
        Utils.setComponentManager(getComponentManager());

        this.context = new XWikiContext();

        this.context.setDatabase("xwiki");
        this.context.setMainXWiki("xwiki");

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

        // Bridge with old XWiki Context, required for old code.
        Execution execution = getComponentManager().lookup(Execution.class);
        execution.getContext().setProperty("xwikicontext", this.context);
        getComponentManager().lookup(XWikiStubContextProvider.class).initialize(this.context);

        // Set a simple application context, as some components fail to start without one.
        Container c = getComponentManager().lookup(Container.class);
        c.setApplicationContext(new TestApplicationContext());

        final CoreConfiguration mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
        this.mockery.checking(new Expectations() {{
            allowing(mockCoreConfiguration).getDefaultDocumentSyntax(); will(returnValue(Syntax.XWIKI_1_0));
        }});
    }

    @After
    public void tearDown() throws Exception
    {
        Utils.setComponentManager(null);
        super.tearDown();
    }

    public XWikiContext getContext()
    {
        return this.context;
    }

    public Mockery getMockery()
    {
        return this.mockery;
    }
}

@@ -927,215 +927,65 @@
 ort 
-com.xpn.xwiki.CoreConfiguration;%0Aimport com.xpn.xwiki.XWikiContext;%0Aimport com.xpn.xwiki.util.XWikiStubContextProvider;%0Aimport com.xpn.xwiki.web.Utils;%0Aimport org.jmock.Expectations;%0Aimport org.jmock.Mock
+org.jmock.Expectations;%0Aimport org.jmock.api.Imposteris
 er
-y
 ;%0Aim
@@ -1297,16 +1297,176 @@
 tCase;%0A%0A
+import com.xpn.xwiki.CoreConfiguration;%0Aimport com.xpn.xwiki.XWikiContext;%0Aimport com.xpn.xwiki.util.XWikiStubContextProvider;%0Aimport com.xpn.xwiki.web.Utils;%0A%0A
 /**%0A * S
@@ -1737,156 +1737,253 @@
   pr
-ivate Mockery mockery = new Mockery()%0A    %7B%0A        %7B%0A            // Used to be able to mock class instances (and not only interfaces).
+otected AbstractBridgedComponentTestCase()%0A    %7B%0A        // We often need to mock com.xpn.xwiki.XWiki class%0A        this(ClassImposteriser.INSTANCE);%0A    %7D%0A%0A    protected AbstractBridgedComponentTestCase(Imposteriser imposteriser)
 %0A    
+%7B%0A
         
 setI
@@ -1978,16 +1978,29 @@
         
+getMockery().
 setImpos
@@ -2007,22 +2007,17 @@
 teriser(
-ClassI
+i
 mposteri
@@ -2023,36 +2023,16 @@
 iser
-.INSTANCE);%0A        %7D%0A    %7D;
+);%0A    %7D
 %0A%0A  
@@ -3253,28 +3253,28 @@
         
-this.m
+getM
 ockery
+()
 .checkin
@@ -3642,81 +3642,7 @@
    %7D
-%0A%0A    public Mockery getMockery()%0A    %7B%0A        return this.mockery;%0A    %7D
 %0A%7D%0A

Allow to provide the Imposteriser in AbstractBridgedComponentTestCase