diff --git a/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java index 3e66df9..fe15fd5 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java @@ -1,18 +1,36 @@ +/* + * Copyright 2015 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.keyvalue.core; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; -import java.util.LinkedList; import java.util.List; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import static org.mockito.Matchers.anyString; import org.mockito.Mock; -import static org.mockito.Mockito.when; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.annotation.Id; import org.springframework.data.keyvalue.core.query.KeyValueQuery; @@ -23,74 +41,75 @@ import org.springframework.data.repository.query.QueryMethod; import org.springframework.data.repository.query.parser.PartTree; import org.springframework.expression.spel.standard.SpelExpression; import org.springframework.expression.spel.support.StandardEvaluationContext; -import org.springframework.util.ObjectUtils; - /** - * @author Macko Martin + * Unit tests for {@link SpelQueryEngine}. + * + * @author Martin Macko + * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class SpelQueryEngineUnitTests { - private final Person BOB_WITH_FIRSTNAME = new Person("bob", 30); - private final Person MIKE_WITHOUT_FIRSTNAME = new Person(null, 25); + static final Person BOB_WITH_FIRSTNAME = new Person("bob", 30); + static final Person MIKE_WITHOUT_FIRSTNAME = new Person(null, 25); - @Mock RepositoryMetadata metadataMock; - @Mock KeyValueAdapter adapterMock; + @Mock KeyValueAdapter adapter; - private Iterable iterablePeople; + SpelQueryEngine engine; + + Iterable people = Arrays.asList(BOB_WITH_FIRSTNAME, MIKE_WITHOUT_FIRSTNAME); @Before public void setUp() { - List people = new LinkedList(); - people.add(BOB_WITH_FIRSTNAME); - people.add(MIKE_WITHOUT_FIRSTNAME); - iterablePeople = people; + + engine = new SpelQueryEngine(); + engine.registerAdapter(adapter); } + /** + * @see DATAKV-114 + */ @Test - public void testExecuteWithPropertyNull() throws Exception { - when(adapterMock.getAllOf(anyString())).thenReturn(iterablePeople); + @SuppressWarnings("unchecked") + public void queriesEntitiesWithNullProperty() throws Exception { - SpelQueryEngine sqe = new SpelQueryEngine(); - sqe.registerAdapter(adapterMock); + doReturn(people).when(adapter).getAllOf(anyString()); - Collection returnCol = sqe.execute(createQueryForMethodWithArgs("findByFirstname", "bob"), null, -1, -1, anyString()); - assertTrue(returnCol.contains(BOB_WITH_FIRSTNAME)); + assertThat((Collection) engine.execute(createQueryForMethodWithArgs("findByFirstname", "bob"), null, -1, -1, + anyString()), contains(BOB_WITH_FIRSTNAME)); } + /** + * @see DATAKV-114 + */ @Test - public void testCountWithPropertyNull() throws Exception { - when(adapterMock.getAllOf(anyString())).thenReturn(iterablePeople); + public void countsEntitiesWithNullProperty() throws Exception { - SpelQueryEngine sqe = new SpelQueryEngine(); - sqe.registerAdapter(adapterMock); + doReturn(people).when(adapter).getAllOf(anyString()); - long returnCol = sqe.count(createQueryForMethodWithArgs("findByFirstname", "bob"), anyString()); - assertThat(returnCol, is(Long.valueOf(1))); + assertThat(engine.count(createQueryForMethodWithArgs("findByFirstname", "bob"), anyString()), is(1L)); } - private SpelExpression createQueryForMethodWithArgs(String methodName, Object... args) - throws NoSuchMethodException, SecurityException { + private static SpelExpression createQueryForMethodWithArgs(String methodName, Object... args) throws Exception { - Class[] argTypes = new Class[args.length]; - if (!ObjectUtils.isEmpty(args)) { + List> types = new ArrayList>(args.length); - for (int i = 0; i < args.length; i++) { - argTypes[i] = args[i].getClass(); - } + for (Object arg : args) { + types.add(arg.getClass()); } - Method method = PersonRepository.class.getMethod(methodName, argTypes); + Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class[types.size()])); + RepositoryMetadata metadata = mock(RepositoryMetadata.class); PartTree partTree = new PartTree(method.getName(), method.getReturnType()); - SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(new QueryMethod(method, - metadataMock).getParameters(), args)); + SpelQueryCreator creator = new SpelQueryCreator(partTree, + new ParametersParameterAccessor(new QueryMethod(method, metadata).getParameters(), args)); - KeyValueQuery q = creator.createQuery(); - q.getCritieria().setEvaluationContext(new StandardEvaluationContext(args)); + KeyValueQuery query = creator.createQuery(); + query.getCritieria().setEvaluationContext(new StandardEvaluationContext(args)); - return q.getCritieria(); + return query.getCritieria(); } static interface PersonRepository { @@ -99,71 +118,18 @@ public class SpelQueryEngineUnitTests { static class Person { - private @Id String id; - private String firstname; - private int age; - - public Person() {} + @Id String id; + String firstname; + int age; public Person(String firstname, int age) { - super(); + this.firstname = firstname; this.age = age; } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public String getFirstname() { return firstname; } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - @Override - public int hashCode() { - int hash = 3; - hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0); - hash = 29 * hash + (this.firstname != null ? this.firstname.hashCode() : 0); - hash = 29 * hash + this.age; - return hash; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Person other = (Person) obj; - if ((this.id == null) ? (other.id != null) : !this.id.equals(other.id)) { - return false; - } - if ((this.firstname == null) ? (other.firstname != null) : !this.firstname.equals(other.firstname)) { - return false; - } - if (this.age != other.age) { - return false; - } - return true; - } - } }