mirror of https://github.com/apache/lucene.git
LUCENE-4779: add replacement for TestSort
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1446202 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9797b73c48
commit
0160c32a73
|
@ -0,0 +1,72 @@
|
|||
package org.apache.lucene.search;
|
||||
|
||||
/*
|
||||
* 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 java.io.IOException;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
/*
|
||||
* Very simple tests of sorting.
|
||||
*
|
||||
* THE RULES:
|
||||
* 1. keywords like 'abstract' and 'static' should not appear in this file.
|
||||
* 2. each test method should be self-contained and understandable.
|
||||
* 3. no test methods should not share code with other test methods.
|
||||
* 4. no testing of things unrelated to sorting.
|
||||
* 5. no tracers.
|
||||
* 6. keyword 'class' should appear only once in this file, here ----
|
||||
* |
|
||||
* -----------------------------------------------------------
|
||||
* |
|
||||
* \./
|
||||
*/
|
||||
public class TestSort2 extends LuceneTestCase {
|
||||
|
||||
public void testDemo() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("id", "1", Field.Store.YES));
|
||||
doc.add(newStringField("value", "foo", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("id", "2", Field.Store.YES));
|
||||
doc.add(newStringField("value", "bar", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = new IndexSearcher(ir);
|
||||
Sort sort = new Sort(new SortField("value", SortField.Type.STRING));
|
||||
|
||||
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
// 'bar' comes before 'foo'
|
||||
assertEquals("2", searcher.doc(td.scoreDocs[0].doc).get("id"));
|
||||
assertEquals("1", searcher.doc(td.scoreDocs[1].doc).get("id"));
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue