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

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.xwiki.model.reference.DocumentReference;

import com.xpn.xwiki.test.AbstractBridgedComponentTestCase;

/**
 * Unit tests for {@link com.xpn.xwiki.XWikiContext}.
 * 
 * @version $Id$
 */
public class XWikiContextTest extends AbstractBridgedComponentTestCase
{
    @Before
    @Override
    public void setUp() throws Exception
    {
        super.setUp();

        getContext().setWiki(new XWiki()
        {
            @Override
            public boolean isVirtualMode()
            {
                return true;
            }
        });
    }

    @Test
    public void testgetUser()
    {
        getContext().setMainXWiki("wiki");
        getContext().setDatabase("wiki");
        getContext().setUserReference(new DocumentReference("wiki", "space", "user"));

        Assert.assertEquals("space.user", getContext().getUser());
        Assert.assertEquals("space.user", getContext().getLocalUser());
        Assert.assertEquals("space.user", getContext().getXWikiUser().getUser());
        Assert.assertTrue("space.user", getContext().getXWikiUser().isMain());

        getContext().setDatabase("wiki1");

        Assert.assertEquals("wiki:space.user", getContext().getUser());
        Assert.assertEquals("space.user", getContext().getLocalUser());
        Assert.assertEquals("wiki:space.user", getContext().getXWikiUser().getUser());
        Assert.assertTrue("space.user", getContext().getXWikiUser().isMain());
    }

    @Test
    public void testSetUser()
    {
        getContext().setMainXWiki("wiki");
        getContext().setDatabase("wiki");
        getContext().setUser("XWiki.user");

        Assert.assertEquals(new DocumentReference("wiki", "XWiki", "user"), getContext().getUserReference());

        getContext().setUser("user");

        Assert.assertEquals(new DocumentReference("wiki", "XWiki", "user"), getContext().getUserReference());
    }

    @Test
    public void testAnonymousUser()
    {
        getContext().setMainXWiki("wiki");
        getContext().setDatabase("wiki");
        getContext().setUserReference(null);

        Assert.assertNull(getContext().getUserReference());
        Assert.assertEquals("XWiki.XWikiGuest", getContext().getUser());
        Assert.assertEquals("XWiki.XWikiGuest", getContext().getLocalUser());
        Assert.assertNull(getContext().getXWikiUser());

        getContext().setDatabase("wiki2");

        Assert.assertNull(getContext().getUserReference());
        Assert.assertEquals("XWiki.XWikiGuest", getContext().getUser());
        Assert.assertEquals("XWiki.XWikiGuest", getContext().getLocalUser());
        Assert.assertNull(getContext().getXWikiUser());
    }
}

@@ -2712,24 +2712,91 @@
 eference());
+%0A        Assert.assertEquals(%22XWiki.user%22, getContext().getUser());
 %0A%0A        ge
@@ -2930,24 +2930,91 @@
 ference());%0A
+        Assert.assertEquals(%22XWiki.user%22, getContext().getUser());%0A
     %7D%0A%0A    @

Introduce APIs using References for context user More tests