diff --git a/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java new file mode 100644 index 000000000..e63a12d45 --- /dev/null +++ b/src/main/java/org/springframework/data/mapping/context/PersistentEntities.java @@ -0,0 +1,101 @@ +/* + * Copyright 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.mapping.context; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.util.TypeInformation; +import org.springframework.util.Assert; + +/** + * Value object to access {@link PersistentEntity} instances managed by {@link MappingContext}s. + * + * @author Oliver Gierke + * @since 1.8 + */ +public class PersistentEntities implements Iterable> { + + private final Iterable> contexts; + + /** + * Creates a new {@link PersistentEntities} for the given {@link MappingContext}s. + * + * @param contexts + */ + public PersistentEntities(Iterable> contexts) { + + Assert.notNull(contexts, "MappingContexts must not be null!"); + this.contexts = contexts; + } + + /** + * Returns the {@link PersistentEntity} for the given type. Will consider all {@link MappingContext}s registered but + * return {@literal null} in case none of the registered ones already have a {@link PersistentEntity} registered for + * the given type. + * + * @param type can be {@literal null}. + * @return + */ + public PersistentEntity getPersistentEntity(Class type) { + + for (MappingContext context : contexts) { + + if (context.hasPersistentEntityFor(type)) { + return context.getPersistentEntity(type); + } + } + + return null; + } + + /** + * Returns all {@link TypeInformation} exposed by the registered {@link MappingContext}s. + * + * @return + */ + public Iterable> getManagedTypes() { + + Set> informations = new HashSet>(); + + for (MappingContext context : contexts) { + informations.addAll(context.getManagedTypes()); + } + + return Collections.unmodifiableSet(informations); + } + + /* + * (non-Javadoc) + * @see java.lang.Iterable#iterator() + */ + @Override + public Iterator> iterator() { + + List> entities = new ArrayList>(); + + for (MappingContext context : contexts) { + entities.addAll(context.getPersistentEntities()); + } + + return entities.iterator(); + } +} diff --git a/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java b/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java new file mode 100644 index 000000000..f302c23ea --- /dev/null +++ b/src/test/java/org/springframework/data/mapping/context/PersistentEntitiesUnitTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 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.mapping.context; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.util.ClassTypeInformation; + +/** + * Unit tests for {@link PersistentEntities}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class PersistentEntitiesUnitTests { + + @Mock SampleMappingContext first; + @Mock SampleMappingContext second; + + /** + * @see DATACMNS-458 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsNullMappingContexts() { + new PersistentEntities(null); + } + + /** + * @see DATACMNS-458 + */ + @Test + public void returnsPersistentEntitiesFromMappingContexts() { + + when(first.hasPersistentEntityFor(Sample.class)).thenReturn(false); + when(second.hasPersistentEntityFor(Sample.class)).thenReturn(true); + + new PersistentEntities(Arrays.asList(first, second)).getPersistentEntity(Sample.class); + + verify(first, times(1)).hasPersistentEntityFor(Sample.class); + verify(first, times(0)).getPersistentEntity(Sample.class); + + verify(second, times(1)).hasPersistentEntityFor(Sample.class); + verify(second, times(1)).getPersistentEntity(Sample.class); + } + + /** + * @see DATACMNS-458 + */ + @Test + public void indicatesManagedType() { + + SampleMappingContext context = new SampleMappingContext(); + context.setInitialEntitySet(Collections.singleton(Sample.class)); + context.initialize(); + + PersistentEntities entities = new PersistentEntities(Arrays.asList(context)); + + assertThat(entities.getPersistentEntity(Sample.class), is(notNullValue())); + assertThat(entities.getPersistentEntity(Object.class), is(nullValue())); + assertThat(entities.getManagedTypes(), hasItem(ClassTypeInformation.from(Sample.class))); + assertThat(entities, hasItem(entities.getPersistentEntity(Sample.class))); + } + + static class Sample { + + } +}