Skip to content

Commit

Permalink
Clean up repository initialization code a bit
Browse files Browse the repository at this point in the history
It was not quite clear how individual parameters are initialized.
Some defaults (e.g. H2 for database) came from various places.
  • Loading branch information
mederly committed Jan 17, 2018
1 parent 649ac5a commit d087471
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 533 deletions.
Expand Up @@ -112,7 +112,6 @@ void initialize() {

// activiti properties related to database connection will be taken from SQL repository
SqlRepositoryConfiguration sqlConfig = null;
String defaultJdbcUrlPrefix = null;
dropDatabase = false;
try {
RepositoryFactory repositoryFactory = (RepositoryFactory) beanFactory.getBean("repositoryFactory");
Expand All @@ -124,22 +123,17 @@ void initialize() {
} else {
SqlRepositoryFactory sqlRepositoryFactory = (SqlRepositoryFactory) repositoryFactory.getFactory();
sqlConfig = sqlRepositoryFactory.getSqlConfiguration();
if (sqlConfig.isEmbedded()) {
defaultJdbcUrlPrefix = sqlRepositoryFactory.prepareJdbcUrlPrefix(sqlConfig);
}
dropDatabase = sqlConfig.isDropIfExists();
}
} catch(NoSuchBeanDefinitionException e) {
} catch (NoSuchBeanDefinitionException e) {
LOGGER.debug("SqlRepositoryFactory is not available, Activiti database configuration (if any) will be taken from 'workflow' configuration section only.");
LOGGER.trace("Reason is", e);
} catch (RepositoryServiceFactoryException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Cannot determine default JDBC URL for embedded database", e);
}

String explicitJdbcUrl = c.getString(KEY_JDBC_URL, null);
if (explicitJdbcUrl == null) {
if (sqlConfig == null || sqlConfig.isEmbedded()) {
jdbcUrl = defaultJdbcUrlPrefix + "-activiti;DB_CLOSE_ON_EXIT=FALSE;MVCC=FALSE";
if (explicitJdbcUrl == null && sqlConfig != null) {
if (sqlConfig.isEmbedded()) {
jdbcUrl = sqlConfig.getDefaultEmbeddedJdbcUrlPrefix() + "-activiti;DB_CLOSE_ON_EXIT=FALSE;MVCC=FALSE";
} else {
jdbcUrl = sqlConfig.getJdbcUrl();
}
Expand Down
Expand Up @@ -85,7 +85,7 @@ private void updateConfigurationFromFile(Configuration configuration, String fil
}

private void updateConfigurationFromProperties(Configuration configuration, Properties properties) {
updateConfigurationStringProperty(configuration, properties, PROPERTY_DATABASE, "h2");
updateConfigurationStringProperty(configuration, properties, PROPERTY_DATABASE);

updateConfigurationBooleanProperty(configuration, properties, PROPERTY_EMBEDDED);
updateConfigurationBooleanProperty(configuration, properties, PROPERTY_DROP_IF_EXISTS);
Expand Down Expand Up @@ -116,6 +116,21 @@ private void updateConfigurationFromProperties(Configuration configuration, Prop
updateConfigurationBooleanProperty(configuration, properties, PROPERTY_USE_ZIP);
updateConfigurationIntegerProperty(configuration, properties, PROPERTY_MIN_POOL_SIZE);
updateConfigurationIntegerProperty(configuration, properties, PROPERTY_MAX_POOL_SIZE);

// Dirty hack, in order to make DataSourceTest happy: if none of database, driver, dialect, embedded is
// present but data source is, let us assume we use H2.
//
// The reason is that when using datasource (and without the dialect set) we do not have the usual information
// we could use to derive the database. We do not want to default to H2, as it could cause problems in
// production. So we switch to H2 in such cases only in the test mode - i.e. here.

if (!configuration.containsKey(PROPERTY_DATABASE)
&& !configuration.containsKey(PROPERTY_DRIVER_CLASS_NAME)
&& !configuration.containsKey(PROPERTY_HIBERNATE_DIALECT)
&& !configuration.containsKey(PROPERTY_EMBEDDED)
&& configuration.containsKey(PROPERTY_DATASOURCE)) {
configuration.setProperty(PROPERTY_DATABASE, Database.H2.name());
}
}

private void updateConfigurationIntegerProperty(Configuration configuration, Properties properties, String propertyName) {
Expand Down
Expand Up @@ -26,19 +26,14 @@
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.delta.PropertyDelta;
import com.evolveum.midpoint.prism.match.PolyStringOrigMatchingRule;
import com.evolveum.midpoint.prism.path.IdItemPathSegment;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.query.EqualFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.builder.QueryBuilder;
import com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil;
import com.evolveum.midpoint.schema.ResultHandler;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand All @@ -48,15 +43,12 @@
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -116,20 +108,15 @@ public void concurrency003_OneWriter_TwoAttributes__OneReader() throws Exception
};


Checker checker = new Checker() {
@Override
public void check(int iteration, String oid) throws Exception {

PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));

String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
LOGGER.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
Checker checker = (iteration, oid) -> {
PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));
String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
LOGGER.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
};

Expand All @@ -156,27 +143,22 @@ public void concurrency004_TwoWriters_TwoAttributesEach__OneReader() throws Exce
};


Checker checker = new Checker() {
@Override
public void check(int iteration, String oid) throws Exception {

PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));

String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String familyName = userRetrieved.asObjectable().getFamilyName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
String referenceDescription = userRetrieved.asObjectable().getAssignment().get(0).getConstruction().getDescription();
LOGGER.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription + ", familyName = " + familyName + ", referenceDescription = " + referenceDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
if (!familyName.equals(referenceDescription)) {
String msg = "Inconsistent object state: FamilyName = " + familyName + ", account construction description = " + referenceDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
Checker checker = (iteration, oid) -> {
PrismObject<UserType> userRetrieved = repositoryService.getObject(UserType.class, oid, null, new OperationResult("dummy"));
String givenName = userRetrieved.asObjectable().getGivenName().getOrig();
String familyName = userRetrieved.asObjectable().getFamilyName().getOrig();
String assignmentDescription = userRetrieved.asObjectable().getAssignment().get(0).getDescription();
String referenceDescription = userRetrieved.asObjectable().getAssignment().get(0).getConstruction().getDescription();
LOGGER.info("[" + iteration + "] givenName = " + givenName + ", assignment description = " + assignmentDescription + ", familyName = " + familyName + ", referenceDescription = " + referenceDescription);
if (!givenName.equals(assignmentDescription)) {
String msg = "Inconsistent object state: GivenName = " + givenName + ", assignment description = " + assignmentDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
if (!familyName.equals(referenceDescription)) {
String msg = "Inconsistent object state: FamilyName = " + familyName + ", account construction description = " + referenceDescription;
LOGGER.error(msg);
throw new AssertionError(msg);
}
};

Expand All @@ -191,12 +173,7 @@ private interface Checker {
private void concurrencyUniversal(String name, long duration, long waitStep, ModifierThread[] modifierThreads, Checker checker) throws Exception {

Session session = getFactory().openSession();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println(">>>>" + connection.getTransactionIsolation());
}
});
session.doWork(connection -> System.out.println(">>>>" + connection.getTransactionIsolation()));
session.close();

final File file = new File("src/test/resources/concurrency/user.xml");
Expand Down Expand Up @@ -313,7 +290,7 @@ public void run() {
}
}

public void runOnce() throws SchemaException {
void runOnce() {

OperationResult result = new OperationResult("run");

Expand All @@ -323,13 +300,14 @@ public void runOnce() throws SchemaException {
String prefix = lastName(attribute1);
String dataWritten = "[" + prefix + ":" + Integer.toString(counter++) + "]";

PrismPropertyDefinition propertyDefinition1 = userPrismDefinition.findPropertyDefinition(attribute1);
PrismPropertyDefinition<?> propertyDefinition1 = userPrismDefinition.findPropertyDefinition(attribute1);
if (propertyDefinition1 == null) {
throw new IllegalArgumentException("No definition for " + attribute1 + " in " + userPrismDefinition);
}
PropertyDelta delta1 = new PropertyDelta(attribute1, propertyDefinition1, prismContext);
PropertyDelta<?> delta1 = new PropertyDelta<>(attribute1, propertyDefinition1, prismContext);
//noinspection unchecked
delta1.setValueToReplace(new PrismPropertyValue(poly ? new PolyString(dataWritten) : dataWritten));
List<ItemDelta> deltas = new ArrayList<ItemDelta>();
List<ItemDelta> deltas = new ArrayList<>();
deltas.add(delta1);

ItemDefinition propertyDefinition2 = null;
Expand All @@ -339,11 +317,11 @@ public void runOnce() throws SchemaException {
throw new IllegalArgumentException("No definition for " + attribute2 + " in " + userPrismDefinition);
}

ItemDelta delta2 = null;
ItemDelta delta2;
if (propertyDefinition2 instanceof PrismContainerDefinition) {
delta2 = new ContainerDelta(attribute2, (PrismContainerDefinition) propertyDefinition2, prismContext);
} else {
delta2 = new PropertyDelta(attribute2, (PrismPropertyDefinition) propertyDefinition2, prismContext);
delta2 = new PropertyDelta(attribute2, (PrismPropertyDefinition) propertyDefinition2, prismContext);
}
if (ConstructionType.COMPLEX_TYPE.equals(propertyDefinition2.getTypeName())) {
ConstructionType act = new ConstructionType();
Expand Down Expand Up @@ -457,22 +435,19 @@ public void concurrency010_SearchIterative() throws Exception {
repositoryService.searchObjectsIterative(UserType.class,
QueryBuilder.queryFor(UserType.class, prismContext)
.item(UserType.F_NAME).eqPoly(name).matchingOrig().build(),
new ResultHandler<UserType>() {
@Override
public boolean handle(PrismObject<UserType> object, OperationResult parentResult) {
LOGGER.info("Handling " + object + "...");
ObjectDelta delta = ObjectDelta.createModificationReplaceProperty(UserType.class, object.getOid(),
UserType.F_FULL_NAME, prismContext, new PolyString(newFullName));
try {
repositoryService.modifyObject(UserType.class,
object.getOid(),
delta.getModifications(),
parentResult);
} catch (Exception e) {
throw new RuntimeException("Exception in handle method", e);
}
return true;
(object, parentResult) -> {
LOGGER.info("Handling " + object + "...");
ObjectDelta delta = ObjectDelta.createModificationReplaceProperty(UserType.class, object.getOid(),
UserType.F_FULL_NAME, prismContext, new PolyString(newFullName));
try {
repositoryService.modifyObject(UserType.class,
object.getOid(),
delta.getModifications(),
parentResult);
} catch (Exception e) {
throw new RuntimeException("Exception in handle method", e);
}
return true;
},
null, false, result);

Expand Down
Expand Up @@ -22,8 +22,6 @@
import org.testng.annotations.Test;

/**
* @see com.evolveum.midpoint.repo.sql.util.DataSourceTestBeanPostprocessor
*
* @author lazyman
*/
@ContextConfiguration(locations = {"../../../../../ctx-test-datasource.xml"})
Expand Down

This file was deleted.

Expand Up @@ -23,7 +23,6 @@
</global>
<repository>
<repositoryServiceFactoryClass>com.evolveum.midpoint.repo.sql.testing.TestSqlRepositoryFactory</repositoryServiceFactoryClass>
<embedded>false</embedded>
<dataSource>java:comp/env/jdbc/db</dataSource>
</repository>
<audit>
Expand Down
Expand Up @@ -25,10 +25,7 @@ It fakes JNDI context with simple H2 file DataSource.
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true" default-autowire="byName">

<bean id="dataSourceTestBeanPostprocessor" class="com.evolveum.midpoint.repo.sql.util.DataSourceTestBeanPostprocessor"
lazy-init="false"/>

<bean id="sampleJNDIDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
<bean id="sampleJNDIDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
lazy-init="false">
<property name="driverClass" value="org.h2.Driver"/>
<property name="jdbcUrl" value="jdbc:h2:file:target/midpoint-home/datasource;DB_CLOSE_ON_EXIT=FALSE;LOCK_MODE=1;LOCK_TIMEOUT=10000"/>
Expand Down

0 comments on commit d087471

Please sign in to comment.