Testsuite PoC - Add more unit tests for the registry (#31468)
Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
parent
c885d682c5
commit
443efb46f0
8 changed files with 173 additions and 10 deletions
|
@ -17,7 +17,7 @@ public class Config {
|
|||
return config.getOptionalValue("kc.test." + ValueTypeAlias.getAlias(valueType), String.class).orElse(null);
|
||||
}
|
||||
|
||||
private static SmallRyeConfig initConfig() {
|
||||
public static SmallRyeConfig initConfig() {
|
||||
SmallRyeConfigBuilder configBuilder = new SmallRyeConfigBuilder()
|
||||
.addDefaultSources()
|
||||
.addDefaultInterceptors()
|
||||
|
|
|
@ -58,6 +58,8 @@ public class Registry {
|
|||
dependency.registerDependency(dependent);
|
||||
deployedInstances.add(dependency);
|
||||
|
||||
requestedInstances.remove(requestedDependency);
|
||||
|
||||
if (LOGGER.isTraceEnabled()) {
|
||||
LOGGER.tracev("Injecting requested dependency {0} into {1}",
|
||||
dependency.getSupplier().getClass().getSimpleName(),
|
||||
|
@ -141,21 +143,17 @@ public class Registry {
|
|||
}
|
||||
|
||||
private void deployRequestedInstances() {
|
||||
Iterator<RequestedInstance<?, ?>> itr = requestedInstances.iterator();
|
||||
while (itr.hasNext()) {
|
||||
RequestedInstance requestedInstance = itr.next();
|
||||
while (!requestedInstances.isEmpty()) {
|
||||
RequestedInstance requestedInstance = requestedInstances.remove(0);
|
||||
|
||||
InstanceContext instance = new InstanceContext(this, requestedInstance.getSupplier(), requestedInstance.getAnnotation(), requestedInstance.getValueType());
|
||||
instance.setValue(requestedInstance.getSupplier().getValue(instance));
|
||||
deployedInstances.add(instance);
|
||||
|
||||
if (LOGGER.isTraceEnabled()) {
|
||||
LOGGER.tracev("Created instance: {0}",
|
||||
requestedInstance.getSupplier().getClass().getSimpleName());
|
||||
}
|
||||
|
||||
deployedInstances.add(instance);
|
||||
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,6 +187,10 @@ public class Registry {
|
|||
destroy.forEach(this::destroy);
|
||||
}
|
||||
|
||||
List<Supplier<?, ?>> getSuppliers() {
|
||||
return suppliers;
|
||||
}
|
||||
|
||||
private RequestedInstance<?, ?> createRequestedInstance(Annotation[] annotations, Class<?> valueType) {
|
||||
for (Annotation a : annotations) {
|
||||
for (Supplier s : suppliers) {
|
||||
|
|
|
@ -14,7 +14,7 @@ public class ValueTypeAlias {
|
|||
TestDatabase.class, "database"
|
||||
);
|
||||
|
||||
public static String getAlias(Class clazz) {
|
||||
public static String getAlias(Class<?> clazz) {
|
||||
String alias = aliases.get(clazz);
|
||||
if (alias == null) {
|
||||
alias = clazz.getSimpleName();
|
||||
|
|
|
@ -5,14 +5,18 @@ import org.hamcrest.Matchers;
|
|||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.keycloak.test.framework.config.Config;
|
||||
import org.keycloak.test.framework.injection.mocks.MockChildAnnotation;
|
||||
import org.keycloak.test.framework.injection.mocks.MockChildSupplier;
|
||||
import org.keycloak.test.framework.injection.mocks.MockChildValue;
|
||||
import org.keycloak.test.framework.injection.mocks.MockInstances;
|
||||
import org.keycloak.test.framework.injection.mocks.MockParent2Supplier;
|
||||
import org.keycloak.test.framework.injection.mocks.MockParentAnnotation;
|
||||
import org.keycloak.test.framework.injection.mocks.MockParentSupplier;
|
||||
import org.keycloak.test.framework.injection.mocks.MockParentValue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RegistryTest {
|
||||
|
||||
private Registry registry;
|
||||
|
@ -114,6 +118,72 @@ public class RegistryTest {
|
|||
Assertions.assertNotSame(child1, parentAndChildTest.child);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecreateIfDifferentLifeCycleRequested() {
|
||||
MockParentSupplier.DEFAULT_LIFECYCLE = LifeCycle.GLOBAL;
|
||||
|
||||
ParentTest parentTest = new ParentTest();
|
||||
|
||||
registry.beforeEach(parentTest);
|
||||
registry.afterEach();
|
||||
registry.afterAll();
|
||||
assertRunning(parentTest.parent);
|
||||
|
||||
MockParentSupplier.DEFAULT_LIFECYCLE = LifeCycle.CLASS;
|
||||
|
||||
ParentTest parentTest2 = new ParentTest();
|
||||
registry.beforeEach(parentTest2);
|
||||
|
||||
assertRunning(parentTest2.parent);
|
||||
assertClosed(parentTest.parent);
|
||||
Assertions.assertNotSame(parentTest2.parent, parentTest.parent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecreateIfNotCompatible() {
|
||||
ParentTest parentTest = new ParentTest();
|
||||
|
||||
registry.beforeEach(parentTest);
|
||||
MockParentValue parent1 = parentTest.parent;
|
||||
registry.afterEach();
|
||||
|
||||
MockParentSupplier.COMPATIBLE = false;
|
||||
|
||||
registry.beforeEach(parentTest);
|
||||
registry.afterEach();
|
||||
|
||||
MockParentValue parent2 = parentTest.parent;
|
||||
|
||||
assertRunning(parent2);
|
||||
assertClosed(parent1);
|
||||
Assertions.assertNotSame(parent2, parent1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectedSupplierDefault() {
|
||||
List<Supplier<?, ?>> suppliers = registry.getSuppliers();
|
||||
Assertions.assertEquals(1, suppliers.stream().filter(s -> s.getValueType().equals(MockParentValue.class)).count());
|
||||
Assertions.assertTrue(suppliers.stream().anyMatch(s -> s.getClass().equals(MockParentSupplier.class)));
|
||||
Assertions.assertFalse(suppliers.stream().anyMatch(s -> s.getClass().equals(MockParent2Supplier.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectedSupplierConfigOverride() {
|
||||
System.setProperty("kc.test.MockParentValue", MockParent2Supplier.class.getSimpleName());
|
||||
try {
|
||||
Config.initConfig();
|
||||
|
||||
registry = new Registry();
|
||||
List<Supplier<?, ?>> suppliers = registry.getSuppliers();
|
||||
Assertions.assertEquals(1, suppliers.stream().filter(s -> s.getValueType().equals(MockParentValue.class)).count());
|
||||
Assertions.assertFalse(suppliers.stream().anyMatch(s -> s.getClass().equals(MockParentSupplier.class)));
|
||||
Assertions.assertTrue(suppliers.stream().anyMatch(s -> s.getClass().equals(MockParent2Supplier.class)));
|
||||
} finally {
|
||||
System.getProperties().remove("kc.test.MockParentValue");
|
||||
Config.initConfig();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependencyCreatedOnDemand() {
|
||||
ChildTest childTest = new ChildTest();
|
||||
|
@ -122,6 +192,22 @@ public class RegistryTest {
|
|||
assertRunning(childTest.child, childTest.child.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependencyRequestedBefore() {
|
||||
ParentAndChildTest test = new ParentAndChildTest();
|
||||
|
||||
registry.beforeEach(test);
|
||||
assertRunning(test.child, test.child.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDependencyRequestedAfter() {
|
||||
ChildAndParentTest test = new ChildAndParentTest();
|
||||
|
||||
registry.beforeEach(test);
|
||||
assertRunning(test.child, test.child.getParent());
|
||||
}
|
||||
|
||||
public static void assertRunning(Object... values) {
|
||||
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasItems(values));
|
||||
MatcherAssert.assertThat(MockInstances.INSTANCES, Matchers.hasSize(values.length));
|
||||
|
@ -150,4 +236,11 @@ public class RegistryTest {
|
|||
MockChildValue child;
|
||||
}
|
||||
|
||||
public static final class ChildAndParentTest {
|
||||
@MockChildAnnotation
|
||||
MockChildValue child;
|
||||
|
||||
@MockParentAnnotation
|
||||
MockParentValue parent;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package org.keycloak.test.framework.injection;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
public class ValueTypeAliasTest {
|
||||
|
||||
@Test
|
||||
public void withAlias() {
|
||||
Assertions.assertEquals("browser", ValueTypeAlias.getAlias(WebDriver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withoutAlias() {
|
||||
Assertions.assertEquals("Keycloak", ValueTypeAlias.getAlias(Keycloak.class));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package org.keycloak.test.framework.injection.mocks;
|
||||
|
||||
import org.keycloak.test.framework.injection.InstanceContext;
|
||||
import org.keycloak.test.framework.injection.LifeCycle;
|
||||
import org.keycloak.test.framework.injection.RequestedInstance;
|
||||
import org.keycloak.test.framework.injection.Supplier;
|
||||
|
||||
public class MockParent2Supplier implements Supplier<MockParentValue, MockParentAnnotation> {
|
||||
|
||||
public static LifeCycle DEFAULT_LIFECYCLE = LifeCycle.CLASS;
|
||||
|
||||
public static void reset() {
|
||||
DEFAULT_LIFECYCLE = LifeCycle.CLASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<MockParentAnnotation> getAnnotationClass() {
|
||||
return MockParentAnnotation.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<MockParentValue> getValueType() {
|
||||
return MockParentValue.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockParentValue getValue(InstanceContext<MockParentValue, MockParentAnnotation> instanceContext) {
|
||||
return new MockParentValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean compatible(InstanceContext<MockParentValue, MockParentAnnotation> a, RequestedInstance<MockParentValue, MockParentAnnotation> b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(InstanceContext<MockParentValue, MockParentAnnotation> instanceContext) {
|
||||
instanceContext.getValue().close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LifeCycle getDefaultLifecycle() {
|
||||
return DEFAULT_LIFECYCLE;
|
||||
}
|
||||
}
|
|
@ -8,9 +8,11 @@ import org.keycloak.test.framework.injection.Supplier;
|
|||
public class MockParentSupplier implements Supplier<MockParentValue, MockParentAnnotation> {
|
||||
|
||||
public static LifeCycle DEFAULT_LIFECYCLE = LifeCycle.CLASS;
|
||||
public static boolean COMPATIBLE = true;
|
||||
|
||||
public static void reset() {
|
||||
DEFAULT_LIFECYCLE = LifeCycle.CLASS;
|
||||
COMPATIBLE = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,7 +32,7 @@ public class MockParentSupplier implements Supplier<MockParentValue, MockParentA
|
|||
|
||||
@Override
|
||||
public boolean compatible(InstanceContext<MockParentValue, MockParentAnnotation> a, RequestedInstance<MockParentValue, MockParentAnnotation> b) {
|
||||
return true;
|
||||
return COMPATIBLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
org.keycloak.test.framework.injection.mocks.MockParentSupplier
|
||||
org.keycloak.test.framework.injection.mocks.MockParent2Supplier
|
||||
org.keycloak.test.framework.injection.mocks.MockChildSupplier
|
Loading…
Reference in a new issue