Refactor registry and fix a bug introduced by page supplier PR (#31322)

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen 2024-07-16 14:02:30 +02:00 committed by GitHub
parent f469994149
commit 51cc2a85c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -86,6 +86,13 @@ public class Registry {
} }
public void beforeEach(Object testInstance) { public void beforeEach(Object testInstance) {
findRequestedInstances(testInstance);
matchDeployedInstancesWithRequestedInstances();
deployRequestedInstances();
injectFields(testInstance);
}
private void findRequestedInstances(Object testInstance) {
Class testClass = testInstance.getClass(); Class testClass = testInstance.getClass();
RequestedInstance requestedServerInstance = createRequestedInstance(testClass.getAnnotations(), null); RequestedInstance requestedServerInstance = createRequestedInstance(testClass.getAnnotations(), null);
requestedInstances.add(requestedServerInstance); requestedInstances.add(requestedServerInstance);
@ -101,7 +108,9 @@ public class Registry {
LOGGER.tracev("Requested suppliers: {0}", LOGGER.tracev("Requested suppliers: {0}",
requestedInstances.stream().map(r -> r.getSupplier().getClass().getSimpleName()).collect(Collectors.joining(", "))); requestedInstances.stream().map(r -> r.getSupplier().getClass().getSimpleName()).collect(Collectors.joining(", ")));
} }
}
private void matchDeployedInstancesWithRequestedInstances() {
Iterator<RequestedInstance<?, ?>> itr = requestedInstances.iterator(); Iterator<RequestedInstance<?, ?>> itr = requestedInstances.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
RequestedInstance<?, ?> requestedInstance = itr.next(); RequestedInstance<?, ?> requestedInstance = itr.next();
@ -124,8 +133,10 @@ public class Registry {
} }
} }
} }
}
itr = requestedInstances.iterator(); private void deployRequestedInstances() {
Iterator<RequestedInstance<?, ?>> itr = requestedInstances.iterator();
while (itr.hasNext()) { while (itr.hasNext()) {
RequestedInstance requestedInstance = itr.next(); RequestedInstance requestedInstance = itr.next();
@ -140,7 +151,9 @@ public class Registry {
itr.remove(); itr.remove();
} }
}
private void injectFields(Object testInstance) {
for (Field f : testInstance.getClass().getDeclaredFields()) { for (Field f : testInstance.getClass().getDeclaredFields()) {
InstanceWrapper<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations()); InstanceWrapper<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations());
try { try {
@ -208,7 +221,17 @@ public class Registry {
} }
private InstanceWrapper getDeployedInstance(RequestedInstance requestedInstance) { private InstanceWrapper getDeployedInstance(RequestedInstance requestedInstance) {
return deployedInstances.stream().filter(i -> i.getValue().equals(requestedInstance.getValueType())).findFirst().orElse(null); Class requestedValueType = requestedInstance.getValueType();
for (InstanceWrapper<?, ?> i : deployedInstances) {
if (requestedValueType != null) {
if (requestedValueType.isAssignableFrom(i.getValue().getClass())) {
return i;
}
} else if (i.getSupplier().equals(requestedInstance.getSupplier())) {
return i;
}
}
return null;
} }
private void loadSuppliers() { private void loadSuppliers() {