DATAMONGO-1990 - Adapt build to changes in is-new-detection.

Removed a couple of Mockito stubbings that became unnecessary after the changes for DATACMNS-1333. Removed PersistableMongoEntityInformation as handling Persistable is now transparently taken care of by PersistentEntityInformation which MappingMongoEntityInformation extends.

Related tickets: DATACMNS-1333.
This commit is contained in:
Oliver Gierke
2018-06-01 13:29:27 +02:00
parent e90c5bad2c
commit 2d9232ca04
5 changed files with 28 additions and 196 deletions

View File

@@ -15,12 +15,10 @@
*/
package org.springframework.data.mongodb.repository.support;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Support class responsible for creating {@link MongoEntityInformation} instances for a given
@@ -47,10 +45,6 @@ final class MongoEntityInformationSupport {
Assert.notNull(entity, "Entity must not be null!");
MappingMongoEntityInformation<T, ID> entityInformation = new MappingMongoEntityInformation<T, ID>(
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
return ClassUtils.isAssignable(Persistable.class, entity.getType())
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
return new MappingMongoEntityInformation<>((MongoPersistentEntity<T>) entity, (Class<ID>) idType);
}
}

View File

@@ -1,103 +0,0 @@
/*
* Copyright 2017-2018 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.mongodb.repository.support;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
/**
* {@link MongoEntityInformation} implementation wrapping an existing {@link MongoEntityInformation} considering
* {@link Persistable} types by delegating {@link #isNew(Object)} and {@link #getId(Object)} to the corresponding
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
*
* @author Christoph Strobl
* @author Oliver Gierke
* @since 1.10
*/
@RequiredArgsConstructor
class PersistableMongoEntityInformation<T, ID> implements MongoEntityInformation<T, ID> {
private final @NonNull MongoEntityInformation<T, ID> delegate;
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
*/
@Override
public String getCollectionName() {
return delegate.getCollectionName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
*/
@Override
public String getIdAttribute() {
return delegate.getIdAttribute();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public boolean isNew(T t) {
if (t instanceof Persistable) {
return ((Persistable<ID>) t).isNew();
}
return delegate.isNew(t);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public ID getId(T t) {
if (t instanceof Persistable) {
return ((Persistable<ID>) t).getId();
}
return delegate.getId(t);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
*/
@Override
public Class<ID> getIdType() {
return delegate.getIdType();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
*/
@Override
public Class<T> getJavaType() {
return delegate.getJavaType();
}
}

View File

@@ -19,14 +19,17 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import lombok.Value;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
import org.springframework.data.repository.core.EntityInformation;
/**
* Unit tests for {@link MappingMongoEntityInformation}.
@@ -37,17 +40,13 @@ import org.springframework.data.mongodb.repository.support.MappingMongoEntityInf
public class MappingMongoEntityInformationUnitTests {
@Mock MongoPersistentEntity<Person> info;
@Before
public void setUp() {
when(info.getType()).thenReturn(Person.class);
when(info.getCollection()).thenReturn("Person");
}
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
@Test // DATAMONGO-248
public void usesEntityCollectionIfNoCustomOneGiven() {
when(info.getCollection()).thenReturn("Person");
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info);
assertThat(information.getCollectionName(), is("Person"));
}
@@ -58,4 +57,20 @@ public class MappingMongoEntityInformationUnitTests {
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info, "foobar");
assertThat(information.getCollectionName(), is("foobar"));
}
@Test // DATAMONGO-1590
public void considersPersistableIsNew() {
EntityInformation<TypeImplementingPersistable, Long> information = new MappingMongoEntityInformation<>(
persistableImplementingEntityTypeInfo);
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
}
@Value
static class TypeImplementingPersistable implements Persistable<Long> {
Long id;
boolean isNew;
}
}

View File

@@ -20,7 +20,6 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.Serializable;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
@@ -43,19 +42,13 @@ import org.springframework.data.repository.Repository;
@RunWith(MockitoJUnitRunner.class)
public class MongoRepositoryFactoryUnitTests {
@Mock
MongoTemplate template;
@Mock MongoTemplate template;
@Mock
MongoConverter converter;
@Mock MongoConverter converter;
@Mock
@SuppressWarnings("rawtypes")
MappingContext mappingContext;
@Mock @SuppressWarnings("rawtypes") MappingContext mappingContext;
@Mock
@SuppressWarnings("rawtypes")
MongoPersistentEntity entity;
@Mock @SuppressWarnings("rawtypes") MongoPersistentEntity entity;
@Before
@SuppressWarnings("unchecked")
@@ -69,7 +62,6 @@ public class MongoRepositoryFactoryUnitTests {
public void usesMappingMongoEntityInformationIfMappingContextSet() {
when(mappingContext.getRequiredPersistentEntity(Person.class)).thenReturn(entity);
when(entity.getType()).thenReturn(Person.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MongoEntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
@@ -81,7 +73,6 @@ public class MongoRepositoryFactoryUnitTests {
public void createsRepositoryWithIdTypeLong() {
when(mappingContext.getRequiredPersistentEntity(Person.class)).thenReturn(entity);
when(entity.getType()).thenReturn(Person.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);

View File

@@ -1,65 +0,0 @@
/*
* Copyright 2017-2018 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.mongodb.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import lombok.Value;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
/**
* Tests for {@link PersistableMongoEntityInformation}.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistableMappingMongoEntityInformationUnitTests {
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
@Before
public void setUp() {
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
}
@Test // DATAMONGO-1590
public void considersPersistableIsNew() {
PersistableMongoEntityInformation<TypeImplementingPersistable, Long> information = new PersistableMongoEntityInformation<TypeImplementingPersistable, Long>(
new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(persistableImplementingEntityTypeInfo));
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
}
@Value
static class TypeImplementingPersistable implements Persistable<Long> {
private static final long serialVersionUID = -1619090149320971099L;
Long id;
boolean isNew;
}
}