2016-08-08 22:17:17 +02:00
|
|
|
/**
|
|
|
|
* Poodinis Dependency Injection Framework
|
2024-02-17 13:08:59 +01:00
|
|
|
* Copyright 2014-2024 Mike Bierlee
|
2016-08-08 22:17:17 +02:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import poodinis;
|
2023-03-06 21:21:08 +01:00
|
|
|
import poodinis.test.testclasses;
|
2016-08-08 22:17:17 +02:00
|
|
|
|
|
|
|
import std.exception;
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
version (unittest) {
|
2016-12-17 23:09:56 +01:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
// Test autowiring concrete type to existing instance
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!ComponentA;
|
|
|
|
auto componentB = new ComponentB();
|
|
|
|
container.autowire(componentB);
|
|
|
|
assert(componentB !is null, "Autowirable dependency failed to autowire");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring interface type to existing instance
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC);
|
|
|
|
auto componentD = new ComponentD();
|
|
|
|
container.autowire(componentD);
|
|
|
|
assert(componentD.componentC !is null, "Autowirable dependency failed to autowire");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring private members
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC);
|
|
|
|
auto componentD = new ComponentD();
|
|
|
|
container.autowire(componentD);
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(componentD.privateComponentC is componentD.componentC,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowire private dependency failed");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring will only happen once
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC).newInstance();
|
|
|
|
auto componentD = new ComponentD();
|
|
|
|
container.autowire(componentD);
|
|
|
|
auto expectedComponent = componentD.componentC;
|
|
|
|
container.autowire(componentD);
|
|
|
|
auto actualComponent = componentD.componentC;
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(expectedComponent is actualComponent,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring the second time wired a different instance");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring unregistered type
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
auto componentD = new ComponentD();
|
2021-05-01 21:16:44 +02:00
|
|
|
assertThrown!(ResolveException)(container.autowire(componentD),
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring unregistered type should throw ResolveException");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring member with non-autowire attribute does not autowire
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
auto componentE = new ComponentE();
|
|
|
|
container.autowire(componentE);
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(componentE.componentC is null,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring should not occur for members with attributes other than @Autowire");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowire class with alias declaration
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!ComponentA;
|
|
|
|
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
|
|
|
|
|
|
|
|
container.autowire(componentDeclarationCocktail);
|
|
|
|
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(componentDeclarationCocktail.componentA !is null,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring class with non-assignable declarations failed");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowire class with qualifier
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC);
|
|
|
|
container.register!(InterfaceA, ComponentX);
|
|
|
|
auto componentX = container.resolve!(InterfaceA, ComponentX);
|
|
|
|
|
|
|
|
auto monkeyShine = new MonkeyShine();
|
|
|
|
container.autowire(monkeyShine);
|
|
|
|
|
|
|
|
assert(monkeyShine.component is componentX, "Autowiring class with qualifier failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowire class with multiple qualifiers
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC);
|
|
|
|
container.register!(InterfaceA, ComponentX);
|
|
|
|
auto componentC = container.resolve!(InterfaceA, ComponentC);
|
|
|
|
auto componentX = container.resolve!(InterfaceA, ComponentX);
|
|
|
|
|
|
|
|
auto bootstrapBootstrap = new BootstrapBootstrap();
|
|
|
|
container.autowire(bootstrapBootstrap);
|
|
|
|
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(bootstrapBootstrap.componentX is componentX,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring class with multiple qualifiers failed");
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(bootstrapBootstrap.componentC is componentC,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring class with multiple qualifiers failed");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test getting instance from autowired registration will autowire instance
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!ComponentA;
|
|
|
|
|
2021-05-01 21:16:44 +02:00
|
|
|
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB),
|
2023-03-06 22:46:07 +01:00
|
|
|
new InstanceFactory(), container).initializeFactoryType().singleInstance();
|
2021-05-01 21:16:44 +02:00
|
|
|
auto instance = cast(ComponentB) registration.getInstance(
|
2023-03-06 22:46:07 +01:00
|
|
|
new AutowireInstantiationContext());
|
2018-01-01 15:54:32 +01:00
|
|
|
|
|
|
|
assert(instance.componentA !is null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring a dynamic array with all qualified types
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!(InterfaceA, ComponentC);
|
|
|
|
container.register!(InterfaceA, ComponentX);
|
|
|
|
|
|
|
|
auto lord = new LordOfTheComponents();
|
|
|
|
container.autowire(lord);
|
|
|
|
|
|
|
|
assert(lord.components.length == 2, "Dynamic array was not autowired");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring new instance of singleinstance registration with newInstance UDA
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!ComponentA;
|
|
|
|
|
|
|
|
auto regularComponentA = container.resolve!ComponentA;
|
|
|
|
auto charlie = new ComponentCharlie();
|
|
|
|
|
|
|
|
container.autowire(charlie);
|
|
|
|
|
2021-05-01 21:16:44 +02:00
|
|
|
assert(charlie.componentA !is regularComponentA,
|
2023-03-06 22:46:07 +01:00
|
|
|
"Autowiring class with AssignNewInstance did not yield a different instance");
|
2018-01-01 15:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring members from base class
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
container.register!ComponentA;
|
|
|
|
container.register!ComponentB;
|
|
|
|
container.register!ComponentZ;
|
|
|
|
|
|
|
|
auto instance = new ComponentZ();
|
|
|
|
container.autowire(instance);
|
|
|
|
|
|
|
|
assert(instance.componentA !is null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test autowiring optional dependencies
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
|
|
|
auto instance = new OuttaTime();
|
|
|
|
|
|
|
|
container.autowire(instance);
|
2016-08-08 22:17:17 +02:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
assert(instance.interfaceA is null);
|
|
|
|
assert(instance.componentA is null);
|
|
|
|
assert(instance.componentCs is null);
|
|
|
|
}
|
2016-12-09 02:05:03 +01:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
// Test autowiring class using value injection
|
2023-03-06 23:24:18 +01:00
|
|
|
unittest {
|
2018-01-01 15:54:32 +01:00
|
|
|
auto container = new shared DependencyContainer();
|
2016-12-09 02:05:03 +01:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
container.register!(ValueInjector!int, TestInjector);
|
|
|
|
container.register!ComponentA;
|
|
|
|
auto instance = new ValuedClass();
|
2016-12-09 02:05:03 +01:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
container.autowire(instance);
|
2016-12-09 02:05:03 +01:00
|
|
|
|
2018-01-01 15:54:32 +01:00
|
|
|
assert(instance.intValue == 8);
|
|
|
|
assert(instance.unrelated !is null);
|
|
|
|
}
|
2016-08-08 22:17:17 +02:00
|
|
|
}
|