Commit 64bdcb78 authored by Nicolai Ommer's avatar Nicolai Ommer

getModule and isModuleLoaded work for implementation classes too

parent 635b9f94
Pipeline #4683 passed with stage
in 2 minutes and 10 seconds
......@@ -7,7 +7,6 @@ package edu.tigers.moduli;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
......@@ -159,7 +158,7 @@ public class Moduli
checkModuleIsUnique(module);
// --- set dependency-list ---
List<String> rawDependencyList = Arrays.asList(config.getStringArray(moduleMessage(i, ".dependency")));
String[] rawDependencyList = config.getStringArray(moduleMessage(i, ".dependency"));
List<Class<? extends AModule>> dependencyList = new ArrayList<>();
for (String dependency : rawDependencyList)
{
......@@ -384,11 +383,14 @@ public class Moduli
@SuppressWarnings("unchecked")
public <T extends AModule> T getModule(Class<T> moduleId)
{
if (!modules.containsKey(moduleId))
final AModule aModule = modules.get(moduleId);
if (aModule != null)
{
throw new ModuleNotFoundException(moduleMessage(moduleId, "not found"));
return (T) aModule;
}
return (T) modules.get(moduleId);
return (T) modules.values().stream()
.filter(m -> m.getClass().equals(moduleId)).findFirst()
.orElseThrow(() -> new ModuleNotFoundException(moduleMessage(moduleId, "not found")));
}
......@@ -400,7 +402,8 @@ public class Moduli
*/
public boolean isModuleLoaded(Class<? extends AModule> moduleId)
{
return modules.containsKey(moduleId);
return modules.containsKey(moduleId)
|| modules.values().stream().map(Object::getClass).anyMatch(c -> c.equals(moduleId));
}
......
......@@ -6,6 +6,12 @@ package edu.tigers.moduli;
import static org.assertj.core.api.Assertions.assertThat;
import edu.tigers.moduli.exceptions.InitModuleException;
import edu.tigers.moduli.exceptions.LoadModulesException;
import edu.tigers.moduli.exceptions.ModuleNotFoundException;
import edu.tigers.moduli.exceptions.StartModuleException;
import edu.tigers.moduli.modules.ConcreteTestModule;
import edu.tigers.moduli.modules.UnusedConcreteTestModule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -18,33 +24,43 @@ import edu.tigers.moduli.modules.TestModule;
public class ModuliTest
{
private static final String MODULE_CONFIG_PATH = "src/test/resources/";
private static final String TEST_CONFIG_XML = "test_config.xml";
private static final String EMPTY_CONFIG_XML = "empty_config.xml";
private static final String CYCLIC_CONFIG_XML = "cyclic_config.xml";
private static final String UNRESOLVED_DEPENDENCY_CONFIG_XML = "unresolved_dependency_config.xml";
private Moduli moduli;
@Before
public void setUp() throws Exception
public void setUp()
{
moduli = new Moduli();
}
@After
public void tearDown() throws Exception
public void tearDown()
{
moduli = null;
}
@Test
public void testModuliCycle() throws Exception
public void testModuliCycle() throws InitModuleException, StartModuleException
{
assertThat(moduli.getModulesState().get()).isEqualTo(ModulesState.NOT_LOADED);
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
moduli.loadModulesSafe(MODULE_CONFIG_PATH + TEST_CONFIG_XML);
assertThat(moduli.getModulesState().get()).isEqualTo(ModulesState.RESOLVED);
assertThat(moduli.isModuleLoaded(TestModule.class)).isTrue();
assertThat(moduli.isModuleLoaded(ConcreteTestModule.class)).isTrue();
assertThat(moduli.isModuleLoaded(UnusedConcreteTestModule.class)).isFalse();
assertThat(moduli.getModule(TestModule.class).isConstructed()).isTrue();
assertThat(moduli.getModule(ConcreteTestModule.class).isConstructed()).isTrue();
moduli.startModules();
assertThat(moduli.getModulesState().get()).isEqualTo(ModulesState.ACTIVE);
......@@ -55,9 +71,9 @@ public class ModuliTest
@Test
public void testModuleCycle() throws Exception
public void testModuleCycle() throws InitModuleException, StartModuleException
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
moduli.loadModulesSafe(MODULE_CONFIG_PATH + TEST_CONFIG_XML);
TestModule module = moduli.getModule(TestModule.class);
assertThat(module.isConstructed()).isTrue();
......@@ -72,9 +88,9 @@ public class ModuliTest
@Test
public void testEmptyConfig() throws Exception
public void testEmptyConfig() throws InitModuleException, StartModuleException
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "empty_config.xml");
moduli.loadModulesSafe(MODULE_CONFIG_PATH + EMPTY_CONFIG_XML);
assertThat(moduli.getModulesState().get()).isEqualTo(ModulesState.RESOLVED);
assertThat(moduli.isModuleLoaded(TestModule.class)).isFalse();
......@@ -87,18 +103,18 @@ public class ModuliTest
@Test
public void testGlobalConfiguration() throws Exception
public void testGlobalConfiguration()
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "empty_config.xml");
moduli.loadModulesSafe(MODULE_CONFIG_PATH + EMPTY_CONFIG_XML);
String env = moduli.getGlobalConfiguration().getString("environment");
assertThat(env).isEqualTo("MODULI");
}
@Test
public void testModuleConfiguration() throws Exception
public void testModuleConfiguration() throws InitModuleException, StartModuleException
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
moduli.loadModulesSafe(MODULE_CONFIG_PATH + TEST_CONFIG_XML);
moduli.startModules();
ConfiguredTestModule module = moduli.getModule(ConfiguredTestModule.class);
assertThat(module.getConfigProperty()).isEqualTo("exists");
......@@ -106,16 +122,27 @@ public class ModuliTest
@Test(expected = DependencyException.class)
public void testCyclicConfiguration() throws Exception
public void testCyclicConfiguration() throws DependencyException, LoadModulesException
{
moduli.loadModules(MODULE_CONFIG_PATH + "cyclic_config.xml");
moduli.loadModules(MODULE_CONFIG_PATH + CYCLIC_CONFIG_XML);
}
@Test(expected = DependencyException.class)
public void testUnresolvedDependencyConfiguration() throws Exception
public void testUnresolvedDependencyConfiguration() throws DependencyException, LoadModulesException
{
moduli.loadModules(MODULE_CONFIG_PATH + "unresolved_dependency_config.xml");
moduli.loadModules(MODULE_CONFIG_PATH + UNRESOLVED_DEPENDENCY_CONFIG_XML);
}
@Test(expected = ModuleNotFoundException.class)
public void testModuleNotFound()
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + TEST_CONFIG_XML);
assertThat(moduli.getModulesState().get()).isEqualTo(ModulesState.RESOLVED);
assertThat(moduli.isModuleLoaded(TestModule.class)).isTrue();
moduli.getModule(UnusedConcreteTestModule.class);
}
}
\ No newline at end of file
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.moduli.modules;
public class UnusedConcreteTestModule extends TestModule
{
private boolean isConstructed;
private boolean isInitialized = false;
private boolean isStarted = false;
private boolean isStopped = false;
private boolean isDeinitialized = false;
public UnusedConcreteTestModule()
{
this.isConstructed = true;
}
@Override
public void initModule()
{
isInitialized = true;
}
@Override
public void startModule()
{
isStarted = true;
}
@Override
public void stopModule()
{
isStopped = true;
}
@Override
public void deinitModule()
{
isDeinitialized = true;
}
@Override
public boolean isConstructed()
{
return isConstructed;
}
@Override
public boolean isInitialized()
{
return isInitialized;
}
@Override
public boolean isStarted()
{
return isStarted;
}
@Override
public boolean isStopped()
{
return isStopped;
}
@Override
public boolean isDeinitialized()
{
return isDeinitialized;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment