Commit 0247e20e authored by NicolaiO's avatar NicolaiO 🐼

Merge branch...

Merge branch '5-the-constructor-with-a-parameter-subnodeconfiguration-should-be-optional' into 'master'

Resolve "The constructor with a parameter SubnodeConfiguration should be optional"

Closes #5

See merge request !4
parents cb10bc15 8e1fe62b
Pipeline #2507 passed with stage
in 39 seconds
stages:
- build
cache:
key: '$CI_PIPELINE_ID'
paths:
- '.m2/repository'
variables:
MVN_OPTIONS: '-B -Dmaven.repo.local=.m2/repository -Dmaven.javadoc.skip=true'
build:
stage: build
image: maven:3-jdk-8
script:
- 'mvn ${MVN_OPTIONS} clean install'
......@@ -29,6 +29,12 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -46,8 +46,6 @@ public class Moduli
private ModulesStateVariable modulesState = new ModulesStateVariable();
private static final Class<?>[] PROP_ARGS_CLASS = new Class[] { SubnodeConfiguration.class };
/**
* Getter modulesState.
......@@ -129,17 +127,15 @@ public class Moduli
// --- create implementation- and properties-class ---
Class<? extends AModule> clazz = (Class<? extends AModule>) Class
.forName(implsPath + config.getString("module(" + i + ").implementation"));
.forName(implsPath + config.getString(moduleMessage(i, "implementation")));
// --- get properties from configuration and put it into a object[] ---
SubnodeConfiguration moduleConfig = config.configurationAt("module(" + i + ").properties");
Object[] propArgs = new Object[] { moduleConfig };
SubnodeConfiguration moduleConfig = config.configurationAt(moduleMessage(i, "properties"));
// --- get constructor of implementation-class with subnodeConfiguration-parameter ---
Constructor<?> clazzConstructor = clazz.getConstructor(PROP_ARGS_CLASS);
Constructor<?> clazzConstructor = clazz.getConstructor();
// --- create object (use constructor) ---
AModule module = (AModule) createObject(clazzConstructor, propArgs);
AModule module = (AModule) createObject(clazzConstructor);
// --- set module config ---
module.setSubnodeConfiguration(moduleConfig);
......@@ -154,7 +150,7 @@ public class Moduli
}
// --- set dependency-list ---
List<String> rawDependencyList = Arrays.asList(config.getStringArray("module(" + i + ").dependency"));
List<String> rawDependencyList = Arrays.asList(config.getStringArray(moduleMessage(i, "dependency")));
List<Class<? extends AModule>> dependencyList = new ArrayList<>();
for (String dependency : rawDependencyList)
{
......@@ -236,7 +232,7 @@ public class Moduli
{
log.trace("Initializing module " + m);
m.initModule();
log.trace("Module " + m + " initialized");
log.trace(moduleMessage(m, "initialized"));
} catch (Exception err)
{
throw new InitModuleException("Could not initialize module " + m, err);
......@@ -257,7 +253,7 @@ public class Moduli
{
log.trace("Starting module " + m);
m.startModule();
log.trace("Module " + m + " started");
log.trace(moduleMessage(m, "started"));
} catch (Exception err)
{
throw new StartModuleException("Could not initialize module " + m, err);
......@@ -290,7 +286,7 @@ public class Moduli
try
{
m.stopModule();
log.trace("Module " + m + " stopped");
log.trace(moduleMessage(m, "stopped"));
} catch (Exception err)
{
log.error("Exception while stopping module: " + m, err);
......@@ -306,7 +302,7 @@ public class Moduli
try
{
m.deinitModule();
log.trace("Module " + m + " deinitialized");
log.trace(moduleMessage(m, "deinitialized"));
} catch (Exception err)
{
log.error("Exception while deinitializing module: " + m, err);
......@@ -338,7 +334,7 @@ public class Moduli
{
if (!modules.containsKey(moduleId))
{
throw new ModuleNotFoundException("Module " + moduleId + " not found");
throw new ModuleNotFoundException(moduleMessage(moduleId, "not found"));
}
return (T) modules.get(moduleId);
}
......@@ -367,15 +363,26 @@ public class Moduli
/**
* Creates an object from a constructor and its arguments.
*/
private Object createObject(final Constructor<?> constructor, final Object[] arguments)
private Object createObject(final Constructor<?> constructor)
{
try
{
return constructor.newInstance(arguments);
return constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | IllegalArgumentException e)
{
log.error(e.getMessage(), e);
throw new IllegalArgumentException("Error constructing module", e);
}
return null;
}
private String moduleMessage(Object module, String message)
{
return "Module " + module + " " + message;
}
private String moduleMessage(int moduleNumber, String property)
{
return "module(" + moduleNumber + ")." + property;
}
}
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.moduli;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import edu.tigers.moduli.listenerVariables.ModulesState;
import edu.tigers.moduli.modules.ConfiguredTestModule;
import edu.tigers.moduli.modules.TestModule;
public class ModuliTest
{
private static final String MODULE_CONFIG_PATH = "src/test/resources/";
private Moduli moduli;
@Before
public void setUp() throws Exception
{
moduli = new Moduli();
}
@After
public void tearDown() throws Exception
{
moduli = null;
}
@Test
public void testModuliCycle() throws Exception
{
assertEquals(ModulesState.NOT_LOADED, moduli.getModulesState().get());
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
assertThat(moduli.getModulesState().get(), is(ModulesState.RESOLVED));
moduli.startModules();
assertThat(moduli.getModulesState().get(), is(ModulesState.ACTIVE));
moduli.stopModules();
assertThat(moduli.getModulesState().get(), is(ModulesState.RESOLVED));
}
@Test
public void testModuleCycle() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
TestModule module = moduli.getModule(TestModule.class);
assertTrue(module.isConstructed());
moduli.startModules();
assertTrue(module.isInitialized());
assertTrue(module.isStarted());
moduli.stopModules();
assertTrue(module.isStopped());
assertTrue(module.isDeinitialized());
}
@Test
public void testEmptyConfig() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "empty_config.xml");
assertThat(moduli.getModulesState().get(), is(ModulesState.RESOLVED));
moduli.startModules();
assertThat(moduli.getModulesState().get(), is(ModulesState.ACTIVE));
moduli.stopModules();
assertThat(moduli.getModulesState().get(), is(ModulesState.RESOLVED));
}
@Test
public void testGlobalConfiguration() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "empty_config.xml");
String env = moduli.getGlobalConfiguration().getString("environment");
assertThat(env, is("MODULI"));
}
@Test
public void testModuleConfiguration() throws Exception
{
moduli.loadModulesSafe(MODULE_CONFIG_PATH + "test_config.xml");
moduli.startModules();
ConfiguredTestModule module = moduli.getModule(ConfiguredTestModule.class);
assertThat(module.getConfigProperty(), is("exists"));
}
}
\ No newline at end of file
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.moduli.modules;
import edu.tigers.moduli.AModule;
import edu.tigers.moduli.exceptions.InitModuleException;
import edu.tigers.moduli.exceptions.StartModuleException;
public class ConfiguredTestModule extends AModule
{
private String configProperty;
@Override
public void initModule() throws InitModuleException
{
configProperty = getSubnodeConfiguration().getString("testProperty");
}
@Override
public void deinitModule()
{
}
@Override
public void startModule() throws StartModuleException
{
}
@Override
public void stopModule()
{
}
public String getConfigProperty()
{
return configProperty;
}
}
/*
* Copyright (c) 2009 - 2017, DHBW Mannheim - TIGERs Mannheim
*/
package edu.tigers.moduli.modules;
import edu.tigers.moduli.AModule;
import edu.tigers.moduli.exceptions.InitModuleException;
import edu.tigers.moduli.exceptions.StartModuleException;
public class TestModule extends AModule
{
private boolean isConstructed = false;
private boolean isInitialized = false;
private boolean isStarted = false;
private boolean isStopped = false;
private boolean isDeinitialized = false;
public TestModule()
{
this.isConstructed = true;
}
@Override
public void initModule() throws InitModuleException
{
isInitialized = true;
}
@Override
public void startModule() throws StartModuleException
{
isStarted = true;
}
@Override
public void stopModule()
{
isStopped = true;
}
@Override
public void deinitModule()
{
isDeinitialized = true;
}
public boolean isConstructed()
{
return isConstructed;
}
public boolean isInitialized()
{
return isInitialized;
}
public boolean isStarted()
{
return isStarted;
}
public boolean isStopped()
{
return isStopped;
}
public boolean isDeinitialized()
{
return isDeinitialized;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<moduleTest>
<moduliPath></moduliPath>
<globalConfiguration>
<environment>MODULI</environment>
</globalConfiguration>
</moduleTest>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<moduleTest>
<moduliPath></moduliPath>
<globalConfiguration>
<environment>MODULI</environment>
</globalConfiguration>
<module id="testModule">
<implementation>edu.tigers.moduli.modules.TestModule</implementation>
<properties></properties>
</module>
<module id="configuredModule">
<implementation>edu.tigers.moduli.modules.ConfiguredTestModule</implementation>
<properties>
<testProperty>exists</testProperty>
</properties>
<dependency>edu.tigers.moduli.modules.TestModule</dependency>
</module>
</moduleTest>
\ No newline at end of file
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