added configuration check

This commit is contained in:
Michael Hunger
2011-03-24 02:22:23 +01:00
parent 6d92dbb197
commit 948e1dfd47
4 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package org.springframework.data.graph.neo4j.config;
import org.neo4j.graphdb.Transaction;
import org.springframework.data.graph.neo4j.support.GraphDatabaseContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import javax.annotation.PostConstruct;
/**
* Validates correct configuration of Neo4j and Spring, especially transaction-managers
*/
public class ConfigurationCheck {
GraphDatabaseContext graphDatabaseContext;
PlatformTransactionManager transactionManager;
public ConfigurationCheck(GraphDatabaseContext graphDatabaseContext, PlatformTransactionManager transactionManager) {
this.graphDatabaseContext = graphDatabaseContext;
this.transactionManager = transactionManager;
}
@PostConstruct
private void checkConfiguration() {
checkInjection();
checkSpringTransactionManager();
checkNeo4jTransactionManager();
}
private void checkInjection() {
assert graphDatabaseContext.getGraphDatabaseService()!=null : "graphDatabaseService not correctly configured, please refer to the manual, setup section";
}
private void checkSpringTransactionManager() {
try {
TransactionStatus transaction = transactionManager.getTransaction(null);
updateStartTime();
transactionManager.commit(transaction);
} catch(Exception e) {
AssertionError error = new AssertionError("transactionManager not correctly configured, please refer to the manual, setup section");
error.initCause(e);
throw error;
}
}
private void checkNeo4jTransactionManager() {
Transaction tx = null;
try {
tx = graphDatabaseContext.beginTx();
updateStartTime();
tx.success();
} catch (Exception e) {
AssertionError error = new AssertionError("transactionManager not correctly configured, please refer to the manual, setup section");
error.initCause(e);
throw error;
} finally {
try {
if (tx != null) tx.finish();
} catch(Exception e) {
// ignore
}
}
}
private void updateStartTime() {
graphDatabaseContext.getReferenceNode().setProperty("startTime", System.currentTimeMillis());
}
}

View File

@@ -154,4 +154,9 @@ public class Neo4jConfiguration {
return transactionManager;
}
}
@Bean
public ConfigurationCheck configurationCheck() throws Exception {
return new ConfigurationCheck(graphDatabaseContext(),transactionManager());
}
}

View File

@@ -0,0 +1,16 @@
package org.springframework.data.graph.neo4j.config;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author mh
* @since 23.03.11
*/
public class ConfigurationConfirmationTest {
@Test(expected = BeanCreationException.class)
public void testInvalidTransactionManagerFails() {
ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("classpath:org/springframework/data/graph/neo4j/config/ConfigurationCofirmationTest-context.xml");
}
}

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:datagraph="http://www.springframework.org/schema/data/graph"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/graph http://www.springframework.org/schema/data/graph/datagraph-1.0.xsd
">
<context:annotation-config/>
<datagraph:config storeDirectory="target/config-test"/>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
</beans>