From e616a53540f63273f808b6be1ac125e9ed260c62 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 6 Feb 2017 14:14:11 +0100 Subject: [PATCH] instantiation integrity checks --- .editorconfig | 11 ++ pom.xml | 24 ++- .../de/joshavg/simpledic/Instantiator.java | 31 ++++ .../de/joshavg/simpledic/IntegrityCheck.java | 142 ++++++++++++++++++ .../de/joshavg/simpledic/SdiContainer.java | 98 ++++-------- .../joshavg/simpledic/ServiceDefinition.java | 37 +++++ .../exception/ClassNotRegistered.java | 8 + .../DependencyNotSatisfiedException.java | 7 - .../NoDefaultConstructorException.java | 7 - .../exception/SdicClassNotFoundException.java | 7 - .../exception/SdicInstantiationException.java | 7 + .../integrity/DependencyCycleDetected.java | 8 + .../integrity/DependencyNotSatisfied.java | 7 + .../DuplicatedServiceClassesFound.java | 5 + .../integrity/MoreThanOneConstructor.java | 8 + .../integrity/SdicClassNotFound.java | 7 + .../java/de/joshavg/simpledic/ErrorTests.java | 50 ++++++ .../joshavg/simpledic/IntegrityCheckTest.java | 18 +++ .../de/joshavg/simpledic/NoDependencies.java | 4 - .../java/de/joshavg/simpledic/ServiceOne.java | 6 - .../simpledic/SimpleDependenciesTest.java | 20 +++ .../de/joshavg/simpledic/TestProperties.java | 11 -- .../joshavg/simpledic/services/Depends1.java | 7 + .../joshavg/simpledic/services/Depends2.java | 7 + .../joshavg/simpledic/services/Depends3.java | 7 + .../services/DependsOnNoDependencies.java | 6 + .../simpledic/services/NoDependencies.java | 4 + .../services/PrivateConstructor.java | 8 + src/test/resources/almostsane.properties | 3 + src/test/resources/cycle.properties | 3 + src/test/resources/duplicated.properties | 3 + src/test/resources/test.properties | 2 - src/test/resources/unknowndep.properties | 1 + 33 files changed, 461 insertions(+), 113 deletions(-) create mode 100644 .editorconfig create mode 100644 src/main/java/de/joshavg/simpledic/Instantiator.java create mode 100644 src/main/java/de/joshavg/simpledic/IntegrityCheck.java create mode 100644 src/main/java/de/joshavg/simpledic/ServiceDefinition.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/ClassNotRegistered.java delete mode 100644 src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java delete mode 100644 src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java delete mode 100644 src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/SdicInstantiationException.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/integrity/DependencyCycleDetected.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/integrity/DependencyNotSatisfied.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/integrity/DuplicatedServiceClassesFound.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/integrity/MoreThanOneConstructor.java create mode 100644 src/main/java/de/joshavg/simpledic/exception/integrity/SdicClassNotFound.java create mode 100644 src/test/java/de/joshavg/simpledic/ErrorTests.java create mode 100644 src/test/java/de/joshavg/simpledic/IntegrityCheckTest.java delete mode 100644 src/test/java/de/joshavg/simpledic/NoDependencies.java delete mode 100644 src/test/java/de/joshavg/simpledic/ServiceOne.java create mode 100644 src/test/java/de/joshavg/simpledic/SimpleDependenciesTest.java delete mode 100644 src/test/java/de/joshavg/simpledic/TestProperties.java create mode 100644 src/test/java/de/joshavg/simpledic/services/Depends1.java create mode 100644 src/test/java/de/joshavg/simpledic/services/Depends2.java create mode 100644 src/test/java/de/joshavg/simpledic/services/Depends3.java create mode 100644 src/test/java/de/joshavg/simpledic/services/DependsOnNoDependencies.java create mode 100644 src/test/java/de/joshavg/simpledic/services/NoDependencies.java create mode 100644 src/test/java/de/joshavg/simpledic/services/PrivateConstructor.java create mode 100644 src/test/resources/almostsane.properties create mode 100644 src/test/resources/cycle.properties create mode 100644 src/test/resources/duplicated.properties delete mode 100644 src/test/resources/test.properties create mode 100644 src/test/resources/unknowndep.properties diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..88a058b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_style = space +indent_size = 4 +tab_width = 4 +trim_trailing_whitespace = true +max_line_length = 120 diff --git a/pom.xml b/pom.xml index e27e8c8..88fa5b8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 de.joshavg @@ -23,10 +23,28 @@ + + org.slf4j + slf4j-api + 1.7.21 + provided + + + org.slf4j + slf4j-simple + 1.7.21 + junit junit 4.12 + test + + + org.hamcrest + hamcrest-core + 1.3 + test com.google.collections @@ -36,4 +54,4 @@ - \ No newline at end of file + diff --git a/src/main/java/de/joshavg/simpledic/Instantiator.java b/src/main/java/de/joshavg/simpledic/Instantiator.java new file mode 100644 index 0000000..610d285 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/Instantiator.java @@ -0,0 +1,31 @@ +package de.joshavg.simpledic; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +class Instantiator { + + private final Class clz; + + Instantiator(Class clz) { + this.clz = clz; + } + + T createInstance() + throws IllegalAccessException, InvocationTargetException, InstantiationException { + @SuppressWarnings("unchecked") + Constructor constructor = (Constructor) clz.getDeclaredConstructors()[0]; + + Class[] parameterTypes = constructor.getParameterTypes(); + Object[] parameters = new Object[parameterTypes.length]; + + for (int i = 0; i < parameterTypes.length; ++i) { + @SuppressWarnings("unchecked") + Class paramClz = (Class) parameterTypes[i]; + parameters[i] = new Instantiator<>(paramClz).createInstance(); + } + + return constructor.newInstance(parameters); + } + +} diff --git a/src/main/java/de/joshavg/simpledic/IntegrityCheck.java b/src/main/java/de/joshavg/simpledic/IntegrityCheck.java new file mode 100644 index 0000000..ce0d5c2 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/IntegrityCheck.java @@ -0,0 +1,142 @@ +package de.joshavg.simpledic; + +import com.google.common.annotations.VisibleForTesting; +import de.joshavg.simpledic.exception.ClassNotRegistered; +import de.joshavg.simpledic.exception.integrity.DependencyCycleDetected; +import de.joshavg.simpledic.exception.integrity.DependencyNotSatisfied; +import de.joshavg.simpledic.exception.integrity.DuplicatedServiceClassesFound; +import de.joshavg.simpledic.exception.integrity.MoreThanOneConstructor; +import de.joshavg.simpledic.exception.integrity.SdicClassNotFound; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.Properties; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class IntegrityCheck { + + private static final Logger LOG = LoggerFactory.getLogger(IntegrityCheck.class); + + private final Properties props; + private final List definitions; + + IntegrityCheck(Properties props) { + this.props = props; + this.definitions = new ArrayList<>(); + } + + void check() { + List> services = fetchAllServices(); + checkConstructorDependencies(services); + checkDuplicateServices(services); + checkCycles(); + } + + private void checkDuplicateServices(List> services) { + int size = services.size(); + int distinctSize = services.stream().distinct().collect(Collectors.toList()).size(); + if (size != distinctSize) { + throw new DuplicatedServiceClassesFound(); + } + } + + private List> fetchAllServices() { + return props.entrySet().stream() + .filter(e -> isServiceName(e.getKey().toString())) + .map(entry -> { + String key = entry.getKey().toString(); + String value = entry.getValue().toString(); + + LOG.info("found service class for name {}: {}", key, value); + try { + Class clz = Class.forName(value); + definitions.add(new ServiceDefinition().setClz(clz).setName(value)); + return clz; + } catch (ClassNotFoundException e) { + throw new SdicClassNotFound(e); + } + }).collect(Collectors.toList()); + } + + private void checkConstructorDependencies(List> services) { + services.stream() + // get the first and only constructor + .map(Class::getDeclaredConstructors) + .collect(ArrayList::new, + this::collectConstructors, + List::addAll) + // get all parameter types + .stream() + .map(Constructor::getParameterTypes) + .collect((Supplier>>) ArrayList::new, + (l, arr) -> { + List> types = Arrays.asList(arr); + l.addAll(types); + }, + List::addAll) + // search for services with needed types + .stream() + .distinct() + .forEach((Class t) -> { + LOG.info("found service dependency: {}", t); + if (!services.contains(t)) { + throw new DependencyNotSatisfied(t); + } + }); + } + + private void collectConstructors(ArrayList l, Constructor[] arr) { + Class clz = arr[0].getDeclaringClass(); + if (arr.length > 1) { + throw new MoreThanOneConstructor(clz); + } + l.add(arr[0]); + findDefinition(clz).setConstructor(arr[0]); + } + + private ServiceDefinition findDefinition(Class clz) { + Optional first = definitions.stream() + .filter(d -> d.getClz() == clz).findFirst(); + + if (!first.isPresent()) { + throw new ClassNotRegistered(clz); + } + + return first.get(); + } + + private void checkCycles() { + definitions.forEach(def -> { + checkConstructorParameter(null, def.getClz()); + }); + } + + private void checkConstructorParameter(Class rootClz, Class paramClz) { + if (rootClz == paramClz) { + throw new DependencyCycleDetected(rootClz); + } + + Class[] parameterTypes = findDefinition(paramClz).getConstructor().getParameterTypes(); + for (Class innerParamClz : parameterTypes) { + if (rootClz == null) { + checkConstructorParameter(paramClz, innerParamClz); + } else { + checkConstructorParameter(rootClz, innerParamClz); + } + } + } + + @VisibleForTesting + static boolean isServiceName(String name) { + return !"service.".equals(name) && name.startsWith("service."); + } + + List getDefinitions() { + return definitions; + } +} diff --git a/src/main/java/de/joshavg/simpledic/SdiContainer.java b/src/main/java/de/joshavg/simpledic/SdiContainer.java index 239ec5a..af38e9f 100644 --- a/src/main/java/de/joshavg/simpledic/SdiContainer.java +++ b/src/main/java/de/joshavg/simpledic/SdiContainer.java @@ -1,27 +1,27 @@ package de.joshavg.simpledic; -import com.google.common.annotations.VisibleForTesting; +import de.joshavg.simpledic.exception.ClassNotRegistered; import de.joshavg.simpledic.exception.ContainerInitException; -import de.joshavg.simpledic.exception.DependencyNotSatisfiedException; -import de.joshavg.simpledic.exception.SdicClassNotFoundException; - +import de.joshavg.simpledic.exception.SdicInstantiationException; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.Arrays; +import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Properties; -import java.util.function.Function; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SdiContainer { + private static final Logger LOG = LoggerFactory.getLogger(SdiContainer.class); + private final Properties props; + private final List definitions; - @VisibleForTesting - SdiContainer(Properties props) { + private SdiContainer(Properties props, List definitions) { this.props = props; + this.definitions = definitions; } public static SdiContainer load() { @@ -32,74 +32,40 @@ public class SdiContainer { Properties props = new Properties(); InputStream inputStream = SdiContainer.class.getClassLoader().getResourceAsStream(filename); + if (inputStream == null) { + throw new ContainerInitException("config file " + filename + " not found", null); + } + try { props.load(inputStream); } catch (IOException e) { throw new ContainerInitException("failed loading properties", e); } - SdiContainer container = new SdiContainer(props); - container.integrityCheck(); - return container; + IntegrityCheck integrityCheck = new IntegrityCheck(props); + integrityCheck.check(); + return new SdiContainer(props, integrityCheck.getDefinitions()); } - private void integrityCheck() { - List> services = fetchAllServices(); - checkConstructorDependencies(services); + private List> serviceClasses() { + return definitions.stream().map(ServiceDefinition::getClz).collect(Collectors.toList()); } - private List> fetchAllServices() { - return props.entrySet().stream() - .filter(e -> isServiceName(e.getKey().toString())) - .map(e -> e.getValue().toString()) - .map(n -> { - try { - return Class.forName(n); - } catch (ClassNotFoundException e) { - throw new SdicClassNotFoundException(e); - } - }).collect(Collectors.toList()); - } + public T createInstance(Class clz) { + if (clz == null) { + throw new NullPointerException(); + } - private void checkConstructorDependencies(List> services) { - for(Class c : services) { - Constructor[] constructors = c.getDeclaredConstructors(); - for(Constructor constructor : constructors) { - Class[] parameterTypes = constructor.getParameterTypes(); - System.out.println(Arrays.toString(parameterTypes)); - System.out.println(Arrays.asList(parameterTypes)); - } + LOG.trace("instance ordered: ", clz); + if (!serviceClasses().contains(clz)) { + throw new ClassNotRegistered(clz); } - /* services.stream() - // get all constructors - .map(Class::getDeclaredConstructors) - .reduce(new ArrayList(), - (l, arr) -> { - l.addAll(Arrays.asList(arr)); - return l; - }, - (l1, l2) -> l1) - // get all parameter types - .stream() - .map(Constructor::getParameterTypes) - .reduce(new ArrayList>(), - (l, arr) -> { - l.addAll(Arrays.asList(arr)); - return l; - }, - (l1, l2) -> l1) - // search for services with needed types - .stream() - .distinct() - .forEach(t -> { - if (!services.contains(t)) { - throw new DependencyNotSatisfiedException(t); - } - });*/ - } - @VisibleForTesting - static boolean isServiceName(String name) { - return name.startsWith("service."); + try { + return new Instantiator<>(clz).createInstance(); + } catch (IllegalAccessException | InvocationTargetException | InstantiationException e) { + throw new SdicInstantiationException(e); + } } + } diff --git a/src/main/java/de/joshavg/simpledic/ServiceDefinition.java b/src/main/java/de/joshavg/simpledic/ServiceDefinition.java new file mode 100644 index 0000000..1107e44 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/ServiceDefinition.java @@ -0,0 +1,37 @@ +package de.joshavg.simpledic; + +import java.lang.reflect.Constructor; + +class ServiceDefinition { + + private Class clz; + private String name; + private Constructor constructor; + + public Class getClz() { + return clz; + } + + public ServiceDefinition setClz(Class clz) { + this.clz = clz; + return this; + } + + public String getName() { + return name; + } + + public ServiceDefinition setName(String name) { + this.name = name; + return this; + } + + public Constructor getConstructor() { + return constructor; + } + + public ServiceDefinition setConstructor(Constructor constructor) { + this.constructor = constructor; + return this; + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/ClassNotRegistered.java b/src/main/java/de/joshavg/simpledic/exception/ClassNotRegistered.java new file mode 100644 index 0000000..9021b9c --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/ClassNotRegistered.java @@ -0,0 +1,8 @@ +package de.joshavg.simpledic.exception; + +public class ClassNotRegistered extends RuntimeException { + + public ClassNotRegistered(Class clz) { + super(String.format("class not registered in container: %s", clz)); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java b/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java deleted file mode 100644 index 54119d8..0000000 --- a/src/main/java/de/joshavg/simpledic/exception/DependencyNotSatisfiedException.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.joshavg.simpledic.exception; - -public class DependencyNotSatisfiedException extends RuntimeException { - public DependencyNotSatisfiedException(Class dependency) { - super(String.format("Dependency %s not satisfied", dependency.getName())); - } -} diff --git a/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java b/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java deleted file mode 100644 index b06a330..0000000 --- a/src/main/java/de/joshavg/simpledic/exception/NoDefaultConstructorException.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.joshavg.simpledic.exception; - -public class NoDefaultConstructorException extends RuntimeException { - public NoDefaultConstructorException(Throwable cause) { - super(cause); - } -} diff --git a/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java b/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java deleted file mode 100644 index 924b78c..0000000 --- a/src/main/java/de/joshavg/simpledic/exception/SdicClassNotFoundException.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.joshavg.simpledic.exception; - -public class SdicClassNotFoundException extends RuntimeException { - public SdicClassNotFoundException(Throwable cause) { - super(cause); - } -} diff --git a/src/main/java/de/joshavg/simpledic/exception/SdicInstantiationException.java b/src/main/java/de/joshavg/simpledic/exception/SdicInstantiationException.java new file mode 100644 index 0000000..ed8554f --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/SdicInstantiationException.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception; + +public class SdicInstantiationException extends RuntimeException { + public SdicInstantiationException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyCycleDetected.java b/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyCycleDetected.java new file mode 100644 index 0000000..fefd84c --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyCycleDetected.java @@ -0,0 +1,8 @@ +package de.joshavg.simpledic.exception.integrity; + +public class DependencyCycleDetected extends RuntimeException { + + public DependencyCycleDetected(Class clz) { + super(String.format("%s has a dependency cycle", clz)); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyNotSatisfied.java b/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyNotSatisfied.java new file mode 100644 index 0000000..510b555 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/integrity/DependencyNotSatisfied.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception.integrity; + +public class DependencyNotSatisfied extends RuntimeException { + public DependencyNotSatisfied(Class dependency) { + super(String.format("Dependency %s not satisfied", dependency.getName())); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/integrity/DuplicatedServiceClassesFound.java b/src/main/java/de/joshavg/simpledic/exception/integrity/DuplicatedServiceClassesFound.java new file mode 100644 index 0000000..a9148c6 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/integrity/DuplicatedServiceClassesFound.java @@ -0,0 +1,5 @@ +package de.joshavg.simpledic.exception.integrity; + +public class DuplicatedServiceClassesFound extends RuntimeException { + +} diff --git a/src/main/java/de/joshavg/simpledic/exception/integrity/MoreThanOneConstructor.java b/src/main/java/de/joshavg/simpledic/exception/integrity/MoreThanOneConstructor.java new file mode 100644 index 0000000..0fac6a5 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/integrity/MoreThanOneConstructor.java @@ -0,0 +1,8 @@ +package de.joshavg.simpledic.exception.integrity; + +public class MoreThanOneConstructor extends RuntimeException { + + public MoreThanOneConstructor(Class clz) { + super(String.format("class %s has more than one constructor", clz)); + } +} diff --git a/src/main/java/de/joshavg/simpledic/exception/integrity/SdicClassNotFound.java b/src/main/java/de/joshavg/simpledic/exception/integrity/SdicClassNotFound.java new file mode 100644 index 0000000..b3f27b4 --- /dev/null +++ b/src/main/java/de/joshavg/simpledic/exception/integrity/SdicClassNotFound.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.exception.integrity; + +public class SdicClassNotFound extends RuntimeException { + public SdicClassNotFound(Throwable cause) { + super(cause); + } +} diff --git a/src/test/java/de/joshavg/simpledic/ErrorTests.java b/src/test/java/de/joshavg/simpledic/ErrorTests.java new file mode 100644 index 0000000..9ee50ac --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/ErrorTests.java @@ -0,0 +1,50 @@ +package de.joshavg.simpledic; + +import de.joshavg.simpledic.exception.ClassNotRegistered; +import de.joshavg.simpledic.exception.ContainerInitException; +import de.joshavg.simpledic.exception.SdicInstantiationException; +import de.joshavg.simpledic.exception.integrity.DependencyCycleDetected; +import de.joshavg.simpledic.exception.integrity.DependencyNotSatisfied; +import de.joshavg.simpledic.exception.integrity.DuplicatedServiceClassesFound; +import de.joshavg.simpledic.services.Depends1; +import de.joshavg.simpledic.services.PrivateConstructor; +import java.util.Map; +import org.junit.Test; + +public class ErrorTests { + + @Test(expected = DuplicatedServiceClassesFound.class) + public void testDuplicationDetection() { + SdiContainer.load("duplicated.properties"); + } + + @Test(expected = DependencyCycleDetected.class) + public void testCycleDetection() { + SdiContainer.load("cycle.properties").createInstance(Depends1.class); + } + + @Test(expected = ClassNotRegistered.class) + public void requestUnknownClass() { + SdiContainer.load("almostsane.properties").createInstance(Map.class); + } + + @Test(expected = DependencyNotSatisfied.class) + public void requestServiceWithUnregisteredDependency() { + SdiContainer.load("unknowndep.properties").createInstance(Depends1.class); + } + + @Test(expected = SdicInstantiationException.class) + public void privateConstructor() { + SdiContainer.load("almostsane.properties").createInstance(PrivateConstructor.class); + } + + @Test(expected = NullPointerException.class) + public void requestNull() { + SdiContainer.load("almostsane.properties").createInstance(null); + } + + @Test(expected = ContainerInitException.class) + public void loadUnknownFile() { + SdiContainer.load("unknown.properties"); + } +} diff --git a/src/test/java/de/joshavg/simpledic/IntegrityCheckTest.java b/src/test/java/de/joshavg/simpledic/IntegrityCheckTest.java new file mode 100644 index 0000000..ade3d9e --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/IntegrityCheckTest.java @@ -0,0 +1,18 @@ +package de.joshavg.simpledic; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +public class IntegrityCheckTest { + + @Test + public void servicesStartWithServiceDot() { + assertThat(IntegrityCheck.isServiceName("dingens"), is(false)); + assertThat(IntegrityCheck.isServiceName("service"), is(false)); + assertThat(IntegrityCheck.isServiceName("service."), is(false)); + assertThat(IntegrityCheck.isServiceName("service.a"), is(true)); + } + +} diff --git a/src/test/java/de/joshavg/simpledic/NoDependencies.java b/src/test/java/de/joshavg/simpledic/NoDependencies.java deleted file mode 100644 index 865cbcd..0000000 --- a/src/test/java/de/joshavg/simpledic/NoDependencies.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.joshavg.simpledic; - -public class NoDependencies { -} diff --git a/src/test/java/de/joshavg/simpledic/ServiceOne.java b/src/test/java/de/joshavg/simpledic/ServiceOne.java deleted file mode 100644 index 959efc3..0000000 --- a/src/test/java/de/joshavg/simpledic/ServiceOne.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.joshavg.simpledic; - -public class ServiceOne { - public ServiceOne(NoDependencies b) { - } -} diff --git a/src/test/java/de/joshavg/simpledic/SimpleDependenciesTest.java b/src/test/java/de/joshavg/simpledic/SimpleDependenciesTest.java new file mode 100644 index 0000000..554e59e --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/SimpleDependenciesTest.java @@ -0,0 +1,20 @@ +package de.joshavg.simpledic; + +import de.joshavg.simpledic.services.DependsOnNoDependencies; +import org.junit.Test; + +public class SimpleDependenciesTest { + + private static final String FILENAME = "almostsane.properties"; + + @Test + public void testIntegrityCheck() { + SdiContainer.load(FILENAME); + } + + @Test + public void createOneDependencyService() { + SdiContainer container = SdiContainer.load(FILENAME); + container.createInstance(DependsOnNoDependencies.class); + } +} diff --git a/src/test/java/de/joshavg/simpledic/TestProperties.java b/src/test/java/de/joshavg/simpledic/TestProperties.java deleted file mode 100644 index b9d0f87..0000000 --- a/src/test/java/de/joshavg/simpledic/TestProperties.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.joshavg.simpledic; - -import org.junit.Test; - -public class TestProperties { - - @Test - public void testIntegrityCheck() { - SdiContainer.load("test.properties"); - } -} diff --git a/src/test/java/de/joshavg/simpledic/services/Depends1.java b/src/test/java/de/joshavg/simpledic/services/Depends1.java new file mode 100644 index 0000000..62a8340 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/Depends1.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.services; + +public class Depends1 { + + public Depends1(Depends2 d) { + } +} diff --git a/src/test/java/de/joshavg/simpledic/services/Depends2.java b/src/test/java/de/joshavg/simpledic/services/Depends2.java new file mode 100644 index 0000000..300ddc1 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/Depends2.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.services; + +public class Depends2 { + + public Depends2(Depends3 d) { + } +} diff --git a/src/test/java/de/joshavg/simpledic/services/Depends3.java b/src/test/java/de/joshavg/simpledic/services/Depends3.java new file mode 100644 index 0000000..5417596 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/Depends3.java @@ -0,0 +1,7 @@ +package de.joshavg.simpledic.services; + +public class Depends3 { + + public Depends3(Depends1 d) { + } +} diff --git a/src/test/java/de/joshavg/simpledic/services/DependsOnNoDependencies.java b/src/test/java/de/joshavg/simpledic/services/DependsOnNoDependencies.java new file mode 100644 index 0000000..c2e7ced --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/DependsOnNoDependencies.java @@ -0,0 +1,6 @@ +package de.joshavg.simpledic.services; + +public class DependsOnNoDependencies { + public DependsOnNoDependencies(NoDependencies b) { + } +} diff --git a/src/test/java/de/joshavg/simpledic/services/NoDependencies.java b/src/test/java/de/joshavg/simpledic/services/NoDependencies.java new file mode 100644 index 0000000..ed1ce59 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/NoDependencies.java @@ -0,0 +1,4 @@ +package de.joshavg.simpledic.services; + +public class NoDependencies { +} diff --git a/src/test/java/de/joshavg/simpledic/services/PrivateConstructor.java b/src/test/java/de/joshavg/simpledic/services/PrivateConstructor.java new file mode 100644 index 0000000..173c190 --- /dev/null +++ b/src/test/java/de/joshavg/simpledic/services/PrivateConstructor.java @@ -0,0 +1,8 @@ +package de.joshavg.simpledic.services; + +public class PrivateConstructor { + + private PrivateConstructor() { + } + +} diff --git a/src/test/resources/almostsane.properties b/src/test/resources/almostsane.properties new file mode 100644 index 0000000..3700df0 --- /dev/null +++ b/src/test/resources/almostsane.properties @@ -0,0 +1,3 @@ +service.one: de.joshavg.simpledic.services.DependsOnNoDependencies +service.nodeps: de.joshavg.simpledic.services.NoDependencies +service.private: de.joshavg.simpledic.services.PrivateConstructor diff --git a/src/test/resources/cycle.properties b/src/test/resources/cycle.properties new file mode 100644 index 0000000..c291799 --- /dev/null +++ b/src/test/resources/cycle.properties @@ -0,0 +1,3 @@ +service.dep1:de.joshavg.simpledic.services.Depends1 +service.dep2:de.joshavg.simpledic.services.Depends2 +service.dep3:de.joshavg.simpledic.services.Depends3 diff --git a/src/test/resources/duplicated.properties b/src/test/resources/duplicated.properties new file mode 100644 index 0000000..ae7fc6c --- /dev/null +++ b/src/test/resources/duplicated.properties @@ -0,0 +1,3 @@ +service.one:de.joshavg.simpledic.services.DependsOnNoDependencies +service.nodeps:de.joshavg.simpledic.services.NoDependencies +service.nodeps2:de.joshavg.simpledic.services.NoDependencies diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties deleted file mode 100644 index 41055f8..0000000 --- a/src/test/resources/test.properties +++ /dev/null @@ -1,2 +0,0 @@ -service.one: de.joshavg.simpledic.ServiceOne -service.nodeps: de.joshavg.simpledic.NoDependencies diff --git a/src/test/resources/unknowndep.properties b/src/test/resources/unknowndep.properties new file mode 100644 index 0000000..cd79132 --- /dev/null +++ b/src/test/resources/unknowndep.properties @@ -0,0 +1 @@ +service.dep1: de.joshavg.simpledic.services.Depends1