DATAKV-271 - Migrate tests to AssertJ.

This commit is contained in:
Mark Paluch
2019-07-10 13:31:13 +02:00
parent 1d55d33def
commit 26107e7777
22 changed files with 403 additions and 507 deletions

View File

@@ -15,15 +15,13 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsInstanceOf.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Date;
import java.util.UUID;
import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.util.ClassTypeInformation;
@@ -34,9 +32,10 @@ public class DefaultIdentifierGeneratorUnitTests {
DefaultIdentifierGenerator generator = DefaultIdentifierGenerator.INSTANCE;
@Test(expected = InvalidDataAccessApiUsageException.class)
@Test
public void shouldThrowExceptionForUnsupportedType() {
generator.generateIdentifierOfType(ClassTypeInformation.from(Date.class));
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
.isThrownBy(() -> generator.generateIdentifierOfType(ClassTypeInformation.from(Date.class)));
}
@Test // DATAKV-136
@@ -44,8 +43,8 @@ public class DefaultIdentifierGeneratorUnitTests {
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(UUID.class));
assertThat(value, is(notNullValue()));
assertThat(value, instanceOf(UUID.class));
assertThat(value).isNotNull();
assertThat(value).isInstanceOf(UUID.class);
}
@Test // DATAKV-136
@@ -53,8 +52,8 @@ public class DefaultIdentifierGeneratorUnitTests {
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(String.class));
assertThat(value, is(notNullValue()));
assertThat(value, instanceOf(String.class));
assertThat(value).isNotNull();
assertThat(value).isInstanceOf(String.class);
}
@Test // DATAKV-136
@@ -62,8 +61,8 @@ public class DefaultIdentifierGeneratorUnitTests {
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(Long.class));
assertThat(value, is(notNullValue()));
assertThat(value, instanceOf(Long.class));
assertThat(value).isNotNull();
assertThat(value).isInstanceOf(Long.class);
}
@Test // DATAKV-136
@@ -71,7 +70,7 @@ public class DefaultIdentifierGeneratorUnitTests {
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(Integer.class));
assertThat(value, is(notNullValue()));
assertThat(value, instanceOf(Integer.class));
assertThat(value).isNotNull();
assertThat(value).isInstanceOf(Integer.class);
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Iterator;
@@ -49,7 +48,7 @@ public class ForwardingCloseableIteratorUnitTests<K, V> {
CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);
try {
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.hasNext()).isTrue();
verify(iteratorMock, times(1)).hasNext();
} finally {
iterator.close();
@@ -65,14 +64,14 @@ public class ForwardingCloseableIteratorUnitTests<K, V> {
CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);
try {
assertThat(iterator.next(), notNullValue());
assertThat(iterator.next()).isNotNull();
verify(iteratorMock, times(1)).next();
} finally {
iterator.close();
}
}
@Test(expected = NoSuchElementException.class) // DATAKV-99
@Test // DATAKV-99
public void nextShouldThrowErrorWhenWrappedIteratorHasNoMoreElements() {
when(iteratorMock.next()).thenThrow(new NoSuchElementException());
@@ -80,7 +79,7 @@ public class ForwardingCloseableIteratorUnitTests<K, V> {
CloseableIterator<Entry<K, V>> iterator = new ForwardingCloseableIterator<>(iteratorMock);
try {
iterator.next();
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(iterator::next);
} finally {
iterator.close();
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.keyvalue.core.IterableConverter.*;
import java.util.ArrayList;
@@ -36,7 +35,7 @@ public class IterableConverterUnitTests {
@Test // DATAKV-101
public void toListShouldReturnEmptyListWhenSourceEmpty() {
assertThat(toList(Collections.emptySet()), empty());
assertThat(toList(Collections.emptySet())).isEmpty();
}
@Test // DATAKV-101
@@ -44,7 +43,7 @@ public class IterableConverterUnitTests {
List<String> source = new ArrayList<>();
assertThat(toList(source), sameInstance(source));
assertThat(toList(source)).isSameAs(source);
}
@Test // DATAKV-101
@@ -53,7 +52,7 @@ public class IterableConverterUnitTests {
Set<String> source = new HashSet<>();
source.add("tyrion");
assertThat(toList(source), instanceOf(List.class));
assertThat(toList(source)).isInstanceOf(List.class);
}
@Test // DATAKV-101
@@ -63,7 +62,7 @@ public class IterableConverterUnitTests {
source.add("tyrion");
source.add("jaime");
assertThat(toList(source), contains(source.toArray(new String[2])));
assertThat(toList(source)).containsExactly(source.toArray(new String[2]));
}
}

View File

@@ -15,13 +15,12 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.core.IsInstanceOf.*;
import static org.hamcrest.core.IsNull.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.NoSuchElementException;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.dao.DataRetrievalFailureException;
@@ -34,42 +33,43 @@ public class KeyValuePersistenceExceptionTranslatorUnitTests {
@Test // DATACMNS-525
public void translateExeptionShouldReturnDataAccessExceptionWhenGivenOne() {
assertThat(translator.translateExceptionIfPossible(new DataRetrievalFailureException("booh")),
instanceOf(DataRetrievalFailureException.class));
assertThat(translator.translateExceptionIfPossible(new DataRetrievalFailureException("booh")))
.isInstanceOf(DataRetrievalFailureException.class);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525, DATAKV-192
@Test // DATACMNS-525, DATAKV-192
public void translateExeptionShouldReturnNullWhenGivenNull() {
assertThat(translator.translateExceptionIfPossible(null), nullValue());
assertThatIllegalArgumentException()
.isThrownBy(() -> assertThat(translator.translateExceptionIfPossible(null)).isNull());
}
@Test // DATACMNS-525
public void translateExeptionShouldTranslateNoSuchElementExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new NoSuchElementException("")),
instanceOf(DataRetrievalFailureException.class));
assertThat(translator.translateExceptionIfPossible(new NoSuchElementException("")))
.isInstanceOf(DataRetrievalFailureException.class);
}
@Test // DATACMNS-525
public void translateExeptionShouldTranslateIndexOutOfBoundsExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new IndexOutOfBoundsException("")),
instanceOf(DataRetrievalFailureException.class));
assertThat(translator.translateExceptionIfPossible(new IndexOutOfBoundsException("")))
.isInstanceOf(DataRetrievalFailureException.class);
}
@Test // DATACMNS-525
public void translateExeptionShouldTranslateIllegalStateExceptionToDataRetrievalFailureException() {
assertThat(translator.translateExceptionIfPossible(new IllegalStateException("")),
instanceOf(DataRetrievalFailureException.class));
assertThat(translator.translateExceptionIfPossible(new IllegalStateException("")))
.isInstanceOf(DataRetrievalFailureException.class);
}
@Test // DATACMNS-525
public void translateExeptionShouldTranslateAnyJavaExceptionToUncategorizedKeyValueException() {
assertThat(translator.translateExceptionIfPossible(new UnsupportedOperationException("")),
instanceOf(UncategorizedKeyValueException.class));
assertThat(translator.translateExceptionIfPossible(new UnsupportedOperationException("")))
.isInstanceOf(UncategorizedKeyValueException.class);
}
@Test // DATACMNS-525
public void translateExeptionShouldReturnNullForNonJavaExceptions() {
assertThat(translator.translateExceptionIfPossible(new NoSuchBeanDefinitionException("")), nullValue());
assertThat(translator.translateExceptionIfPossible(new NoSuchBeanDefinitionException(""))).isNull();
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -32,6 +31,7 @@ import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.annotation.AliasFor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.annotation.Id;
@@ -72,21 +72,22 @@ public class KeyValueTemplateTests {
operations.insert("1", FOO_ONE);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowExceptionForNullId() {
operations.insert(null, FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> operations.insert(null, FOO_ONE));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowExceptionForNullObject() {
operations.insert("some-id", null);
assertThatIllegalArgumentException().isThrownBy(() -> operations.insert("some-id", null));
}
@Test(expected = DuplicateKeyException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowExecptionWhenObjectOfSameTypeAlreadyExists() {
operations.insert("1", FOO_ONE);
operations.insert("1", FOO_TWO);
assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(() -> operations.insert("1", FOO_TWO));
}
@Test // DATACMNS-525
@@ -102,7 +103,7 @@ public class KeyValueTemplateTests {
ClassWithStringId source = new ClassWithStringId();
ClassWithStringId target = operations.insert(source);
assertThat(target, sameInstance(source));
assertThat(target).isSameAs(source);
}
@Test // DATACMNS-525
@@ -113,28 +114,28 @@ public class KeyValueTemplateTests {
operations.insert(source);
assertThat(operations.findById("one", ClassWithStringId.class), is(Optional.of(source)));
assertThat(operations.findById("one", ClassWithStringId.class)).isEqualTo(Optional.of(source));
}
@Test // DATACMNS-525
public void findByIdShouldReturnObjectWithMatchingIdAndType() {
operations.insert("1", FOO_ONE);
assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_ONE)));
assertThat(operations.findById("1", Foo.class)).isEqualTo(Optional.of(FOO_ONE));
}
@Test // DATACMNS-525
public void findByIdSouldReturnOptionalEmptyIfNoMatchingIdFound() {
operations.insert("1", FOO_ONE);
assertThat(operations.findById("2", Foo.class), is(Optional.empty()));
assertThat(operations.findById("2", Foo.class)).isEmpty();
}
@Test // DATACMNS-525
public void findByIdShouldReturnOptionalEmptyIfNoMatchingTypeFound() {
operations.insert("1", FOO_ONE);
assertThat(operations.findById("1", Bar.class), is(Optional.empty()));
assertThat(operations.findById("1", Bar.class)).isEmpty();
}
@Test // DATACMNS-525
@@ -144,8 +145,8 @@ public class KeyValueTemplateTests {
operations.insert("2", FOO_TWO);
List<Foo> result = (List<Foo>) operations.find(STRING_QUERY, Foo.class);
assertThat(result, hasSize(1));
assertThat(result.get(0), is(FOO_TWO));
assertThat(result).hasSize(1);
assertThat(result.get(0)).isEqualTo(FOO_TWO);
}
@Test // DATACMNS-525
@@ -155,7 +156,7 @@ public class KeyValueTemplateTests {
operations.insert("2", FOO_TWO);
operations.insert("3", FOO_THREE);
assertThat(operations.findInRange(5, 5, Foo.class), emptyIterable());
assertThat(operations.findInRange(5, 5, Foo.class)).isEmpty();
}
@Test // DATACMNS-525
@@ -163,7 +164,7 @@ public class KeyValueTemplateTests {
operations.insert("1", FOO_ONE);
operations.update("1", FOO_TWO);
assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_TWO)));
assertThat(operations.findById("1", Foo.class)).isEqualTo(Optional.of(FOO_TWO));
}
@Test // DATACMNS-525
@@ -172,7 +173,7 @@ public class KeyValueTemplateTests {
operations.insert("1", FOO_ONE);
operations.update("1", BAR_ONE);
assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_ONE)));
assertThat(operations.findById("1", Foo.class)).isEqualTo(Optional.of(FOO_ONE));
}
@Test // DATACMNS-525
@@ -180,31 +181,31 @@ public class KeyValueTemplateTests {
operations.insert("1", FOO_ONE);
operations.delete("1", Foo.class);
assertThat(operations.findById("1", Foo.class), is(Optional.empty()));
assertThat(operations.findById("1", Foo.class)).isEmpty();
}
@Test // DATACMNS-525
public void deleteReturnsNullWhenNotExisting() {
operations.insert("1", FOO_ONE);
assertThat(operations.delete("2", Foo.class), nullValue());
assertThat(operations.delete("2", Foo.class)).isNull();
}
@Test // DATACMNS-525
public void deleteReturnsRemovedObject() {
operations.insert("1", FOO_ONE);
assertThat(operations.delete("1", Foo.class), is(FOO_ONE));
assertThat(operations.delete("1", Foo.class)).isEqualTo(FOO_ONE);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void deleteThrowsExceptionWhenIdCannotBeExctracted() {
operations.delete(FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> operations.delete(FOO_ONE));
}
@Test // DATACMNS-525
public void countShouldReturnZeroWhenNoElementsPresent() {
assertThat(operations.count(Foo.class), is(0L));
assertThat(operations.count(Foo.class)).isEqualTo(0L);
}
@Test // DATACMNS-525
@@ -213,7 +214,7 @@ public class KeyValueTemplateTests {
operations.insert("1", ALIASED);
operations.insert("2", SUBCLASS_OF_ALIASED);
assertThat(operations.findAll(ALIASED.getClass()), containsInAnyOrder(ALIASED, SUBCLASS_OF_ALIASED));
assertThat((List) operations.findAll(ALIASED.getClass())).contains(ALIASED, SUBCLASS_OF_ALIASED);
}
@Data

View File

@@ -15,10 +15,8 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -27,16 +25,14 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -64,8 +60,6 @@ import org.springframework.data.keyvalue.core.query.KeyValueQuery;
@RunWith(MockitoJUnitRunner.Silent.class)
public class KeyValueTemplateUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
private static final Foo FOO_ONE = new Foo("one");
private static final Foo FOO_TWO = new Foo("two");
private static final TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor ALIASED_USING_ALIAS_FOR = new TypeWithCustomComposedKeySpaceAnnotationUsingAliasFor(
@@ -79,19 +73,19 @@ public class KeyValueTemplateUnitTests {
private @Mock ApplicationEventPublisher publisherMock;
@Before
public void setUp() throws InstantiationException, IllegalAccessException {
public void setUp() {
this.template = new KeyValueTemplate(adapterMock);
this.template.setApplicationEventPublisher(publisherMock);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void shouldThrowExceptionWhenCreatingNewTempateWithNullAdapter() {
new KeyValueTemplate(null);
assertThatIllegalArgumentException().isThrownBy(() -> new KeyValueTemplate(null));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void shouldThrowExceptionWhenCreatingNewTempateWithNullMappingContext() {
new KeyValueTemplate(adapterMock, null);
assertThatIllegalArgumentException().isThrownBy(() -> new KeyValueTemplate(adapterMock, null));
}
@Test // DATACMNS-525
@@ -115,29 +109,26 @@ public class KeyValueTemplateUnitTests {
ClassWithStringId object = new ClassWithStringId();
assertThat(template.insert(object), is(object));
assertThat(template.insert("1", object), is(object));
assertThat(template.insert(object)).isEqualTo(object);
assertThat(template.insert("1", object)).isEqualTo(object);
}
@Test // DATACMNS-525
public void insertShouldThrowExceptionWhenObectWithIdAlreadyExists() {
exception.expect(DuplicateKeyException.class);
exception.expectMessage("id 1");
when(adapterMock.contains(anyString(), anyString())).thenReturn(true);
template.insert("1", FOO_ONE);
assertThatExceptionOfType(DuplicateKeyException.class).isThrownBy(() -> template.insert("1", FOO_ONE));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowExceptionForNullId() {
template.insert(null, FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> template.insert(null, FOO_ONE));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowExceptionForNullObject() {
template.insert("some-id", null);
assertThatIllegalArgumentException().isThrownBy(() -> template.insert("some-id", null));
}
@Test // DATACMNS-525
@@ -145,12 +136,12 @@ public class KeyValueTemplateUnitTests {
ClassWithStringId target = template.insert(new ClassWithStringId());
assertThat(target.id, notNullValue());
assertThat(target.id).isNotNull();
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void insertShouldThrowErrorWhenIdCannotBeResolved() {
template.insert(FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> template.insert(FOO_ONE));
}
@Test // DATACMNS-525
@@ -159,7 +150,7 @@ public class KeyValueTemplateUnitTests {
ClassWithStringId source = new ClassWithStringId();
ClassWithStringId target = template.insert(source);
assertThat(target, sameInstance(source));
assertThat(target).isSameAs(source);
}
@Test // DATACMNS-525
@@ -175,7 +166,7 @@ public class KeyValueTemplateUnitTests {
@Test // DATACMNS-525
public void findByIdShouldReturnOptionalEmptyWhenNoElementsPresent() {
assertThat(template.findById("1", Foo.class), is(Optional.empty()));
assertThat(template.findById("1", Foo.class)).isEmpty();
}
@Test // DATACMNS-525
@@ -186,9 +177,9 @@ public class KeyValueTemplateUnitTests {
verify(adapterMock, times(1)).get("1", Foo.class.getName(), Foo.class);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525, DATAKV-187
@Test // DATACMNS-525, DATAKV-187
public void findByIdShouldThrowExceptionWhenGivenNullId() {
template.findById(null, Foo.class);
assertThatIllegalArgumentException().isThrownBy(() -> template.findById(null, Foo.class));
}
@Test // DATACMNS-525
@@ -199,9 +190,9 @@ public class KeyValueTemplateUnitTests {
verify(adapterMock, times(1)).getAllOf(Foo.class.getName());
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void findAllOfShouldThrowExceptionWhenGivenNullType() {
template.findAll(null);
assertThatIllegalArgumentException().isThrownBy(() -> template.findAll(null));
}
@Test // DATACMNS-525
@@ -221,9 +212,9 @@ public class KeyValueTemplateUnitTests {
template.findInRange(1, 5, Foo.class);
verify(adapterMock, times(1)).find(captor.capture(), eq(Foo.class.getName()), eq(Foo.class));
assertThat(captor.getValue().getOffset(), is(1L));
assertThat(captor.getValue().getRows(), is(5));
assertThat(captor.getValue().getCriteria(), nullValue());
assertThat(captor.getValue().getOffset()).isEqualTo(1L);
assertThat(captor.getValue().getRows()).isEqualTo(5);
assertThat(captor.getValue().getCriteria()).isNull();
}
@Test // DATACMNS-525
@@ -240,18 +231,18 @@ public class KeyValueTemplateUnitTests {
ClassWithStringId object = new ClassWithStringId();
object.id = "foo";
assertThat(template.update(object), is(object));
assertThat(template.update("1", object), is(object));
assertThat(template.update(object)).isEqualTo(object);
assertThat(template.update("1", object)).isEqualTo(object);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void updateShouldThrowExceptionWhenGivenNullId() {
template.update(null, FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> template.update(null, FOO_ONE));
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void updateShouldThrowExceptionWhenGivenNullObject() {
template.update("1", null);
assertThatIllegalArgumentException().isThrownBy(() -> template.update("1", null));
}
@Test // DATACMNS-525
@@ -265,9 +256,9 @@ public class KeyValueTemplateUnitTests {
verify(adapterMock, times(1)).put(source.id, source, ClassWithStringId.class.getName());
}
@Test(expected = InvalidDataAccessApiUsageException.class) // DATACMNS-525
@Test // DATACMNS-525
public void updateShouldThrowErrorWhenIdInformationCannotBeExtracted() {
template.update(FOO_ONE);
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> template.update(FOO_ONE));
}
@Test // DATACMNS-525
@@ -289,9 +280,9 @@ public class KeyValueTemplateUnitTests {
verify(adapterMock, times(1)).delete("some-id", ClassWithStringId.class.getName(), ClassWithStringId.class);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void deleteThrowsExceptionWhenIdCannotBeExctracted() {
template.delete(FOO_ONE);
assertThatIllegalArgumentException().isThrownBy(() -> template.delete(FOO_ONE));
}
@Test // DATACMNS-525
@@ -304,12 +295,12 @@ public class KeyValueTemplateUnitTests {
when(adapterMock.count(Foo.class.getName())).thenReturn(2L);
assertThat(template.count(Foo.class), is(2L));
assertThat(template.count(Foo.class)).isEqualTo(2L);
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void countShouldThrowErrorOnNullType() {
template.count(null);
assertThatIllegalArgumentException().isThrownBy(() -> template.count(null));
}
@Test // DATACMNS-525
@@ -335,20 +326,20 @@ public class KeyValueTemplateUnitTests {
Collection foo = Arrays.asList(ALIASED_USING_ALIAS_FOR, SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);
when(adapterMock.getAllOf("aliased")).thenReturn(foo);
assertThat(template.findAll(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass()),
containsInAnyOrder(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR));
assertThat((Iterable) template.findAll(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass()))
.contains(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);
}
@Test // DATACMNS-525
public void insertSouldRespectTypeAliasAndFilterNonMatching() {
template.insert("1", ALIASED_USING_ALIAS_FOR);
assertThat(template.findById("1", SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass()), is(Optional.empty()));
assertThat(template.findById("1", SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass())).isEmpty();
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void setttingNullPersistenceExceptionTranslatorShouldThrowException() {
template.setExceptionTranslator(null);
assertThatIllegalArgumentException().isThrownBy(() -> template.setExceptionTranslator(null));
}
@Test // DATAKV-91
@@ -414,9 +405,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -432,9 +423,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -450,9 +441,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -468,9 +459,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -486,8 +477,8 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -504,9 +495,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -524,8 +515,8 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
}
@Test // DATAKV-91, DATAKV-104
@@ -543,9 +534,9 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKey(), is("1"));
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getPayload(), is(FOO_ONE));
assertThat(captor.getValue().getKey()).isEqualTo("1");
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
assertThat(captor.getValue().getPayload()).isEqualTo(FOO_ONE);
}
@Test // DATAKV-91, DATAKV-104, DATAKV-187
@@ -561,7 +552,7 @@ public class KeyValueTemplateUnitTests {
verify(publisherMock, times(1)).publishEvent(captor.capture());
verifyNoMoreInteractions(publisherMock);
assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName()));
assertThat(captor.getValue().getKeyspace()).isEqualTo(Foo.class.getName());
}
@Test // DATAKV-129

View File

@@ -16,13 +16,12 @@
package org.springframework.data.keyvalue.core;
import static java.lang.Integer.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.number.OrderingComparison.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Comparator;
import org.junit.Test;
import org.springframework.expression.spel.standard.SpelExpressionParser;
/**
@@ -44,79 +43,79 @@ public class SpelPropertyComperatorUnitTests {
public void shouldCompareStringAscCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<>("stringProperty", PARSER);
assertThat(comparator.compare(ONE, TWO), is(ONE.getStringProperty().compareTo(TWO.getStringProperty())));
assertThat(comparator.compare(ONE, TWO)).isEqualTo(ONE.getStringProperty().compareTo(TWO.getStringProperty()));
}
@Test // DATACMNS-525
public void shouldCompareStringDescCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<SomeType>("stringProperty", PARSER).desc();
assertThat(comparator.compare(ONE, TWO), is(TWO.getStringProperty().compareTo(ONE.getStringProperty())));
assertThat(comparator.compare(ONE, TWO)).isEqualTo(TWO.getStringProperty().compareTo(ONE.getStringProperty()));
}
@Test // DATACMNS-525
public void shouldCompareIntegerAscCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<>("integerProperty", PARSER);
assertThat(comparator.compare(ONE, TWO), is(ONE.getIntegerProperty().compareTo(TWO.getIntegerProperty())));
assertThat(comparator.compare(ONE, TWO)).isEqualTo(ONE.getIntegerProperty().compareTo(TWO.getIntegerProperty()));
}
@Test // DATACMNS-525
public void shouldCompareIntegerDescCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<SomeType>("integerProperty", PARSER).desc();
assertThat(comparator.compare(ONE, TWO), is(TWO.getIntegerProperty().compareTo(ONE.getIntegerProperty())));
assertThat(comparator.compare(ONE, TWO)).isEqualTo(TWO.getIntegerProperty().compareTo(ONE.getIntegerProperty()));
}
@Test // DATACMNS-525
public void shouldComparePrimitiveIntegerAscCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<>("primitiveProperty", PARSER);
assertThat(comparator.compare(ONE, TWO),
is(valueOf(ONE.getPrimitiveProperty()).compareTo(valueOf(TWO.getPrimitiveProperty()))));
assertThat(comparator.compare(ONE, TWO))
.isEqualTo(valueOf(ONE.getPrimitiveProperty()).compareTo(valueOf(TWO.getPrimitiveProperty())));
}
@Test // DATACMNS-525
public void shouldNotFailOnNullValues() {
Comparator<SomeType> comparator = new SpelPropertyComparator<>("stringProperty", PARSER);
assertThat(comparator.compare(ONE, new SomeType(null, null, 2)), is(1));
assertThat(comparator.compare(ONE, new SomeType(null, null, 2))).isEqualTo(1);
}
@Test // DATACMNS-525
public void shouldComparePrimitiveIntegerDescCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<SomeType>("primitiveProperty", PARSER).desc();
assertThat(comparator.compare(ONE, TWO),
is(valueOf(TWO.getPrimitiveProperty()).compareTo(valueOf(ONE.getPrimitiveProperty()))));
assertThat(comparator.compare(ONE, TWO))
.isEqualTo(valueOf(TWO.getPrimitiveProperty()).compareTo(valueOf(ONE.getPrimitiveProperty())));
}
@Test // DATACMNS-525
public void shouldSortNullsFirstCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<SomeType>("stringProperty", PARSER).nullsFirst();
assertThat(comparator.compare(ONE, new SomeType(null, null, 2)), equalTo(1));
assertThat(comparator.compare(ONE, new SomeType(null, null, 2))).isEqualTo(1);
}
@Test // DATACMNS-525
public void shouldSortNullsLastCorrectly() {
Comparator<SomeType> comparator = new SpelPropertyComparator<SomeType>("stringProperty", PARSER).nullsLast();
assertThat(comparator.compare(ONE, new SomeType(null, null, 2)), equalTo(-1));
assertThat(comparator.compare(ONE, new SomeType(null, null, 2))).isEqualTo(-1);
}
@Test // DATACMNS-525
public void shouldCompareNestedTypesCorrectly() {
Comparator<WrapperType> comparator = new SpelPropertyComparator<>("nestedType.stringProperty", PARSER);
assertThat(comparator.compare(WRAPPER_ONE, WRAPPER_TWO),
is(WRAPPER_ONE.getNestedType().getStringProperty().compareTo(WRAPPER_TWO.getNestedType().getStringProperty())));
assertThat(comparator.compare(WRAPPER_ONE, WRAPPER_TWO)).isEqualTo(
WRAPPER_ONE.getNestedType().getStringProperty().compareTo(WRAPPER_TWO.getNestedType().getStringProperty()));
}
@Test // DATACMNS-525
public void shouldCompareNestedTypesCorrectlyWhenOneOfThemHasNullValue() {
SpelPropertyComparator<WrapperType> comparator = new SpelPropertyComparator<>("nestedType.stringProperty", PARSER);
assertThat(comparator.compare(WRAPPER_ONE, new WrapperType("two", null)), is(greaterThanOrEqualTo(1)));
assertThat(comparator.compare(WRAPPER_ONE, new WrapperType("two", null))).isGreaterThanOrEqualTo(1);
}
static class WrapperType {

View File

@@ -15,9 +15,7 @@
*/
package org.springframework.data.keyvalue.core;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
@@ -30,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
@@ -71,8 +70,8 @@ public class SpelQueryEngineUnitTests {
doReturn(people).when(adapter).getAllOf(anyString());
assertThat(engine.execute(createQueryForMethodWithArgs("findByFirstname", "bob"), null, -1, -1, anyString()),
contains(BOB_WITH_FIRSTNAME));
assertThat(engine.execute(createQueryForMethodWithArgs("findByFirstname", "bob"), null, -1, -1, anyString()))
.containsExactly(BOB_WITH_FIRSTNAME);
}
@Test // DATAKV-114
@@ -80,7 +79,7 @@ public class SpelQueryEngineUnitTests {
doReturn(people).when(adapter).getAllOf(anyString());
assertThat(engine.count(createQueryForMethodWithArgs("findByFirstname", "bob"), anyString()), is(1L));
assertThat(engine.count(createQueryForMethodWithArgs("findByFirstname", "bob"), anyString())).isEqualTo(1L);
}
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.keyvalue.core.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -25,6 +24,7 @@ import java.lang.annotation.Target;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.keyvalue.TypeWithDirectKeySpaceAnnotation;
@@ -49,42 +49,42 @@ public class AnnotationBasedKeySpaceResolverUnitTests {
@Test // DATACMNS-525
public void shouldResolveKeySpaceDefaultValueCorrectly() {
assertThat(resolver.resolveKeySpace(EntityWithDefaultKeySpace.class), is("daenerys"));
assertThat(resolver.resolveKeySpace(EntityWithDefaultKeySpace.class)).isEqualTo("daenerys");
}
@Test // DATAKV-105
public void shouldReturnNullWhenNoKeySpaceFoundOnComposedPersistentAnnotation() {
assertThat(resolver.resolveKeySpace(TypeWithInhteritedPersistentAnnotationNotHavingKeySpace.class), nullValue());
assertThat(resolver.resolveKeySpace(TypeWithInhteritedPersistentAnnotationNotHavingKeySpace.class)).isNull();
}
@Test // DATAKV-105
public void shouldReturnNullWhenPersistentIsFoundOnNonComposedAnnotation() {
assertThat(resolver.resolveKeySpace(TypeWithPersistentAnnotationNotHavingKeySpace.class), nullValue());
assertThat(resolver.resolveKeySpace(TypeWithPersistentAnnotationNotHavingKeySpace.class)).isNull();
}
@Test // DATAKV-105
public void shouldReturnNullWhenPersistentIsNotFound() {
assertThat(resolver.resolveKeySpace(TypeWithoutKeySpace.class), nullValue());
assertThat(resolver.resolveKeySpace(TypeWithoutKeySpace.class)).isNull();
}
@Test // DATACMNS-525
public void shouldResolveInheritedKeySpaceCorrectly() {
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpace.class), is("viserys"));
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpace.class)).isEqualTo("viserys");
}
@Test // DATACMNS-525
public void shouldResolveDirectKeySpaceAnnotationCorrectly() {
assertThat(resolver.resolveKeySpace(TypeWithDirectKeySpaceAnnotation.class), is("rhaegar"));
assertThat(resolver.resolveKeySpace(TypeWithDirectKeySpaceAnnotation.class)).isEqualTo("rhaegar");
}
@Test // DATAKV-129
public void shouldResolveKeySpaceUsingAliasForCorrectly() {
assertThat(resolver.resolveKeySpace(EntityWithSetKeySpaceUsingAliasFor.class), is("viserys"));
assertThat(resolver.resolveKeySpace(EntityWithSetKeySpaceUsingAliasFor.class)).isEqualTo("viserys");
}
@Test // DATAKV-129
public void shouldResolveKeySpaceUsingAliasForCorrectlyOnSubClass() {
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpaceUsingAliasFor.class), is("viserys"));
assertThat(resolver.resolveKeySpace(EntityWithInheritedKeySpaceUsingAliasFor.class)).isEqualTo("viserys");
}
@PersistentAnnotationWithExplicitKeySpaceUsingAliasFor

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.keyvalue.repository;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -31,6 +30,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.PageRequest;
@@ -141,7 +141,7 @@ public class SimpleKeyValueRepositoryUnitTests {
public void existsByIdReturnsFalseForEmptyOptional() {
when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.empty());
assertThat(repo.existsById("one"), is(false));
assertThat(repo.existsById("one")).isFalse();
}
@Test // DATAKV-186
@@ -149,7 +149,7 @@ public class SimpleKeyValueRepositoryUnitTests {
public void existsByIdReturnsTrueWhenOptionalValuePresent() {
when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.of(new Foo()));
assertTrue(repo.existsById("one"));
assertThat(repo.existsById("one")).isTrue();
}
@Test // DATACMNS-525

View File

@@ -15,9 +15,7 @@
*/
package org.springframework.data.keyvalue.repository.query;
import static org.hamcrest.core.IsNot.*;
import static org.hamcrest.core.IsSame.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -29,6 +27,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.keyvalue.Person;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.core.SpelCriteria;
@@ -71,8 +70,8 @@ public class CachingKeyValuePartTreeQueryUnitTests {
SpelCriteria first = (SpelCriteria) query.prepareQuery(args).getCriteria();
SpelCriteria second = (SpelCriteria) query.prepareQuery(args).getCriteria();
assertThat(first.getExpression(), sameInstance(second.getExpression()));
assertThat(first.getContext(), not(sameInstance(second.getContext())));
assertThat(first.getExpression()).isSameAs(second.getExpression());
assertThat(first.getContext()).isNotSameAs(second.getContext());
}
static interface Repo {

View File

@@ -15,19 +15,18 @@
*/
package org.springframework.data.keyvalue.repository.query;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.List;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.keyvalue.Person;
@@ -67,7 +66,7 @@ public class KeyValuePartTreeQueryUnitTests {
Object first = query.prepareQuery(args).getCriteria();
Object second = query.prepareQuery(args).getCriteria();
assertThat(first, not(sameInstance(second)));
assertThat(first).isNotSameAs(second);
}
@Test // DATAKV-142
@@ -85,8 +84,8 @@ public class KeyValuePartTreeQueryUnitTests {
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { PageRequest.of(2, 3) });
assertThat(query.getOffset(), is(6L));
assertThat(query.getRows(), is(3));
assertThat(query.getOffset()).isEqualTo(6L);
assertThat(query.getRows()).isEqualTo(3);
}
@Test // DATAKV-142
@@ -103,7 +102,7 @@ public class KeyValuePartTreeQueryUnitTests {
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] {});
assertThat(query.getRows(), is(3));
assertThat(query.getRows()).isEqualTo(3);
}
@Test // DATAKV-142
@@ -121,11 +120,10 @@ public class KeyValuePartTreeQueryUnitTests {
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
assertThat(query.getCriteria(), is(notNullValue()));
assertThat(query.getCriteria(), IsInstanceOf.instanceOf(SpelCriteria.class));
assertThat(((SpelCriteria) query.getCriteria()).getExpression().getExpressionString(),
is("#it?.firstname?.equals([0])"));
assertThat(query.getRows(), is(3));
assertThat(query.getCriteria()).isInstanceOf(SpelCriteria.class);
assertThat(((SpelCriteria) query.getCriteria()).getExpression().getExpressionString())
.isEqualTo("#it?.firstname?.equals([0])");
assertThat(query.getRows()).isEqualTo(3);
}
interface Repo {

View File

@@ -15,11 +15,11 @@
*/
package org.springframework.data.keyvalue.repository.query;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import lombok.Data;
import lombok.SneakyThrows;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -31,6 +31,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.annotation.Id;
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
@@ -65,258 +66,260 @@ public class SpelQueryCreatorUnitTests {
@Mock RepositoryMetadata metadataMock;
@Test // DATACMNS-525
public void equalsReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByFirstname", BRAN.firstname).against(BRAN), is(true));
public void equalsReturnsTrueWhenMatching() {
assertThat(evaluate("findByFirstname", BRAN.firstname).against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void equalsReturnsFalseWhenNotMatching() throws Exception {
assertThat(evaluate("findByFirstname", BRAN.firstname).against(RICKON), is(false));
public void equalsReturnsFalseWhenNotMatching() {
assertThat(evaluate("findByFirstname", BRAN.firstname).against(RICKON)).isFalse();
}
@Test // DATACMNS-525
public void isTrueAssertedProperlyWhenTrue() throws Exception {
assertThat(evaluate("findBySkinChangerIsTrue").against(BRAN), is(true));
public void isTrueAssertedProperlyWhenTrue() {
assertThat(evaluate("findBySkinChangerIsTrue").against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void isTrueAssertedProperlyWhenFalse() throws Exception {
assertThat(evaluate("findBySkinChangerIsTrue").against(RICKON), is(false));
public void isTrueAssertedProperlyWhenFalse() {
assertThat(evaluate("findBySkinChangerIsTrue").against(RICKON)).isFalse();
}
@Test // DATACMNS-525
public void isFalseAssertedProperlyWhenTrue() throws Exception {
assertThat(evaluate("findBySkinChangerIsFalse").against(BRAN), is(false));
public void isFalseAssertedProperlyWhenTrue() {
assertThat(evaluate("findBySkinChangerIsFalse").against(BRAN)).isFalse();
}
@Test // DATACMNS-525
public void isFalseAssertedProperlyWhenFalse() throws Exception {
assertThat(evaluate("findBySkinChangerIsFalse").against(RICKON), is(true));
public void isFalseAssertedProperlyWhenFalse() {
assertThat(evaluate("findBySkinChangerIsFalse").against(RICKON)).isTrue();
}
@Test // DATACMNS-525
public void isNullAssertedProperlyWhenAttributeIsNull() throws Exception {
assertThat(evaluate("findByLastnameIsNull").against(BRAN), is(true));
public void isNullAssertedProperlyWhenAttributeIsNull() {
assertThat(evaluate("findByLastnameIsNull").against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void isNullAssertedProperlyWhenAttributeIsNotNull() throws Exception {
assertThat(evaluate("findByLastnameIsNull").against(ROBB), is(false));
public void isNullAssertedProperlyWhenAttributeIsNotNull() {
assertThat(evaluate("findByLastnameIsNull").against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void isNotNullFalseTrueWhenAttributeIsNull() throws Exception {
assertThat(evaluate("findByLastnameIsNotNull").against(BRAN), is(false));
public void isNotNullFalseTrueWhenAttributeIsNull() {
assertThat(evaluate("findByLastnameIsNotNull").against(BRAN)).isFalse();
}
@Test // DATACMNS-525
public void isNotNullReturnsTrueAttributeIsNotNull() throws Exception {
assertThat(evaluate("findByLastnameIsNotNull").against(ROBB), is(true));
public void isNotNullReturnsTrueAttributeIsNotNull() {
assertThat(evaluate("findByLastnameIsNotNull").against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void startsWithReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByFirstnameStartingWith", "r").against(ROBB), is(true));
public void startsWithReturnsTrueWhenMatching() {
assertThat(evaluate("findByFirstnameStartingWith", "r").against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void startsWithReturnsFalseWhenNotMatching() throws Exception {
assertThat(evaluate("findByFirstnameStartingWith", "r").against(BRAN), is(false));
public void startsWithReturnsFalseWhenNotMatching() {
assertThat(evaluate("findByFirstnameStartingWith", "r").against(BRAN)).isFalse();
}
@Test // DATACMNS-525
public void likeReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByFirstnameLike", "ob").against(ROBB), is(true));
public void likeReturnsTrueWhenMatching() {
assertThat(evaluate("findByFirstnameLike", "ob").against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void likeReturnsFalseWhenNotMatching() throws Exception {
assertThat(evaluate("findByFirstnameLike", "ra").against(ROBB), is(false));
public void likeReturnsFalseWhenNotMatching() {
assertThat(evaluate("findByFirstnameLike", "ra").against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void endsWithReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByFirstnameEndingWith", "bb").against(ROBB), is(true));
public void endsWithReturnsTrueWhenMatching() {
assertThat(evaluate("findByFirstnameEndingWith", "bb").against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void endsWithReturnsFalseWhenNotMatching() throws Exception {
assertThat(evaluate("findByFirstnameEndingWith", "an").against(ROBB), is(false));
}
@Test(expected = InvalidDataAccessApiUsageException.class) // DATACMNS-525
public void startsWithIgnoreCaseReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByFirstnameIgnoreCase", "R").against(ROBB), is(true));
public void endsWithReturnsFalseWhenNotMatching() {
assertThat(evaluate("findByFirstnameEndingWith", "an").against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void greaterThanReturnsTrueForHigherValues() throws Exception {
assertThat(evaluate("findByAgeGreaterThan", BRAN.age).against(ROBB), is(true));
public void startsWithIgnoreCaseReturnsTrueWhenMatching() {
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
.isThrownBy(() -> evaluate("findByFirstnameIgnoreCase", "R").against(ROBB));
}
@Test // DATACMNS-525
public void greaterThanReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByAgeGreaterThan", BRAN.age).against(RICKON), is(false));
public void greaterThanReturnsTrueForHigherValues() {
assertThat(evaluate("findByAgeGreaterThan", BRAN.age).against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void afterReturnsTrueForHigherValues() throws Exception {
assertThat(evaluate("findByBirthdayAfter", ROBB.birthday).against(BRAN), is(true));
public void greaterThanReturnsFalseForLowerValues() {
assertThat(evaluate("findByAgeGreaterThan", BRAN.age).against(RICKON)).isFalse();
}
@Test // DATACMNS-525
public void afterReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByBirthdayAfter", BRAN.birthday).against(ROBB), is(false));
public void afterReturnsTrueForHigherValues() {
assertThat(evaluate("findByBirthdayAfter", ROBB.birthday).against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void greaterThanEaualsReturnsTrueForHigherValues() throws Exception {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(ROBB), is(true));
public void afterReturnsFalseForLowerValues() {
assertThat(evaluate("findByBirthdayAfter", BRAN.birthday).against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void greaterThanEqualsReturnsTrueForEqualValues() throws Exception {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(BRAN), is(true));
public void greaterThanEaualsReturnsTrueForHigherValues() {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void greaterThanEqualsReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(RICKON), is(false));
public void greaterThanEqualsReturnsTrueForEqualValues() {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void lessThanReturnsTrueForHigherValues() throws Exception {
assertThat(evaluate("findByAgeLessThan", BRAN.age).against(ROBB), is(false));
public void greaterThanEqualsReturnsFalseForLowerValues() {
assertThat(evaluate("findByAgeGreaterThanEqual", BRAN.age).against(RICKON)).isFalse();
}
@Test // DATACMNS-525
public void lessThanReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByAgeLessThan", BRAN.age).against(RICKON), is(true));
public void lessThanReturnsTrueForHigherValues() {
assertThat(evaluate("findByAgeLessThan", BRAN.age).against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void beforeReturnsTrueForLowerValues() throws Exception {
assertThat(evaluate("findByBirthdayBefore", BRAN.birthday).against(ROBB), is(true));
public void lessThanReturnsFalseForLowerValues() {
assertThat(evaluate("findByAgeLessThan", BRAN.age).against(RICKON)).isTrue();
}
@Test // DATACMNS-525
public void beforeReturnsFalseForHigherValues() throws Exception {
assertThat(evaluate("findByBirthdayBefore", ROBB.birthday).against(BRAN), is(false));
public void beforeReturnsTrueForLowerValues() {
assertThat(evaluate("findByBirthdayBefore", BRAN.birthday).against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void lessThanEaualsReturnsTrueForHigherValues() throws Exception {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(ROBB), is(false));
public void beforeReturnsFalseForHigherValues() {
assertThat(evaluate("findByBirthdayBefore", ROBB.birthday).against(BRAN)).isFalse();
}
@Test // DATACMNS-525
public void lessThanEaualsReturnsTrueForEqualValues() throws Exception {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(BRAN), is(true));
public void lessThanEaualsReturnsTrueForHigherValues() {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void lessThanEqualsReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(RICKON), is(true));
public void lessThanEaualsReturnsTrueForEqualValues() {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(BRAN)).isTrue();
}
@Test // DATACMNS-525
public void betweenEqualsReturnsTrueForValuesInBetween() throws Exception {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(ARYA), is(true));
public void lessThanEqualsReturnsFalseForLowerValues() {
assertThat(evaluate("findByAgeLessThanEqual", BRAN.age).against(RICKON)).isTrue();
}
@Test // DATACMNS-525
public void betweenEqualsReturnsFalseForHigherValues() throws Exception {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(JON), is(false));
public void betweenEqualsReturnsTrueForValuesInBetween() {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(ARYA)).isTrue();
}
@Test // DATACMNS-525
public void betweenEqualsReturnsFalseForLowerValues() throws Exception {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(RICKON), is(false));
public void betweenEqualsReturnsFalseForHigherValues() {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(JON)).isFalse();
}
@Test // DATACMNS-525
public void connectByAndReturnsTrueWhenAllPropertiesMatching() throws Exception {
assertThat(evaluate("findByAgeGreaterThanAndLastname", BRAN.age, JON.lastname).against(JON), is(true));
public void betweenEqualsReturnsFalseForLowerValues() {
assertThat(evaluate("findByAgeBetween", BRAN.age, ROBB.age).against(RICKON)).isFalse();
}
@Test // DATACMNS-525
public void connectByAndReturnsFalseWhenOnlyFewPropertiesMatch() throws Exception {
assertThat(evaluate("findByAgeGreaterThanAndLastname", BRAN.age, JON.lastname).against(ROBB), is(false));
public void connectByAndReturnsTrueWhenAllPropertiesMatching() {
assertThat(evaluate("findByAgeGreaterThanAndLastname", BRAN.age, JON.lastname).against(JON)).isTrue();
}
@Test // DATACMNS-525
public void connectByOrReturnsTrueWhenOnlyFewPropertiesMatch() throws Exception {
assertThat(evaluate("findByAgeGreaterThanOrLastname", BRAN.age, JON.lastname).against(ROBB), is(true));
public void connectByAndReturnsFalseWhenOnlyFewPropertiesMatch() {
assertThat(evaluate("findByAgeGreaterThanAndLastname", BRAN.age, JON.lastname).against(ROBB)).isFalse();
}
@Test // DATACMNS-525
public void connectByOrReturnsTrueWhenAllPropertiesMatch() throws Exception {
assertThat(evaluate("findByAgeGreaterThanOrLastname", BRAN.age, JON.lastname).against(JON), is(true));
public void connectByOrReturnsTrueWhenOnlyFewPropertiesMatch() {
assertThat(evaluate("findByAgeGreaterThanOrLastname", BRAN.age, JON.lastname).against(ROBB)).isTrue();
}
@Test // DATACMNS-525
public void regexReturnsTrueWhenMatching() throws Exception {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(JON), is(true));
public void connectByOrReturnsTrueWhenAllPropertiesMatch() {
assertThat(evaluate("findByAgeGreaterThanOrLastname", BRAN.age, JON.lastname).against(JON)).isTrue();
}
@Test // DATACMNS-525
public void regexReturnsFalseWhenNotMatching() throws Exception {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(ROBB), is(false));
public void regexReturnsTrueWhenMatching() {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(JON)).isTrue();
}
@Test // DATACMNS-525
public void regexReturnsFalseWhenNotMatching() {
assertThat(evaluate("findByLastnameMatches", "^s.*w$").against(ROBB)).isFalse();
}
@Test // DATAKV-169
public void inReturnsMatchCorrectly() throws Exception {
public void inReturnsMatchCorrectly() {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(ROBB), is(true));
assertThat(evaluate("findByFirstnameIn", list).against(ROBB)).isTrue();
}
@Test // DATAKV-169
public void inNotMatchingReturnsCorrectly() throws Exception {
public void inNotMatchingReturnsCorrectly() {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
assertThat(evaluate("findByFirstnameIn", list).against(JON)).isFalse();
}
@Test // DATAKV-169
public void inWithNullCompareValuesCorrectly() throws Exception {
public void inWithNullCompareValuesCorrectly() {
ArrayList<String> list = new ArrayList<>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(JON), is(false));
assertThat(evaluate("findByFirstnameIn", list).against(JON)).isFalse();
}
@Test // DATAKV-169
public void inWithNullSourceValuesMatchesCorrectly() throws Exception {
public void inWithNullSourceValuesMatchesCorrectly() {
ArrayList<String> list = new ArrayList<>();
list.add(ROBB.firstname);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(false));
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10))).isFalse();
}
@Test // DATAKV-169
public void inMatchesNullValuesCorrectly() throws Exception {
public void inMatchesNullValuesCorrectly() {
ArrayList<String> list = new ArrayList<>();
list.add(null);
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10)), is(true));
assertThat(evaluate("findByFirstnameIn", list).against(new Person(null, 10))).isTrue();
}
@Test // DATAKV-185
public void noDerivedQueryArgumentsMatchesAlways() throws Exception {
public void noDerivedQueryArgumentsMatchesAlways() {
assertThat(evaluate("findBy").against(JON), is(true));
assertThat(evaluate("findBy").against(null), is(true));
assertThat(evaluate("findBy").against(JON)).isTrue();
assertThat(evaluate("findBy").against(null)).isTrue();
}
private Evaluation evaluate(String methodName, Object... args) throws Exception {
@SneakyThrows
private Evaluation evaluate(String methodName, Object... args) {
return new Evaluation(createQueryForMethodWithArgs(methodName, args).getCriteria());
}

View File

@@ -15,11 +15,9 @@
*/
package org.springframework.data.keyvalue.repository.support;
import static org.hamcrest.collection.IsArrayWithSize.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.keyvalue.repository.support.KeyValueQuerydslUtils.*;
import org.hamcrest.collection.IsArrayContainingInOrder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Sort;
@@ -53,14 +51,14 @@ public class KeyValueQuerydslUtilsUnitTests {
this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
}
@Test(expected = IllegalArgumentException.class) // DATACMNS-525
@Test // DATACMNS-525
public void toOrderSpecifierThrowsExceptioOnNullPathBuilder() {
toOrderSpecifier(Sort.by("firstname"), null);
assertThatIllegalArgumentException().isThrownBy(() -> toOrderSpecifier(Sort.by("firstname"), null));
}
@Test // DATACMNS-525, DATAKV-197
public void toOrderSpecifierReturnsEmptyArrayWhenSortIsUnsorted() {
assertThat(toOrderSpecifier(Sort.unsorted(), builder), arrayWithSize(0));
assertThat(toOrderSpecifier(Sort.unsorted(), builder)).hasSize(0);
}
@Test // DATACMNS-525
@@ -70,8 +68,7 @@ public class KeyValueQuerydslUtilsUnitTests {
OrderSpecifier<?>[] specifiers = toOrderSpecifier(sort, builder);
assertThat(specifiers,
IsArrayContainingInOrder.arrayContaining(QPerson.person.firstname.asc()));
assertThat(specifiers).containsExactly(QPerson.person.firstname.asc());
}
@Test // DATACMNS-525
@@ -81,8 +78,7 @@ public class KeyValueQuerydslUtilsUnitTests {
OrderSpecifier<?>[] specifiers = toOrderSpecifier(sort, builder);
assertThat(specifiers,
IsArrayContainingInOrder.arrayContaining(QPerson.person.firstname.desc()));
assertThat(specifiers).containsExactly(QPerson.person.firstname.desc());
}
@Test // DATACMNS-525
@@ -92,8 +88,7 @@ public class KeyValueQuerydslUtilsUnitTests {
OrderSpecifier<?>[] specifiers = toOrderSpecifier(sort, builder);
assertThat(specifiers, IsArrayContainingInOrder.arrayContaining(QPerson.person.firstname.desc(),
QPerson.person.age.asc()));
assertThat(specifiers).containsExactly(QPerson.person.firstname.desc(), QPerson.person.age.asc());
}
@Test // DATACMNS-525
@@ -103,7 +98,6 @@ public class KeyValueQuerydslUtilsUnitTests {
OrderSpecifier<?>[] specifiers = toOrderSpecifier(sort, builder);
assertThat(specifiers,
IsArrayContainingInOrder.arrayContaining(QPerson.person.firstname.desc().nullsLast()));
assertThat(specifiers).containsExactly(QPerson.person.firstname.desc().nullsLast());
}
}

View File

@@ -15,14 +15,12 @@
*/
package org.springframework.data.keyvalue.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery;
import org.springframework.data.repository.Repository;
@@ -37,8 +35,6 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator;
*/
public class KeyValueRepositoryFactoryBeanUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
KeyValueRepositoryFactoryBean<?, ?, ?> factoryBean;
@Before
@@ -47,37 +43,38 @@ public class KeyValueRepositoryFactoryBeanUnitTests {
SampleRepository.class);
}
@Test(expected = IllegalArgumentException.class) // DATAKV-123
@Test // DATAKV-123
public void rejectsNullKeyValueOperations() {
factoryBean.setKeyValueOperations(null);
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.setKeyValueOperations(null));
}
@Test(expected = IllegalArgumentException.class) // DATAKV-123
@Test // DATAKV-123
public void rejectsNullQueryCreator() {
factoryBean.setQueryCreator(null);
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.setQueryCreator(null));
}
@Test(expected = IllegalArgumentException.class) // DATAKV-123
@Test // DATAKV-123
public void rejectsUninitializedInstance() {
factoryBean.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.afterPropertiesSet());
}
@SuppressWarnings("unchecked")
@Test(expected = IllegalArgumentException.class) // DATAKV-123
@Test // DATAKV-123
public void rejectsInstanceWithoutKeyValueOperations() {
Class<? extends AbstractQueryCreator<?, ?>> creatorType = (Class<? extends AbstractQueryCreator<?, ?>>) mock(
AbstractQueryCreator.class).getClass();
factoryBean.setQueryCreator(creatorType);
factoryBean.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class) // DATAKV-123
@Test // DATAKV-123
public void rejectsInstanceWithoutQueryCreator() {
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
factoryBean.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.afterPropertiesSet());
}
@Test // DATAKV-123
@@ -92,12 +89,12 @@ public class KeyValueRepositoryFactoryBeanUnitTests {
factoryBean.setKeyValueOperations(mock(KeyValueOperations.class));
factoryBean.setQueryType(queryType);
assertThat(factoryBean.createRepositoryFactory(), is(notNullValue()));
assertThat(factoryBean.createRepositoryFactory()).isNotNull();
}
@Test(expected = IllegalArgumentException.class) // DATAKV-112
@Test // DATAKV-112
public void rejectsNullQueryType() {
factoryBean.setQueryType(null);
assertThatIllegalArgumentException().isThrownBy(() -> factoryBean.setQueryType(null));
}
interface SampleRepository extends Repository<Object, Object> {}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2015-2019 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
*
* https://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.keyvalue.test.util;
import java.util.AbstractMap;
import java.util.Map;
import org.hamcrest.CustomMatcher;
import org.hamcrest.core.IsEqual;
/**
* @author Christoph Strobl
* @author Thomas Darimont
*/
public class IsEntry extends CustomMatcher<Map.Entry<?, ?>> {
private final Map.Entry<?, ?> expected;
private IsEntry(Map.Entry<?, ?> entry) {
super(String.format("an entry %s=%s.", entry != null ? entry.getKey() : "null", entry != null ? entry.getValue()
: "null"));
this.expected = entry;
}
@Override
public boolean matches(Object item) {
if (item == null && expected == null) {
return true;
}
if (!(item instanceof Map.Entry)) {
return false;
}
Map.Entry<?, ?> actual = (Map.Entry<?, ?>) item;
return new IsEqual<Object>(expected.getKey()).matches(actual.getKey())
&& new IsEqual<Object>(expected.getValue()).matches(actual.getValue());
}
public static IsEntry isEntry(Object key, Object value) {
return isEntry(new EntryImpl(key, value));
}
public static IsEntry isEntry(Map.Entry<?, ?> entry) {
return new IsEntry(entry);
}
private static class EntryImpl extends AbstractMap.SimpleEntry<Object, Object> {
private static final long serialVersionUID = 1L;
private EntryImpl(Object key, Object value) {
super(key, value);
}
@Override
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}
}
}