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 /** 037 * Get the current application context. 038 * 039 * @return the {@link ApplicationContext}. 040 */ 041 ApplicationContext getApplicationContext(); 042 043 /** 044 * Create a rule to support Spring. 045 * <p> 046 * For instance: 047 * 048 * <pre> 049 * @Rule 050 * public final SpringRule spring = SpringRule.of( 051 * AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, "classpath:sample.xml"); 052 * </pre> 053 * 054 * @param autowireMode 055 * The autowiring mode (of the test class). 056 * @param location 057 * the first location for the bean context. 058 * @param nextLocation 059 * optional additional location for the bean context. 060 * @return the Rule. 061 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_NAME 062 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#AUTOWIRE_BY_TYPE 063 */ 064 static SpringRule of(int autowireMode, String location, 065 String... nextLocation) { 066 String[] tmp = new String[nextLocation.length + 1]; 067 tmp[0] = location; 068 System.arraycopy(nextLocation, 0, tmp, 1, nextLocation.length); 069 return new SpringRuleImpl(tmp, autowireMode); 070 } 071 072 /** 073 * Get a bean from the used ApplicationContext. 074 * 075 * @param requiredType 076 * the requiredType. 077 * @return the bean 078 * @see org.springframework.context.ApplicationContext#getBean(Class) 079 * @param <T> 080 * THe type of the bean. 081 */ 082 default <T> T getBean(Class<T> requiredType) { 083 return getApplicationContext().getBean(requiredType); 084 } 085 086 /** 087 * Validate if a bean with a name exists. 088 * 089 * @param name 090 * the name 091 * @return true if the bean exists. 092 * @see org.springframework.context.ApplicationContext#containsBean(String) 093 */ 094 default boolean containsBean(String name) { 095 return getApplicationContext().containsBean(name); 096 } 097 098 /** 099 * Get a bean, passing argument. 100 * 101 * @param name 102 * the name 103 * @param arguments 104 * the arguments 105 * @return the bean 106 * @throws BeansException 107 * if the bean could not be created 108 * @see org.springframework.context.ApplicationContext#getBean(Class, 109 * Object...) 110 */ 111 default Object getBean(String name, Object... arguments) 112 throws BeansException { 113 return getApplicationContext().getBean(name, arguments); 114 } 115 116 /** 117 * Get a bean, by name. 118 * 119 * @param name 120 * the name 121 * @return the bean 122 * @throws BeansException 123 * if the bean could not be created 124 * @see org.springframework.context.ApplicationContext#getBean(String) 125 */ 126 default Object getBean(String name) throws BeansException { 127 return getApplicationContext().getBean(name); 128 } 129 130}