Best Citrus code snippet using com.consol.citrus.context.SpringBeanReferenceResolver.resolve
Source:SpringBeanReferenceResolver.java
...19import java.util.Map;20import java.util.Optional;21import java.util.function.Function;22import java.util.stream.Collectors;23import com.consol.citrus.context.resolver.TypeAliasResolver;24import com.consol.citrus.exceptions.CitrusRuntimeException;25import com.consol.citrus.spi.ReferenceResolver;26import com.consol.citrus.spi.SimpleReferenceResolver;27import org.slf4j.Logger;28import org.slf4j.LoggerFactory;29import org.springframework.beans.BeansException;30import org.springframework.beans.factory.NoSuchBeanDefinitionException;31import org.springframework.context.ApplicationContext;32import org.springframework.context.ApplicationContextAware;33/**34 * Spring bean reference resolver operates on given application context to resolve bean references.35 *36 * @author Christoph Deppisch37 * @since 2.538 */39public class SpringBeanReferenceResolver implements ReferenceResolver, ApplicationContextAware {40 /** Logger */41 private static final Logger log = LoggerFactory.getLogger(SpringBeanReferenceResolver.class);42 private ApplicationContext applicationContext;43 private ReferenceResolver fallback = new SimpleReferenceResolver();44 private final Map<String, TypeAliasResolver<?, ?>> typeAliasResolvers = new HashMap<>();45 /**46 * Default constructor.47 */48 public SpringBeanReferenceResolver() {49 super();50 }51 /**52 * Constructor initializes with given application context.53 * @param applicationContext54 */55 public SpringBeanReferenceResolver(ApplicationContext applicationContext) {56 this.applicationContext = applicationContext;57 }58 @Override59 public <T> T resolve(Class<T> requiredType) {60 try {61 return applicationContext.getBean(requiredType);62 } catch (NoSuchBeanDefinitionException e) {63 if (fallback.isResolvable(requiredType)) {64 return fallback.resolve(requiredType);65 }66 return resolveAlias(requiredType, this::resolve)67 .orElseThrow(() -> new CitrusRuntimeException(String.format("Unable to find bean reference for type '%s'", requiredType), e));68 }69 }70 @Override71 public <T> T resolve(String name, Class<T> type) {72 try {73 return applicationContext.getBean(name, type);74 } catch (NoSuchBeanDefinitionException e) {75 if (fallback.isResolvable(name, type)) {76 return fallback.resolve(name, type);77 }78 return resolveAlias(type, clazz -> resolve(name, clazz))79 .orElseThrow(() -> new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name), e));80 }81 }82 @Override83 public Object resolve(String name) {84 try {85 return applicationContext.getBean(name);86 } catch (NoSuchBeanDefinitionException e) {87 if (fallback.isResolvable(name)) {88 return fallback.resolve(name);89 }90 throw new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name), e);91 }92 }93 @Override94 public <T> Map<String, T> resolveAll(Class<T> requiredType) {95 Map<String, T> beans = applicationContext.getBeansOfType(requiredType);96 if (beans.isEmpty()) {97 if (fallback.isResolvable(requiredType)) {98 return fallback.resolveAll(requiredType);99 }100 return resolveAllAlias(requiredType, this::resolveAll)101 .orElseGet(HashMap::new);102 }103 return beans;104 }105 @Override106 public boolean isResolvable(String name) {107 return applicationContext.containsBean(name) || fallback.isResolvable(name);108 }109 @Override110 public boolean isResolvable(Class<?> type) {111 boolean canResolve = applicationContext.getBeanNamesForType(type).length > 0 || fallback.isResolvable(type);112 if (!canResolve) {113 Optional<TypeAliasResolver<?, ?>> aliasResolver = typeAliasResolvers.values().stream()114 .filter(resolver -> resolver.isAliasFor(type))115 .findFirst();116 if (aliasResolver.isEmpty()) {117 aliasResolver = TypeAliasResolver.lookup().values().stream()118 .filter(resolver -> resolver.isAliasFor(type))119 .findFirst();120 }121 if (aliasResolver.isPresent()) {122 canResolve = applicationContext.getBeanNamesForType(aliasResolver.get().getAliasType()).length > 0 || fallback.isResolvable(aliasResolver.get().getAliasType());123 }124 }125 return canResolve;126 }127 @Override128 public boolean isResolvable(String name, Class<?> type) {129 boolean canResolve = Arrays.asList(applicationContext.getBeanNamesForType(type)).contains(name) || fallback.isResolvable(name, type);130 if (!canResolve) {131 if (typeAliasResolvers.containsKey(name) && typeAliasResolvers.get(name).isAliasFor(type)) {132 canResolve = Arrays.asList(applicationContext.getBeanNamesForType(typeAliasResolvers.get(name).getAliasType())).contains(name) || fallback.isResolvable(name, typeAliasResolvers.get(name).getAliasType());133 }134 Optional<TypeAliasResolver<?, ?>> aliasResolver = typeAliasResolvers.values().stream()135 .filter(resolver -> resolver.isAliasFor(type))136 .findFirst();137 if (aliasResolver.isEmpty()) {138 aliasResolver = TypeAliasResolver.lookup().values().stream()139 .filter(resolver -> resolver.isAliasFor(type))140 .findFirst();141 }142 if (aliasResolver.isPresent()) {143 canResolve = Arrays.asList(applicationContext.getBeanNamesForType(aliasResolver.get().getAliasType())).contains(name) || fallback.isResolvable(name, aliasResolver.get().getAliasType());144 }145 }146 return canResolve;147 }148 /**149 * Specifies the fallback.150 * @param fallback151 */152 public SpringBeanReferenceResolver withFallback(ReferenceResolver fallback) {153 this.fallback = fallback;154 return this;155 }156 @Override157 public void bind(String name, Object value) {158 fallback.bind(name, value);159 }160 @SuppressWarnings("unchecked")161 private <T> Optional<T> resolveAlias(Class<T> source, Function<Class<?>, ?> supplier) {162 Optional<TypeAliasResolver<?, ?>> aliasResolver = typeAliasResolvers.values().stream()163 .filter(resolver -> resolver.isAliasFor(source))164 .findFirst();165 if (aliasResolver.isEmpty()) {166 aliasResolver = TypeAliasResolver.lookup().values().stream()167 .filter(resolver -> resolver.isAliasFor(source))168 .findFirst();169 }170 if (aliasResolver.isPresent()) {171 TypeAliasResolver<T, ?> resolver = (TypeAliasResolver<T, ?>) aliasResolver.get();172 try {173 return Optional.of(resolver.adapt(supplier.apply(resolver.getAliasType())));174 } catch (Exception e) {175 log.warn(String.format("Unable to resolve alias type %s for required source %s", resolver.getAliasType(), source));176 return Optional.empty();177 }178 }179 return Optional.empty();180 }181 @SuppressWarnings("unchecked")182 private <T> Optional<Map<String, T>> resolveAllAlias(Class<T> source, Function<Class<?>, Map<String, ?>> supplier) {183 Optional<TypeAliasResolver<?, ?>> aliasResolver = typeAliasResolvers.values().stream()184 .filter(resolver -> resolver.isAliasFor(source))185 .findFirst();186 if (aliasResolver.isEmpty()) {187 aliasResolver = TypeAliasResolver.lookup().values().stream()188 .filter(resolver -> resolver.isAliasFor(source))189 .findFirst();190 }191 if (aliasResolver.isPresent()) {192 TypeAliasResolver<T, ?> resolver = (TypeAliasResolver<T, ?>) aliasResolver.get();193 try {194 return Optional.of(supplier.apply(resolver.getAliasType())195 .entrySet()196 .stream()197 .collect(Collectors.toMap(Map.Entry::getKey, v -> resolver.adapt(v.getValue()))));198 } catch (Exception e) {199 log.warn(String.format("Unable to resolve alias type %s for required source %s", resolver.getAliasType(), source));200 return Optional.empty();201 }202 }203 return Optional.empty();204 }205 public void registerTypeAliasResolver(String name, TypeAliasResolver<?, ?> aliasResolver) {206 this.typeAliasResolvers.put(name, aliasResolver);207 }208 @Override209 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {210 this.applicationContext = applicationContext;211 }212 /**213 * Specifies the fallback....
Source:SpringBeanReferenceResolverTest.java
...29/**30 * @author Christoph Deppisch31 */32public class SpringBeanReferenceResolverTest extends UnitTestSupport {33 private SpringBeanReferenceResolver resolver;34 @Mock35 private org.springframework.oxm.Marshaller marshaller;36 @Mock37 private org.springframework.oxm.Unmarshaller unmarshaller;38 @BeforeMethod39 public void setup() {40 MockitoAnnotations.openMocks(this);41 resolver = new SpringBeanReferenceResolver(applicationContext);42 }43 @Test44 public void shouldResolveMarshallerAlias() {45 String marshallerName = "springOxmMarshaller";46 Assert.assertFalse(resolver.isResolvable(Marshaller.class));47 Assert.assertFalse(resolver.isResolvable(marshallerName, Marshaller.class));48 Assert.assertEquals(resolver.resolveAll(Marshaller.class).size(), 0L);49 resolver.bind(marshallerName, marshaller);50 Assert.assertTrue(resolver.isResolvable(Marshaller.class));51 Assert.assertTrue(resolver.isResolvable(marshallerName, Marshaller.class));52 Assert.assertEquals(resolver.resolve(Marshaller.class).getClass(), MarshallerAdapter.class);53 Assert.assertEquals(resolver.resolve(marshallerName, Marshaller.class).getClass(), MarshallerAdapter.class);54 Assert.assertEquals(resolver.resolveAll(Marshaller.class).size(), 1L);55 }56 @Test57 public void shouldResolveUnmarshallerAlias() {58 String unmarshallerName = "springOxmUnmarshaller";59 Assert.assertFalse(resolver.isResolvable(Unmarshaller.class));60 Assert.assertFalse(resolver.isResolvable(unmarshallerName, Unmarshaller.class));61 Assert.assertEquals(resolver.resolveAll(Unmarshaller.class).size(), 0L);62 resolver.bind(unmarshallerName, unmarshaller);63 Assert.assertTrue(resolver.isResolvable(Unmarshaller.class));64 Assert.assertTrue(resolver.isResolvable(unmarshallerName, Unmarshaller.class));65 Assert.assertEquals(resolver.resolve(Unmarshaller.class).getClass(), MarshallerAdapter.class);66 Assert.assertEquals(resolver.resolve(unmarshallerName, Unmarshaller.class).getClass(), MarshallerAdapter.class);67 Assert.assertEquals(resolver.resolveAll(Unmarshaller.class).size(), 1L);68 }69}...
resolve
Using AI Code Generation
1package com.consol.citrus;2import java.util.HashMap;3import java.util.Map;4import org.springframework.beans.factory.annotation.Autowired;5import org.springframework.context.ApplicationContext;6import org.springframework.context.annotation.Bean;7import org.springframework.context.annotation.Configuration;8import org.springframework.context.annotation.ImportResource;9import org.springframework.context.support.ClassPathXmlApplicationContext;10import com.consol.citrus.context.SpringBeanReferenceResolver;11public class Test4 {12 public static void main(String[] args) {13 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:config.xml");14 SpringBeanReferenceResolver resolver = new SpringBeanReferenceResolver(context);15 Map<String, Object> map = new HashMap<String, Object>();16 map.put("name", "test");17 map.put("age", 25);18 System.out.println(resolver.resolve("test", map));19 }20}21 <property name="name" value="${name}"/>22 <property name="age" value="${age}"/>23package com.consol.citrus;24public class TestBean {25 private String name;26 private int age;27 public String getName() {28 return name;29 }30 public void setName(String name) {31 this.name = name;32 }33 public int getAge() {34 return age;35 }36 public void setAge(int age) {37 this.age = age;38 }39 public String toString() {
resolve
Using AI Code Generation
1package com.consol.citrus;2import org.springframework.context.support.ClassPathXmlApplicationContext;3public class 4 {4 public static void main(String[] args) {5 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");6 SpringBeanReferenceResolver resolver = new SpringBeanReferenceResolver(context);7 System.out.println(resolver.resolve("testBean"));8 }9}
resolve
Using AI Code Generation
1import org.springframework.context.support.ClassPathXmlApplicationContext;2import org.springframework.context.ApplicationContext;3import org.springframework.context.ConfigurableApplicationContext;4import org.springframework.beans.factory.BeanFactory;5import org.springframework.beans.factory.ListableBeanFactory;6import org.springframework.beans.factory.BeanFactoryAware;7import org.springframework.beans.factory.BeanFactoryUtils;8import org.springframework.beans.factory.BeanFactory;9import org.springframework.beans.factory.ListableBeanFactory;10import org.springframework.beans.factory.BeanFactoryAware;11import org.springframework.beans.factory.BeanFactoryUtils;12import org.springframework.beans.factory.NoSuchBeanDefinitionException;13import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;14import org.springframework.beans.factory.support.DefaultListableBeanFactory;15import org.springframework.beans.factory.support.BeanDefinitionRegistry;16import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;17import org.springframework.beans.factory.config.BeanDefinition;18import org.springframework.beans.factory.support.RootBeanDefinition;19import org.springframework.beans.factory.support.BeanDefinitionBuilder;20import org.springframework.beans.factory.config.AutowireCapableBeanFactory;21import org.springframework.beans.factory.config.BeanDefinitionHolder;22import org.springframework.beans.factory.config.BeanPostProcessor;23import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;24import org.springframework.beans.factory.config.BeanDefinitionCustomizer;25import org.springframework.beans.factory.config.BeanDefinitionCustomizer;26import org.springframework.beans.factory.support.Bean
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!