[New] implemented a RuntimeMock for easy mockin the container
This can be used in the different projects to testing components which need a runtime
This commit is contained in:
parent
a6fe2dce67
commit
1e8648bc2a
|
@ -1,11 +0,0 @@
|
||||||
package li.strolch.testbase.runtime;
|
|
||||||
|
|
||||||
import li.strolch.runtime.component.StrolchComponent;
|
|
||||||
import li.strolch.runtime.configuration.StrolchConfiguration;
|
|
||||||
|
|
||||||
public interface ComponentMock {
|
|
||||||
|
|
||||||
public StrolchComponent getComponent();
|
|
||||||
|
|
||||||
public void mock(StrolchConfiguration configuration);
|
|
||||||
}
|
|
|
@ -3,13 +3,28 @@ package li.strolch.testbase.runtime;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
import li.strolch.runtime.component.ComponentContainer;
|
||||||
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ch.eitchnet.utils.helper.FileHelper;
|
import ch.eitchnet.utils.helper.FileHelper;
|
||||||
|
|
||||||
public class RuntimeMock {
|
public class RuntimeMock {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(RuntimeMock.class);
|
||||||
private static final String TARGET = "target"; //$NON-NLS-1$
|
private static final String TARGET = "target"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
private static ComponentContainer container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the container
|
||||||
|
*/
|
||||||
|
public static ComponentContainer getContainer() {
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
public static void mockRuntime(File rootPathF, File configSrc) {
|
public static void mockRuntime(File rootPathF, File configSrc) {
|
||||||
|
|
||||||
if (!rootPathF.getParentFile().getName().equals(TARGET)) {
|
if (!rootPathF.getParentFile().getName().equals(TARGET)) {
|
||||||
|
@ -46,19 +61,41 @@ public class RuntimeMock {
|
||||||
msg = MessageFormat.format(msg, configSrc.getAbsolutePath(), configPathF.getAbsolutePath());
|
msg = MessageFormat.format(msg, configSrc.getAbsolutePath(), configPathF.getAbsolutePath());
|
||||||
throw new RuntimeException(msg);
|
throw new RuntimeException(msg);
|
||||||
}
|
}
|
||||||
//
|
}
|
||||||
// // initialize a runtime configuration
|
|
||||||
// Map<String, String> runtimeConfigurationValues = new HashMap<>();
|
public static void startContainer(File rootPathF) {
|
||||||
// RuntimeConfiguration runtimeConfiguration = new RuntimeConfiguration("RuntimeMock", runtimeConfigurationValues, //$NON-NLS-1$
|
|
||||||
// rootPathF);
|
ComponentContainer container = new ComponentContainer();
|
||||||
//
|
try {
|
||||||
// Map<String, ComponentConfiguration> configurationByComponent = new HashMap<>();
|
|
||||||
// StrolchConfiguration configuration = new StrolchConfiguration(runtimeConfiguration, configurationByComponent);
|
container.setup(rootPathF);
|
||||||
//
|
container.initialize();
|
||||||
// // initialize the component configuration
|
container.start();
|
||||||
// for (ComponentMock componentMock : runtimeMocks) {
|
|
||||||
// //configurationByComponent.put(key, value)
|
RuntimeMock.container = container;
|
||||||
// componentMock.mock(configuration);
|
|
||||||
// }
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to start mocked container due to: " + e.getMessage(), e); //$NON-NLS-1$
|
||||||
|
destroyRuntime();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void destroyRuntime() {
|
||||||
|
|
||||||
|
if (container == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
container.stop();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info("Failed to stop container: " + e.getMessage()); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
container.destroy();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.info("Failed to destroy container: " + e.getMessage()); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue