DATAJPA-274 - Introduce JPA Metamodel based MappingContext implementation.

Implemented rudimentary MappingContext to expose a JpaPersistentEntity through RepositoryFactoryInformation.
This commit is contained in:
Oliver Gierke
2012-11-28 18:15:49 +01:00
parent 69bcc3d887
commit 258af43397
9 changed files with 418 additions and 7 deletions

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2012 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.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link JpaMetamodelMappingContext}.
*
* @author Oliver Gierke
* @since 1.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
public class JpaMetamodelMappingContextIntegrationTests {
JpaMetamodelMappingContext context;
@PersistenceContext
EntityManager em;
@Before
public void setUp() {
context = new JpaMetamodelMappingContext(em.getMetamodel());
}
@Test
public void setsUpMappingContextCorrectly() {
JpaPersistentEntityImpl<?> entity = context.getPersistentEntity(User.class);
assertThat(entity, is(notNullValue()));
}
@Test
public void detectsIdProperty() {
JpaPersistentEntityImpl<?> entity = context.getPersistentEntity(User.class);
assertThat(entity.getIdProperty(), is(notNullValue()));
}
@Test
public void detectsAssociation() {
JpaPersistentEntityImpl<?> entity = context.getPersistentEntity(User.class);
assertThat(entity, is(notNullValue()));
JpaPersistentProperty property = entity.getPersistentProperty("manager");
assertThat(property.isAssociation(), is(true));
}
@Test
public void detectsPropertyIsEntity() {
JpaPersistentEntityImpl<?> entity = context.getPersistentEntity(User.class);
assertThat(entity, is(notNullValue()));
JpaPersistentProperty property = entity.getPersistentProperty("manager");
assertThat(property.isEntity(), is(true));
property = entity.getPersistentProperty("lastname");
assertThat(property.isEntity(), is(false));
}
}

View File

@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.Metamodel;
import org.junit.Before;
import org.junit.Test;
@@ -60,6 +61,8 @@ public class JpaRepositoryFactoryBeanUnitTests {
PersistenceExceptionTranslator translator;
@Mock
Repository<?, ?> repository;
@Mock
Metamodel metamodel;
@Before
@SuppressWarnings("unchecked")
@@ -70,6 +73,7 @@ public class JpaRepositoryFactoryBeanUnitTests {
when(beanFactory.getBeansOfType(eq(PersistenceExceptionTranslator.class), anyBoolean(), anyBoolean())).thenReturn(
beans);
when(factory.getRepository(any(Class.class), any(Object.class))).thenReturn(repository);
when(entityManager.getMetamodel()).thenReturn(metamodel);
// Setup standard factory configuration
factoryBean = new DummyJpaRepositoryFactoryBean<SimpleSampleRepository, User, Integer>();