/** * Define a Spring namespace definition to use. * @param definition the namespace definition */ public void xmlns(Map<String, String> definition) { if (!definition.isEmpty()) { for (Map.Entry<String,String> entry : definition.entrySet()) { String namespace = entry.getKey(); String uri = entry.getValue(); if (uri == null) { throw new IllegalArgumentException("Namespace definition must supply a non-null URI"); } NamespaceHandler namespaceHandler = this.xmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri); if (namespaceHandler == null) { throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(new DescriptiveResource(("Groovy"))))); } this.namespaces.put(namespace, uri); } } }
private GroovyDynamicElementReader createDynamicElementReader(String namespace) { XmlReaderContext readerContext = this.xmlBeanDefinitionReader.createReaderContext(new DescriptiveResource("Groovy")); BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, getEnvironment()); boolean decorating = (this.currentBeanDefinition != null); if (!decorating) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace); } return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) { @Override protected void afterInvocation() { if (!this.decorating) { currentBeanDefinition = null; } } }; }
@Test public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() { RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setResource(new DescriptiveResource("XML or something")); beanFactory.registerBeanDefinition("bar", rbd); beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class)); beanFactory.setAllowBeanDefinitionOverriding(false); ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor(); try { pp.postProcessBeanFactory(beanFactory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { assertTrue(ex.getMessage().contains("bar")); assertTrue(ex.getMessage().contains("SingletonBeanConfig")); assertTrue(ex.getMessage().contains(TestBean.class.getName())); } }
/** * Define a Spring XML namespace definition to use. * @param definition the namespace definition */ public void xmlns(Map<String, String> definition) { if (!definition.isEmpty()) { for (Map.Entry<String,String> entry : definition.entrySet()) { String namespace = entry.getKey(); String uri = entry.getValue(); if (uri == null) { throw new IllegalArgumentException("Namespace definition must supply a non-null URI"); } NamespaceHandler namespaceHandler = this.groovyDslXmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve( uri); if (namespaceHandler == null) { throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(new DescriptiveResource(("Groovy"))))); } this.namespaces.put(namespace, uri); } } }
private GroovyDynamicElementReader createDynamicElementReader(String namespace) { XmlReaderContext readerContext = this.groovyDslXmlBeanDefinitionReader.createReaderContext(new DescriptiveResource( "Groovy")); BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext); boolean decorating = (this.currentBeanDefinition != null); if (!decorating) { this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(namespace); } return new GroovyDynamicElementReader(namespace, this.namespaces, delegate, this.currentBeanDefinition, decorating) { @Override protected void afterInvocation() { if (!this.decorating) { currentBeanDefinition = null; } } }; }
@Test @SuppressWarnings("deprecation") public void velocityConfigurerWithCsvPathAndNonFileAccess() throws IOException, VelocityException { VelocityConfigurer vc = new VelocityConfigurer(); vc.setResourceLoaderPath("file:/mydir,file:/yourdir"); vc.setResourceLoader(new ResourceLoader() { @Override public Resource getResource(String location) { if ("file:/yourdir/test".equals(location)) { return new DescriptiveResource(""); } return new ByteArrayResource("test".getBytes(), "test"); } @Override public ClassLoader getClassLoader() { return getClass().getClassLoader(); } }); vc.setPreferFileSystemAccess(false); vc.afterPropertiesSet(); assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class)); VelocityEngine ve = vc.createVelocityEngine(); assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", Collections.emptyMap())); }
/** * Define a Spring XML namespace definition to use. * @param definition the namespace definition */ public void xmlns(Map<String, String> definition) { if (!definition.isEmpty()) { for (Map.Entry<String,String> entry : definition.entrySet()) { String namespace = entry.getKey(); String uri = entry.getValue(); if (uri == null) { throw new IllegalArgumentException("Namespace definition must supply a non-null URI"); } NamespaceHandler namespaceHandler = this.groovyDslXmlBeanDefinitionReader.getNamespaceHandlerResolver().resolve(uri); if (namespaceHandler == null) { throw new BeanDefinitionParsingException(new Problem("No namespace handler found for URI: " + uri, new Location(new DescriptiveResource(("Groovy"))))); } this.namespaces.put(namespace, uri); } } }
public void testVelocityConfigurerWithCsvPathAndNonFileAccess() throws IOException, VelocityException { VelocityConfigurer vc = new VelocityConfigurer(); vc.setResourceLoaderPath("file:/mydir,file:/yourdir"); vc.setResourceLoader(new ResourceLoader() { @Override public Resource getResource(String location) { if ("file:/yourdir/test".equals(location)) { return new DescriptiveResource(""); } return new ByteArrayResource("test".getBytes(), "test"); } @Override public ClassLoader getClassLoader() { return getClass().getClassLoader(); } }); vc.setPreferFileSystemAccess(false); vc.afterPropertiesSet(); assertThat(vc.createVelocityEngine(), instanceOf(VelocityEngine.class)); VelocityEngine ve = vc.createVelocityEngine(); assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", new HashMap())); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; }
/** * Create a new {@link ConfigurationClass} with the given name. * @param metadata the metadata for the underlying class to represent * @param beanName name of the {@code @Configuration} class bean * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(AnnotationMetadata metadata, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = metadata; this.resource = new DescriptiveResource(metadata.getClassName()); this.beanName = beanName; }
@Test public void postProcessorDoesNotOverrideRegularBeanDefinitions() { RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setResource(new DescriptiveResource("XML or something")); beanFactory.registerBeanDefinition("bar", rbd); beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class)); ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor(); pp.postProcessBeanFactory(beanFactory); beanFactory.getBean("foo", Foo.class); beanFactory.getBean("bar", TestBean.class); }
@Test public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() { RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setResource(new DescriptiveResource("XML or something")); BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy( new BeanDefinitionHolder(rbd, "bar"), beanFactory, true); beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition()); beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class)); ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor(); pp.postProcessBeanFactory(beanFactory); beanFactory.getBean("foo", Foo.class); beanFactory.getBean("bar", TestBean.class); }
@Test public void testWarn() throws Exception { Problem problem = new Problem("VGER", new Location(new DescriptiveResource("here")), null, new IllegalArgumentException()); Log log = mock(Log.class); FailFastProblemReporter reporter = new FailFastProblemReporter(); reporter.setLogger(log); reporter.warning(problem); verify(log).warn(any(), isA(IllegalArgumentException.class)); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @throws IllegalArgumentException if beanName is null (as of Spring 3.1.1) * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; this.importedBy = null; }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.getName()); this.beanName = beanName; }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @throws IllegalArgumentException if beanName is null (as of Spring 3.1.1) * @see ConfigurationClass#ConfigurationClass(Class, boolean) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; this.imported = false; }
@Override public Resource[] convert(String[] source) { Resource[] result = new Resource[source.length]; for (int i = 0; i < source.length; i++) { result[i] = new DescriptiveResource(source[i].substring(1)); } return result; }
@Test public void getLoadedConfigurationFiles_normal_propertyFilesReturned() { setUpBeanRetrieval(ConfigurationPropertyPlaceholderConfigurer.class, mockPropertyConfigurer); configurationMBean.setApplicationContext(mockApplicationContext); Resource resourceA = new DescriptiveResource("a"); Resource resourceB = new DescriptiveResource("b"); when(mockPropertyConfigurer.getLoadedPropertyFiles()).thenReturn(asList(resourceA, resourceB)); List<String> loadedPropertyFiles = configurationMBean.getLoadedConfigurationFiles(); assertThat(loadedPropertyFiles, contains("a", "b")); }
@Test public void getLoadedSpringDefinitionFiles_normal_modulesReturned() throws MalformedURLException { setUpBeanRetrieval(SpringContextLoader.class, mockContextLoader); configurationMBean.setApplicationContext(mockApplicationContext); Resource[] resources = new Resource[]{new DescriptiveResource("a"), new DescriptiveResource("b")}; when(mockResourceFinder.findContextDefinitions()).thenReturn(resources); assertThat(configurationMBean.getLoadedSpringDefinitionFiles(), contains("a", "b")); }
@Test public void getLoadedSpringDefinitionFiles_contextLoadingSubSystemNotLoaded_emptyListReturned() throws MalformedURLException { setUpMissingBean(SpringContextLoader.class); configurationMBean.setApplicationContext(mockApplicationContext); Resource[] resources = new Resource[]{new DescriptiveResource("a"), new DescriptiveResource("b")}; when(mockResourceFinder.findContextDefinitions()).thenReturn(resources); assertThat(configurationMBean.getLoadedSpringDefinitionFiles(), is(empty())); }
@Test public void getBeansInContext_normal_sortedAndFilteredBeanListReturned() { when(mockApplicationContext.getBeanDefinitionNames()).thenReturn(new String[]{"b", "a", "c"}); when(mockApplicationContext.getBean("a")).thenReturn(new ContextTestBean1()); when(mockApplicationContext.getBean("b")).thenReturn(new ContextTestBean2()); when(mockApplicationContext.getBean("c")).thenReturn(new DescriptiveResource("c")); // expected to be filtered configurationMBean.setApplicationContext(mockApplicationContext); assertThat(configurationMBean.getBeansInContext(), contains( format("a (%s)", ContextTestBean1.class.getName()), format("b (%s)", ContextTestBean2.class.getName()) )); }
@Override public Resource[] convert(String[] source) { return Arrays.stream(source).map(s -> s.substring(1)).map(DescriptiveResource::new).toArray(Resource[]::new); }
@Test(expected=BeanDefinitionParsingException.class) public void testError() throws Exception { FailFastProblemReporter reporter = new FailFastProblemReporter(); reporter.error(new Problem("VGER", new Location(new DescriptiveResource("here")), null, new IllegalArgumentException())); }
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { RootBeanDefinition beanDefinition = new RootBeanDefinition(FunctionFactoryBean.class); beanDefinition.setSource(new DescriptiveResource("Function")); registry.registerBeanDefinition("function", beanDefinition); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @throws IllegalArgumentException if beanName is null (as of Spring 3.1.1) * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; }
/** * Create a new {@link ConfigurationClass} representing a class that was imported * using the {@link Import} annotation or automatically processed as a nested * configuration class (if imported is {@code true}). * @param clazz the underlying {@link Class} to represent * @param importedBy the configuration class importing this one or {@code null} * @since 3.1.1 */ public ConfigurationClass(Class<?> clazz, ConfigurationClass importedBy) { this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.importedBy.add(importedBy); }
/** * Set a description of the resource that this bean definition * came from (for the purpose of showing context in case of errors). */ public void setResourceDescription(String resourceDescription) { this.resource = new DescriptiveResource(resourceDescription); }
/** * Load bean definitions from the specified XML file. * @param inputSource the SAX InputSource to read from * @param resourceDescription a description of the resource * (can be {@code null} or empty) * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ public int loadBeanDefinitions(InputSource inputSource, String resourceDescription) throws BeanDefinitionStoreException { return doLoadBeanDefinitions(inputSource, new DescriptiveResource(resourceDescription)); }
/** * Create a new {@link ConfigurationClass} representing a class that was imported * using the {@link Import} annotation or automatically processed as a nested * configuration class (if imported is {@code true}). * @param clazz the underlying {@link Class} to represent * @param importedBy the configuration class importing this one or {@code null} * @since 3.1.1 */ public ConfigurationClass(Class<?> clazz, ConfigurationClass importedBy) { this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.importedBy = importedBy; }
/** * Create a new {@link ConfigurationClass} representing a class that was imported * using the {@link Import} annotation or automatically processed as a nested * configuration class (if imported is {@code true}). * @param clazz the underlying {@link Class} to represent * @param importedBy the configuration class importing this one or {@code null} * @since 3.1.1 */ public ConfigurationClass(Class<?> clazz, ConfigurationClass importedBy) { this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.getName()); this.importedBy.add(importedBy); }
/** * Create a new {@link ConfigurationClass} representing a class that was imported * using the {@link Import} annotation or automatically processed as a nested * configuration class (if imported is {@code true}). * @param clazz the underlying {@link Class} to represent * @param imported whether the given configuration class is being imported * @since 3.1.1 */ public ConfigurationClass(Class<?> clazz, boolean imported) { this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.imported = imported; }