All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.test.jmock.MockingComponentManager Maven / Gradle / Ivy

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

import java.lang.reflect.Type;

import org.jmock.Mockery;
import org.xwiki.component.descriptor.ComponentDescriptor;
import org.xwiki.component.descriptor.DefaultComponentDescriptor;
import org.xwiki.component.embed.EmbeddableComponentManager;
import org.xwiki.component.util.ReflectionUtils;

/**
 * Adds method to {@link EmbeddableComponentManager} to easily register mock components when writing tests.
 *
 * @version $Id: 95a86ccdce089268fce96a3c98ddd2081ca576fa $
 * @since 4.2M1
 * @deprecated use {@link org.xwiki.test.junit5.mockito.ComponentTest} instead
 */
@Deprecated(since = "4.3.1")
public class MockingComponentManager extends EmbeddableComponentManager
{
    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param role the role of the component to register
     * @param hint the hint of the component to register
     * @param mockId the JMock id (this is needed when registering several mocks for the same interface)
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    public  T registerMockComponent(Mockery mockery, Type role, String hint, String mockId) throws Exception
    {
        DefaultComponentDescriptor descriptor = createComponentDescriptor(role);
        descriptor.setRoleHint(hint);
        return registerMockComponent(mockery, descriptor, mockId);
    }

    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param role the role of the component to register
     * @param hint the hint of the component to register
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    public  T registerMockComponent(Mockery mockery, Type role, String hint) throws Exception
    {
        return registerMockComponent(mockery, role, hint, null);
    }

    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param role the role of the component to register
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    public  T registerMockComponent(Mockery mockery, Type role) throws Exception
    {
        return registerMockComponent(mockery, this.createComponentDescriptor(role));
    }

    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param role the role of the component to register
     * @param mockId the JMock id (this is needed when registering several mocks for the same interface)
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    public  T registerMockComponentWithId(Mockery mockery, Type role, String mockId) throws Exception
    {
        return registerMockComponent(mockery, this.createComponentDescriptor(role), mockId);
    }

    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param descriptor the component descriptor for the component to register
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    private  T registerMockComponent(Mockery mockery, ComponentDescriptor descriptor) throws Exception
    {
        return registerMockComponent(mockery, descriptor, null);
    }

    /**
     * Register a mock component.
     *
     * @param mockery the JMock mockery instance to create the mock component
     * @param descriptor the component descriptor for the component to register
     * @param mockId the JMock id (this is needed when registering several mocks for the same interface)
     * @param  the component role class
     * @return the registered mock component instance
     * @throws Exception if the registration failed
     */
    @SuppressWarnings("unchecked")
    private  T registerMockComponent(Mockery mockery, ComponentDescriptor descriptor, String mockId)
        throws Exception
    {
        T instance;
        if (mockId != null) {
            instance = mockery.mock((Class) ReflectionUtils.getTypeClass(descriptor.getRoleType()), mockId);
        } else {
            instance =
                mockery.mock((Class) ReflectionUtils.getTypeClass(descriptor.getRoleType()), descriptor
                    .getRoleType().toString());
        }

        registerComponent(descriptor, instance);

        return instance;
    }

    /**
     * Create a Component Descriptor.
     *
     * @param role the role of the component to register
     * @param  the component role class
     * @return the component descriptor
     */
    private  DefaultComponentDescriptor createComponentDescriptor(Type role)
    {
        DefaultComponentDescriptor descriptor = new DefaultComponentDescriptor<>();
        descriptor.setRoleType(role);

        // Default to very high priority for retro compatibility reason (calling this method used to always register
        // the component without caring about any kind of priority and changing that behavior might affect a lot of unit
        // tests
        descriptor.setRoleHintPriority(0);

        return descriptor;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy