diff --git a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java index f4079977f..8a0f0097e 100644 --- a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java +++ b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryBean.java @@ -1,75 +1,75 @@ -/* - * Copyright 2011-2017 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.jpa.repository.cdi; - -import java.lang.annotation.Annotation; -import java.util.Set; - -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.persistence.EntityManager; - -import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; -import org.springframework.data.repository.cdi.CdiRepositoryBean; -import org.springframework.data.repository.config.CustomRepositoryImplementationDetector; -import org.springframework.util.Assert; - -/** - * A bean which represents a JPA repository. - * - * @author Dirk Mahler - * @author Oliver Gierke - * @author Mark Paluch - * @param The type of the repository. - */ -class JpaRepositoryBean extends CdiRepositoryBean { - - private final Bean entityManagerBean; - - /** - * Constructs a {@link JpaRepositoryBean}. - * - * @param beanManager must not be {@literal null}. - * @param entityManagerBean must not be {@literal null}. - * @param qualifiers must not be {@literal null}. - * @param repositoryType must not be {@literal null}. - * @param detector can be {@literal null}. - */ - JpaRepositoryBean(BeanManager beanManager, Bean entityManagerBean, Set qualifiers, - Class repositoryType, CustomRepositoryImplementationDetector detector) { - - super(qualifiers, repositoryType, beanManager, detector); - - Assert.notNull(entityManagerBean, "EntityManager bean must not be null!"); - this.entityManagerBean = entityManagerBean; - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.lang.Object) - */ - @Override - public T create(CreationalContext creationalContext, Class repositoryType, Object customImplementation) { - - // Get an instance from the associated entity manager bean. - EntityManager entityManager = getDependencyInstance(entityManagerBean, EntityManager.class); - - // Create the JPA repository instance and return it. - JpaRepositoryFactory factory = new JpaRepositoryFactory(entityManager); - return factory.getRepository(repositoryType, customImplementation); - } -} +/* + * Copyright 2011-2017 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.jpa.repository.cdi; + +import java.lang.annotation.Annotation; +import java.util.Set; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.persistence.EntityManager; + +import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; +import org.springframework.data.repository.cdi.CdiRepositoryBean; +import org.springframework.data.repository.config.CustomRepositoryImplementationDetector; +import org.springframework.util.Assert; + +/** + * A bean which represents a JPA repository. + * + * @author Dirk Mahler + * @author Oliver Gierke + * @author Mark Paluch + * @param The type of the repository. + */ +class JpaRepositoryBean extends CdiRepositoryBean { + + private final Bean entityManagerBean; + + /** + * Constructs a {@link JpaRepositoryBean}. + * + * @param beanManager must not be {@literal null}. + * @param entityManagerBean must not be {@literal null}. + * @param qualifiers must not be {@literal null}. + * @param repositoryType must not be {@literal null}. + * @param detector can be {@literal null}. + */ + JpaRepositoryBean(BeanManager beanManager, Bean entityManagerBean, Set qualifiers, + Class repositoryType, CustomRepositoryImplementationDetector detector) { + + super(qualifiers, repositoryType, beanManager, detector); + + Assert.notNull(entityManagerBean, "EntityManager bean must not be null!"); + this.entityManagerBean = entityManagerBean; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.lang.Object) + */ + @Override + public T create(CreationalContext creationalContext, Class repositoryType, Object customImplementation) { + + // Get an instance from the associated entity manager bean. + EntityManager entityManager = getDependencyInstance(entityManagerBean, EntityManager.class); + + // Create the JPA repository instance and return it. + JpaRepositoryFactory factory = new JpaRepositoryFactory(entityManager); + return factory.getRepository(repositoryType, customImplementation); + } +} diff --git a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryExtension.java b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryExtension.java index eb1a60f52..07057cff5 100644 --- a/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryExtension.java +++ b/src/main/java/org/springframework/data/jpa/repository/cdi/JpaRepositoryExtension.java @@ -1,126 +1,126 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.HashSet; -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 javax.persistence.EntityManager; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.data.repository.cdi.CdiRepositoryBean; -import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; - -/** - * A portable CDI extension which registers beans for Spring Data JPA repositories. - * - * @author Dirk Mahler - * @author Oliver Gierke - * @author Mark Paluch - */ -public class JpaRepositoryExtension extends CdiRepositoryExtensionSupport { - - private static final Logger LOGGER = LoggerFactory.getLogger(JpaRepositoryExtension.class); - - private final Map, Bean> entityManagers = new HashMap, Bean>(); - - public JpaRepositoryExtension() { - LOGGER.info("Activating CDI extension for Spring Data JPA repositories."); - } - - /** - * Implementation of a an observer which checks for EntityManager beans and stores them in {@link #entityManagers} for - * later association with corresponding repository beans. - * - * @param The type. - * @param processBean The annotated type as defined by CDI. - */ - @SuppressWarnings("unchecked") - void processBean(@Observes ProcessBean processBean) { - Bean bean = processBean.getBean(); - for (Type type : bean.getTypes()) { - // Check if the bean is an EntityManager. - if (type instanceof Class && EntityManager.class.isAssignableFrom((Class) type)) { - Set qualifiers = new HashSet(bean.getQualifiers()); - if (bean.isAlternative() || !entityManagers.containsKey(qualifiers)) { - LOGGER.debug("Discovered '{}' with qualifiers {}.", EntityManager.class.getName(), qualifiers); - entityManagers.put(qualifiers, (Bean) bean); - } - } - } - } - - /** - * Implementation of a an observer which registers beans to the CDI container for the detected Spring Data - * repositories. - *

- * The repository beans are associated to the EntityManagers using their qualifiers. - * - * @param beanManager The BeanManager instance. - */ - void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { - - for (Entry, Set> entry : getRepositoryTypes()) { - - Class repositoryType = entry.getKey(); - Set qualifiers = entry.getValue(); - - // Create the bean representing the repository. - CdiRepositoryBean repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); - LOGGER.info("Registering bean for '{}' with qualifiers {}.", repositoryType.getName(), qualifiers); - - // Register the bean to the extension and the container. - registerBean(repositoryBean); - afterBeanDiscovery.addBean(repositoryBean); - } - } - - /** - * Creates a {@link Bean}. - * - * @param The type of the repository. - * @param repositoryType The class representing the repository. - * @param beanManager The BeanManager instance. - * @return The bean. - */ - private CdiRepositoryBean createRepositoryBean(Class repositoryType, Set qualifiers, - BeanManager beanManager) { - - // Determine the entity manager bean which matches the qualifiers of the repository. - Bean entityManagerBean = entityManagers.get(qualifiers); - - if (entityManagerBean == null) { - throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", - EntityManager.class.getName(), qualifiers)); - } - - // Construct and return the repository bean. - return new JpaRepositoryBean(beanManager, entityManagerBean, qualifiers, repositoryType, +/* + * Copyright 2011 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.jpa.repository.cdi; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.HashSet; +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 javax.persistence.EntityManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.repository.cdi.CdiRepositoryBean; +import org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport; + +/** + * A portable CDI extension which registers beans for Spring Data JPA repositories. + * + * @author Dirk Mahler + * @author Oliver Gierke + * @author Mark Paluch + */ +public class JpaRepositoryExtension extends CdiRepositoryExtensionSupport { + + private static final Logger LOGGER = LoggerFactory.getLogger(JpaRepositoryExtension.class); + + private final Map, Bean> entityManagers = new HashMap, Bean>(); + + public JpaRepositoryExtension() { + LOGGER.info("Activating CDI extension for Spring Data JPA repositories."); + } + + /** + * Implementation of a an observer which checks for EntityManager beans and stores them in {@link #entityManagers} for + * later association with corresponding repository beans. + * + * @param The type. + * @param processBean The annotated type as defined by CDI. + */ + @SuppressWarnings("unchecked") + void processBean(@Observes ProcessBean processBean) { + Bean bean = processBean.getBean(); + for (Type type : bean.getTypes()) { + // Check if the bean is an EntityManager. + if (type instanceof Class && EntityManager.class.isAssignableFrom((Class) type)) { + Set qualifiers = new HashSet(bean.getQualifiers()); + if (bean.isAlternative() || !entityManagers.containsKey(qualifiers)) { + LOGGER.debug("Discovered '{}' with qualifiers {}.", EntityManager.class.getName(), qualifiers); + entityManagers.put(qualifiers, (Bean) bean); + } + } + } + } + + /** + * Implementation of a an observer which registers beans to the CDI container for the detected Spring Data + * repositories. + *

+ * The repository beans are associated to the EntityManagers using their qualifiers. + * + * @param beanManager The BeanManager instance. + */ + void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) { + + for (Entry, Set> entry : getRepositoryTypes()) { + + Class repositoryType = entry.getKey(); + Set qualifiers = entry.getValue(); + + // Create the bean representing the repository. + CdiRepositoryBean repositoryBean = createRepositoryBean(repositoryType, qualifiers, beanManager); + LOGGER.info("Registering bean for '{}' with qualifiers {}.", repositoryType.getName(), qualifiers); + + // Register the bean to the extension and the container. + registerBean(repositoryBean); + afterBeanDiscovery.addBean(repositoryBean); + } + } + + /** + * Creates a {@link Bean}. + * + * @param The type of the repository. + * @param repositoryType The class representing the repository. + * @param beanManager The BeanManager instance. + * @return The bean. + */ + private CdiRepositoryBean createRepositoryBean(Class repositoryType, Set qualifiers, + BeanManager beanManager) { + + // Determine the entity manager bean which matches the qualifiers of the repository. + Bean entityManagerBean = entityManagers.get(qualifiers); + + if (entityManagerBean == null) { + throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", + EntityManager.class.getName(), qualifiers)); + } + + // Construct and return the repository bean. + return new JpaRepositoryBean(beanManager, entityManagerBean, qualifiers, repositoryType, getCustomImplementationDetector()); - } -} + } +} diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/MailMessage.java b/src/test/java/org/springframework/data/jpa/domain/sample/MailMessage.java index 055ffcf89..b546742cf 100755 --- a/src/test/java/org/springframework/data/jpa/domain/sample/MailMessage.java +++ b/src/test/java/org/springframework/data/jpa/domain/sample/MailMessage.java @@ -1,59 +1,59 @@ -/* - * 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.jpa.domain.sample; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.OneToOne; - -/** - * @author Thomas Darimont - */ -@Entity -public class MailMessage { - - @Id @GeneratedValue private Long id; - - @OneToOne(cascade = CascadeType.ALL) private MailSender mailSender; - - private String content; - - public MailSender getMailSender() { - return mailSender; - } - - public void setMailSender(MailSender sender) { - this.mailSender = sender; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } -} +/* + * 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.jpa.domain.sample; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +/** + * @author Thomas Darimont + */ +@Entity +public class MailMessage { + + @Id @GeneratedValue private Long id; + + @OneToOne(cascade = CascadeType.ALL) private MailSender mailSender; + + private String content; + + public MailSender getMailSender() { + return mailSender; + } + + public void setMailSender(MailSender sender) { + this.mailSender = sender; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/MailSender.java b/src/test/java/org/springframework/data/jpa/domain/sample/MailSender.java index db5faf614..7112f7cce 100644 --- a/src/test/java/org/springframework/data/jpa/domain/sample/MailSender.java +++ b/src/test/java/org/springframework/data/jpa/domain/sample/MailSender.java @@ -1,98 +1,98 @@ -/* - * 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.jpa.domain.sample; - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.ManyToOne; - -/** - * @author Thomas Darimont - */ -@Entity -public class MailSender { - - @Id @GeneratedValue private Long id; - - private String name; - - @ManyToOne(fetch = FetchType.LAZY) private MailUser mailUser; - - public MailSender() {} - - public MailSender(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public MailUser getMailUser() { - return mailUser; - } - - public void setMailUser(MailUser mailUser) { - this.mailUser = mailUser; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - MailSender other = (MailSender) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; - } -} +/* + * 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.jpa.domain.sample; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +/** + * @author Thomas Darimont + */ +@Entity +public class MailSender { + + @Id @GeneratedValue private Long id; + + private String name; + + @ManyToOne(fetch = FetchType.LAZY) private MailUser mailUser; + + public MailSender() {} + + public MailSender(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public MailUser getMailUser() { + return mailUser; + } + + public void setMailUser(MailUser mailUser) { + this.mailUser = mailUser; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MailSender other = (MailSender) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java index cace66d73..8fb73e9e8 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/CdiExtensionIntegrationTests.java @@ -1,88 +1,88 @@ -/* - * Copyright 2011-2017 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.jpa.repository.cdi; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import java.util.Set; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.spi.Bean; - -import org.apache.webbeans.cditest.CdiTestContainer; -import org.apache.webbeans.cditest.CdiTestContainerLoader; -import org.junit.BeforeClass; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Integration tests for Spring Data JPA CDI extension. - * - * @author Dirk Mahler - * @author Oliver Gierke - * @author Mark Paluch - */ -public class CdiExtensionIntegrationTests { - - private static Logger LOGGER = LoggerFactory.getLogger(CdiExtensionIntegrationTests.class); - - static CdiTestContainer container; - - @BeforeClass - public static void setUp() throws Exception { - - container = CdiTestContainerLoader.getCdiContainer(); - container.bootContainer(); - - LOGGER.debug("CDI container bootstrapped!"); - } - - @Test // DATAJPA-319 - @SuppressWarnings("rawtypes") - public void foo() { - - Set> beans = container.getBeanManager().getBeans(PersonRepository.class); - - assertThat(beans, hasSize(1)); - assertThat(beans.iterator().next().getScope(), is(equalTo((Class) ApplicationScoped.class))); - } - - @Test - public void saveAndFindAll() { - - RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); - - Person person = new Person(); - repositoryConsumer.save(person); - repositoryConsumer.findAll(); - } - - @Test // DATAJPA-584 - public void returnOneFromCustomImpl() { - - RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); - assertThat(repositoryConsumer.returnOne(), is(1)); - } - - @Test // DATAJPA-584 - public void useQualifiedCustomizedUserRepo() { - - RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); - repositoryConsumer.doSomethonOnUserDB(); - } -} +/* + * Copyright 2011-2017 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.jpa.repository.cdi; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.Set; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.spi.Bean; + +import org.apache.webbeans.cditest.CdiTestContainer; +import org.apache.webbeans.cditest.CdiTestContainerLoader; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Integration tests for Spring Data JPA CDI extension. + * + * @author Dirk Mahler + * @author Oliver Gierke + * @author Mark Paluch + */ +public class CdiExtensionIntegrationTests { + + private static Logger LOGGER = LoggerFactory.getLogger(CdiExtensionIntegrationTests.class); + + static CdiTestContainer container; + + @BeforeClass + public static void setUp() throws Exception { + + container = CdiTestContainerLoader.getCdiContainer(); + container.bootContainer(); + + LOGGER.debug("CDI container bootstrapped!"); + } + + @Test // DATAJPA-319 + @SuppressWarnings("rawtypes") + public void foo() { + + Set> beans = container.getBeanManager().getBeans(PersonRepository.class); + + assertThat(beans, hasSize(1)); + assertThat(beans.iterator().next().getScope(), is(equalTo((Class) ApplicationScoped.class))); + } + + @Test + public void saveAndFindAll() { + + RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); + + Person person = new Person(); + repositoryConsumer.save(person); + repositoryConsumer.findAll(); + } + + @Test // DATAJPA-584 + public void returnOneFromCustomImpl() { + + RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); + assertThat(repositoryConsumer.returnOne(), is(1)); + } + + @Test // DATAJPA-584 + public void useQualifiedCustomizedUserRepo() { + + RepositoryConsumer repositoryConsumer = container.getInstance(RepositoryConsumer.class); + repositoryConsumer.doSomethonOnUserDB(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/EntityManagerFactoryProducer.java b/src/test/java/org/springframework/data/jpa/repository/cdi/EntityManagerFactoryProducer.java index d7cfd7811..c06fcd451 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/EntityManagerFactoryProducer.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/EntityManagerFactoryProducer.java @@ -1,39 +1,39 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Disposes; -import javax.enterprise.inject.Produces; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import org.hibernate.Version; - -class EntityManagerFactoryProducer { - - @Produces - @ApplicationScoped - public EntityManagerFactory createEntityManagerFactory() { - - String hibernateVersion = Version.getVersionString(); - return Persistence.createEntityManagerFactory(hibernateVersion.startsWith("5.2") ? "cdi-52" : "cdi"); - } - - public void close(@Disposes EntityManagerFactory entityManagerFactory) { - entityManagerFactory.close(); - } -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.hibernate.Version; + +class EntityManagerFactoryProducer { + + @Produces + @ApplicationScoped + public EntityManagerFactory createEntityManagerFactory() { + + String hibernateVersion = Version.getVersionString(); + return Persistence.createEntityManagerFactory(hibernateVersion.startsWith("5.2") ? "cdi-52" : "cdi"); + } + + public void close(@Disposes EntityManagerFactory entityManagerFactory) { + entityManagerFactory.close(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/Person.java b/src/test/java/org/springframework/data/jpa/repository/cdi/Person.java index 00cf53b2d..bd9331f81 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/Person.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/Person.java @@ -1,47 +1,47 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -class Person { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - private String name; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +class Person { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/PersonDB.java b/src/test/java/org/springframework/data/jpa/repository/cdi/PersonDB.java index 893407628..148129a76 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/PersonDB.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/PersonDB.java @@ -1,30 +1,30 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.inject.Qualifier; - -@Qualifier -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) -@interface PersonDB { - -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Qualifier; + +@Qualifier +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER }) +@interface PersonDB { + +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/PersonRepository.java b/src/test/java/org/springframework/data/jpa/repository/cdi/PersonRepository.java index 19e85e023..53d84874e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/PersonRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/PersonRepository.java @@ -1,28 +1,28 @@ -/* - * Copyright 2011-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.jpa.repository.cdi; - -import java.util.List; - -import org.springframework.data.repository.cdi.Eager; - -@Eager -public interface PersonRepository { - - List findAll(); - - void save(Person person); -} +/* + * Copyright 2011-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.jpa.repository.cdi; + +import java.util.List; + +import org.springframework.data.repository.cdi.Eager; + +@Eager +public interface PersonRepository { + + List findAll(); + + void save(Person person); +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedEntityManagerProducer.java b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedEntityManagerProducer.java index ddbfe25c8..df3e668ff 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedEntityManagerProducer.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedEntityManagerProducer.java @@ -1,48 +1,48 @@ -/* - * Copyright 2011-2014 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.jpa.repository.cdi; - -import javax.enterprise.inject.Disposes; -import javax.enterprise.inject.Produces; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -/** - * @author Oliver Gierke - * @author Mark Paluch - */ -class QualifiedEntityManagerProducer { - - @Produces - @PersonDB - public EntityManager createPersonDBEntityManager(EntityManagerFactory entityManagerFactory) { - return entityManagerFactory.createEntityManager(); - } - - public void closePersonDB(@Disposes @PersonDB EntityManager entityManager) { - entityManager.close(); - } - - @Produces - @UserDB - public EntityManager createUserDBEntityManager(EntityManagerFactory entityManagerFactory) { - return entityManagerFactory.createEntityManager(); - } - - public void closeUserDB(@Disposes @UserDB EntityManager entityManager) { - entityManager.close(); - } -} +/* + * Copyright 2011-2014 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.jpa.repository.cdi; + +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +/** + * @author Oliver Gierke + * @author Mark Paluch + */ +class QualifiedEntityManagerProducer { + + @Produces + @PersonDB + public EntityManager createPersonDBEntityManager(EntityManagerFactory entityManagerFactory) { + return entityManagerFactory.createEntityManager(); + } + + public void closePersonDB(@Disposes @PersonDB EntityManager entityManager) { + entityManager.close(); + } + + @Produces + @UserDB + public EntityManager createUserDBEntityManager(EntityManagerFactory entityManagerFactory) { + return entityManagerFactory.createEntityManager(); + } + + public void closeUserDB(@Disposes @UserDB EntityManager entityManager) { + entityManager.close(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedPersonRepository.java b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedPersonRepository.java index f26f97af9..e2cb37bc3 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedPersonRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/QualifiedPersonRepository.java @@ -1,23 +1,23 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import org.springframework.data.repository.Repository; - -@PersonDB -public interface QualifiedPersonRepository extends PersonRepository, Repository { - -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import org.springframework.data.repository.Repository; + +@PersonDB +public interface QualifiedPersonRepository extends PersonRepository, Repository { + +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java b/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java index 05fd2b65e..2f5a78d3b 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/RepositoryConsumer.java @@ -1,49 +1,49 @@ -/* - * Copyright 2011-2014 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.jpa.repository.cdi; - -import javax.inject.Inject; - -/** - * @author Oliver Gierke - * @author Mark Paluch - */ -@Transactional -class RepositoryConsumer { - - @Inject PersonRepository unqualifiedRepo; - @Inject @PersonDB PersonRepository qualifiedRepo; - @Inject SamplePersonRepository samplePersonRepository; - @Inject @UserDB QualifiedCustomizedUserRepository qualifiedCustomizedUserRepository; - - public void findAll() { - unqualifiedRepo.findAll(); - qualifiedRepo.findAll(); - } - - public void save(Person person) { - unqualifiedRepo.save(person); - qualifiedRepo.save(person); - } - - public int returnOne() { - return samplePersonRepository.returnOne(); - } - - public void doSomethonOnUserDB() { - qualifiedCustomizedUserRepository.doSomething(); - } -} +/* + * Copyright 2011-2014 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.jpa.repository.cdi; + +import javax.inject.Inject; + +/** + * @author Oliver Gierke + * @author Mark Paluch + */ +@Transactional +class RepositoryConsumer { + + @Inject PersonRepository unqualifiedRepo; + @Inject @PersonDB PersonRepository qualifiedRepo; + @Inject SamplePersonRepository samplePersonRepository; + @Inject @UserDB QualifiedCustomizedUserRepository qualifiedCustomizedUserRepository; + + public void findAll() { + unqualifiedRepo.findAll(); + qualifiedRepo.findAll(); + } + + public void save(Person person) { + unqualifiedRepo.save(person); + qualifiedRepo.save(person); + } + + public int returnOne() { + return samplePersonRepository.returnOne(); + } + + public void doSomethonOnUserDB() { + qualifiedCustomizedUserRepository.doSomething(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/Transactional.java b/src/test/java/org/springframework/data/jpa/repository/cdi/Transactional.java index c59bb56f4..6ab67f444 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/Transactional.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/Transactional.java @@ -1,30 +1,30 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.interceptor.InterceptorBinding; - -@InterceptorBinding -@Target({ ElementType.METHOD, ElementType.TYPE }) -@Retention(RetentionPolicy.RUNTIME) -@interface Transactional { - -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.interceptor.InterceptorBinding; + +@InterceptorBinding +@Target({ ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +@interface Transactional { + +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/TransactionalInterceptor.java b/src/test/java/org/springframework/data/jpa/repository/cdi/TransactionalInterceptor.java index eacf7a90c..f995dbbd5 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/TransactionalInterceptor.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/TransactionalInterceptor.java @@ -1,69 +1,69 @@ -/* - * Copyright 2011 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. - */ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import javax.enterprise.inject.Any; -import javax.inject.Inject; -import javax.interceptor.AroundInvoke; -import javax.interceptor.Interceptor; -import javax.interceptor.InvocationContext; -import javax.persistence.EntityManager; -import javax.persistence.EntityTransaction; - -@Transactional -@Interceptor -class TransactionalInterceptor { - - @Inject - @Any - private EntityManager entityManager; - - @AroundInvoke - public Object runInTransaction(InvocationContext ctx) throws Exception { - EntityTransaction entityTransaction = this.entityManager.getTransaction(); - boolean isNew = !entityTransaction.isActive(); - try { - if (isNew) { - entityTransaction.begin(); - } - Object result = ctx.proceed(); - if (isNew) { - entityTransaction.commit(); - } - return result; - } catch (RuntimeException r) { - if (isNew) { - entityTransaction.rollback(); - } - throw r; - } - } -} +/* + * Copyright 2011 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. + */ +/* + * Copyright 2011 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.jpa.repository.cdi; + +import javax.enterprise.inject.Any; +import javax.inject.Inject; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptor; +import javax.interceptor.InvocationContext; +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; + +@Transactional +@Interceptor +class TransactionalInterceptor { + + @Inject + @Any + private EntityManager entityManager; + + @AroundInvoke + public Object runInTransaction(InvocationContext ctx) throws Exception { + EntityTransaction entityTransaction = this.entityManager.getTransaction(); + boolean isNew = !entityTransaction.isActive(); + try { + if (isNew) { + entityTransaction.begin(); + } + Object result = ctx.proceed(); + if (isNew) { + entityTransaction.commit(); + } + return result; + } catch (RuntimeException r) { + if (isNew) { + entityTransaction.rollback(); + } + throw r; + } + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedEntityManagerProducer.java b/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedEntityManagerProducer.java index 6b90f9f37..b7ef58996 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedEntityManagerProducer.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedEntityManagerProducer.java @@ -1,33 +1,33 @@ -/* - * Copyright 2011 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.jpa.repository.cdi; - -import javax.enterprise.inject.Disposes; -import javax.enterprise.inject.Produces; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -class UnqualifiedEntityManagerProducer { - - @Produces - public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) { - return entityManagerFactory.createEntityManager(); - } - - public void close(@Disposes EntityManager entityManager) { - entityManager.close(); - } -} +/* + * Copyright 2011 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.jpa.repository.cdi; + +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +class UnqualifiedEntityManagerProducer { + + @Produces + public EntityManager createEntityManager(EntityManagerFactory entityManagerFactory) { + return entityManagerFactory.createEntityManager(); + } + + public void close(@Disposes EntityManager entityManager) { + entityManager.close(); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedPersonRepository.java b/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedPersonRepository.java index 4e986f175..4ba255d41 100644 --- a/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedPersonRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/cdi/UnqualifiedPersonRepository.java @@ -1,22 +1,22 @@ -/* - * Copyright 2011-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.jpa.repository.cdi; - -import org.springframework.data.repository.Repository; - -public interface UnqualifiedPersonRepository extends PersonRepository, Repository { - -} +/* + * Copyright 2011-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.jpa.repository.cdi; + +import org.springframework.data.repository.Repository; + +public interface UnqualifiedPersonRepository extends PersonRepository, Repository { + +} diff --git a/src/test/resources/META-INF/beans.xml b/src/test/resources/META-INF/beans.xml index 73ae3a251..71dc6db1f 100644 --- a/src/test/resources/META-INF/beans.xml +++ b/src/test/resources/META-INF/beans.xml @@ -1,6 +1,6 @@ - - - - + + + + diff --git a/src/test/resources/hibernate.xml b/src/test/resources/hibernate.xml index 27d04d5b4..8b1cbf337 100644 --- a/src/test/resources/hibernate.xml +++ b/src/test/resources/hibernate.xml @@ -4,9 +4,9 @@ xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> - + - + - - \ No newline at end of file + +