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

import org.xwiki.context.ExecutionContext;
import org.xwiki.context.ExecutionContextException;
import org.xwiki.context.ExecutionContextManager;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;

/**
 * Validate XWikiStubContextInitializer and DefaultXWikiStubContextProvider.
 * 
 * @version $Id$
 */
public class XWikiStubContextInitializerTest extends AbstractBridgedXWikiComponentTestCase
{
    private ExecutionContextManager executionContextManager;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        this.executionContextManager = getComponentManager().lookup(ExecutionContextManager.class);
    }

    public void testWithAndWithoutXWikiContext() throws ExecutionContextException, InterruptedException
    {
        XWikiContext xcontext = new XWikiContext();
        xcontext.put("key", "value");

        ExecutionContext context = new ExecutionContext();
        context.setProperty("xwikicontext", xcontext);

        this.executionContextManager.initialize(context);

        assertSame(xcontext, context.getProperty("xwikicontext"));

        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                ExecutionContext context = new ExecutionContext();

                try {
                    executionContextManager.initialize(context);
                } catch (ExecutionContextException e) {
                    fail("Failed to initialize execution context: " + e.getStackTrace());
                }

                XWikiContext xcontext = (XWikiContext) context.getProperty("xwikicontext");
                assertNotNull(xcontext);
                assertEquals("value", xcontext.get("key"));
            }
        });

        thread.join();
    }
}

@@ -2062,24 +2062,96 @@
 ontext%22));%0A%0A
+        final ExecutionContext daemonContext = new ExecutionContext();%0A%0A
         Thre
@@ -2246,76 +2246,8 @@
   %7B%0A
-                ExecutionContext context = new ExecutionContext();%0A%0A
     
@@ -2311,33 +2311,39 @@
 ager.initialize(
-c
+daemonC
 ontext);%0A       
@@ -2499,25 +2499,24 @@
       %7D%0A
-%0A
         
         
@@ -2499,32 +2499,75 @@
       %7D%0A        
+    %7D%0A        %7D);%0A%0A        thread.join();%0A%0A
         XWikiCon
@@ -2563,33 +2563,39 @@
    XWikiContext 
-x
+daemonX
 context = (XWiki
@@ -2603,17 +2603,23 @@
 ontext) 
-c
+daemonC
 ontext.g
@@ -2650,32 +2650,24 @@
 %22);%0A        
-    
-    
 assertNotNul
@@ -2679,32 +2679,73 @@
 ntext);%0A        
+assertNotSame(xcontext, daemonXcontext);%0A
         assertEq
@@ -2784,58 +2784,8 @@
 ));%0A
-            %7D%0A        %7D);%0A%0A        thread.join();%0A
     

Add an automatic stub provider and initializer for XWikiContext * improve unit test