[New] rewrote the abstract test to use mocking of the container
This commit is contained in:
parent
5e82fa7fc5
commit
e415dcd6bf
|
@ -23,20 +23,17 @@ package li.strolch.persistence.impl.dao.test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import li.strolch.persistence.api.StrolchPersistenceHandler;
|
|
||||||
import li.strolch.persistence.impl.XmlPersistenceHandler;
|
import li.strolch.persistence.impl.XmlPersistenceHandler;
|
||||||
import li.strolch.runtime.configuration.ComponentConfiguration;
|
import li.strolch.runtime.configuration.ComponentConfiguration;
|
||||||
import li.strolch.runtime.configuration.RuntimeConfiguration;
|
import li.strolch.runtime.configuration.ConfigurationParser;
|
||||||
|
import li.strolch.runtime.configuration.StrolchConfiguration;
|
||||||
|
import li.strolch.testbase.runtime.RuntimeMock;
|
||||||
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import ch.eitchnet.utils.helper.FileHelper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*
|
*
|
||||||
|
@ -44,6 +41,7 @@ import ch.eitchnet.utils.helper.FileHelper;
|
||||||
public abstract class AbstractDaoImplTest {
|
public abstract class AbstractDaoImplTest {
|
||||||
|
|
||||||
private static final String RUNTIME_PATH = "target/strolchRuntime/"; //$NON-NLS-1$
|
private static final String RUNTIME_PATH = "target/strolchRuntime/"; //$NON-NLS-1$
|
||||||
|
private static final String CONFIG_SRC = "src/test/resources/runtime/config"; //$NON-NLS-1$
|
||||||
protected static final Logger logger = LoggerFactory.getLogger(AbstractDaoImplTest.class);
|
protected static final Logger logger = LoggerFactory.getLogger(AbstractDaoImplTest.class);
|
||||||
|
|
||||||
protected static XmlPersistenceHandler persistenceHandler;
|
protected static XmlPersistenceHandler persistenceHandler;
|
||||||
|
@ -51,36 +49,23 @@ public abstract class AbstractDaoImplTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
|
|
||||||
File file = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
if (file.exists()) {
|
File configSrc = new File(CONFIG_SRC);
|
||||||
if (!FileHelper.deleteFile(file, true)) {
|
RuntimeMock.mockRuntime(rootPath, configSrc);
|
||||||
String msg = "Failed to delete {0}"; //$NON-NLS-1$
|
|
||||||
msg = MessageFormat.format(msg, file.getAbsolutePath());
|
|
||||||
throw new RuntimeException(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file = new File(file, XmlPersistenceHandler.DB_STORE_PATH);
|
File dbStorePath = new File(rootPath, XmlPersistenceHandler.DB_STORE_PATH);
|
||||||
if (!file.mkdirs()) {
|
if (!dbStorePath.mkdirs()) {
|
||||||
String msg = "Failed to create path {0}"; //$NON-NLS-1$
|
String msg = "Failed to create path {0}"; //$NON-NLS-1$
|
||||||
msg = MessageFormat.format(msg, file.getAbsolutePath());
|
msg = MessageFormat.format(msg, dbStorePath.getAbsolutePath());
|
||||||
throw new RuntimeException(msg);
|
throw new RuntimeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize a runtime configuration
|
|
||||||
Map<String, String> runtimeConfigurationValues = new HashMap<>();
|
|
||||||
String rootPath = RUNTIME_PATH;
|
|
||||||
RuntimeConfiguration runtimeConfiguration = new RuntimeConfiguration(runtimeConfigurationValues, rootPath);
|
|
||||||
|
|
||||||
// initialize the component configuration
|
// initialize the component configuration
|
||||||
String componentName = StrolchPersistenceHandler.class.getName();
|
StrolchConfiguration strolchConfiguration = ConfigurationParser.parseConfiguration(rootPath);
|
||||||
Map<String, String> configurationValues = new HashMap<>();
|
ComponentConfiguration componentConfiguration = strolchConfiguration
|
||||||
ComponentConfiguration componentConfiguration = new ComponentConfiguration(runtimeConfiguration, componentName,
|
.getComponentConfiguration("PersistenceHandler"); //$NON-NLS-1$
|
||||||
configurationValues);
|
|
||||||
|
|
||||||
persistenceHandler = new XmlPersistenceHandler();
|
persistenceHandler = new XmlPersistenceHandler();
|
||||||
persistenceHandler.initialize(componentConfiguration);
|
persistenceHandler.initialize(componentConfiguration);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,16 @@ public class XmlOrderDaoTest extends AbstractDaoImplTest {
|
||||||
private static final String NAME = "Test Order"; //$NON-NLS-1$
|
private static final String NAME = "Test Order"; //$NON-NLS-1$
|
||||||
private static final String TYPE = "ToStock"; //$NON-NLS-1$
|
private static final String TYPE = "ToStock"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateOrder() {
|
||||||
|
|
||||||
|
// create
|
||||||
|
Order newOrder = createOrder("MyTestOrder", "Test Name", "TestType", System.currentTimeMillis(), State.CREATED); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx();) {
|
||||||
|
persistenceHandler.getOrderDao(tx).save(newOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCrud() {
|
public void shouldCrud() {
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,16 @@ public class XmlResourceDaoTest extends AbstractDaoImplTest {
|
||||||
private static final String NAME = "Test Resource"; //$NON-NLS-1$
|
private static final String NAME = "Test Resource"; //$NON-NLS-1$
|
||||||
private static final String TYPE = "Box"; //$NON-NLS-1$
|
private static final String TYPE = "Box"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateOrder() {
|
||||||
|
|
||||||
|
// create
|
||||||
|
Resource newResource = createResource("MyTestResource", "Test Name", "TestType"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
try (StrolchTransaction tx = persistenceHandler.openTx();) {
|
||||||
|
persistenceHandler.getResourceDao(tx).save(newResource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCrud() {
|
public void shouldCrud() {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<StrolchConfiguration>
|
||||||
|
<Runtime>
|
||||||
|
<applicationName>StrolchPersistenceTest</applicationName>
|
||||||
|
<Properties>
|
||||||
|
<verbose>true</verbose>
|
||||||
|
</Properties>
|
||||||
|
</Runtime>
|
||||||
|
<Component>
|
||||||
|
<name>PersistenceHandler</name>
|
||||||
|
<api>li.strolch.persistence.api.StrolchPersistenceHandler</api>
|
||||||
|
<impl>li.strolch.persistence.impl.XmlPersistenceHandler</impl>
|
||||||
|
<Properties>
|
||||||
|
<verbose>true</verbose>
|
||||||
|
</Properties>
|
||||||
|
</Component>
|
||||||
|
</StrolchConfiguration>
|
Loading…
Reference in New Issue