DATACMNS-457 - MappingContext now provides information about managed types.

Added MappingContext.getManagedTypes() as well as ….hasPersistentEntity(Class) to access all types currently managed by the MappingContext and find out whether a type is managed by the context.
This commit is contained in:
Oliver Gierke
2014-03-01 12:37:23 +01:00
parent 2a8f4ddf93
commit 4969b9094a
3 changed files with 83 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 by the original author(s).
* Copyright 2011-2014 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
*
* @param E the concrete {@link PersistentEntity} type the {@link MappingContext} implementation creates
* @param P the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates
* @author Jon Brisbin <jbrisbin@vmware.com>
* @author Jon Brisbin
* @author Oliver Gierke
*/
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
@@ -139,6 +139,15 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
return getPersistentEntity(ClassTypeInformation.from(type));
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.MappingContext#hasPersistentEntityFor(java.lang.Class)
*/
@Override
public boolean hasPersistentEntityFor(Class<?> type) {
return type == null ? false : persistentEntities.containsKey(ClassTypeInformation.from(type));
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation)
@@ -303,6 +312,23 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.context.PersistentEntityAware#getManagedTypes()
*/
@Override
public Collection<TypeInformation<?>> getManagedTypes() {
try {
read.lock();
return Collections.unmodifiableSet(new HashSet<TypeInformation<?>>(persistentEntities.keySet()));
} finally {
read.unlock();
}
}
/**
* Creates the concrete {@link PersistentEntity} instance.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* 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.
@@ -50,6 +50,15 @@ public interface MappingContext<E extends PersistentEntity<?, P>, P extends Pers
*/
E getPersistentEntity(Class<?> type);
/**
* Returns whether the {@link MappingContext} currently contains a {@link PersistentEntity} for the type.
*
* @param type can be {@literal null}.
* @return
* @since 1.8
*/
boolean hasPersistentEntityFor(Class<?> type);
/**
* Returns a {@link PersistentEntity} for the given {@link TypeInformation}. Will return {@literal null} for types
* that are considered simple ones.
@@ -87,4 +96,12 @@ public interface MappingContext<E extends PersistentEntity<?, P>, P extends Pers
* @return the {@link PersistentPropertyPath} representing the given property path on the given type.
*/
PersistentPropertyPath<P> getPersistentPropertyPath(String propertyPath, Class<?> type);
/**
* Returns the {@link TypeInformation}s for all {@link PersistentEntity}s in the {@link MappingContext}.
*
* @return all {@link TypeInformation}s for the {@link PersistentEntity}s in the {@link MappingContext}.
* @since 1.8
*/
Collection<TypeInformation<?>> getManagedTypes();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 by the original author(s).
* Copyright 2011-2014 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.mapping.context;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.beans.PropertyDescriptor;
@@ -27,6 +29,7 @@ import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
/**
@@ -36,6 +39,35 @@ import org.springframework.data.util.TypeInformation;
*/
public class AbstractMappingContextIntegrationTests<T extends PersistentProperty<T>> {
/**
* @see DATACMNS-457
*/
@Test
public void returnsManagedType() {
SampleMappingContext context = new SampleMappingContext();
context.setInitialEntitySet(Collections.singleton(Person.class));
context.initialize();
assertThat(context.getManagedTypes(), hasItem(ClassTypeInformation.from(Person.class)));
}
/**
* @see DATACMNS-457
*/
@Test
public void indicatesManagedType() {
SampleMappingContext context = new SampleMappingContext();
context.setInitialEntitySet(Collections.singleton(Person.class));
context.initialize();
assertThat(context.hasPersistentEntityFor(Person.class), is(true));
}
/**
* @see DATACMNS-65
*/
@Test
public void foo() throws InterruptedException {
@@ -56,7 +88,7 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
entity.doWithProperties(new PropertyHandler<T>() {
public void doWithPersistentProperty(T persistentProperty) {
try {
Thread.sleep(1000);
Thread.sleep(250);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@@ -66,7 +98,7 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
});
a.start();
Thread.sleep(2800);
Thread.sleep(700);
b.start();
a.join();
@@ -88,12 +120,12 @@ public class AbstractMappingContextIntegrationTests<T extends PersistentProperty
PersistentProperty prop = mock(PersistentProperty.class);
when(prop.getTypeInformation()).thenReturn((TypeInformation) owner.getTypeInformation());
when(prop.getTypeInformation()).thenReturn(owner.getTypeInformation());
when(prop.getName()).thenReturn(field.getName());
when(prop.getPersistentEntityType()).thenReturn(Collections.EMPTY_SET);
try {
Thread.sleep(800);
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}