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

import org.jmock.Expectations;
import org.jmock.States;
import org.junit.Assert;
import org.junit.Test;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.bridge.event.DocumentDeletedEvent;
import org.xwiki.bridge.event.WikiDeletedEvent;
import org.xwiki.component.descriptor.DefaultComponentDescriptor;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceValueProvider;
import org.xwiki.observation.ObservationManager;
import org.xwiki.test.jmock.AbstractComponentTestCase;

/**
 * Unit tests for {@link ContextComponentManager} which indirectly test
 * {@link org.xwiki.component.internal.WikiComponentManager} and
 * {@link org.xwiki.component.internal.UserComponentManager} (and their ancillary classes).
 * 
 * @version $Id$
 * @since 2.1RC1
 */
public class ContextComponentManagerTest extends AbstractComponentTestCase
{
    /**
     * Mock document access bridge.
     */
    private DocumentAccessBridge mockDocumentAccessBridge;

    private EntityReferenceValueProvider mockCurrentValueProvider;

    public static interface Role
    {
    }

    public static class RoleImpl implements Role
    {
    }

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

        // Document Access Bridge Mock
        this.mockDocumentAccessBridge = registerMockComponent(DocumentAccessBridge.class);
        this.mockCurrentValueProvider = registerMockComponent(EntityReferenceValueProvider.class, "current");
    }

    @Test
    public void testRegisterComponentInUserComponentManager() throws Exception
    {
        final States state = getMockery().states("test");

        getMockery().checking(new Expectations()
        {
            {
                allowing(mockDocumentAccessBridge).getCurrentUserReference();
                when(state.isNot("otheruser"));
                will(returnValue(new DocumentReference("wiki", "XWiki", "user1")));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.WIKI);
                will(returnValue("wiki"));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.SPACE);
                will(returnValue("space"));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.DOCUMENT);
                will(returnValue("document"));
            }
        });

        ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");

        // Lookup not yet registered component (and feel the caches with null CMs)

        try {
            contextCM.getInstance(Role.class);
            Assert.fail("Should have raised an exception");
        } catch (ComponentLookupException expected) {
            // No need to assert the message, we just want to ensure an exception is raised.
        }

        // Register component for the current user
        ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "user");
        DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
        cd.setRole(Role.class);
        cd.setImplementation(RoleImpl.class);
        userCM.registerComponent(cd);

        // Verify we can lookup the component from the Context CM        
        Assert.assertNotNull(contextCM.getInstance(Role.class));

        // Now verify that we cannot look it up anymore if there's another user in the context
        state.become("otheruser");
        getMockery().checking(new Expectations()
        {
            {
                allowing(mockDocumentAccessBridge).getCurrentUserReference();
                will(returnValue(new DocumentReference("wiki", "XWiki", "user2")));<commit_msg>[misc] Remove trailing white spaces<commit_after>/*
 * 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.component.internal;

import org.jmock.Expectations;
import org.jmock.States;
import org.junit.Assert;
import org.junit.Test;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.bridge.event.DocumentDeletedEvent;
import org.xwiki.bridge.event.WikiDeletedEvent;
import org.xwiki.component.descriptor.DefaultComponentDescriptor;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceValueProvider;
import org.xwiki.observation.ObservationManager;
import org.xwiki.test.jmock.AbstractComponentTestCase;

/**
 * Unit tests for {@link ContextComponentManager} which indirectly test
 * {@link org.xwiki.component.internal.WikiComponentManager} and
 * {@link org.xwiki.component.internal.UserComponentManager} (and their ancillary classes).
 * 
 * @version $Id$
 * @since 2.1RC1
 */
public class ContextComponentManagerTest extends AbstractComponentTestCase
{
    /**
     * Mock document access bridge.
     */
    private DocumentAccessBridge mockDocumentAccessBridge;

    private EntityReferenceValueProvider mockCurrentValueProvider;

    public static interface Role
    {
    }

    public static class RoleImpl implements Role
    {
    }

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

        // Document Access Bridge Mock
        this.mockDocumentAccessBridge = registerMockComponent(DocumentAccessBridge.class);
        this.mockCurrentValueProvider = registerMockComponent(EntityReferenceValueProvider.class, "current");
    }

    @Test
    public void testRegisterComponentInUserComponentManager() throws Exception
    {
        final States state = getMockery().states("test");

        getMockery().checking(new Expectations()
        {
            {
                allowing(mockDocumentAccessBridge).getCurrentUserReference();
                when(state.isNot("otheruser"));
                will(returnValue(new DocumentReference("wiki", "XWiki", "user1")));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.WIKI);
                will(returnValue("wiki"));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.SPACE);
                will(returnValue("space"));
                allowing(mockCurrentValueProvider).getDefaultValue(EntityType.DOCUMENT);
                will(returnValue("document"));
            }
        });

        ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");

        // Lookup not yet registered component (and feel the caches with null CMs)

        try {
            contextCM.getInstance(Role.class);
            Assert.fail("Should have raised an exception");
        } catch (ComponentLookupException expected) {
            // No need to assert the message, we just want to ensure an exception is raised.
        }

        // Register component for the current user
        ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "user");
        DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
        cd.setRole(Role.class);
        cd.setImplementation(RoleImpl.class);
        userCM.registerComponent(cd);

        // Verify we can lookup the component from the Context CM
        Assert.assertNotNull(contextCM.getInstance(Role.class));

        // Now verify that we cannot look it up anymore if there's another user in the context
        state.become("otheruser");
        getMockery().checking(new Expectations()
        {
            {
                allowing(mockDocumentAccessBridge).getCurrentUserReference();
                will(returnValue(new DocumentReference("wiki", "XWiki", "user2")));
{'fraction_non_alphanumeric': 0.08551952076017352, 'fraction_numerical': 0.0037182400330510227, 'mean_word_length': 3.3913832199546485, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4236753', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2312, 'n_words': 699}