Skip to content
Permalink
Browse files

Support for @ResourceDef

  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Aug 7, 2017
1 parent e0a3365 commit f8f9e64025cfdc2756c1e6a281fba3d2a9f6527a
@@ -4,6 +4,7 @@ Version 0.4.0
- Interceptors for @PostConstruct and @PreDestroy
- Using bytebuddy for proxying
- Support for @NoPostConstructFor method annotation for JUnit 4 & 5
- Support for @ResourceDef members

Version 0.3.0
- Improved bean lifecycle management

This file was deleted.

Oops, something went wrong.
@@ -140,7 +140,7 @@ public EntityManagerFactory resolvePersistenceUnit(

@Override
public void cleanup() {
LOG.debug("Cleanning up EntityManagers");
// TODO find out why this gets called so insanely often
entityManagers.values().forEach(it -> {
if (it.isOpen()) {
it.close();
@@ -39,9 +39,6 @@
public class TestInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(TestInterceptor.class);

@Resource
private EJBContext ejbContext;

public enum Type {
AROUND_INVOKE,
POST_CONSTRUCT,
@@ -65,23 +62,20 @@ public Invocation(final Object target, final Method method, final Type type) {
@PostConstruct
public Object logPostConstruct(final InvocationContext invocationContext) throws Exception {
LOG.info("PostConstruct: {} {}", invocationContext.getTarget(), invocationContext.getMethod());
assertNotNull(ejbContext);
INVOCATIONS.add(new Invocation(invocationContext.getTarget(), invocationContext.getMethod(), POST_CONSTRUCT));
return invocationContext.proceed();
}

@PreDestroy
public Object logPreDestroy(final InvocationContext invocationContext) throws Exception {
LOG.info("PreDestroy: {} {}", invocationContext.getTarget(), invocationContext.getMethod());
assertNotNull(ejbContext);
INVOCATIONS.add(new Invocation(invocationContext.getTarget(), invocationContext.getMethod(), PRE_DESTROY));
return invocationContext.proceed();
}

@AroundInvoke
public Object logMethodEntry(final InvocationContext invocationContext) throws Exception {
LOG.info("AroundInvoke: {} {}", invocationContext.getTarget(), invocationContext.getMethod());
assertNotNull(ejbContext);
INVOCATIONS.add(new Invocation(invocationContext.getTarget(), invocationContext.getMethod(), AROUND_INVOKE));
return invocationContext.proceed();
}
@@ -9,6 +9,7 @@ dependencies {
compile project(":flyway")
compile project(":liquibase")
compile project(":eclipselink")
compile project(":util-resourcedef")

testCompile libLogback
}
@@ -10,6 +10,7 @@ dependencies {
compile project(":liquibase")
compile project(":eclipselink")
compile project(":util-nopostconstruct")
compile project(":util-resourcedef")

testCompile libLogback
testCompile libGroovy
@@ -8,6 +8,7 @@ dependencies {
compile project(":flyway")
compile project(":eclipselink")
compile project(":util-nopostconstruct")
compile project(":util-resourcedef")

testCompile libLogback
}
@@ -18,4 +18,6 @@ include 'liquibase' // Liquibase integration
include 'eclipselink' // EclipseLink integration
include 'rest' // Support for testing JAX-RS REST resources

include 'util-nopostconstruct'
// Various utility packages
include 'util-nopostconstruct' // @NoPostConstructFor
include 'util-resourcedef' // @ResourceDef
@@ -0,0 +1,13 @@
apply plugin: 'java'

dependencies {
compile libWeld
compile libSlf4j
compile project(":common")
compile project(":core-spi")

testCompile libJUnit4
testCompile libLogback
testCompile project(":core")
testCompile project(":mockito")
}
@@ -0,0 +1,26 @@
/*
* 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.util.resourcedef;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResourceDef {
}
@@ -0,0 +1,63 @@
/*
* 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.util.resourcedef;

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

import javax.annotation.Resource;
import javax.enterprise.inject.spi.InjectionPoint;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Set;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.reflect.FieldUtils.getAllFields;

@TestInstanceScope
public class ResourceDefResourceProvider implements ResourceProvider {
@Resource(mappedName = "testeefi/instance/instance")
private Object testInstance;

@Override
public Object resolve(final InjectionPoint injectionPoint) {
final Type type = injectionPoint.getType();
final Set<Field> candidates = stream(getAllFields(testInstance.getClass()))
.filter(field -> field.getAnnotation(ResourceDef.class) != null)
.filter(field -> field.getType().isAssignableFrom((Class) type))
.collect(toSet());
if (candidates.isEmpty()) {
return null;
}
if (candidates.size() > 1) {
throw new TestEEfiException("Ambiguous fields for injection point " + injectionPoint + " found: " + candidates);
}
final Field field = candidates.iterator().next();
field.setAccessible(true);
try {
return field.get(testInstance);
} catch (final IllegalAccessException e) {
throw new TestEEfiException("Failed to retrieve @ResourceDef field value from " + field, e);
}
}

@Override
public Object resolve(final String jndiName, final String mappedName) {
return null;
}
}
@@ -0,0 +1,53 @@
/*
* 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.util.resourcedef;

import fi.testee.runtime.TestRuntime;
import fi.testee.runtime.TestSetup;
import org.junit.Test;
import org.mockito.Mock;

import javax.annotation.Resource;

import static org.junit.Assert.assertSame;

public class ResourceDefTest {

@Test
public void resourceDef_works() {
final TestSetup setup = new TestSetup(TestClass.class, TestRuntime.instance()).init();
final TestClass instance = new TestClass();
final TestSetup.TestInstance context = setup.prepareTestInstance("myTest", instance, null);
try {
assertSame(instance.mock, instance.injected);
} finally {
context.shutdown();
setup.shutdown();
}
}

public interface SomeInterface {

}

public static class TestClass {
@ResourceDef
@Mock
private SomeInterface mock;
@Resource
private SomeInterface injected;
}
}

0 comments on commit f8f9e64

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