001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit.extension.spring;
021
022import org.springframework.beans.BeansException;
023import org.springframework.context.ApplicationContext;
024
025import ch.powerunit.extension.spring.impl.SpringRuleImpl;
026import ch.powerunit.rules.TestListenerRule;
027
028/**
029 * This is a {@link ch.powerunit.TestRule TestRule} to provide support to use
030 * Spring inside PowerUnit.
031 * 
032 * @author borettim
033 *
034 */
035public interface SpringRule extends TestListenerRule {
036        ApplicationContext getApplicationContext();
037
038        /**
039         * Create a rule to support Spring.
040         * <p>
041         * For instance:
042         * 
043         * <pre>
044         * &#064;Rule
045         * public final SpringRule spring = SpringRule.of(
046         *              AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, &quot;classpath:sample.xml&quot;);
047         * </pre>
048         * 
049         * @param autowireMode
050         *            The autowiring mode (of the test class).
051         * @param location
052         *            the first location for the bean context.
053         * @param nextLocation
054         *            optional additional location for the bean context.
055         * @return the Rule.
056         * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME
057         * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE
058         */
059        static SpringRule of(int autowireMode, String location,
060                        String... nextLocation) {
061                String[] tmp = new String[nextLocation.length + 1];
062                tmp[0] = location;
063                System.arraycopy(nextLocation, 0, tmp, 1, nextLocation.length);
064                return new SpringRuleImpl(tmp, autowireMode);
065        }
066
067        /**
068         * Get a bean from the used ApplicationContext.
069         * 
070         * @param requiredType
071         *            the requiredType.
072         * @return the bean
073         * @see org.springframework.context.ApplicationContext#getBean(Class)
074         * @param <T>
075         *            THe type of the bean.
076         */
077        default <T> T getBean(Class<T> requiredType) {
078                return getApplicationContext().getBean(requiredType);
079        }
080
081        /**
082         * Validate if a bean with a name exists.
083         * 
084         * @param name
085         *            the name
086         * @return true if the bean exists.
087         * @see org.springframework.context.ApplicationContext#containsBean(String)
088         */
089        default boolean containsBean(String name) {
090                return getApplicationContext().containsBean(name);
091        }
092
093        /**
094         * Get a bean, passing argument.
095         * 
096         * @param name
097         *            the name
098         * @param arguments
099         *            the arguments
100         * @return the bean
101         * @throws BeansException
102         *             if the bean could not be created
103         * @see org.springframework.context.ApplicationContext#getBean(Class,
104         *      Object...)
105         */
106        default Object getBean(String name, Object... arguments)
107                        throws BeansException {
108                return getApplicationContext().getBean(name, arguments);
109        }
110
111        /**
112         * Get a bean, by name.
113         * 
114         * @param name
115         *            the name
116         * @return the bean
117         * @throws BeansException
118         *             if the bean could not be created
119         * @see org.springframework.context.ApplicationContext#getBean(String)
120         */
121        default Object getBean(String name) throws BeansException {
122                return getApplicationContext().getBean(name);
123        }
124
125}