Permalink
Browse files

plugin components implementation

  • Loading branch information...
1 parent a73e3d5 commit 5898598883f83d2aede913a814d976a3c21f8383 @adrianoalmeida7 adrianoalmeida7 committed Jul 7, 2011
@@ -0,0 +1,39 @@
+package br.com.caelum.vraptor.neo4j;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.servlet.ServletContext;
+
+import org.neo4j.kernel.AbstractGraphDatabase;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+
+import br.com.caelum.vraptor.ioc.ApplicationScoped;
+import br.com.caelum.vraptor.ioc.Component;
+import br.com.caelum.vraptor.ioc.ComponentFactory;
+
+@Component
+@ApplicationScoped
+public class Neo4jEmbeddedGraphDatabaseFactory implements ComponentFactory<AbstractGraphDatabase> {
+
+ final String databaseDirectory;
+ private EmbeddedGraphDatabase db;
+
+ public Neo4jEmbeddedGraphDatabaseFactory(ServletContext ctx) {
+ String configuredDirectory = ctx.getInitParameter("br.com.caelum.vraptor.neo4j.database");
+ this.databaseDirectory = configuredDirectory!= null ? configuredDirectory: "/tmp/database";
+ }
+
+ @PostConstruct
+ public void initialize() {
+ db = new EmbeddedGraphDatabase(databaseDirectory);
+ }
+
+ public AbstractGraphDatabase getInstance() {
+ return this.db;
+ }
+
+ @PreDestroy
+ public void destroy() {
+ db.shutdown();
+ }
+}
@@ -0,0 +1,24 @@
+package br.com.caelum.vraptor.neo4j;
+
+import org.neo4j.graphdb.index.IndexManager;
+import org.neo4j.kernel.AbstractGraphDatabase;
+
+import br.com.caelum.vraptor.ioc.ApplicationScoped;
+import br.com.caelum.vraptor.ioc.Component;
+import br.com.caelum.vraptor.ioc.ComponentFactory;
+
+@ApplicationScoped
+@Component
+public class Neo4jIndexManagerFactory implements ComponentFactory<IndexManager>{
+
+ private IndexManager index;
+
+ public Neo4jIndexManagerFactory(AbstractGraphDatabase db) {
+ index = db.index();
+ }
+
+ public IndexManager getInstance() {
+ return this.index;
+ }
+
+}
@@ -0,0 +1,43 @@
+package br.com.caelum.vraptor.neo4j;
+
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.kernel.AbstractGraphDatabase;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+
+import br.com.caelum.vraptor.InterceptionException;
+import br.com.caelum.vraptor.Intercepts;
+import br.com.caelum.vraptor.Validator;
+import br.com.caelum.vraptor.core.InterceptorStack;
+import br.com.caelum.vraptor.interceptor.Interceptor;
+import br.com.caelum.vraptor.resource.ResourceMethod;
+
+@Intercepts
+public class Neo4jTransactionInterceptor implements Interceptor {
+
+ private final AbstractGraphDatabase db;
+ private final Validator validator;
+
+ public Neo4jTransactionInterceptor(AbstractGraphDatabase db, Validator validator) {
+ this.db = db;
+ this.validator = validator;
+ }
+
+ public boolean accepts(ResourceMethod method) {
+ return true;
+ }
+
+ public void intercept(InterceptorStack stack, ResourceMethod method,
+ Object object) throws InterceptionException {
+ Transaction tx = db.beginTx();
+ try {
+ stack.next(method, object);
+ if(! validator.hasErrors()) {
+ tx.success();
+ } else {
+ tx.failure();
+ }
+ } finally {
+ tx.finish();
+ }
+ }
+}
@@ -0,0 +1,30 @@
+package br.com.caelum.vraptor.neo4j;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.ServletContext;
+
+import org.junit.Test;
+
+public class Neo4jEmbeddedGraphDatabaseFactoryTest {
+
+ @Test
+ public void should_use_default_database_directory_when_nothing_was_configured() {
+ ServletContext context = mock(ServletContext.class);
+ when(context.getInitParameter("br.com.caelum.vraptor.neo4j.database")).thenReturn(null);
+ Neo4jEmbeddedGraphDatabaseFactory factory = new Neo4jEmbeddedGraphDatabaseFactory(context);
+
+ assertEquals("/tmp/database", factory.databaseDirectory);
+ }
+
+ @Test
+ public void should_use_the_configured_database_directory_when_configured() {
+ ServletContext context = mock(ServletContext.class);
+ when(context.getInitParameter("br.com.caelum.vraptor.neo4j.database")).thenReturn("/a/db/directory");
+ Neo4jEmbeddedGraphDatabaseFactory factory = new Neo4jEmbeddedGraphDatabaseFactory(context);
+
+ assertEquals("/a/db/directory", factory.databaseDirectory);
+ }
+}
@@ -0,0 +1,49 @@
+package br.com.caelum.vraptor.neo4j;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.kernel.AbstractGraphDatabase;
+
+import br.com.caelum.vraptor.Validator;
+import br.com.caelum.vraptor.core.InterceptorStack;
+
+public class Neo4jTransactionInterceptorTest {
+
+ @Test
+ public void should_be_a_successful_transaction_if_has_no_validation_error() {
+ AbstractGraphDatabase db = mock(AbstractGraphDatabase.class);
+ Validator validator = mock(Validator.class);
+ Transaction tx = mock(Transaction.class);
+ InterceptorStack stack = mock(InterceptorStack.class);
+
+ when(db.beginTx()).thenReturn(tx);
+ when(validator.hasErrors()).thenReturn(false);
+ Neo4jTransactionInterceptor interceptor = new Neo4jTransactionInterceptor(db, validator);
+
+ interceptor.intercept(stack, null, null);
+
+ verify(tx).success();
+ verify(tx).finish();
+ }
+
+ @Test
+ public void should_be_marked_as_failed_transaction_if_has_validation_error() {
+ AbstractGraphDatabase db = mock(AbstractGraphDatabase.class);
+ Validator validator = mock(Validator.class);
+ Transaction tx = mock(Transaction.class);
+ InterceptorStack stack = mock(InterceptorStack.class);
+
+ when(db.beginTx()).thenReturn(tx);
+ when(validator.hasErrors()).thenReturn(true);
+ Neo4jTransactionInterceptor interceptor = new Neo4jTransactionInterceptor(db, validator);
+
+ interceptor.intercept(stack, null, null);
+
+ verify(tx).failure();
+ verify(tx).finish();
+ }
+}

0 comments on commit 5898598

Please sign in to comment.