-
Notifications
You must be signed in to change notification settings - Fork 20
Constretto 2.1
- Improved support for Junit 4.X after refactoring the JUnit Rule ConstrettoRule added in 2.0.4 to be used as a @ClassRule. As a consequence the constretto-test-junit4 module has been merged into the constretto-test module. Look at the example for details
- LDAP configuration support. You can add configuration either by using DSN or by providing a LDAP search. Example
- NOTE: Constretto will not close or even handle LDAP connection issues for you. The will make it easier to integrate with Spring LDAP or other third-party libraries.
- The development of this feature has been sponsored by NextGenTel AS
- Dropped support for Spring 2.X in favour of the latest Spring 3.2 release.
- The @Configure annotation can now be used on constructors (w/o Spring)
- The reconfigure() call on the ConstrettoConfiguration interface has been deprecated as it is not thread-safe.
- It will be completely removed in the next release
- As always: special thanks goes to those who made contributions to this release
- only one constructor per class may be have the @Configure annotation or Constretto will complain
Given the following POJO:
public class SimpleConstructorInjectableBean {
private String key1;
@Configure
public SimpleConstructorInjectableBean(final String key1) {
this.key1 = key1;
}
public String getKey1() {
return key1;
}
}
An instance may be Constructed using the "as" method:
ConstrettoConfiguration configuration = ...
SimpleConstructorInjectableBean configBean = configuration.as(SimpleConstructorInjectableBean.class);
If you use Spring and have enabled Constretto-Spring support you can mix @Autowired and @Configure annotations:
public class AutowiredAndConfiguredConstructorInjectionBean {
private SimpleConstructorInjectableBean simpleConstructorInjectableBean;
private String key2;
@Autowired
@Configure
public AutowiredAndConfiguredConstructorInjectionBean(final SimpleConstructorInjectableBean simpleConstructorInjectableBean,
final String key2) {
this.simpleConstructorInjectableBean = simpleConstructorInjectableBean;
this.key2 = key2;
}
public SimpleConstructorInjectableBean getSimpleConstructorInjectableBean() {
return simpleConstructorInjectableBean;
}
public String getKey2() {
return key2;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:constretto="http://constretto.org/schema/constretto"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://constretto.org/schema/constretto http://constretto.org/schema/constretto/constretto-1.2.xsd">
<!--- annotation-config must be "true" for constructor injection to work --->
<constretto:configuration annotation-config="true" property-placeholder="false">
<constretto:stores>
<constretto:properties-store>
<constretto:resource location="classpath:properties/test1.properties"/>
</constretto:properties-store>
</constretto:stores>
</constretto:configuration>
<bean class="org.constretto.spring.configuration.helper.SimpleConstructorInjectableBean"/>
<bean class="org.constretto.spring.configuration.helper.AutowiredAndConfiguredConstructorInjectionBean" />
</beans>
Add LDAP entry either by using its distinguished name or by providing LDAP search parameters:
final ConstrettoConfiguration configuration = new ConstrettoBuilder()
.createLdapConfigurationStore(initialDirContext)
.addDsn("cn=Kaare Nilsen,dc=constretto,dc=org")
// all attributes in the given entry will be available (ie. configuration key "cn" will have the value \"Kaare Nilsen")
.addDsnWithKey("sidekick", "cn=Jon-Anders Teigen,dc=constretto,dc=org")
// maps LDAP attributes with prefix "sidekick". (ie. "sidekick.cn" will have the value "Jon-Anders Teigen")
.addUsingSearch("dc=constretto,dc=org", "(&(cn=K*)(objectClass=inetOrgPerson))", "uid")
// Adds all LDAP objects matching the query to configuration attributes prefixed with the value of the "uid" attribute
// ie. if the uid attribute for "cn=Kaare Nilsen,dc=constretto,dc=org" is "kaare", its "cn" attribute will be available as "kaare.cn"
.done()
.getConfiguration();
Constretto will help you setting the specified tags or environment settings for a Junit test by providing ConstrettoRule to be used as either a @ClassRule (running before class initialization like @BeforeClass and @AfterClass) or a @Rule (wrapping a test method invocation like @Before and @After).
The @ClassRule use case is useful if you use a custom JUnitRunner such as the ones provided by Spring Test as it will ensure that specified tags are set before the JUnitRunner is invoked (and reset to its previous value afterwords). @ClassRule may also be used for JUnit Suites.
@Tags({"purejunit", "test"})
public class ConstrettoRuleTest {
@Rule
public ConstrettoRule constrettoRule = new ConstrettoRule();
@Test
public void someTestRequiringEnvironmentTagsToBeSet() {
ConstrettoConfiguration configuration = new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration();
// current tags will be "purejunit" and "test"
// more logic here...
}
}
You may also use ConstrettoRule with the @Environment annotation from Constretto Spring
import org.constretto.ConstrettoConfiguration;
import org.constretto.spring.ConfigurationAnnotationConfigurer;
import org.constretto.spring.annotation.Environment;
import org.constretto.spring.internal.resolver.DefaultAssemblyContextResolver;
import org.constretto.test.Environment;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
@Environment("junit")
@RunWith(ConstrettoSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyTest.TestConfiguration.class)
public class MyTest {
// Using Spring Java config for this example
@Configuration
public static class TestConfiguration {
@Bean
public static ConstrettoConfiguration constrettoConfiguration() {
return new ConstrettoBuilder(true).getConfiguration();
}
// Using Spring Java config for this example
@Bean
public static ConfigurationAnnotationConfigurer configurationAnnotationConfigurer(final ConstrettoConfiguration configuration) {
return new ConfigurationAnnotationConfigurer(configuration, new DefaultAssemblyContextResolver());
}
}
// The current environment value will be injected by the ConfigurationAnnotationConfigurer
// It is enabled by default in Spring XML configurations if you include the "<constretto:configuration/>" tag
@Environment
private List<String> injectedEnvironment;
// ClassRules will be invoked before the class (like @BeforeClass/@AfterClass) and thus need to be static
@ClassRule
public static ConstrettoRule constrettoRule = new ConstrettoRule();
@Test
public void testApplyEnvironment() throws Exception {
assertArrayEquals(new String[]{"junit"}, injectedEnvironment.toArray(new String[1]));
}
}