Added tests for not tokenized serializer.

This commit is contained in:
Vesa Martilla 2011-01-25 11:32:07 +00:00
parent 12959c8b25
commit d3976eb2dd
5 changed files with 213 additions and 9 deletions

View File

@ -51,7 +51,42 @@
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-test-sources/java</outputDirectory>
<processor>com.mysema.query.apt.QuerydslAnnotationProcessor</processor>
<showWarnings>false</showWarnings>
<logOnlyOnError>true</logOnlyOnError>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -16,6 +16,8 @@ import java.util.Map;
import javax.annotation.Nullable;
import net.jcip.annotations.ThreadSafe;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
@ -41,7 +43,6 @@ import com.mysema.query.types.PathType;
*
*/
public class LuceneSerializer {
private static final Map<Class<?>, Integer> sortFields = new HashMap<Class<?>, Integer>();
static {
@ -149,10 +150,10 @@ public class LuceneSerializer {
protected Query eq(Operation<?> operation, QueryMetadata metadata) {
verifyArguments(operation);
String field = toField(operation.getArg(0));
//TODO Implement this
//boolean tokenized = isTokenized(operation.getArg(0));
if (Number.class.isAssignableFrom(operation.getArg(1).getType())) {
return new TermQuery(new Term(field, convertNumber(((Constant<Number>) operation
.getArg(1)).getConstant())));
@ -200,7 +201,7 @@ public class LuceneSerializer {
Collection values = (Collection) ((Constant) operation.getArg(1)).getConstant();
BooleanQuery bq = new BooleanQuery();
for (Object value : values) {
bq.add(eq(field, StringUtils.split(value.toString()), metadata), Occur.SHOULD);
bq.add(eq(field, splitTerms ? StringUtils.split(value.toString()) : new String[] { value.toString() }, metadata), Occur.SHOULD);
}
return bq;
}
@ -336,14 +337,14 @@ public class LuceneSerializer {
}
protected String toField(Path<?> path) {
String rv = path.getMetadata().getExpression().toString();
String rv = path.getMetadata().getExpression().toString();
if (path.getMetadata().getParent() != null){
Path<?> parent = path.getMetadata().getParent();
if (parent.getMetadata().getPathType() != PathType.VARIABLE){
rv = toField(parent) + "." + rv;
}
}
return rv;
return rv;
}
private void verifyArguments(Operation<?> operation) {

View File

@ -0,0 +1,137 @@
package com.mysema.query;
import static com.mysema.query.QPerson.person;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import com.mysema.query.lucene.LuceneSerializer;
import com.mysema.query.types.Expression;
public class LuceneSerializerNotTokenizedTest {
private RAMDirectory idx;
private IndexWriter writer;
private Searcher searcher;
private LuceneSerializer serializer;
private final QueryMetadata metadata = new DefaultQueryMetadata();
private final Person clooney = new Person("actor_1", "George Clooney", new LocalDate(1961, 4, 6));
private final Person pitt = new Person("actor_2", "Brad Pitt", new LocalDate(1963, 12, 18));
private void testQuery(Expression<?> expr, String expectedQuery, int expectedHits) throws Exception {
Query query = serializer.toQuery(expr, metadata);
TopDocs docs = searcher.search(query, 100);
assertEquals(expectedHits, docs.totalHits);
assertEquals(expectedQuery, query.toString());
}
private Document createDocument(Person person) {
Document doc = new Document();
doc.add(new Field("id", person.getId(), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("name", person.getName(), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("birthDate", person.getBirthDate().toString(), Store.YES, Index.NOT_ANALYZED));
return doc;
}
@Before
public void Before() throws Exception {
serializer = new LuceneSerializer(false, false);
idx = new RAMDirectory();
writer = new IndexWriter(idx, new StandardAnalyzer(Version.LUCENE_CURRENT), true, MaxFieldLength.UNLIMITED);
writer.addDocument(createDocument(clooney));
writer.addDocument(createDocument(pitt));
writer.optimize();
writer.close();
searcher = new IndexSearcher(idx);
}
@Test
public void Equals_By_Id_Matches() throws Exception {
testQuery(person.id.eq("actor_1"), "id:actor_1", 1);
}
@Test
public void Equals_By_Id_Does_Not_Match() throws Exception {
testQuery(person.id.eq("actor_8"), "id:actor_8", 0);
}
@Test
public void Equals_By_Name_Matches() throws Exception {
testQuery(person.name.eq("George Clooney"), "name:George Clooney", 1);
}
@Test
public void Equals_By_Name_Ignoring_Case_Does_Not_Match() throws Exception {
testQuery(person.name.equalsIgnoreCase("george clooney"), "name:george clooney", 0);
}
@Test
public void Equals_By_Name_Does_Not_Match() throws Exception {
testQuery(person.name.eq("George Looney"), "name:George Looney", 0);
}
@Test
public void Starts_With_Name_Should_Match() throws Exception {
testQuery(person.name.startsWith("George C"), "name:George C*", 1);
}
@Test
public void Starts_With_Name_Should_Not_Match() throws Exception {
testQuery(person.name.startsWith("George L"), "name:George L*", 0);
}
@Test
public void Ends_With_Name_Should_Match() throws Exception {
testQuery(person.name.endsWith("e Clooney"), "name:*e Clooney", 1);
}
@Test
public void Ends_With_Name_Should_Not_Match() throws Exception {
testQuery(person.name.endsWith("e Looney"), "name:*e Looney", 0);
}
@Test
public void Contains_Name_Should_Match() throws Exception {
testQuery(person.name.contains("oney"), "name:*oney*", 1);
}
@Test
public void Contains_Name_Should_Not_Match() throws Exception {
testQuery(person.name.contains("bloney"), "name:*bloney*", 0);
}
@Test
public void In_Names_Should_Match_2() throws Exception {
testQuery(person.name.in("Brad Pitt", "George Clooney"), "name:Brad Pitt name:George Clooney", 2);
}
@Test
public void Or_By_Name_Should_Match_2() throws Exception {
testQuery(person.name.eq("Brad Pitt").or(person.name.eq("George Clooney")), "name:Brad Pitt name:George Clooney", 2);
}
@Test
public void Equals_By_Birth_Date() throws Exception {
testQuery(person.birthDate.eq(clooney.getBirthDate()), "birthDate:1961-04-06", 1);
}
}

View File

@ -97,7 +97,6 @@ public class LuceneSerializerTest {
@Before
public void setUp() throws Exception {
// TODO Tests for non lower case
serializer = new LuceneSerializer(true,true);
entityPath = new PathBuilder<Object>(Object.class, "obj");
title = entityPath.getString("title");
@ -188,7 +187,7 @@ public class LuceneSerializerTest {
public void eq() throws Exception {
testQuery(rating.eq("Good"), "rating:good", 1);
}
@Test
public void eq_with_deep_path() throws Exception{
StringPath deepPath = entityPath.get("property1", Object.class).getString("property2");

View File

@ -0,0 +1,32 @@
package com.mysema.query;
import org.joda.time.LocalDate;
import com.mysema.query.annotations.QueryEntity;
@QueryEntity
public class Person {
private final String id;
private final String name;
private final LocalDate birthDate;
public Person(String id, String name, LocalDate birthDate) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public String getId() {
return id;
}
public LocalDate getBirthDate() {
return birthDate;
}
public String getName() {
return name;
}
}