Complete JavaFX MVVM examples, bindings and library documentation
This commit is contained in:
@@ -19,6 +19,10 @@ import java.util.Objects;
|
||||
* for resolving and creating components.
|
||||
*/
|
||||
public class ApplicationContext {
|
||||
/**
|
||||
* Components this context can create, keyed by the registered type.
|
||||
* The component scan fills the map, and manual singleton registration updates it.
|
||||
*/
|
||||
private final Map<Class<?>, ComponentData> components;
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,33 +12,54 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
/**
|
||||
* Checks that {@link ApplicationContext} injects constructor, field, and setter dependencies.
|
||||
*/
|
||||
class ApplicationContextInjectionTest {
|
||||
|
||||
/**
|
||||
* Context scanned from the successful injection fixtures.
|
||||
*/
|
||||
private ApplicationContext context;
|
||||
|
||||
/**
|
||||
* Scans the successful injection fixtures before each test.
|
||||
*/
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
context = new ApplicationContext("de.neitzel.injection.testcomponents.injection.success");
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a service whose dependency is passed to the constructor.
|
||||
*/
|
||||
@Test
|
||||
void resolvesConstructorInjection() {
|
||||
ConstructorInjectedService service = context.getComponent(ConstructorInjectedService.class);
|
||||
assertNotNull(service.getRootDependency());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a service whose dependency is injected into a field.
|
||||
*/
|
||||
@Test
|
||||
void resolvesFieldInjection() {
|
||||
FieldInjectedService service = context.getComponent(FieldInjectedService.class);
|
||||
assertNotNull(service.getRootDependency());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a service whose dependency is injected through a setter.
|
||||
*/
|
||||
@Test
|
||||
void resolvesSetterInjection() {
|
||||
SetterInjectedService service = context.getComponent(SetterInjectedService.class);
|
||||
assertNotNull(service.getRootDependency());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a mixed graph and reuses the same singleton dependency throughout.
|
||||
*/
|
||||
@Test
|
||||
void resolvesMixedInjectionGraphAndSingletonReuse() {
|
||||
MixedInjectionService service = context.getComponent(MixedInjectionService.class);
|
||||
@@ -48,6 +69,9 @@ class ApplicationContextInjectionTest {
|
||||
assertSame(rootDependency, service.getSetterInjectedService().getRootDependency());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance while singleton dependencies stay the instances already in the context.
|
||||
*/
|
||||
@Test
|
||||
void createComponentInjectsANewInstanceAndKeepsTheSingleton() {
|
||||
MixedInjectionService singleton = context.getComponent(MixedInjectionService.class);
|
||||
@@ -63,6 +87,9 @@ class ApplicationContextInjectionTest {
|
||||
assertSame(singleton.getSetterInjectedService(), created.getSetterInjectedService());
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps a created instance out of the singleton cache.
|
||||
*/
|
||||
@Test
|
||||
void createComponentDoesNotBecomeTheSingleton() {
|
||||
MixedInjectionService created = context.createComponent(MixedInjectionService.class);
|
||||
|
||||
@@ -7,8 +7,14 @@ import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Checks that the component scanner reports unusable injection plans.
|
||||
*/
|
||||
class ComponentScannerInjectionPlanTest {
|
||||
|
||||
/**
|
||||
* Reports an error when a component declares more than one {@code @Inject} constructor.
|
||||
*/
|
||||
@Test
|
||||
void reportsMultipleInjectConstructorsAsError() {
|
||||
ComponentScanner scanner = new ComponentScanner("de.neitzel.injection.testcomponents.injection.error");
|
||||
@@ -16,6 +22,9 @@ class ComponentScannerInjectionPlanTest {
|
||||
assertTrue(scanner.getErrors().stream().anyMatch(error -> error.contains(MultipleInjectConstructorsComponent.class.getName())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports an error when every constructor needs arguments and none is marked {@code @Inject}.
|
||||
*/
|
||||
@Test
|
||||
void reportsMissingUsableConstructorAsError() {
|
||||
ComponentScanner scanner = new ComponentScanner("de.neitzel.injection.testcomponents.injection.error");
|
||||
|
||||
@@ -13,6 +13,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Checks which types the component scanner accepts in the test component packages.
|
||||
*/
|
||||
class InjectableComponentScannerTest {
|
||||
|
||||
/**
|
||||
|
||||
+13
@@ -3,12 +3,25 @@ package de.neitzel.injection.testcomponents.injection.error;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture with two {@code @Inject} constructors, which the scanner must reject.
|
||||
*/
|
||||
@Singleton
|
||||
public class MultipleInjectConstructorsComponent {
|
||||
/**
|
||||
* Declares an injectable constructor that takes a string.
|
||||
*
|
||||
* @param firstDependency unused string dependency
|
||||
*/
|
||||
@Inject
|
||||
public MultipleInjectConstructorsComponent(String firstDependency) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Declares a second injectable constructor that takes an integer.
|
||||
*
|
||||
* @param secondDependency unused integer dependency
|
||||
*/
|
||||
@Inject
|
||||
public MultipleInjectConstructorsComponent(Integer secondDependency) {
|
||||
}
|
||||
|
||||
+13
@@ -2,11 +2,24 @@ package de.neitzel.injection.testcomponents.injection.error;
|
||||
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture with no default constructor and no {@code @Inject} constructor.
|
||||
*/
|
||||
@Singleton
|
||||
public class NoInjectConstructorWithoutDefault {
|
||||
/**
|
||||
* Accepts a string and is not marked for injection.
|
||||
*
|
||||
* @param firstDependency unused string dependency
|
||||
*/
|
||||
public NoInjectConstructorWithoutDefault(String firstDependency) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an integer and is not marked for injection.
|
||||
*
|
||||
* @param secondDependency unused integer dependency
|
||||
*/
|
||||
public NoInjectConstructorWithoutDefault(Integer secondDependency) {
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -3,15 +3,31 @@ package de.neitzel.injection.testcomponents.injection.success;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture whose dependency is passed to the constructor.
|
||||
*/
|
||||
@Singleton
|
||||
public class ConstructorInjectedService {
|
||||
/**
|
||||
* Dependency received through the constructor.
|
||||
*/
|
||||
private final RootDependency rootDependency;
|
||||
|
||||
/**
|
||||
* Stores the constructor dependency.
|
||||
*
|
||||
* @param rootDependency the dependency
|
||||
*/
|
||||
@Inject
|
||||
public ConstructorInjectedService(RootDependency rootDependency) {
|
||||
this.rootDependency = rootDependency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the constructor dependency.
|
||||
*
|
||||
* @return the dependency
|
||||
*/
|
||||
public RootDependency getRootDependency() {
|
||||
return rootDependency;
|
||||
}
|
||||
|
||||
+11
@@ -3,11 +3,22 @@ package de.neitzel.injection.testcomponents.injection.success;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture whose dependency is injected into a field.
|
||||
*/
|
||||
@Singleton
|
||||
public class FieldInjectedService {
|
||||
/**
|
||||
* Dependency injected into the field.
|
||||
*/
|
||||
@Inject
|
||||
private RootDependency rootDependency;
|
||||
|
||||
/**
|
||||
* Returns the injected dependency.
|
||||
*
|
||||
* @return the dependency
|
||||
*/
|
||||
public RootDependency getRootDependency() {
|
||||
return rootDependency;
|
||||
}
|
||||
|
||||
+37
@@ -3,32 +3,69 @@ package de.neitzel.injection.testcomponents.injection.success;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture that receives dependencies by constructor, field, and setter.
|
||||
*/
|
||||
@Singleton
|
||||
public class MixedInjectionService {
|
||||
/**
|
||||
* Dependency received through the constructor.
|
||||
*/
|
||||
private final ConstructorInjectedService constructorInjectedService;
|
||||
|
||||
/**
|
||||
* Dependency injected into the field.
|
||||
*/
|
||||
@Inject
|
||||
private FieldInjectedService fieldInjectedService;
|
||||
|
||||
/**
|
||||
* Dependency injected through the setter.
|
||||
*/
|
||||
private SetterInjectedService setterInjectedService;
|
||||
|
||||
/**
|
||||
* Stores the constructor dependency.
|
||||
*
|
||||
* @param constructorInjectedService the constructor dependency
|
||||
*/
|
||||
@Inject
|
||||
public MixedInjectionService(ConstructorInjectedService constructorInjectedService) {
|
||||
this.constructorInjectedService = constructorInjectedService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the constructor dependency.
|
||||
*
|
||||
* @return the constructor dependency
|
||||
*/
|
||||
public ConstructorInjectedService getConstructorInjectedService() {
|
||||
return constructorInjectedService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the field dependency.
|
||||
*
|
||||
* @return the field dependency
|
||||
*/
|
||||
public FieldInjectedService getFieldInjectedService() {
|
||||
return fieldInjectedService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setter dependency.
|
||||
*
|
||||
* @return the setter dependency
|
||||
*/
|
||||
public SetterInjectedService getSetterInjectedService() {
|
||||
return setterInjectedService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the setter dependency.
|
||||
*
|
||||
* @param setterInjectedService the setter dependency
|
||||
*/
|
||||
@Inject
|
||||
public void setSetterInjectedService(SetterInjectedService setterInjectedService) {
|
||||
this.setterInjectedService = setterInjectedService;
|
||||
|
||||
+3
@@ -2,6 +2,9 @@ package de.neitzel.injection.testcomponents.injection.success;
|
||||
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Shared singleton dependency used by the injection fixtures.
|
||||
*/
|
||||
@Singleton
|
||||
public class RootDependency {
|
||||
}
|
||||
|
||||
+16
@@ -3,14 +3,30 @@ package de.neitzel.injection.testcomponents.injection.success;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Fixture whose dependency is injected through a setter.
|
||||
*/
|
||||
@Singleton
|
||||
public class SetterInjectedService {
|
||||
/**
|
||||
* Dependency injected through the setter.
|
||||
*/
|
||||
private RootDependency rootDependency;
|
||||
|
||||
/**
|
||||
* Returns the injected dependency.
|
||||
*
|
||||
* @return the dependency
|
||||
*/
|
||||
public RootDependency getRootDependency() {
|
||||
return rootDependency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the injected dependency.
|
||||
*
|
||||
* @param rootDependency the dependency
|
||||
*/
|
||||
@Inject
|
||||
public void setRootDependency(RootDependency rootDependency) {
|
||||
this.rootDependency = rootDependency;
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
package de.neitzel.injection.testcomponents.test1ok;
|
||||
|
||||
/**
|
||||
* Shared supertype of more than one scanned component.
|
||||
*/
|
||||
public class SuperClass {
|
||||
}
|
||||
|
||||
+6
@@ -2,8 +2,14 @@ package de.neitzel.injection.testcomponents.test1ok;
|
||||
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Singleton component in the root of the successful scan package.
|
||||
*/
|
||||
@Singleton
|
||||
public class TestComponent1_1 extends SuperClass implements TestInterface1_2 {
|
||||
/**
|
||||
* Creates the component with no dependencies.
|
||||
*/
|
||||
public TestComponent1_1() {
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -1,4 +1,7 @@
|
||||
package de.neitzel.injection.testcomponents.test1ok;
|
||||
|
||||
/**
|
||||
* Interface implemented by exactly one scanned component.
|
||||
*/
|
||||
public interface TestInterface1_1 {
|
||||
}
|
||||
|
||||
+3
@@ -1,4 +1,7 @@
|
||||
package de.neitzel.injection.testcomponents.test1ok;
|
||||
|
||||
/**
|
||||
* Interface implemented by more than one scanned component.
|
||||
*/
|
||||
public interface TestInterface1_2 {
|
||||
}
|
||||
|
||||
+6
@@ -5,8 +5,14 @@ import de.neitzel.injection.testcomponents.test1ok.TestInterface1_1;
|
||||
import de.neitzel.injection.testcomponents.test1ok.TestInterface1_2;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Singleton component in a subpackage of the successful scan package.
|
||||
*/
|
||||
@Singleton
|
||||
public class TestComponent1_2 extends SuperClass implements TestInterface1_1, TestInterface1_2 {
|
||||
/**
|
||||
* Creates the component with no dependencies.
|
||||
*/
|
||||
public TestComponent1_2() {
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -7,6 +7,11 @@ import jakarta.inject.Singleton;
|
||||
*/
|
||||
@Singleton
|
||||
public class TestComponent2_1 {
|
||||
/**
|
||||
* Requires a string the scanner cannot resolve.
|
||||
*
|
||||
* @param test unresolved constructor dependency
|
||||
*/
|
||||
public TestComponent2_1(String test) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user