Skip to content

Commit

Permalink
Types register as InterfaceAndProvider must not have default constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jwdeveloper committed Jun 29, 2024
1 parent 54222f8 commit e20e312
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 52 deletions.
40 changes: 17 additions & 23 deletions DepenDance-Full/src/test/java/common/ContainerTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@


public abstract class ContainerTestBase {
public static DependanceContainerBuilder builder;

@Before
public void before() {
builder = Dependance.newContainer();
}

public DependanceContainerBuilder builder() {
return Dependance.newContainer();
}

protected void shouldBeNull(Container container, Class<?> clazz) {
var instance1 = container.find(clazz);
Expand Down Expand Up @@ -66,33 +64,29 @@ protected void shouldBeEqualsInstance(Container container, Class<?> clazz, Objec
Assert.assertEquals(instance, instance1);
}

protected void shouldThrows(Class<? extends Throwable> throwablClass, Runnable action)
{
try
{
protected void shouldThrows(Class<? extends Throwable> throwablClass, Runnable action) {
try {
action.run();
}
catch (Exception e)
{
if(throwablClass.equals(e.getClass()))
{
} catch (Exception e) {
if (throwablClass.equals(e.getClass())) {
return;
}
Throwable cause = e.getCause();
do {

Throwable cause = null;
do
{
cause = e.getCause();
if (cause == null) {
break;
}

if(cause.getClass().equals(throwablClass))
{
return;
}
if (cause.getClass().equals(throwablClass)) {
return;
}
cause = cause.getCause();
}
while (cause != null);

throw e;
}

throw new RuntimeException("Exception " + throwablClass.getSimpleName() + "has not been thrown!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,32 @@
import io.github.jwdeveloper.dependance.injector.api.exceptions.ContainerException;
import io.github.jwdeveloper.dependance.injector.api.exceptions.DeathCycleException;
import io.github.jwdeveloper.dependance.injector.api.exceptions.InjectionNotFoundException;
import io.github.jwdeveloper.dependance.injector.api.exceptions.NoConsturctorException;
import io.github.jwdeveloper.dependance.injector.implementation.utilites.Messages;
import org.junit.Assert;
import org.junit.Test;

import java.util.logging.Logger;

public class ContainerExceptionsTests extends ContainerTestBase {

@Test
public void shouldThrowWhenClassHasNoConstructor() {
shouldThrows(NoConsturctorException.class, () ->
{
var container = builder()
.registerSingleton(Logger.class)
.build();
container.find(Logger.class);
});
}


@Test
public void shouldReturnNullWhenNotRegistered() {
shouldThrows(InjectionNotFoundException.class, () ->
{
var container = builder.build();
var container = builder().build();
container.find(ExampleClass.class);
});
}
Expand All @@ -51,7 +67,7 @@ public void shouldReturnNullWhenNotRegistered() {
public void shouldThrowWhenDeathCycle() {
shouldThrows(DeathCycleException.class, () ->
{
builder.registerSingleton(CycleDependencyExample.class)
builder().registerSingleton(CycleDependencyExample.class)
.registerSingleton(A.class)
.registerSingleton(B.class)
.registerSingleton(C.class)
Expand All @@ -63,7 +79,7 @@ public void shouldThrowWhenDeathCycle() {
public void shouldThrowWhenThereAreMoreConstructorsWithoutAnnotation() {
Assert.assertThrows(Messages.INJECTION_USE_ANNOTATION_WITH_MORE_CONSTUROCTORS, ContainerException.class, () ->
{
builder.registerSingleton(ClassWithMoreConstructors.class)
builder().registerSingleton(ClassWithMoreConstructors.class)
.build();
});
}
Expand All @@ -73,7 +89,7 @@ public void shouldThrowsWhenClassHaveTypeInConstructorThatIsNotRegistered() {

Assert.assertThrows(Messages.INJECTION_CANT_BE_CREATED, ContainerException.class, () ->
{
var container = builder.registerSingleton(ClassWithParamterNotRegistered.class)
var container = builder().registerSingleton(ClassWithParamterNotRegistered.class)
.build();
container.find(ClassWithParamterNotRegistered.class);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ContainerSearchTests extends ContainerTestBase {
@Test
public void ShouldFindByClassesCommonInterface() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleCommonInterface.class, ExampleClass.class)
.registerSingleton(ExampleCommonInterface.class, ExampleClassV2.class)
.registerSingleton(ExampleCommonInterface.class, ExampleClassV3.class)
Expand All @@ -52,7 +52,7 @@ public void ShouldFindByClassesCommonInterface() {
@Test
public void shouldFindBySuperInterface() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleCommonInterface.class, ExampleClass.class)
.registerSingleton(ExampleCommonInterface.class, ExampleClassV2.class)
.registerSingleton(ExampleCommonInterface.class, ExampleClassV3.class)
Expand All @@ -72,7 +72,7 @@ public void shouldFindBySuperInterface() {
@Test
public void shouldFindBySuperInterfaceWithObjectInstance() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleInterface.class, new ExampleClass())
.registerSingleton(ExampleInterface.class, new ExampleClassV2())
.registerSingleton(ExampleInterface.class, new ExampleClassV3())
Expand All @@ -92,7 +92,7 @@ public void shouldFindBySuperInterfaceWithObjectInstance() {
@Test
public void ShouldFindBySuperClass() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class)
.registerSingleton(ExampleClassV2.class)
.build();
Expand All @@ -109,7 +109,7 @@ public void ShouldFindBySuperClass() {
@Test
public void ShouldFindBySuperSuperClass() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class)
.registerSingleton(ExampleClassV2.class)
.build();
Expand All @@ -127,7 +127,7 @@ public void ShouldFindBySuperSuperClass() {
@Test
public void ShouldFindByAnnotation() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class)
.registerSingleton(ExampleClassV2.class)
.build();
Expand Down
18 changes: 9 additions & 9 deletions DepenDance-Full/src/test/java/containers/ContainerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ContainerTests extends ContainerTestBase {
@Test
public void shouldRegisterWithType() {
//Arrange
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class)
.registerTransient(ExampleClassV2.class)
.build();
Expand All @@ -48,7 +48,7 @@ public void shouldRegisterWithType() {

@Test
public void shouldRegisterWithTypeAndImplementation() {
var container = builder
var container = builder()
.registerSingleton(ExampleInterface.class, ExampleClass.class)
.registerTransient(ExampleInterfaceV2.class, ExampleClassV2.class)
.build();
Expand All @@ -62,7 +62,7 @@ public void shouldRegisterWithTypeAndImplementation() {
@Test
public void shouldRegisterWithInstance() {
var instance = new ExampleClass();
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class, instance)
.build();
shouldBeNotNull(container, ExampleClass.class);
Expand All @@ -74,7 +74,7 @@ public void shouldRegisterWithInstance() {
public void shouldRegisterWithProvider() {

var instance = new ExampleClass();
var container = builder
var container = builder()
.registerSingleton(ExampleClass.class, (e) -> instance)
.registerTransient(ExampleClassV2.class, (e) -> new ExampleClassV2())
.build();
Expand All @@ -88,7 +88,7 @@ public void shouldRegisterWithProvider() {
@Test
public void shouldRegisterSingletonWithList() {

var container = builder
var container = builder()
.registerSingleton(ExampleCommonInterface.class, ExampleClass.class)
.registerTransient(ExampleCommonInterface.class, ExampleClassV2.class)
.registerTransient(ExampleCommonInterface.class, ExampleClassV3.class)
Expand All @@ -107,7 +107,7 @@ public void shouldRegisterSingletonWithList() {
@Test
public void shouldRegisterTransientWithList() {

var container = builder
var container = builder()
.registerSingleton(ExampleCommonInterface.class, ExampleClass.class)
.registerTransient(ExampleCommonInterface.class, ExampleClassV2.class)

Expand All @@ -124,7 +124,7 @@ public void shouldRegisterTransientWithList() {

@Test
public void shouldPassListToObjectConstructor() {
var container = builder
var container = builder()
.registerTransient(ExampleCommonInterface.class, ExampleClass.class)
.registerTransient(ExampleCommonInterface.class, ExampleClassV2.class)
.registerTransientList(ExampleCommonInterface.class)
Expand All @@ -137,7 +137,7 @@ public void shouldPassListToObjectConstructor() {

@Test
public void shouldRegister2DifferentLists() {
var container = builder
var container = builder()
.registerSingleton(ExampleInterface.class, new ExampleClass())
.registerSingleton(ExampleInterface.class, new ExampleClass())
.registerSingleton(ExampleInterface.class, new ExampleClass())
Expand All @@ -155,7 +155,7 @@ public void shouldRegister2DifferentLists() {

@Test
public void shouldHandleCaseWhenGenericTypeIsSame() {
var container = builder
var container = builder()
.registerSingleton(ExampleInterface.class, new ExampleClass())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.jwdeveloper.dependance.injector.api.exceptions;

public class NoConsturctorException extends ContainerException
{
public NoConsturctorException() {
}

public NoConsturctorException(String message) {
super(message);
}

public NoConsturctorException(String message, Object... values) {
super(message, values);
}

public NoConsturctorException(String message, Throwable cause) {
super(message, cause);
}

public NoConsturctorException(Throwable cause) {
super(cause);
}

public NoConsturctorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean register(RegistrationInfo registrationInfo) {
if (registrationInfo._interface() != null) {
clazz += registrationInfo._interface().getName();
}
if (registrationInfo._interface() != null) {
if (registrationInfo.implementation() != null) {
clazz += " " + registrationInfo.implementation().getName();
}
throw new ContainerException(String.format(Messages.INJECTION_CANT_REGISTER, clazz), exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.github.jwdeveloper.dependance.injector.api.annotations.Inject;
import io.github.jwdeveloper.dependance.injector.api.exceptions.ContainerException;
import io.github.jwdeveloper.dependance.injector.api.exceptions.DeathCycleException;
import io.github.jwdeveloper.dependance.injector.api.exceptions.NoConsturctorException;
import io.github.jwdeveloper.dependance.injector.api.factory.InjectionInfoFactory;
import io.github.jwdeveloper.dependance.injector.api.models.InjectionInfo;
import io.github.jwdeveloper.dependance.injector.api.models.RegistrationInfo;
Expand All @@ -47,7 +48,6 @@ public Pair<Class<?>, InjectionInfo> create(RegistrationInfo info) throws Except
case InterfaceAndProvider -> InterfaceAndProviderStrategy(info);
case List -> ListStrategy(info);
};

}

private Pair<Class<?>, InjectionInfo> OnlyImplStrategy(RegistrationInfo info) throws Exception {
Expand Down Expand Up @@ -79,7 +79,7 @@ private Pair<Class<?>, InjectionInfo> PrepareInjectionInfo(Class<?> impl, Class<
var constructor = getConstructor(impl);
throwIfCycleDependency(constructor, impl, impl);

var extendedTypes = getExtentedTypes(impl);
var extendedTypes = getExtendedTypes(impl);
var implementedTypes = getImplementedTypes(impl);

var annotationsClasses = new HashSet<>(extendedTypes);
Expand Down Expand Up @@ -109,7 +109,7 @@ private Pair<Class<?>, InjectionInfo> InterfaceAndProviderStrategy(RegistrationI
result.setInjectionValueType(Function.class);


var extendedTypes = getExtentedTypes(_interface);
var extendedTypes = getExtendedTypes(_interface);
var implementedTypes = getImplementedTypes(_interface);
implementedTypes.add(_interface);
extendedTypes.add(_interface);
Expand Down Expand Up @@ -170,7 +170,7 @@ private Set<Class<? extends Annotation>> getAnnotations(Class<?> type, Set<Class
return annotations;
}

private Set<Class<?>> getExtentedTypes(Class<?> type) {
private Set<Class<?>> getExtendedTypes(Class<?> type) {
var superClassTypes = new HashSet<Class<?>>();
var subClass = type.getSuperclass();
while (subClass != null && !subClass.equals(Object.class)) {
Expand All @@ -192,7 +192,7 @@ private Constructor getConstructor(Class<?> _class) {

return consturctor;
}
throw new ContainerException(Messages.INJECTION_USE_ANNOTATION_WITH_MORE_CONSTUROCTORS);
throw new NoConsturctorException(Messages.INJECTION_USE_ANNOTATION_WITH_MORE_CONSTUROCTORS);
}

private Field[] getInjectedFields(Class<?> _class) {
Expand All @@ -214,8 +214,15 @@ private void throwIfCycleDependency(Constructor constructor, Class<?> current, C
if (param.isInterface()) {
continue;
}
var ctr = getConstructor(param);
throwIfCycleDependency(ctr, param, root);

try {
var ctr = getConstructor(param);
throwIfCycleDependency(ctr, param, root);
}
catch (NoConsturctorException e)
{
//pass
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Messages


public static final String INJECTION_ALREADY_EXISTS = "Class %s has been already register inside Dependency Injection container";
public static final String INJECTION_CANT_BE_CREATED = "Can not create instance injection of type type %s";
public static final String INJECTION_CANT_REGISTER = "Can not register injection of type type %s";
public static final String INJECTION_CANT_BE_CREATED = "Can not create instance injection of type %s";
public static final String INJECTION_CANT_REGISTER = "Can not register injection of type %s";

public static final String INJECTION_LIST_WITH_INTERFACE = "Only Interface can be use as List parameter %s";
public static final String INJECTION_USE_ANNOTATION_WITH_MORE_CONSTUROCTORS = "You need to use annotation Inject.class while there is more then one constructor";
Expand Down

0 comments on commit e20e312

Please sign in to comment.