Skip to content
Permalink
Browse files

Fixed issue in fi.testee.spi.base.BeanReplacementManager where new EJ…

…Bs were instantiated on each call when being injected with @Inject

(cherry picked from commit 0f781f9)
  • Loading branch information...
Alex Stockinger
Alex Stockinger committed Aug 9, 2017
1 parent a3d2406 commit e7ceb9804341c1d582bd011d9ccdca6ad76af84c
@@ -1,3 +1,6 @@
Version 0.4.3
- Fixed issue in fi.testee.spi.base.BeanReplacementManager where new EJBs were instantiated on each call when being injected with @Inject

Version 0.4.2
- Fixed injection into fields with inherited classes / interfaces when using @ResourceRef

@@ -150,22 +150,20 @@ public void preDestroy(final T instance) {

@Override
public ResourceReferenceFactory<T> getResourceReferenceFactory() {
return () -> {
final T replacement = findReplacementFor(sessionBean.getDescriptor().getBeanClass());
if (replacement != null) {
return new ResourceReference<T>() {
@Override
public T getInstance() {
return replacement;
}

@Override
public void release() {
// Replacements don't need to be released
}
};
final T replacement = findReplacementFor(sessionBean.getDescriptor().getBeanClass());
if (replacement == null) {
return sessionBean.getResourceReferenceFactory();
}
return () -> new ResourceReference<T>() {
@Override
public T getInstance() {
return replacement;
}

@Override
public void release() {
// Replacements don't need to be released
}
return sessionBean.getResourceReferenceFactory().createResource();
};
}
};
@@ -11,4 +11,5 @@ dependencies {
compile project(":util-resourcedef")

testCompile libLogback
testRuntime libJUnit5PlatformLauncher
}
@@ -0,0 +1,72 @@
/*
* 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.junit5;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.ejb.EJB;
import javax.inject.Inject;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertSame;


@ExtendWith(TestEEfi.class)
public class InstantiationTest {
@EJB
private SessionBean ejbViaEjb;
@Inject
private SessionBean ejbViaInject;
@Inject
private ManagedBean cdiViaInject;

@Test
public void ejb_via_ejb() {
assertIsSingleton(() -> ejbViaEjb);
}

@Test
public void ejb_via_inject() {
assertIsSingleton(() -> ejbViaInject);
}

@Test
public void cdi_via_inject() {
assertIsSingleton(() -> cdiViaInject);
}

private void assertIsSingleton(final Supplier<BaseClass> baseClass) {
final BaseClass instance = baseClass.get().getThis();
assertSame(instance, baseClass.get().getThis());
}

public static class BaseClass {
public BaseClass getThis() {
return this;
}
}

@javax.ejb.Singleton
public static class SessionBean extends BaseClass {

}

@javax.inject.Singleton
public static class ManagedBean extends BaseClass {

}
}

0 comments on commit e7ceb98

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