/*******************************************************************************
* Copyright (c) 2012 Spring IDE Developers
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Spring IDE Developers - initial API and implementation
*******************************************************************************/
package org.springframework.ide.eclipse.data.jdt.core;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link QueryMethodCandidate}.
*
* @author Oliver Gierke
*/
public class QueryMethodCandidateUnitTests {
@Test
public void findsCompleteMatch() {
QueryMethodCandidate candidate = new QueryMethodCandidate("findByFirstname", User.class);
QueryMethodPart part = candidate.getPartAtPosition(6);
assertThat(part.isCompletePathMatch(), is(false));
}
@Test
public void startsAfterKeyword() {
QueryMethodCandidate candidate = new QueryMethodCandidate("findByFirstnameAndLast", User.class);
QueryMethodPart part = candidate.getPartAtPosition("findByFirstnameAndLa".length());
assertThat(part.isCompletePathMatch(), is(false));
assertThat(part.isRoot(), is(true));
assertThat(part.getSeed(), is("la"));
}
@Test
public void startsAfterSecondKeyword() {
QueryMethodCandidate candidate = new QueryMethodCandidate("findByFirstnameAndLastnameOrFirst", User.class);
QueryMethodPart part = candidate.getPartAtPosition("findByFirstnameAndLastnameOrFir".length());
assertThat(part.isCompletePathMatch(), is(false));
assertThat(part.isRoot(), is(true));
assertThat(part.getSeed(), is("fir"));
}
/**
* @see IDE-1255
*/
@Test
public void returnsNullQueryMethodPartForInvocationsInsideThePrefix() {
QueryMethodCandidate candidate = new QueryMethodCandidate("findByLastname", User.class);
QueryMethodPart part = candidate.getPartAtPosition(4);
assertThat(part, is(nullValue()));
}
/**
* @see IDE-1255
*/
@Test
public void doesNotReturnProposalForRequestOutsideMethodName() {
String methodName = "findByLastname";
QueryMethodCandidate candidate = new QueryMethodCandidate(methodName, User.class);
QueryMethodPart part = candidate.getPartAtPosition(methodName.length() + 1);
assertThat(part, is(nullValue()));
}
static class User {
String firstname;
String lastname;
}
}