Skip to content

Commit

Permalink
General tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
P3ridot committed Jul 19, 2023
1 parent d01b1f3 commit 1b7ccb3
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class InjectorProcessor {
private final Injector injector;
private final Map<Executable, Annotation[]> injectableCache = new HashMap<>();

final Bind<Annotation> autoConstructBind;
private final Bind<Annotation> autoConstructBind;

InjectorProcessor(Injector injector) {
this.injector = injector;
Expand Down Expand Up @@ -188,4 +188,8 @@ protected Bind<Annotation> fetchBind(@Nullable Annotation annotation, Property p
return handlers;
}

Bind<Annotation> getAutoConstructBind() {
return this.autoConstructBind;
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.panda_lang.utilities.inject;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Inject;
import org.panda_lang.utilities.inject.annotations.Injectable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

final class DependencyInjectionFieldsTest {

@Injectable
@Retention(RetentionPolicy.RUNTIME)
private @interface Custom { }
private @interface Custom {}

private static class Service extends AbstractService {

Expand Down Expand Up @@ -68,7 +68,7 @@ void shouldInjectFields() {
resources.annotatedWithTested(Custom.class).assignHandler((property, custom, objects) -> objects[0].toString());
});

Service service = injector.newInstanceWithFields(Service.class,"custom argument");
Service service = injector.newInstanceWithFields(Service.class, "custom argument");
assertEquals("Hello Field 7", service.serve());
assertEquals("1.2 254623242914889729", service.serveAbstract());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class DependencyInjectionHandlerTest {

@Injectable
@Retention(RetentionPolicy.RUNTIME)
private @interface Custom { }
private @interface Custom {}

@Injectable
@Retention(RetentionPolicy.RUNTIME)
private @interface TestAnnotation { }
private @interface TestAnnotation {}

private static class Service1 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ void shouldNotInjectInstances() {
});
});

assertThrows(InvalidParameterException.class, () -> injector.forConstructor(InvalidClass.class).newInstance(), "Class has contain one and only one constructor");
assertThrows(InvalidParameterException.class, () -> injector.forConstructor(InvalidClass.class).newInstance(), "Class has to contain one and only constructor");
assertThrows(DependencyInjectionException.class, () -> injector.newInstance(Service.class));
}

private static class Bean { }
private static class Bean {}

private interface Custom { }
private static class CustomImpl implements Custom { }
private interface Custom {}

private static class CustomImpl implements Custom {}

private static class Service {

public Service(Bean bean, Custom custom) {
assertNotNull(bean);
assertNotNull(custom);
}

}

private static class InvalidClass { // 2 constructors (only 1 is allowed)

public InvalidClass(Bean bean, Custom custom) {
assertNotNull(bean);
assertNotNull(custom);
Expand All @@ -57,6 +61,7 @@ public InvalidClass(Bean bean, Custom custom) {
public InvalidClass(Bean bean) {
assertNotNull(bean);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package org.panda_lang.utilities.inject;

import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Injectable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Injectable;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DependencyInjectionUtilsTest {

Expand All @@ -36,18 +36,18 @@ void testAnnotation() {
assertDoesNotThrow(() -> DependencyInjectionUtils.testAnnotation(InjectableAnnotation.class));
}

private static class NotAnAnnotation { }
private static class NotAnAnnotation {}

private @interface DefaultAnnotation { }
private @interface DefaultAnnotation {}

@Retention(RetentionPolicy.CLASS)
private @interface ClassAnnotation { }
private @interface ClassAnnotation {}

@Retention(RetentionPolicy.RUNTIME)
private @interface RuntimeAnnotation { }
private @interface RuntimeAnnotation {}

@Injectable
@Retention(RetentionPolicy.RUNTIME)
private @interface InjectableAnnotation { }
private @interface InjectableAnnotation {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package org.panda_lang.utilities.inject;

import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Inject;
import org.panda_lang.utilities.inject.annotations.Injectable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Inject;
import org.panda_lang.utilities.inject.annotations.Injectable;

final class DependencyInjectionWikiTest {

Expand Down Expand Up @@ -53,9 +52,10 @@ void testWikiExample() {

@Injectable // mark annotation as DI ready annotation
@Retention(RetentionPolicy.RUNTIME) // make sure that the annotation is visible at runtime
private @interface AwesomeRandom { }
private @interface AwesomeRandom {}

private static final class Entity {

private final UUID id;

@Inject //it's not required, but it might be useful to disable "unused method" warnings/scanning for annotations
Expand All @@ -66,6 +66,7 @@ private Entity(@AwesomeRandom UUID random) {
public UUID getId() {
return id;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.AutoConstruct;
import org.panda_lang.utilities.inject.annotations.Injectable;
import org.panda_lang.utilities.inject.shared.AnnotationUtils;
import org.panda_lang.utilities.inject.shared.DummyProperty;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -28,13 +27,26 @@ final class InjectorProcessorTest {

@Injectable
@Retention(RetentionPolicy.RUNTIME)
private @interface TestAnnotation { }
private @interface TestAnnotation {}

private static class TestClass {

public TestClass(int intProp, @TestAnnotation String stringAnnotatedProp) { }
private int intProp;
private float floatProp;
private String stringProp;
@TestAnnotation
private String stringAnnotatedProp;
@AutoConstruct
private Object autoConstructProp;

public TestClass(int intProp, float floatProp) { }
public TestClass(int intProp, @TestAnnotation String stringAnnotatedProp) {}

public TestClass(int intProp, float floatProp) {}

private static PropertyField getProperty(String fieldName) throws NoSuchFieldException {
Field field = TestClass.class.getDeclaredField(fieldName);
return new PropertyField(field);
}

}

Expand All @@ -49,21 +61,21 @@ void shouldFetchAnnotations() {
@Test
void shouldFetchBinds() throws Exception {
// Fetch single
Property intProperty = new DummyProperty<>("intProp", int.class);
Property intProperty = TestClass.getProperty("intProp");
assertDoesNotThrow(() -> this.processor.fetchBind(null, intProperty));

Property stringProperty = new DummyProperty<>("stringProp", String.class);
Property stringProperty = TestClass.getProperty("stringProp");
Bind<Annotation> stringBind = assertDoesNotThrow(() -> this.processor.fetchBind(null, stringProperty));
assertEquals("Test", stringBind.getValue(stringProperty, null));

Property stringAnnotatedProperty = new DummyProperty<>("stringAnnotatedProp", String.class, TestAnnotation.class);
TestAnnotation testAnnotation = AnnotationUtils.instanceAnnotation(TestAnnotation.class);
Property stringAnnotatedProperty = TestClass.getProperty("stringAnnotatedProp");
TestAnnotation testAnnotation = stringAnnotatedProperty.getAnnotation(TestAnnotation.class);
Bind<Annotation> annotatedStringBind = assertDoesNotThrow(() -> this.processor.fetchBind(testAnnotation, stringAnnotatedProperty));
assertEquals("TestAnnotation", annotatedStringBind.getValue(stringAnnotatedProperty, testAnnotation));

Property autoConstructProperty = new DummyProperty<>("autoConstructProp", Object.class, AutoConstruct.class);
Property autoConstructProperty = TestClass.getProperty("autoConstructProp");
Bind<Annotation> autoConstructBind = assertDoesNotThrow(() -> this.processor.fetchBind(null, autoConstructProperty));
assertEquals(this.processor.autoConstructBind, autoConstructBind);
assertEquals(this.processor.getAutoConstructBind(), autoConstructBind);

// Fetch multiple
Constructor<?> testConstructor = TestClass.class.getConstructors()[0];
Expand All @@ -76,18 +88,14 @@ void shouldFetchBinds() throws Exception {
}

@Test
void shouldNotFetchBinds() {
void shouldNotFetchBinds() throws Exception {
// Fetch single
Property floatProperty = new DummyProperty<>("floatProp", float.class);
Property doubleProperty = new DummyProperty<>("doubleProp", double.class);

Property floatProperty = TestClass.getProperty("floatProp");
assertThrows(MissingBindException.class, () -> this.processor.fetchBind(null, floatProperty));
assertThrows(MissingBindException.class, () -> this.processor.fetchBind(null, doubleProperty));

// Fetch multiple
Constructor<?> testConstructor = TestClass.class.getConstructors()[1];
Annotation[] annotations = this.processor.fetchAnnotations(testConstructor);

assertThrows(MissingBindException.class, () -> this.processor.fetchBinds(annotations, testConstructor));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public void failMethod() {
}

@Retention(RetentionPolicy.RUNTIME)
private @interface TestAnnotation { }
private @interface TestAnnotation {}

@Retention(RetentionPolicy.RUNTIME)
private @interface TestAnnotation2 { }
private @interface TestAnnotation2 {}

@Test
void shouldInvokeMethods() throws NoSuchMethodException {
void shouldInvokeMethods() {
Injector injector = DependencyInjection.createInjector(resources -> {
resources.on(int.class).assignInstance(3);
});
Expand All @@ -61,7 +61,7 @@ void shouldInvokeMethods() throws NoSuchMethodException {
}

@Test
void shouldNotInvokeMethods() throws NoSuchMethodException {
void shouldNotInvokeMethods() {
Injector injector = DependencyInjection.createInjector(resources -> {
resources.on(int.class).assignInstance(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

final class AutoConstructTest {

Expand Down

This file was deleted.

Loading

0 comments on commit 1b7ccb3

Please sign in to comment.