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

import java.util.HashMap;
import java.util.Map;

import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;

import junit.framework.Assert;

import org.apache.velocity.VelocityContext;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import org.xwiki.context.Execution;
import org.xwiki.context.ExecutionContext;
import org.xwiki.script.ScriptContextManager;
import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
import org.xwiki.test.jmock.annotation.MockingRequirement;
import org.xwiki.velocity.VelocityManager;

/**
 * Unit tests for {@link DefaultVelocityManager}.
 * 
 * @version $Id$
 */
@MockingRequirement(DefaultVelocityManager.class)
public class DefaultVelocityManagerTest extends AbstractMockingComponentTestCase
{
    /**
     * The component being tested.
     */
    private VelocityManager velocityManager;

    @Before
    public void configure() throws Exception
    {
        this.velocityManager = getComponentManager().getInstance(VelocityManager.class);
    }

    /**
     * Tests that the Execution Context and the XWiki Context share the same reference of the Velocity Context after a
     * call to {@link VelocityManager#getVelocityContext()}. There is old code that accesses the Velocity Context from
     * the XWiki Context.
     */
    @Test
    public void testGetVelocityContextUpdatesXContext() throws Exception
    {
        final Execution execution = getComponentManager().getInstance(Execution.class);
        final ExecutionContext executionContext = new ExecutionContext();

        final ScriptContextManager scriptContextManager = getComponentManager().getInstance(ScriptContextManager.class);
        final ScriptContext scriptContext = new SimpleScriptContext();

        final VelocityContext velocityContext = new VelocityContext();
        executionContext.newProperty("velocityContext").initial(velocityContext).inherited().cloneValue().declare();

        Map<String, Object> xwikiContext = new HashMap<String, Object>();
        executionContext.setProperty("xwikicontext", xwikiContext);

        getMockery().checking(new Expectations()
        {
            {
                allowing(execution).getContext();
                will(returnValue(executionContext));

                oneOf(scriptContextManager).getScriptContext();
                will(returnValue(scriptContext));
            }
        });

        velocityManager.getVelocityContext();
        Assert.assertEquals(velocityContext, xwikiContext.get("vcontext"));
    }
}

@@ -1048,40 +1048,8 @@
 t;%0A%0A
-import junit.framework.Assert;%0A%0A
 impo
@@ -1104,49 +1104,16 @@
 rg.j
-mock.Expectations;%0Aimport org.junit.Befor
+unit.Rul
 e;%0Ai
@@ -1285,29 +1285,23 @@
 est.
-j
 mock
-.Abstract
+ito.
 Mocki
-ng
+to
 Comp
@@ -1305,23 +1305,26 @@
 omponent
-TestCas
+MockingRul
 e;%0Aimpor
@@ -1339,91 +1339,105 @@
 iki.
-test.jmock.annotation.MockingRequiremen
+velocity.VelocityManager;%0A%0Aimport junit.framework.Asser
 t;%0A
+%0A
 import 
-org.xwiki.vel
+static org.mockito.M
 oc
+k
 it
-y.VelocityManager
+o.when
 ;%0A%0A/
@@ -1491,17 +1491,16 @@
 ger%7D.%0A *
- 
 %0A * @ver
@@ -1517,366 +1517,178 @@
  */%0A
-@MockingRequirement(DefaultVelocityManager.class)%0Apublic class DefaultVelocityManagerTest extends AbstractMockingComponentTestCase%0A%7B%0A    /**%0A     * The component being tested.%0A     */%0A    private VelocityManager velocityManager;%0A%0A    @Before%0A    public void configure() throws Exception%0A    %7B%0A        this.velocityManager = getComponentManager().getInstance(
+public class DefaultVelocityManagerTest%0A%7B%0A    @Rule%0A    public final MockitoComponentMockingRule%3CVelocityManager%3E mocker =%0A        new MockitoComponentMockingRule(Default
 Velo
@@ -1706,22 +1706,16 @@
 .class);
-%0A    %7D
 %0A%0A    /*
@@ -2077,38 +2077,32 @@
 n%0A    %7B%0A        
-final 
 Execution execut
@@ -2107,37 +2107,27 @@
 ution = 
-getComponentManager()
+this.mocker
 .getInst
@@ -2157,22 +2157,16 @@
         
-final 
 Executio
@@ -2213,33 +2213,32 @@
 nContext();%0A
-%0A
         
 final Script
@@ -2217,37 +2217,99 @@
 text();%0A        
-final
+when(execution.getContext()).thenReturn(executionContext);%0A%0A       
  ScriptContextMa
@@ -2341,29 +2341,19 @@
 r = 
-getComponentManager()
+this.mocker
 .get
@@ -2398,22 +2398,16 @@
         
-final 
 ScriptCo
@@ -2455,25 +2455,24 @@
 text();%0A
-%0A
         
 final Ve
@@ -2463,21 +2463,97 @@
         
-final
+when(scriptContextManager.getScriptContext()).thenReturn(scriptContext);%0A%0A       
  Velocit
@@ -2875,341 +2875,43 @@
     
-getMockery().checking(new Expectations()%0A        %7B%0A            %7B%0A                allowing(execution).getContext();%0A                will(returnValue(executionContext));%0A%0A                oneOf(scriptContextManager).getScriptContext();%0A                will(returnValue(scriptContext));%0A            %7D%0A        %7D);%0A%0A        velocityManager
+this.mocker.getComponentUnderTest()
 .get

Move test to use Mockito