Index: src/test/org/apache/lucene/index/TestTerm.java =================================================================== --- src/test/org/apache/lucene/index/TestTerm.java (revision 0) +++ src/test/org/apache/lucene/index/TestTerm.java (revision 0) @@ -0,0 +1,112 @@ +package org.apache.lucene.index; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import junit.framework.TestCase; + +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; + +import java.io.IOException; + +/** + * Unit tests related to {@link Term}. + * + * @author danz + */ +public class TestTerm extends TestCase { + + public TestTerm( final String name ) { + super( name ); + } + + protected void setUp() { + } + + protected void tearDown() { + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is null and the {@link Term#text} of the argument is null. + */ + public void testCompareToTextNullNull() throws IOException { + final Term a = new Term( "field" , null , true ); + final Term b = new Term( "field" , null , true ); + + assertEquals( "compareTo result" , 0 , a.compareTo( b ) ); + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is null and the {@link Term#text} of the argument is not null. + */ + public void testCompareToTextNullNotNull() throws IOException { + final Term a = new Term( "field" , null , true ); + final Term b = new Term( "field" , "abc" , true ); + + assertTrue( "compareTo result LE -1" , a.compareTo( b ) <= -1 ); + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is not null and the {@link Term#text} of the argument is null. + */ + public void testCompareToTextNotNullNull() throws IOException { + final Term a = new Term( "field" , "abc" , true ); + final Term b = new Term( "field" , null , true ); + + assertTrue( "compareTo result GE +1" , a.compareTo( b ) >= +1 ); + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is equal to the {@link Term#text} of the argument. + */ + public void testCompareToTextEquals() throws IOException { + final Term a = new Term( "field" , "abc" , true ); + final Term b = new Term( "field" , "abc" , true ); + + assertEquals( "compareTo result" , 0 , a.compareTo( b ) ); + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is before the {@link Term#text} of the argument. + */ + public void testCompareToTextBefore() throws IOException { + final Term a = new Term( "field" , "abc" , true ); + final Term b = new Term( "field" , "xyz" , true ); + + assertTrue( "compareTo result LE -1" , a.compareTo( b ) <= -1 ); + } + + /** + * Call to {@link Term#compareTo} where the {@link Term#text} of the target + * is after the {@link Term#text} of the argument. + */ + public void testCompareToTextAfter() throws IOException { + final Term a = new Term( "field" , "xyz" , true ); + final Term b = new Term( "field" , "abc" , true ); + + assertTrue( "compareTo result GE +1" , a.compareTo( b ) >= +1 ); + } + +} // class Index: src/java/org/apache/lucene/index/Term.java =================================================================== --- src/java/org/apache/lucene/index/Term.java (revision 512303) +++ src/java/org/apache/lucene/index/Term.java (working copy) @@ -76,16 +76,26 @@ return compareTo((Term)other); } - /** Compares two terms, returning a negative integer if this - term belongs before the argument, zero if this term is equal to the - argument, and a positive integer if this term belongs after the argument. - - The ordering of terms is first by field, then by text.*/ - public final int compareTo(Term other) { - if (field == other.field) // fields are interned - return text.compareTo(other.text); - else - return field.compareTo(other.field); + /** + * Compares two terms, returning a negative integer if this term + * belongs before the argument, 0 (zero) if this term is equal to the + * argument, and a positive integer if this term belongs after the + * argument. The ordering of terms is first by field, then by text. + */ + public final int compareTo( final Term other ) { + if ( field == other.field ) { // fields are interned + if ( text == null && other.text == null ) { + return 0; + } else if ( text == null ) { + return -1; + } else if ( other.text == null ) { + return 1; + } else { + return text.compareTo( other.text ); + } + } else { + return field.compareTo( other.field ); + } } /** Resets the field and text of a Term. */