Skip to content
Permalink
Browse files

Using more CDI internally

  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Aug 7, 2017
1 parent 9a2e70b commit e0a3365506323476b62a4a05b738e7bf78366ce0
Showing with 521 additions and 264 deletions.
  1. +1 −0 changelog.txt
  2. +0 −2 core-spi/src/main/java/fi/testee/spi/ResourceProvider.java
  3. +36 −0 core-spi/src/main/java/fi/testee/spi/scope/TestInstanceScope.java
  4. +36 −0 core-spi/src/main/java/fi/testee/spi/scope/TestSetupScope.java
  5. +1 −1 core/src/main/java/fi/testee/ejb/EjbContainer.java
  6. +3 −7 core/src/main/java/fi/testee/ejb/EjbResourceProvider.java
  7. +18 −8 core/src/main/java/fi/testee/jdbc/JdbcResourceProvider.java
  8. +6 −0 core/src/main/java/fi/testee/jdbc/TesteeDataSource.java
  9. +4 −3 core/src/main/java/fi/testee/runtime/DependencyInjectionRealm.java
  10. +61 −0 core/src/main/java/fi/testee/runtime/ManualResourceProviderBuilder.java
  11. +4 −2 core/src/main/java/fi/testee/runtime/TestDataSetup.java
  12. +196 −0 core/src/main/java/fi/testee/runtime/TestInstanceRealm.java
  13. +1 −1 core/src/main/java/fi/testee/runtime/TestRuntime.java
  14. +68 −148 core/src/main/java/fi/testee/runtime/TestSetup.java
  15. +26 −36 core/src/main/java/fi/testee/runtime/TransactionalContext.java
  16. +3 −1 core/src/main/java/fi/testee/services/JpaInjectionServicesImpl.java
  17. +2 −3 core/src/test/java/fi/testee/BaseDependencyInjectionTest.java
  18. +2 −2 core/src/test/java/fi/testee/LifecycleTest.java
  19. +2 −2 core/src/test/java/fi/testee/PersistenceUnitTest.java
  20. +1 −1 core/src/test/java/fi/testee/jdbc/JdbcTest.java
  21. +2 −2 cucumber/src/main/java/cucumber/runtime/TestEEfiObjectFactory.java
  22. +2 −2 easymock/src/test/java/fi/testee/easymock/EasyMockTest.java
  23. +1 −1 junit4-all/src/test/resources/logback.xml
  24. +3 −3 junit4/src/main/java/fi/testee/junit4/TestEEfi.java
  25. +7 −7 junit5/src/main/java/fi/testee/junit5/TestEEfi.java
  26. +2 −2 mockito/src/test/java/fi/testee/mockito/InjectMockTest.java
  27. +6 −10 rest/src/main/java/fi/testee/rest/RestServerImpl.java
  28. +21 −12 ...e/SimpleResourceProvider.java → rest/src/main/java/fi/testee/rest/RestServerResourceProvider.java
  29. +2 −3 rest/src/test/java/fi/testee/rest/LifecycleTest.java
  30. +1 −2 rest/src/test/java/fi/testee/rest/RestTest.java
  31. +1 −1 rest/src/test/resources/logback.xml
  32. +2 −2 util-nopostconstruct/src/test/java/fi/testee/util/nopostconstruct/NoPostConstructTest.java
@@ -1,5 +1,6 @@
Version 0.4.0
- Vastly improved bean lifecycle management
- More usage of CDI internally
- Interceptors for @PostConstruct and @PreDestroy
- Using bytebuddy for proxying
- Support for @NoPostConstructFor method annotation for JUnit 4 & 5
@@ -26,6 +26,4 @@
Object resolve(InjectionPoint injectionPoint);

Object resolve(String jndiName, String mappedName);

void shutdown(boolean rollback);
}
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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.
*/
package fi.testee.spi.scope;

import javax.inject.Qualifier;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
@Target({TYPE})
public @interface TestInstanceScope {
public static final TestInstanceScope INSTANCE = new TestInstanceScope(){
@Override
public Class<? extends Annotation> annotationType() {
return TestInstanceScope.class;
}
};
}
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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.
*/
package fi.testee.spi.scope;

import javax.inject.Qualifier;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
@Target({TYPE})
public @interface TestSetupScope {
public static final TestSetupScope INSTANCE = new TestSetupScope(){
@Override
public Class<? extends Annotation> annotationType() {
return TestSetupScope.class;
}
};
}
@@ -181,7 +181,7 @@ private void inject(final Object o, final Field field, final Object instanceToIn
}
}

public static final SessionBeanModifier IDENTITY_SESSION_BEAN_MODIFIER = new IdentitySessionBeanModifier();
public static final SessionBeanModifier IDENTITY = new IdentitySessionBeanModifier();

public void shutdown() {
// This cleans up remaining beans that were part of reference cycles
@@ -16,16 +16,18 @@
package fi.testee.ejb;

import fi.testee.spi.ResourceProvider;
import fi.testee.spi.scope.TestInstanceScope;

import javax.ejb.EJBContext;
import javax.enterprise.inject.spi.InjectionPoint;

@TestInstanceScope
public class EjbResourceProvider implements ResourceProvider {
private EJBContext ejbContext = new EJBContextImpl();

@Override
public Object resolve(final InjectionPoint injectionPoint) {
if(injectionPoint.getAnnotated().getBaseType() == EJBContext.class) {
if (injectionPoint.getAnnotated().getBaseType() == EJBContext.class) {
return ejbContext;
}
return null;
@@ -35,10 +37,4 @@ public Object resolve(final InjectionPoint injectionPoint) {
public Object resolve(String jndiName, String mappedName) {
return null;
}

@Override
public void shutdown(boolean rollback) {

}

}
@@ -18,9 +18,12 @@
import fi.testee.exceptions.TestEEfiException;
import fi.testee.spi.ConnectionFactory;
import fi.testee.spi.ResourceProvider;
import fi.testee.utils.JdbcUtils;
import fi.testee.spi.scope.TestSetupScope;
import fi.testee.utils.AnnotationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.sql.DataSource;
@@ -32,11 +35,16 @@

import static java.util.stream.Collectors.toMap;

@TestSetupScope
public class JdbcResourceProvider implements ResourceProvider {
private static final Logger LOG = LoggerFactory.getLogger(JdbcResourceProvider.class);

@Resource(mappedName = "testeefi/setup/class")
private Class<?> testSetupClass;
@Resource(mappedName = "testeefi/setup/connectionFactoryManager")
private ConnectionFactoryManager connectionFactoryManager;
@Resource(mappedName = "testeefi/setup/rollbackTransactions")
private boolean rollbackTransactions;

private final Map<String, TesteeDataSource> dataSources = new HashMap<>();
private Map<String, ConnectionFactory> connectionFactories;
@@ -84,18 +92,20 @@ public Object resolve(final String jndiName, final String mappedName) {
));
}

public void shutdown(final boolean rollback) {
final JdbcUtils.JdbcConsumer<TesteeDataSource> action = rollback
? TesteeDataSource::rollback
: TesteeDataSource::commit;
@PreDestroy
public void shutdown() {
dataSources.values().forEach(it -> {
try {
action.run(it);
it.close();
if (rollbackTransactions) {
LOG.trace("Rolling back {}", it);
it.rollback();
} else {
LOG.trace("Committing {}", it);
it.commit();
}
} catch (final SQLException e) {
throw new TestEEfiException("Failed to shut down JDBC resources", e);
}
});

}
}
@@ -154,4 +154,10 @@ private void withConnection(final JdbcUtils.JdbcConsumer<Connection> runnable) t
}
}

@Override
public String toString() {
return "TesteeDataSource{" +
"name='" + name + '\'' +
'}';
}
}
@@ -58,11 +58,11 @@
public class DependencyInjectionRealm implements DependencyInjection {
private static final Logger LOG = LoggerFactory.getLogger(DependencyInjectionRealm.class);
private final String contextId = UUID.randomUUID().toString();
private final Bootstrap bootstrap;
private final DeploymentImpl deployment;
private final CreationalContextImpl<Object> rootContext = new CreationalContextImpl<>(null);
private Bootstrap bootstrap;
private DeploymentImpl deployment;

DependencyInjectionRealm(
public DependencyInjectionRealm init(
final ServiceRegistry serviceRegistry,
final BeanArchiveDiscovery beanArchiveDiscovery,
final Environments environment,
@@ -82,6 +82,7 @@
.deployBeans()
.validateBeans()
.endInitialization();
return this;
}

private void ensureTransactionServices(final ServiceRegistry serviceRegistry) {
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 Alex Stockinger
*
* 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.
*/
package fi.testee.runtime;

import fi.testee.spi.ResourceProvider;

import javax.annotation.Resource;
import javax.enterprise.inject.spi.InjectionPoint;
import java.util.HashMap;
import java.util.Map;

public class ManualResourceProviderBuilder {
private final Map<String, Object> resources = new HashMap<>();

public static ManualResourceProviderBuilder manualResourceProvider() {
return new ManualResourceProviderBuilder();
}

public ManualResourceProviderBuilder put(final String key, final Object value) {
resources.put(key, value);
return this;
}

public ResourceProvider build() {
return new ManualResourceProvider(resources);
}

private static final class ManualResourceProvider implements ResourceProvider {
private final Map<String, Object> resources;

public ManualResourceProvider(Map<String, Object> resources) {
this.resources = resources;
}

@Override
public Object resolve(final InjectionPoint injectionPoint) {
if (injectionPoint.getAnnotated().getAnnotation(Resource.class) == null) {
return null;
}
return resources.get(injectionPoint.getAnnotated().getAnnotation(Resource.class).mappedName());
}

@Override
public Object resolve(final String jndiName, final String mappedName) {
return null;
}
}
}
@@ -55,9 +55,11 @@ static void setupTestData(
final Class<?> setupClass,
final ServiceRegistry serviceRegistry
) {
final JpaInjectionServicesImpl jpaInjectionServices = serviceRegistry.get(JpaInjectionServicesImpl.class);
final ResourceInjectionServices resourceInjectionServices = serviceRegistry.get(ResourceInjectionServices.class);
final Set<Object> testDataSetupAccessors = new HashSet<>(asList(
testDataSources(serviceRegistry.get(ResourceInjectionServices.class)),
testPersistenceUnits(serviceRegistry.get(JpaInjectionServicesImpl.class))
testDataSources(resourceInjectionServices),
testPersistenceUnits(jpaInjectionServices)
));
setupTestData(setupClass, testDataSetupAccessors);
}
Oops, something went wrong.

0 comments on commit e0a3365

Please sign in to comment.
You can’t perform that action at this time.