diff --git a/spring-data-neo4j/pom.xml b/spring-data-neo4j/pom.xml index 50c284c39..a4d660f29 100644 --- a/spring-data-neo4j/pom.xml +++ b/spring-data-neo4j/pom.xml @@ -120,8 +120,21 @@ org.apache.openwebbeans.test cditest-owb ${webbeans} + + + javassist + javassist + + test + + + org.javassist + javassist + 3.18.1-GA + test + javax.servlet diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java index 26edef598..d12ec1918 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryBean.java @@ -15,8 +15,7 @@ */ package org.springframework.data.neo4j.repository.cdi; - - +import org.springframework.data.neo4j.core.GraphDatabase; import org.springframework.data.neo4j.repository.GraphRepositoryFactory; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; @@ -30,37 +29,28 @@ import java.lang.annotation.Annotation; import java.util.Set; /** - * {@link org.springframework.data.repository.cdi.CdiRepositoryBean} to create - * Neo4j repository instances via CDI. - * + * {@link org.springframework.data.repository.cdi.CdiRepositoryBean} to create Neo4j repository instances via CDI. + * * @author Nicki Watt */ public class Neo4jCdiRepositoryBean extends CdiRepositoryBean { - private final Bean neo4jMappingContext; - private final Bean template; + private final Bean graphDatabase; /** * Creates a new {@link Neo4jCdiRepositoryBean}. * - * @param neo4jMappingContext must not be {@literal null}. - * @param template must not be {@literal null}. + * @param graphDatabase must not be {@literal null}. * @param qualifiers must not be {@literal null}. * @param repositoryType must not be {@literal null}. * @param beanManager must not be {@literal null}. */ - public Neo4jCdiRepositoryBean(Bean neo4jMappingContext, - Bean template, - Set qualifiers, - Class repositoryType, - BeanManager beanManager) { + public Neo4jCdiRepositoryBean(Bean graphDatabase, + Set qualifiers, Class repositoryType, BeanManager beanManager) { super(qualifiers, repositoryType, beanManager); - Assert.notNull(neo4jMappingContext); - Assert.notNull(template); - this.neo4jMappingContext = neo4jMappingContext; - this.template = template; + this.graphDatabase = graphDatabase; } /* @@ -70,10 +60,10 @@ public class Neo4jCdiRepositoryBean extends CdiRepositoryBean { @Override protected T create(CreationalContext creationalContext, Class repositoryType) { - Neo4jMappingContext neo4jMapCtx = getDependencyInstance(neo4jMappingContext, Neo4jMappingContext.class); - Neo4jTemplate neo4jTemplate = getDependencyInstance(template, Neo4jTemplate.class); + Neo4jMappingContext neo4jMapCtx = new Neo4jMappingContext(); + Neo4jTemplate neo4jTemplate = new Neo4jTemplate(getDependencyInstance(graphDatabase, GraphDatabase.class)); - GraphRepositoryFactory factory = new GraphRepositoryFactory(neo4jTemplate,neo4jMapCtx); + GraphRepositoryFactory factory = new GraphRepositoryFactory(neo4jTemplate, neo4jMapCtx); return factory.getRepository(repositoryType); } } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java index 66ffc241d..b1cc84d38 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiRepositoryExtension.java @@ -15,19 +15,6 @@ */ package org.springframework.data.neo4j.repository.cdi; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.springframework.data.neo4j.support.Neo4jTemplate; -import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; -import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; - -import javax.enterprise.event.Observes; -import javax.enterprise.inject.UnsatisfiedResolutionException; -import javax.enterprise.inject.spi.AfterBeanDiscovery; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.ProcessBean; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.HashMap; @@ -36,6 +23,19 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.UnsatisfiedResolutionException; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.ProcessBean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.neo4j.core.GraphDatabase; +import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; +import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; + /** * CDI extension to export Neo4j repositories. * @@ -45,10 +45,9 @@ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport { private static final Logger LOG = LoggerFactory.getLogger(Neo4jCdiRepositoryExtension.class); - private final Map, Bean> neo4jMappingContexts = new HashMap, Bean>(); - private final Map, Bean> neo4jTemplates = new HashMap, Bean>(); + private final Map, Bean> graphDatabases = new HashMap, Bean>(); - public Neo4jCdiRepositoryExtension() { + public Neo4jCdiRepositoryExtension() { LOG.info("Activating CDI extension for Spring Data Neo4j repositories."); } @@ -58,23 +57,15 @@ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport { Bean bean = processBean.getBean(); for (Type type : bean.getTypes()) { - if (type instanceof Class && Neo4jMappingContext.class.isAssignableFrom((Class) type)) { + if (type instanceof Class && GraphDatabase.class.isAssignableFrom((Class) type)) { + if (LOG.isDebugEnabled()) { - LOG.debug(String.format("Discovered %s with qualifiers %s.", Neo4jMappingContext.class.getName(), + LOG.debug(String.format("Discovered %s with qualifiers %s.", GraphDatabase.class.getName(), bean.getQualifiers())); } - // Store the EntityManager bean using its qualifiers. - neo4jMappingContexts.put(new HashSet(bean.getQualifiers()), (Bean) bean); + + graphDatabases.put(new HashSet(bean.getQualifiers()), (Bean) bean); } - - if (type instanceof Class && Neo4jTemplate.class.isAssignableFrom((Class) type)) { - if (LOG.isDebugEnabled()) { - LOG.debug(String.format("Discovered %s with qualifiers %s.", Neo4jTemplate.class.getName(), - bean.getQualifiers())); - } - // Store the EntityManager bean using its qualifiers. - neo4jTemplates.put(new HashSet(bean.getQualifiers()), (Bean) bean); - } } } @@ -107,20 +98,14 @@ public class Neo4jCdiRepositoryExtension extends CdiRepositoryExtensionSupport { */ private Bean createRepositoryBean(Class repositoryType, Set qualifiers, BeanManager beanManager) { - // Determine the Neo4jOperations bean which matches the qualifiers of the repository. - Bean neo4jMappingContextBean = this.neo4jMappingContexts.get(qualifiers); - Bean neo4jTemplateBean = this.neo4jTemplates.get(qualifiers); + Bean graphDatabase = this.graphDatabases.get(qualifiers); - if (neo4jMappingContextBean == null) { + if (graphDatabase == null) { throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", - Neo4jMappingContext.class.getName(), qualifiers)); + Neo4jMappingContext.class.getName(), qualifiers)); } - if (neo4jTemplateBean == null) { - throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", - Neo4jTemplate.class.getName(), qualifiers)); - } - // Construct and return the repository bean. - return new Neo4jCdiRepositoryBean(neo4jMappingContextBean, neo4jTemplateBean, qualifiers, repositoryType, beanManager); + return new Neo4jCdiRepositoryBean(graphDatabase, qualifiers, repositoryType, + beanManager); } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java index a01ad51a4..795e76b2b 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiExtensionIntegrationTests.java @@ -15,24 +15,24 @@ */ package org.springframework.data.neo4j.repository.cdi; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + import org.apache.webbeans.cditest.CdiTestContainer; import org.apache.webbeans.cditest.CdiTestContainerLoader; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.neo4j.graphdb.Transaction; +import org.springframework.data.neo4j.core.GraphDatabase; import org.springframework.data.neo4j.model.Person; -import org.springframework.data.neo4j.support.Neo4jTemplate; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; /** * Integration tests for {@link Neo4jCdiRepositoryExtension}. * + * @see DATAGRAPH-340 * @author Nicki Watt + * @author Oliver Gierke */ public class CdiExtensionIntegrationTests { @@ -50,98 +50,99 @@ public class CdiExtensionIntegrationTests { } @Test + @SuppressWarnings("null") public void testRepositoryStyle1IsCreatedCorrectly() { - Neo4jTemplate template = container.getInstance(Neo4jTemplate.class); + GraphDatabase database = container.getInstance(GraphDatabase.class); RepositoryClient client = container.getInstance(RepositoryClient.class); - CdiPersonRepository repository = client.getRepository(); + CdiPersonRepository repository = client.repository; assertThat(repository, is(notNullValue())); - Person person = null; - Person result = null; + Person person = null; + Person result = null; - Transaction tx = template.getGraphDatabaseService().beginTx(); - try { - repository.deleteAll(); + Transaction tx = database.beginTx(); + try { + repository.deleteAll(); - person = new Person("Simon", 28); - result = repository.save(person); - tx.success(); - } catch (Exception e) { - tx.failure(); - } finally { - tx.finish(); - } + person = new Person("Simon", 28); + result = repository.save(person); + tx.success(); + } catch (Exception e) { + tx.failure(); + } finally { + tx.finish(); + } - assertThat(result, is(notNullValue())); + assertThat(result, is(notNullValue())); Long resultId = result.getId(); - Person lookedUpPerson = repository.findOne(person.getId()); - assertThat(lookedUpPerson.getId(), is(resultId)); + Person lookedUpPerson = repository.findOne(person.getId()); + assertThat(lookedUpPerson.getId(), is(resultId)); } - @Test - public void testRepositoryStyle2IsCreatedCorrectly() { + @Test + @SuppressWarnings("null") + public void testRepositoryStyle2IsCreatedCorrectly() { - Neo4jTemplate template = container.getInstance(Neo4jTemplate.class); - RepositoryClient client = container.getInstance(RepositoryClient.class); - CdiPersonRepository2 repository = client.getRepository2(); + GraphDatabase database = container.getInstance(GraphDatabase.class); + RepositoryClient client = container.getInstance(RepositoryClient.class); + CdiPersonRepository2 repository = client.repository2; - assertThat(repository, is(notNullValue())); + assertThat(repository, is(notNullValue())); - Person person = null; - Person result = null; + Person person = null; + Person result = null; - Transaction tx = template.getGraphDatabaseService().beginTx(); - try { - repository.deleteAll(); + Transaction tx = database.beginTx(); + try { + repository.deleteAll(); - person = new Person("Simon", 28); - result = repository.save(person); - tx.success(); - } catch (Exception e) { - tx.failure(); - } finally { - tx.finish(); - } + person = new Person("Simon", 28); + result = repository.save(person); + tx.success(); + } catch (Exception e) { + tx.failure(); + } finally { + tx.finish(); + } - assertThat(result, is(notNullValue())); - Long resultId = result.getId(); - Person lookedUpPerson = repository.findOne(person.getId()); - assertThat(lookedUpPerson.getId(), is(resultId)); - } + assertThat(result, is(notNullValue())); + Long resultId = result.getId(); + Person lookedUpPerson = repository.findOne(person.getId()); + assertThat(lookedUpPerson.getId(), is(resultId)); + } - @Test - @Ignore // uncomment me to see issue (Note also need to uncomment - // @Inject in RepositoryClient - public void demoNonWorkingRepository() { + @Test + @SuppressWarnings("null") + public void neo4jCrudRepositorySubTypeWorks() { - Neo4jTemplate template = container.getInstance(Neo4jTemplate.class); - RepositoryClient client = container.getInstance(RepositoryClient.class); - NonWorkingCdiPersonRepository repository = client.getNonWorkingRepository(); + GraphDatabase database = container.getInstance(GraphDatabase.class); + RepositoryClient client = container.getInstance(RepositoryClient.class); + CdiPersonRepository3 repository = client.repository3; - assertThat(repository, is(notNullValue())); + assertThat(repository, is(notNullValue())); - Person person = null; - Person result = null; + Person person = null; + Person result = null; - Transaction tx = template.getGraphDatabaseService().beginTx(); - try { - repository.deleteAll(); + Transaction tx = database.beginTx(); + try { + repository.deleteAll(); - person = new Person("Simon", 28); - result = repository.save(person); - tx.success(); - } catch (Exception e) { - tx.failure(); - } finally { - tx.finish(); - } + person = new Person("Simon", 28); + result = repository.save(person); + tx.success(); + } catch (Exception e) { + tx.failure(); + } finally { + tx.finish(); + } - assertThat(result, is(notNullValue())); - Long resultId = result.getId(); - Person lookedUpPerson = repository.findOne(person.getId()); - assertThat(lookedUpPerson.getId(), is(resultId)); + assertThat(result, is(notNullValue())); + Long resultId = result.getId(); + Person lookedUpPerson = repository.findOne(person.getId()); + assertThat(lookedUpPerson.getId(), is(resultId)); - } + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/NonWorkingCdiPersonRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiPersonRepository3.java similarity index 90% rename from spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/NonWorkingCdiPersonRepository.java rename to spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiPersonRepository3.java index f6ec557a7..b0d106355 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/NonWorkingCdiPersonRepository.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/CdiPersonRepository3.java @@ -19,7 +19,7 @@ import org.springframework.data.neo4j.model.Person; import org.springframework.data.neo4j.repository.CRUDRepository; -public interface NonWorkingCdiPersonRepository extends CRUDRepository { +public interface CdiPersonRepository3 extends CRUDRepository { } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCDIProducer.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCDIProducer.java deleted file mode 100644 index a0b2bfded..000000000 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCDIProducer.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.neo4j.repository.cdi; - - -import org.springframework.data.neo4j.core.GraphDatabase; -import org.springframework.data.neo4j.support.DelegatingGraphDatabase; -import org.springframework.data.neo4j.support.GraphDatabaseFactory; -import org.springframework.data.neo4j.support.Neo4jTemplate; -import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Produces; - -/** - * Simple component exposing {@link Neo4jTemplate} and {@link Neo4jMappingContext} - * instances as CDI bean. - * - * @author Nicki Watt - */ -class Neo4jCDIProducer { - - @Produces - @ApplicationScoped - public Neo4jTemplate createNeo4jTemplate() { - return new Neo4jTemplate(createGraphDatabase()); - } - - //@Produces - //@ApplicationScoped - private GraphDatabase createGraphDatabase() { - - GraphDatabaseFactory factory = new GraphDatabaseFactory(); - GraphDatabase graphDatabase = null; - try { - factory.setStoreLocation("target/cdi-test-db"); - graphDatabase = factory.getObject(); - registerShutdownHook(graphDatabase); - } catch (Exception e) { - throw new IllegalStateException("Unable to start up factory"); - } - - return graphDatabase; - - } - - private static void registerShutdownHook( final GraphDatabase graphDb ) - { - // Registers a shutdown hook for the Neo4j instance so that it - // shuts down nicely when the VM exits (even if you "Ctrl-C" the - // running application). - Runtime.getRuntime().addShutdownHook( new Thread() - { - @Override - public void run() - { - ((DelegatingGraphDatabase)graphDb).getGraphDatabaseService().shutdown(); - } - } ); - } - - @Produces - @ApplicationScoped - public Neo4jMappingContext createNeo4jMappingContext() { - return new Neo4jMappingContext(); - } - -} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiProducer.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiProducer.java new file mode 100644 index 000000000..319b1647e --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/Neo4jCdiProducer.java @@ -0,0 +1,46 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.neo4j.repository.cdi; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; + +import org.springframework.data.neo4j.core.GraphDatabase; +import org.springframework.data.neo4j.support.GraphDatabaseFactory; + +/** + * Simple component exposing a {@link GraphDatabase} as CDI bean. + * + * @author Nicki Watt + * @author Oliver Gierke + */ +class Neo4jCdiProducer { + + @Produces + @ApplicationScoped + GraphDatabase createGraphDatabase() throws Exception { + + GraphDatabaseFactory factory = new GraphDatabaseFactory(); + factory.setStoreLocation("target/cdi-test-db"); + + return factory.getObject(); + } + + void shutdown(@Disposes GraphDatabase database) { + database.shutdown(); + } +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java index 838df27ac..5a56602ac 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/cdi/RepositoryClient.java @@ -19,35 +19,11 @@ import javax.inject.Inject; /** * @author Nicki Watt + * @author Oliver Gierke */ class RepositoryClient { - @Inject - CdiPersonRepository repository; - - @Inject - CdiPersonRepository2 repository2; - - // @Inject - //- Uncomment me, but I have problems - // (javassist.bytecode.DuplicateMemberException: duplicate method) - NonWorkingCdiPersonRepository nonWorkingCdiPersonRepository; - - /** - * @return the repository - */ - public CdiPersonRepository getRepository() { - return repository; - } - - /** - * @return the repository - */ - public CdiPersonRepository2 getRepository2() { - return repository2; - } - - public NonWorkingCdiPersonRepository getNonWorkingRepository() { - return nonWorkingCdiPersonRepository; - } + @Inject CdiPersonRepository repository; + @Inject CdiPersonRepository2 repository2; + @Inject CdiPersonRepository3 repository3; }