diff --git a/.travis.yml b/.travis.yml index 393b7bb7..3951c9e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,11 @@ env: - secure: njJrgrBWrEHK86vEK8UlmM5SxlptsyU7ghWOPsQl5Dr0WvseZWtT/7A9bDPmcNxp2EJBx4xt0BAIDnhnqcHwdKDrYjpHPabneDipEchdm2p+vI96lqb1zZ5Du+xvQGzZlXPhf1VocWSFXRACAJ2+Nky6nbTrhKCr101kBA8oSe4= language: java jdk: -- openjdk6 - openjdk7 - oraclejdk7 +- oraclejdk8 after_success: -- mvn clean cobertura:cobertura coveralls:cobertura +- mvn clean cobertura:cobertura coveralls:report - echo "sonatype-nexus-snapshots\${env.REPO_USER}\${env.REPO_PASSWORD}" > ~/settings.xml - mvn deploy --settings ~/settings.xml diff --git a/README.md b/README.md index 9429d701..0ac30501 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,13 @@ All current releases are compiled with target JDK 1.6. Starting with Constretto [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.constretto/constretto-core/badge.svg)](http://mvnrepository.com/artifact/org.constretto/constretto-core) [![Coverage Status](https://img.shields.io/coveralls/constretto/constretto-core.svg)](https://coveralls.io/r/constretto/constretto-core) +## What's new in 2.2.3 +* fix issue affecting singleton in constretto-spring contributed by @nicolasyanncouturier in PR #59 +* add support for "file://"-urls in the FileResource class. Contributed by @kenglxn in PR #58 +* streamline build by removing the build dependency on [ApacheDS](https://directory.apache.org/apacheds/) in favour of the [embedded-ldap-junit] (https://github.com/zapodot/embedded-ldap-junit) library. +* stop building with JDK6 (though still providing JDK6-compatible bytecode) +* update deps: JUnit 4.12 (was 4.11), ini4j 0.5.4 (was 0.5.2) and snakeyml 1.16 (was 1.14) + ## What's new in 2.2.2 * Support for YAML store contributed by [hamnis](//github.com/hamnis) - pull request #48 * Change scope of Constretto-test in the Constretto Spring module to "test" - pull request #47 diff --git a/constretto-api/pom.xml b/constretto-api/pom.xml index e68e7544..1248cb4c 100644 --- a/constretto-api/pom.xml +++ b/constretto-api/pom.xml @@ -11,7 +11,7 @@ constretto org.constretto - 2.2.3-SNAPSHOT + 2.2.4-SNAPSHOT 4.0.0 constretto-api diff --git a/constretto-core/pom.xml b/constretto-core/pom.xml index 1d5d4219..b921e491 100644 --- a/constretto-core/pom.xml +++ b/constretto-core/pom.xml @@ -11,7 +11,7 @@ constretto org.constretto - 2.2.3-SNAPSHOT + 2.2.4-SNAPSHOT 4.0.0 constretto-core @@ -30,6 +30,12 @@ junit junit + + org.zapodot + embedded-ldap-junit + ${embedded-ldap-junit.version} + test + org.slf4j jcl-over-slf4j @@ -70,12 +76,6 @@ org.mockito mockito-all - - org.apache.directory.server - apacheds-test-framework - ${apacheds.version} - test - org.slf4j diff --git a/constretto-core/src/main/java/org/constretto/model/FileResource.java b/constretto-core/src/main/java/org/constretto/model/FileResource.java index f9c1058b..e2257fc6 100644 --- a/constretto-core/src/main/java/org/constretto/model/FileResource.java +++ b/constretto-core/src/main/java/org/constretto/model/FileResource.java @@ -16,6 +16,8 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; /** * @author Kaare Nilsen @@ -43,10 +45,19 @@ public InputStream getInputStream() { private String extractFileNameFromFileResource(String path) { String fileName; if (path.startsWith(FILE_PREFIX)) { - fileName = path.substring(FILE_PREFIX.length(), path.length()); + fileName = decode(path.substring(FILE_PREFIX.length(), path.length())); } else { fileName = path; } return fileName; } + + private String decode(String path) { + try { + return URLDecoder.decode(path, "UTF-8"); + } catch (UnsupportedEncodingException ignored) { + } + return path; + } + } diff --git a/constretto-core/src/main/java/org/constretto/model/Resource.java b/constretto-core/src/main/java/org/constretto/model/Resource.java index 861224c2..de7ede53 100644 --- a/constretto-core/src/main/java/org/constretto/model/Resource.java +++ b/constretto-core/src/main/java/org/constretto/model/Resource.java @@ -52,4 +52,8 @@ public String toString() { sb.append('}'); return sb.toString(); } + + public String getPath(){ + return this.path; + } } diff --git a/constretto-core/src/test/java/org/constretto/internal/store/ldap/LdapConfigurationStoreEmbeddedLdapTest.java b/constretto-core/src/test/java/org/constretto/internal/store/ldap/LdapConfigurationStoreEmbeddedLdapTest.java index e118dc97..bbad1179 100644 --- a/constretto-core/src/test/java/org/constretto/internal/store/ldap/LdapConfigurationStoreEmbeddedLdapTest.java +++ b/constretto-core/src/test/java/org/constretto/internal/store/ldap/LdapConfigurationStoreEmbeddedLdapTest.java @@ -1,27 +1,17 @@ package org.constretto.internal.store.ldap; -import com.sun.jndi.ldap.DefaultResponseControlFactory; -import com.sun.jndi.ldap.LdapCtxFactory; -import org.apache.directory.server.annotations.CreateLdapServer; -import org.apache.directory.server.annotations.CreateTransport; -import org.apache.directory.server.core.annotations.ApplyLdifFiles; -import org.apache.directory.server.core.annotations.CreateDS; -import org.apache.directory.server.core.annotations.CreatePartition; -import org.apache.directory.server.core.integ.AbstractLdapTestUnit; -import org.apache.directory.server.core.integ.FrameworkRunner; import org.constretto.ConstrettoBuilder; import org.constretto.ConstrettoConfiguration; import org.constretto.annotation.Configuration; import org.constretto.model.TaggedPropertySet; +import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; +import org.zapodot.junit.ldap.EmbeddedLdapRule; +import org.zapodot.junit.ldap.EmbeddedLdapRuleBuilder; -import javax.naming.Context; -import javax.naming.directory.InitialDirContext; -import javax.naming.ldap.LdapContext; +import javax.naming.directory.DirContext; import java.util.Arrays; import java.util.Collection; -import java.util.Hashtable; import java.util.List; import static org.junit.Assert.assertEquals; @@ -30,16 +20,14 @@ /** * @author zapodot at gmail dot com */ -@RunWith(FrameworkRunner.class) -@CreateLdapServer( - transports = { - @CreateTransport(protocol = "LDAP", port = LdapConfigurationStoreEmbeddedLdapTest.LDAP_PORT) - } -) -@CreateDS( name = "LdapConfigurationStoreEmbeddedLdapTest", partitions = @CreatePartition(name = "constretto", suffix = "dc=constretto,dc=org")) -@ApplyLdifFiles("constretto.ldif") -public class LdapConfigurationStoreEmbeddedLdapTest extends AbstractLdapTestUnit { - +public class LdapConfigurationStoreEmbeddedLdapTest { + + @Rule + public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance() + .bindingToPort(LDAP_PORT) + .usingDomainDsn("dc=constretto,dc=org") + .importingLdifs("constretto.ldif") + .build(); public static final int LDAP_PORT = 27389; public static class ConfigurableType { @@ -54,16 +42,14 @@ public static class ConfigurableType { @Test public void testParseConfigurationUsingAddDsn() throws Exception { - Hashtable ldapEnvironment = createLdapEnvironment(); - - final InitialDirContext dirContext = new InitialDirContext(ldapEnvironment); + final DirContext dirContext = embeddedLdapRule.dirContext(); final LdapConfigurationStore configurationStore = LdapConfigurationStoreBuilder.usingDirContext(dirContext) - .addDsn("cn=Kaare Nilsen,dc=constretto,dc=org") - .addDsnWithKey("sidekick", "cn=Jon-Anders Teigen,dc=constretto,dc=org") - .done(); + .addDsn("cn=Kaare Nilsen,dc=constretto,dc=org") + .addDsnWithKey("sidekick", + "cn=Jon-Anders Teigen,dc=constretto,dc=org") + .done(); final Collection propertySets = configurationStore.parseConfiguration(); assertEquals(1, propertySets.size()); - dirContext.close(); ConstrettoConfiguration constrettoConfiguration = createConfiguration(configurationStore); final ConfigurableType configurationObject = constrettoConfiguration.as(ConfigurableType.class); assertTrue(configurationObject.names.containsAll(Arrays.asList("Kaare Nilsen", "Kåre Nilsen"))); @@ -73,13 +59,13 @@ public void testParseConfigurationUsingAddDsn() throws Exception { @Test public void testDsnMultiValue() throws Exception { - final InitialDirContext initialDirContext = new InitialDirContext(createLdapEnvironment()); + final DirContext initialDirContext = embeddedLdapRule.dirContext(); final ConstrettoConfiguration configuration = new ConstrettoBuilder(false) .createLdapConfigurationStore(initialDirContext) - .addDsn("cn=role_developer,ou=groups,dc=constretto,dc=org") + .addDsn("cn=role_developer,ou=groups,dc=constretto,dc=org") .done() .getConfiguration(); - final List members = configuration.evaluateToList(String.class, "uniquemember"); + final List members = configuration.evaluateToList(String.class, "uniqueMember"); assertEquals(2, members.size()); @@ -87,7 +73,8 @@ public void testDsnMultiValue() throws Exception { @Test public void testParseConfigurationUsingSearch() throws Exception { - final InitialDirContext initialDirContext = new InitialDirContext(createLdapEnvironment()); + + final DirContext initialDirContext = embeddedLdapRule.dirContext(); final ConstrettoConfiguration configuration = new ConstrettoBuilder(false) .createLdapConfigurationStore(initialDirContext) .addUsingSearch( @@ -96,8 +83,8 @@ public void testParseConfigurationUsingSearch() throws Exception { "uid") .done() .getConfiguration(); - assertTrue(configuration.evaluateToList(String.class, "kaarenilsen.cn").containsAll(Arrays.asList("Kaare Nilsen", "Kåre Nilsen"))); - initialDirContext.close(); + assertTrue(configuration.evaluateToList(String.class, "kaarenilsen.cn") + .containsAll(Arrays.asList("Kaare Nilsen", "Kåre Nilsen"))); } @@ -106,15 +93,4 @@ private ConstrettoConfiguration createConfiguration(LdapConfigurationStore confi configurationStore).getConfiguration(); } - private Hashtable createLdapEnvironment() { - Hashtable ldapEnvironment = new Hashtable(); - ldapEnvironment.put(LdapContext.CONTROL_FACTORIES, DefaultResponseControlFactory.class.getName()); - ldapEnvironment.put(Context.PROVIDER_URL, String.format("ldap://localhost:%1$s", LDAP_PORT)); - ldapEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, LdapCtxFactory.class.getName()); - ldapEnvironment.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); - ldapEnvironment.put(Context.SECURITY_CREDENTIALS, "secret"); - ldapEnvironment.put(Context.SECURITY_PROTOCOL, "simple"); - return ldapEnvironment; - } - } diff --git a/constretto-core/src/test/java/org/constretto/model/ClassPathResourceTest.java b/constretto-core/src/test/java/org/constretto/model/ClassPathResourceTest.java index 728e40d3..29b1c82d 100644 --- a/constretto-core/src/test/java/org/constretto/model/ClassPathResourceTest.java +++ b/constretto-core/src/test/java/org/constretto/model/ClassPathResourceTest.java @@ -33,6 +33,11 @@ public void testOpenNoneExistingClassPathResource() throws Exception { public void testToString() throws Exception { final ClassPathResource classPathResource = new ClassPathResource(NON_EXISITING_CLASSPATH_RESOURCE); assertEquals("ClassPathResource{path='" + NON_EXISITING_CLASSPATH_RESOURCE + "'}", classPathResource.toString()); + } + @Test + public void testGetPath() throws Exception { + final ClassPathResource classPathResource = new ClassPathResource(NON_EXISITING_CLASSPATH_RESOURCE); + assertEquals(NON_EXISITING_CLASSPATH_RESOURCE, classPathResource.getPath()); } } diff --git a/constretto-core/src/test/java/org/constretto/model/FileResourceTest.java b/constretto-core/src/test/java/org/constretto/model/FileResourceTest.java index b8333550..2ab52f75 100644 --- a/constretto-core/src/test/java/org/constretto/model/FileResourceTest.java +++ b/constretto-core/src/test/java/org/constretto/model/FileResourceTest.java @@ -57,6 +57,26 @@ public void testOpenFileResourceForFilePrefixOnlyPath() throws Exception { public void testToString() throws Exception { final FileResource fileResource = new FileResource("file:src/test/resources/cache1.ini"); assertEquals("FileResource{path='file:src/test/resources/cache1.ini'}", fileResource.toString()); + } + + @Test + public void testGetPath() throws Exception { + String testPath = "file:src/test/resources/cache1.ini"; + final FileResource fileResource = new FileResource(testPath); + assertEquals(fileResource.getPath(), testPath); + } + /** + * If file name starts with file: chances are it is a file url. + * Constretto should decode this url since it uses new File(String) which does not support + * url encoding. + */ + @Test + public void testWithSpaces() throws Exception { + File file = new File("src/test/resources/dir with spaces/test.properties"); + + String string = file.toURI().toURL().toString(); + final FileResource existing = new FileResource(string); + assertTrue(existing.exists()); } } diff --git a/constretto-core/src/test/java/org/constretto/model/UrlResourceIntegrationTest.java b/constretto-core/src/test/java/org/constretto/model/UrlResourceIntegrationTest.java new file mode 100644 index 00000000..45666765 --- /dev/null +++ b/constretto-core/src/test/java/org/constretto/model/UrlResourceIntegrationTest.java @@ -0,0 +1,23 @@ +package org.constretto.model; + +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * These tests will fail if run behind a enterprise proxy + * + * Created by zapodot on 01.12.2015. + */ +public class UrlResourceIntegrationTest { + + + @Test + @Ignore("Disabled for now. Set up test container that makes it posible to test without requiring access to the Internet") + public void validUrlsThatDoExistShouldWork() throws Exception { + final UrlResource urlResource = new UrlResource("https://github.com"); + assertTrue(urlResource.exists()); + } + +} diff --git a/constretto-core/src/test/java/org/constretto/model/UrlResourceTest.java b/constretto-core/src/test/java/org/constretto/model/UrlResourceTest.java index cb7cffd5..879ad11e 100644 --- a/constretto-core/src/test/java/org/constretto/model/UrlResourceTest.java +++ b/constretto-core/src/test/java/org/constretto/model/UrlResourceTest.java @@ -35,17 +35,18 @@ public void validUrlsThatDoNotExistShouldWork() throws Exception { assertFalse(urlResource.exists()); } - @Test - public void validUrlsThatDoExistShouldWork() throws Exception { - final UrlResource urlResource = new UrlResource("http://vg.no"); - assertTrue(urlResource.exists()); - } @Test public void testToString() throws Exception { final UrlResource urlResource = new UrlResource("http://vg.no"); assertEquals("UrlResource{path='http://vg.no'}", urlResource.toString()); + } + + @Test + public void testGetPath() throws Exception { + final UrlResource urlResource = new UrlResource("http://vg.no"); + assertEquals(urlResource.getPath(), "http://vg.no"); } } diff --git a/constretto-core/src/test/resources/dir with spaces/test.properties b/constretto-core/src/test/resources/dir with spaces/test.properties new file mode 100644 index 00000000..b090a87e --- /dev/null +++ b/constretto-core/src/test/resources/dir with spaces/test.properties @@ -0,0 +1,26 @@ +# +# Copyright 2008 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +somedb.username=user0 + +@production.somedb.username=user1 + +@systest.somedb.username=user2 + +datasources.customer.password=password + +url.child=#{base-url}/child +@production.base-url=http://constretto.org \ No newline at end of file diff --git a/constretto-spring/pom.xml b/constretto-spring/pom.xml index f173b22a..3245438f 100644 --- a/constretto-spring/pom.xml +++ b/constretto-spring/pom.xml @@ -11,7 +11,7 @@ constretto org.constretto - 2.2.3-SNAPSHOT + 2.2.4-SNAPSHOT 4.0.0 constretto-spring diff --git a/constretto-spring/src/main/java/org/constretto/spring/ConfigurationAnnotationConfigurer.java b/constretto-spring/src/main/java/org/constretto/spring/ConfigurationAnnotationConfigurer.java index 994f3cd6..04a7de7c 100644 --- a/constretto-spring/src/main/java/org/constretto/spring/ConfigurationAnnotationConfigurer.java +++ b/constretto-spring/src/main/java/org/constretto/spring/ConfigurationAnnotationConfigurer.java @@ -25,6 +25,9 @@ import org.constretto.spring.resolver.AssemblyContextResolver; import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.*; import java.lang.reflect.Constructor; @@ -50,11 +53,12 @@ * @see org.constretto.spring.annotation.Environment */ public class ConfigurationAnnotationConfigurer extends InstantiationAwareBeanPostProcessorAdapter implements - BeanFactoryPostProcessor { + BeanFactoryPostProcessor, BeanFactoryAware { private ConstrettoConfiguration configuration; private AssemblyContextResolver assemblyContextResolver; private Map, Constructor> configurableConstructorCache = Collections.synchronizedMap(new HashMap, Constructor>()); private final static Object constructorCacheLockObject = new Object(); + private BeanFactory beanFactory; public ConfigurationAnnotationConfigurer(ConstrettoConfiguration configuration, AssemblyContextResolver assemblyContextResolver) { @@ -76,8 +80,12 @@ public Constructor[] determineCandidateConstructors(final Class beanClass, @Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { - injectConfiguration(bean); - injectEnvironment(bean); + try { + if (beanFactory != null && beanFactory.isSingleton(beanName)) { + injectConfiguration(bean); + injectEnvironment(bean); + } + } catch (NoSuchBeanDefinitionException e) {} return true; } @@ -144,4 +152,9 @@ private Constructor resolveConfigurableConstructor(Class beanClass) { return constructorsWithConfigureAnnotation[0]; } } + + @Override + public void setBeanFactory(final BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } } diff --git a/constretto-spring/src/test/java/org/constretto/spring/configuration/EnvironmentAnnotatedFieldTest.java b/constretto-spring/src/test/java/org/constretto/spring/configuration/EnvironmentAnnotatedFieldTest.java index 790db05b..a22f54ed 100644 --- a/constretto-spring/src/test/java/org/constretto/spring/configuration/EnvironmentAnnotatedFieldTest.java +++ b/constretto-spring/src/test/java/org/constretto/spring/configuration/EnvironmentAnnotatedFieldTest.java @@ -16,6 +16,9 @@ import org.constretto.spring.assembly.helper.AlwaysDevelopmentEnvironmentResolver; import org.junit.Assert; import org.junit.Test; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import java.util.List; @@ -26,11 +29,18 @@ public class EnvironmentAnnotatedFieldTest { @Test public void givenClassWithEnvironmentAnnotatedPropertyThenInjectEnvironment() throws Exception { - TestClazz testClazz = new TestClazz(); ConfigurationAnnotationConfigurer annotationConfigurer = new ConfigurationAnnotationConfigurer( new DefaultConstrettoConfiguration(null), new AlwaysDevelopmentEnvironmentResolver()); + + TestClazz testClazz = new TestClazz(); + annotationConfigurer.setBeanFactory(new TestBeanFactory(true)); annotationConfigurer.postProcessAfterInstantiation(testClazz, "testBean"); Assert.assertTrue(testClazz.getEnvironments().contains("development")); + + TestClazz notSingletonTestClazz = new TestClazz(); + annotationConfigurer.setBeanFactory(new TestBeanFactory(false)); + annotationConfigurer.postProcessAfterInstantiation(notSingletonTestClazz, "testBean"); + Assert.assertNull(notSingletonTestClazz.getEnvironments()); } private class TestClazz { @@ -42,4 +52,63 @@ public List getEnvironments() { return environments; } } + + private class TestBeanFactory implements BeanFactory { + + private boolean singleton; + + TestBeanFactory(boolean singleton) { + this.singleton = singleton; + } + + @Override + public Object getBean(final String name) throws BeansException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public T getBean(final String name, final Class requiredType) throws BeansException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public T getBean(final Class requiredType) throws BeansException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public Object getBean(final String name, final Object... args) throws BeansException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public boolean containsBean(final String name) { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public boolean isSingleton(final String name) throws NoSuchBeanDefinitionException { + return singleton; + } + + @Override + public boolean isPrototype(final String name) throws NoSuchBeanDefinitionException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public boolean isTypeMatch(final String name, final Class targetType) throws NoSuchBeanDefinitionException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public Class getType(final String name) throws NoSuchBeanDefinitionException { + throw new UnsupportedOperationException("Not implemented"); + } + + @Override + public String[] getAliases(final String name) { + throw new UnsupportedOperationException("Not implemented"); + } + } } diff --git a/constretto-spring/src/test/java/org/constretto/spring/javaconfig/BasicConstrettoConfigurationTest.java b/constretto-spring/src/test/java/org/constretto/spring/javaconfig/BasicConstrettoConfigurationTest.java index 526bd7ca..c0b63963 100644 --- a/constretto-spring/src/test/java/org/constretto/spring/javaconfig/BasicConstrettoConfigurationTest.java +++ b/constretto-spring/src/test/java/org/constretto/spring/javaconfig/BasicConstrettoConfigurationTest.java @@ -5,11 +5,17 @@ import org.constretto.model.Resource; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; /** * @author zapodot at gmail dot com @@ -19,21 +25,23 @@ public class BasicConstrettoConfigurationTest { public static final String DEFAULT_VALUE = "Default"; - @Configuration(required = true) - private String key1; - @Value("${key1}") - private String key1AsValue; + @Autowired + private TestBean testBean; - @Value("${nothere:" + DEFAULT_VALUE + "}") - private String defaultValue; + @Autowired + @Qualifier("requestScopedBean") + private TestBean requestScopedTestBean; @Test public void testKeyConfigured() throws Exception { final String expectedValue = "value1"; - assertEquals(expectedValue, key1); - assertEquals(expectedValue, key1AsValue); - assertEquals(DEFAULT_VALUE, defaultValue); + assertEquals(expectedValue, testBean.key1); + assertEquals(expectedValue, testBean.key1AsValue); + assertEquals(DEFAULT_VALUE, testBean.defaultValue); + assertNull(requestScopedTestBean.key1); + assertNull(requestScopedTestBean.key1AsValue); + assertNull(requestScopedTestBean.defaultValue); } @org.springframework.context.annotation.Configuration @@ -46,7 +54,29 @@ public org.constretto.ConstrettoConfiguration constrettoConfiguration() { .done() .getConfiguration(); } + + @Bean + public TestBean testBean() { + return new TestBean(); + } + + @Bean(name = "requestScopedBean") + @Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS) + public TestBean requestScopedBean() { + return new TestBean(); + } + } + public static class TestBean { + @Configuration(required = true) + private String key1; + + @Value("${key1}") + private String key1AsValue; + + @Value("${nothere:" + DEFAULT_VALUE + "}") + private String defaultValue; + } } diff --git a/constretto-test/pom.xml b/constretto-test/pom.xml index e6e58005..5f95655c 100644 --- a/constretto-test/pom.xml +++ b/constretto-test/pom.xml @@ -10,7 +10,7 @@ constretto org.constretto - 2.2.3-SNAPSHOT + 2.2.4-SNAPSHOT 4.0.0 constretto-test @@ -33,6 +33,7 @@ org.springframework spring-test + compile true diff --git a/constretto-test/src/test/java/org/constretto/test/ConstrettoRuleEnvironmentTest.java b/constretto-test/src/test/java/org/constretto/test/ConstrettoRuleEnvironmentTest.java index db646d94..c94cd067 100644 --- a/constretto-test/src/test/java/org/constretto/test/ConstrettoRuleEnvironmentTest.java +++ b/constretto-test/src/test/java/org/constretto/test/ConstrettoRuleEnvironmentTest.java @@ -1,5 +1,7 @@ package org.constretto.test; +import java.util.List; + import org.constretto.ConstrettoBuilder; import org.constretto.ConstrettoConfiguration; import org.constretto.spring.ConfigurationAnnotationConfigurer; @@ -8,13 +10,17 @@ import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.test.context.ContextConfiguration; -import java.util.List; - import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNull; /** * This source code is the property of NextGenTel AS @@ -28,30 +34,61 @@ public class ConstrettoRuleEnvironmentTest { public static final String ENVIRONMENT_VALUE = "junit"; + @Autowired + private TestBean testBean; + + @Autowired + @Qualifier("requestScopedBean") + private TestBean requestScopedBean; + @Configuration public static class TestConfiguration { + @Autowired + private BeanFactory beanFactory; + @Bean - public static ConstrettoConfiguration constrettoConfiguration() { + public ConstrettoConfiguration constrettoConfiguration() { return new ConstrettoBuilder(true).getConfiguration(); } @Bean - public static ConfigurationAnnotationConfigurer configurationAnnotationConfigurer(final ConstrettoConfiguration configuration) { - return new ConfigurationAnnotationConfigurer(configuration, new DefaultAssemblyContextResolver()); + public ConfigurationAnnotationConfigurer configurationAnnotationConfigurer(final ConstrettoConfiguration configuration) { + final ConfigurationAnnotationConfigurer configurer = new ConfigurationAnnotationConfigurer(configuration, + new DefaultAssemblyContextResolver()); + configurer.setBeanFactory(beanFactory); + return configurer; + } + + @Bean + TestBean testBean() { + return new TestBean(); + } + + @Bean(name = "requestScopedBean") + @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) + TestBean requestScopedBean() { + return new TestBean(); } } - @Environment - private List injectedEnvironment; - @ClassRule - public static ConstrettoRule constrettoRule = new ConstrettoRule(); @Test public void testApplyEnvironment() throws Exception { - assertArrayEquals(new String[]{ENVIRONMENT_VALUE}, injectedEnvironment.toArray(new String[1])); + assertArrayEquals(new String[]{ENVIRONMENT_VALUE}, testBean.injectedEnvironment.toArray(new String[1])); + + assertNull(requestScopedBean.injectedEnvironment); + } + + static class TestBean { + + @Environment + private List injectedEnvironment; + + @ClassRule + public static ConstrettoRule constrettoRule = new ConstrettoRule(); } } diff --git a/constretto-test/src/test/java/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest.java b/constretto-test/src/test/java/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest.java index 22217463..8a706cd0 100644 --- a/constretto-test/src/test/java/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest.java +++ b/constretto-test/src/test/java/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest.java @@ -10,6 +10,9 @@ */ package org.constretto.test; +import java.util.ArrayList; +import java.util.List; + import org.constretto.annotation.Tags; import org.constretto.spring.annotation.Environment; import org.constretto.test.helper.Color; @@ -17,11 +20,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; -import java.util.ArrayList; -import java.util.List; - /** * @author Kaare Nilsen * @author Thor Åge Eldby @@ -32,19 +33,30 @@ @ContextConfiguration public class ConstrettoSpringJUnit4ClassRunnerTest { - @Tags - List currentEnvironment; + @Autowired @Qualifier("testBean") + TestBean testBean; - @Autowired - Color color; + @Autowired @Qualifier("prototypeScopedTestBean") + TestBean prototypeScopedTestBean; @Test public void givenEnvironmentAnnotationOnTestClassWhenRunningTestThenConstrettoKnowsEnvironment() { List expected = new ArrayList() {{ add("springjunit"); }}; - Assert.assertArrayEquals(expected.toArray(new String[0]), currentEnvironment.toArray(new String[0])); - Assert.assertEquals("green", color.name()); + Assert.assertArrayEquals(expected.toArray(new String[0]), testBean.currentEnvironment.toArray(new String[0])); + Assert.assertEquals("green", testBean.color.name()); + Assert.assertNull(prototypeScopedTestBean.currentEnvironment); + Assert.assertEquals("green", prototypeScopedTestBean.color.name()); + } + + static class TestBean { + + @Autowired + Color color; + + @Tags + List currentEnvironment; } } diff --git a/constretto-test/src/test/resources/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest-context.xml b/constretto-test/src/test/resources/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest-context.xml index 712415c6..29e4abc3 100644 --- a/constretto-test/src/test/resources/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest-context.xml +++ b/constretto-test/src/test/resources/org/constretto/test/ConstrettoSpringJUnit4ClassRunnerTest-context.xml @@ -18,4 +18,8 @@ + + + + diff --git a/pom.xml b/pom.xml index 43e4bab1..cc808294 100644 --- a/pom.xml +++ b/pom.xml @@ -7,11 +7,12 @@ either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - + 4.0.0 org.constretto constretto - 2.2.3-SNAPSHOT + 2.2.4-SNAPSHOT Constretto :: Parent - ${project.version} 2008 http://constretto.github.io @@ -35,8 +36,8 @@ scm:git:git@github.com:constretto/constretto-core.git scm:git:git@github.com:constretto/constretto-core.git http://github.com/constretto/constretto-core - HEAD - + HEAD + github @@ -101,19 +102,19 @@ 1.9.5 1.0 1.7.4 - 0.5.2 - 1.14 + 0.5.4 + 1.16 2.6 1.9.1 2.2.4 1.9.2 2.5.2 3.2.11.RELEASE - 4.11 + 4.12 1.3.1.RELEASE - 1.7.6 - 1.5.7 - 1.6.0 + 1.7.13 + 1.14.0 + 0.5.2 2.9.1 2.3 1.5 @@ -121,9 +122,44 @@ 2.6 1.3.1 2.17 + 1.6.6 + 4.1.0 + + release + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + ${nexus-staging-maven-plugin.version} + true + + sonatype-nexus-staging + https://oss.sonatype.org/ + true + + + + + + sonatype-oss-release @@ -240,6 +276,7 @@ org.springframework spring-test ${spring.version} + test org.springframework @@ -324,6 +361,12 @@ UTF-8 + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + org.apache.maven.plugins maven-javadoc-plugin @@ -331,13 +374,13 @@ javadoc:aggregate - site - aggregate + jar + -Xdoclint:none http://docs.oracle.com/javase/6/docs/api/ http://docs.oracle.com/javase/1.5.0/docs/api/ @@ -351,7 +394,7 @@ org.eluder.coveralls coveralls-maven-plugin - 2.2.0 + ${maven-coverall-plugin.version} org.codehaus.mojo @@ -364,7 +407,7 @@ - org.apache.maven.plugins + org.apache.maven.plugins maven-resources-plugin ${maven-resources-plugin.version} @@ -399,14 +442,16 @@ org.apache.maven.plugins - maven-release-plugin - 2.5 - - true - false - sonatype-oss-release - deploy - + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + +