Repository: lucenenet Updated Branches: refs/heads/branch_4x f63ba31d4 -> da25f85ac
Moar tests Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/da25f85a Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/da25f85a Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/da25f85a Branch: refs/heads/branch_4x Commit: da25f85ac2a6d347a6b2c44d9a1464e13192f7ab Parents: f63ba31 Author: synhershko <[email protected]> Authored: Mon Apr 7 23:59:10 2014 +0300 Committer: synhershko <[email protected]> Committed: Mon Apr 7 23:59:10 2014 +0300 ---------------------------------------------------------------------- src/core/Index/IIndexableField.cs | 4 - test/core/Document/TestDocument.cs | 464 ++++++++++--------- test/core/Lucene.Net.Test.csproj | 1 + .../JavaCompatibility/LuceneTestCase.cs | 11 +- .../JavaCompatibility/SystemTypesHelpers.cs | 16 +- 5 files changed, 260 insertions(+), 236 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/da25f85a/src/core/Index/IIndexableField.cs ---------------------------------------------------------------------- diff --git a/src/core/Index/IIndexableField.cs b/src/core/Index/IIndexableField.cs index 29ff853..3af3995 100644 --- a/src/core/Index/IIndexableField.cs +++ b/src/core/Index/IIndexableField.cs @@ -1,10 +1,6 @@ using Lucene.Net.Analysis; using Lucene.Net.Util; -using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; namespace Lucene.Net.Index { http://git-wip-us.apache.org/repos/asf/lucenenet/blob/da25f85a/test/core/Document/TestDocument.cs ---------------------------------------------------------------------- diff --git a/test/core/Document/TestDocument.cs b/test/core/Document/TestDocument.cs index 66bd7fe..f232b9a 100644 --- a/test/core/Document/TestDocument.cs +++ b/test/core/Document/TestDocument.cs @@ -16,121 +16,131 @@ */ using System; - +using Lucene.Net.Index; +using Lucene.Net.Util; using NUnit.Framework; -using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer; -using IndexWriter = Lucene.Net.Index.IndexWriter; -using Term = Lucene.Net.Index.Term; -using RAMDirectory = Lucene.Net.Store.RAMDirectory; -using IndexSearcher = Lucene.Net.Search.IndexSearcher; -using Query = Lucene.Net.Search.Query; -using ScoreDoc = Lucene.Net.Search.ScoreDoc; -using Searcher = Lucene.Net.Search.Searcher; -using TermQuery = Lucene.Net.Search.TermQuery; -using LuceneTestCase = Lucene.Net.Util.LuceneTestCase; - namespace Lucene.Net.Documents -{ - - /// <summary>Tests {@link Document} class.</summary> - [TestFixture] +{ + /// <summary>Tests {@link Document} class.</summary> public class TestDocument:LuceneTestCase - { - + { internal System.String binaryVal = "this text will be stored as a byte array in the index"; internal System.String binaryVal2 = "this text will be also stored as a byte array in the index"; - - [Test] - public virtual void TestBinaryField() - { - Document doc = new Document(); - IFieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO); - IFieldable binaryFld = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal), Field.Store.YES); - IFieldable binaryFld2 = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal2), Field.Store.YES); - - doc.Add(stringFld); - doc.Add(binaryFld); - - Assert.AreEqual(2, doc.fields_ForNUnit.Count); - - Assert.IsTrue(binaryFld.IsBinary); - Assert.IsTrue(binaryFld.IsStored); - Assert.IsFalse(binaryFld.IsIndexed); - Assert.IsFalse(binaryFld.IsTokenized); - - System.String binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(doc.GetBinaryValue("binary"))); - Assert.IsTrue(binaryTest.Equals(binaryVal)); - - System.String stringTest = doc.Get("string"); - Assert.IsTrue(binaryTest.Equals(stringTest)); - - doc.Add(binaryFld2); - - Assert.AreEqual(3, doc.fields_ForNUnit.Count); - - byte[][] binaryTests = doc.GetBinaryValues("binary"); - - Assert.AreEqual(2, binaryTests.Length); - - binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[0])); - System.String binaryTest2 = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[1])); - - Assert.IsFalse(binaryTest.Equals(binaryTest2)); - - Assert.IsTrue(binaryTest.Equals(binaryVal)); - Assert.IsTrue(binaryTest2.Equals(binaryVal2)); - - doc.RemoveField("string"); - Assert.AreEqual(2, doc.fields_ForNUnit.Count); - - doc.RemoveFields("binary"); - Assert.AreEqual(0, doc.fields_ForNUnit.Count); - } - - /// <summary> Tests {@link Document#RemoveField(String)} method for a brand new Document - /// that has not been indexed yet. - /// - /// </summary> - /// <throws> Exception on error </throws> - [Test] - public virtual void TestRemoveForNewDocument() - { - Document doc = MakeDocumentWithFields(); - Assert.AreEqual(8, doc.fields_ForNUnit.Count); - doc.RemoveFields("keyword"); - Assert.AreEqual(6, doc.fields_ForNUnit.Count); - doc.RemoveFields("doesnotexists"); // removing non-existing fields is siltenlty ignored - doc.RemoveFields("keyword"); // removing a field more than once - Assert.AreEqual(6, doc.fields_ForNUnit.Count); - doc.RemoveField("text"); - Assert.AreEqual(5, doc.fields_ForNUnit.Count); - doc.RemoveField("text"); - Assert.AreEqual(4, doc.fields_ForNUnit.Count); - doc.RemoveField("text"); - Assert.AreEqual(4, doc.fields_ForNUnit.Count); - doc.RemoveField("doesnotexists"); // removing non-existing fields is siltenlty ignored - Assert.AreEqual(4, doc.fields_ForNUnit.Count); - doc.RemoveFields("unindexed"); - Assert.AreEqual(2, doc.fields_ForNUnit.Count); - doc.RemoveFields("unstored"); - Assert.AreEqual(0, doc.fields_ForNUnit.Count); - doc.RemoveFields("doesnotexists"); // removing non-existing fields is siltenlty ignored - Assert.AreEqual(0, doc.fields_ForNUnit.Count); - } - - [Test] - public virtual void TestConstructorExceptions() - { - new Field("name", "value", Field.Store.YES, Field.Index.NO); // okay - new Field("name", "value", Field.Store.NO, Field.Index.NOT_ANALYZED); // okay - Assert.Throws<ArgumentException>(() => new Field("name", "value", Field.Store.NO, Field.Index.NO)); + [Test] + public void testBinaryField() + { + Document doc = new Document(); - new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.NO); // okay + FieldType ft = new FieldType(); + ft.Stored = true; + IIndexableField stringFld = new Field("string", binaryVal, ft); + IIndexableField binaryFld = new StoredField("binary", binaryVal.getBytes("UTF-8")); + IIndexableField binaryFld2 = new StoredField("binary", binaryVal2.getBytes("UTF-8")); - Assert.Throws<ArgumentException>(() => new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.YES)); - } + doc.Add(stringFld); + doc.Add(binaryFld); + + assertEquals(2, doc.GetFields().Count); + + assertTrue(binaryFld.BinaryValue != null); + assertTrue(binaryFld.FieldTypeValue.Stored); + assertFalse(binaryFld.FieldTypeValue.Indexed); + + String binaryTest = doc.GetBinaryValue("binary").Utf8ToString(); + assertTrue(binaryTest.equals(binaryVal)); + + String stringTest = doc.Get("string"); + assertTrue(binaryTest.equals(stringTest)); + + doc.Add(binaryFld2); + + assertEquals(3, doc.GetFields().Count); + + BytesRef[] binaryTests = doc.GetBinaryValues("binary"); + + assertEquals(2, binaryTests.Length); + + binaryTest = binaryTests[0].Utf8ToString(); + String binaryTest2 = binaryTests[1].Utf8ToString(); + + assertFalse(binaryTest.equals(binaryTest2)); + + assertTrue(binaryTest.equals(binaryVal)); + assertTrue(binaryTest2.equals(binaryVal2)); + + doc.RemoveField("string"); + assertEquals(2, doc.GetFields().Count); + + doc.RemoveFields("binary"); + assertEquals(0, doc.GetFields().Count); + } + + /// <summary> Tests {@link Document#RemoveField(String)} method for a brand new Document + /// that has not been indexed yet. + /// + /// </summary> + /// <throws> Exception on error </throws> + [Test] + public void testRemoveForNewDocument() + { + Document doc = makeDocumentWithFields(); + assertEquals(8, doc.GetFields().size()); + doc.RemoveFields("keyword"); + assertEquals(6, doc.GetFields().size()); + doc.RemoveFields("doesnotexists"); // removing non-existing fields is + // siltenlty ignored + doc.RemoveFields("keyword"); // removing a field more than once + assertEquals(6, doc.GetFields().size()); + doc.RemoveField("text"); + assertEquals(5, doc.GetFields().size()); + doc.RemoveField("text"); + assertEquals(4, doc.GetFields().size()); + doc.RemoveField("text"); + assertEquals(4, doc.GetFields().size()); + doc.RemoveField("doesnotexists"); // removing non-existing fields is + // siltenlty ignored + assertEquals(4, doc.GetFields().size()); + doc.RemoveFields("unindexed"); + assertEquals(2, doc.GetFields().size()); + doc.RemoveFields("unstored"); + assertEquals(0, doc.GetFields().size()); + doc.RemoveFields("doesnotexists"); // removing non-existing fields is + // siltenlty ignored + assertEquals(0, doc.GetFields().size()); + } + + [Test] + public void testConstructorExceptions() + { + FieldType ft = new FieldType(); + ft.Stored = true; + new Field("name", "value", ft); // okay + new StringField("name", "value", Field.Store.NO); // okay + try + { + new Field("name", "value", new FieldType()); + fail(); + } + catch (ArgumentException e) + { + // expected exception + } + new Field("name", "value", ft); // okay + try + { + FieldType ft2 = new FieldType(); + ft2.Stored = true; + ft2.StoreTermVectors = true; + new Field("name", "value", ft2); + fail(); + } + catch (ArgumentException e) + { + // expected exception + } + } /// <summary> Tests {@link Document#GetValues(String)} method for a brand new Document /// that has not been indexed yet. @@ -140,133 +150,127 @@ namespace Lucene.Net.Documents [Test] public virtual void TestGetValuesForNewDocument() { - DoAssert(MakeDocumentWithFields(), false); + doAssert(makeDocumentWithFields(), false); } - - /// <summary> Tests {@link Document#GetValues(String)} method for a Document retrieved from - /// an index. - /// - /// </summary> - /// <throws> Exception on error </throws> - [Test] - public virtual void TestGetValuesForIndexedDocument() - { - RAMDirectory dir = new RAMDirectory(); - IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); - writer.AddDocument(MakeDocumentWithFields()); - writer.Close(); - - Searcher searcher = new IndexSearcher(dir, true); - - // search for something that does exists - Query query = new TermQuery(new Term("keyword", "test1")); - - // ensure that queries return expected results without DateFilter first - ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs; - Assert.AreEqual(1, hits.Length); - - DoAssert(searcher.Doc(hits[0].Doc), true); - searcher.Close(); - } - - private Document MakeDocumentWithFields() - { - Document doc = new Document(); - doc.Add(new Field("keyword", "test1", Field.Store.YES, Field.Index.NOT_ANALYZED)); - doc.Add(new Field("keyword", "test2", Field.Store.YES, Field.Index.NOT_ANALYZED)); - doc.Add(new Field("text", "test1", Field.Store.YES, Field.Index.ANALYZED)); - doc.Add(new Field("text", "test2", Field.Store.YES, Field.Index.ANALYZED)); - doc.Add(new Field("unindexed", "test1", Field.Store.YES, Field.Index.NO)); - doc.Add(new Field("unindexed", "test2", Field.Store.YES, Field.Index.NO)); - doc.Add(new Field("unstored", "test1", Field.Store.NO, Field.Index.ANALYZED)); - doc.Add(new Field("unstored", "test2", Field.Store.NO, Field.Index.ANALYZED)); - return doc; - } - - private void DoAssert(Document doc, bool fromIndex) - { - System.String[] keywordFieldValues = doc.GetValues("keyword"); - System.String[] textFieldValues = doc.GetValues("text"); - System.String[] unindexedFieldValues = doc.GetValues("unindexed"); - System.String[] unstoredFieldValues = doc.GetValues("unstored"); - - Assert.IsTrue(keywordFieldValues.Length == 2); - Assert.IsTrue(textFieldValues.Length == 2); - Assert.IsTrue(unindexedFieldValues.Length == 2); - // this test cannot work for documents retrieved from the index - // since unstored fields will obviously not be returned - if (!fromIndex) - { - Assert.IsTrue(unstoredFieldValues.Length == 2); - } - - Assert.IsTrue(keywordFieldValues[0].Equals("test1")); - Assert.IsTrue(keywordFieldValues[1].Equals("test2")); - Assert.IsTrue(textFieldValues[0].Equals("test1")); - Assert.IsTrue(textFieldValues[1].Equals("test2")); - Assert.IsTrue(unindexedFieldValues[0].Equals("test1")); - Assert.IsTrue(unindexedFieldValues[1].Equals("test2")); - // this test cannot work for documents retrieved from the index - // since unstored fields will obviously not be returned - if (!fromIndex) - { - Assert.IsTrue(unstoredFieldValues[0].Equals("test1")); - Assert.IsTrue(unstoredFieldValues[1].Equals("test2")); - } - } - - [Test] - public virtual void TestFieldSetValue() + + /// <summary> Tests {@link Document#GetValues(String)} method for a Document retrieved from + /// an index. + /// + /// </summary> + /// <throws> Exception on error </throws> +// [Test] +// public void testGetValuesForIndexedDocument() +// { +// Directory dir = newDirectory(); +// RandomIndexWriter writer = new RandomIndexWriter(random(), dir); +// writer.addDocument(makeDocumentWithFields()); +// IndexReader reader = writer.getReader(); +// +// IndexSearcher searcher = newSearcher(reader); +// +// // search for something that does exists +// Query query = new TermQuery(new Term("keyword", "test1")); +// +// // ensure that queries return expected results without DateFilter first +// ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; +// assertEquals(1, hits.length); +// +// doAssert(searcher.doc(hits[0].doc), true); +// writer.close(); +// reader.close(); +// dir.close(); +// } + + private Document makeDocumentWithFields() { - - Field field = new Field("id", "id1", Field.Store.YES, Field.Index.NOT_ANALYZED); - Document doc = new Document(); - doc.Add(field); - doc.Add(new Field("keyword", "test", Field.Store.YES, Field.Index.NOT_ANALYZED)); - - RAMDirectory dir = new RAMDirectory(); - IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED); - writer.AddDocument(doc); - field.SetValue("id2"); - writer.AddDocument(doc); - field.SetValue("id3"); - writer.AddDocument(doc); - writer.Close(); - - Searcher searcher = new IndexSearcher(dir, true); - - Query query = new TermQuery(new Term("keyword", "test")); - - // ensure that queries return expected results without DateFilter first - ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs; - Assert.AreEqual(3, hits.Length); - int result = 0; - for (int i = 0; i < 3; i++) - { - Document doc2 = searcher.Doc(hits[i].Doc); - Field f = doc2.GetField("id"); - if (f.StringValue.Equals("id1")) - result |= 1; - else if (f.StringValue.Equals("id2")) - result |= 2; - else if (f.StringValue.Equals("id3")) - result |= 4; - else - Assert.Fail("unexpected id field"); - } - searcher.Close(); - dir.Close(); - Assert.AreEqual(7, result, "did not see all IDs"); + Document doc = new Document(); + FieldType stored = new FieldType(); + stored.Stored = true; + doc.Add(new StringField("keyword", "test1", Field.Store.YES)); + doc.Add(new StringField("keyword", "test2", Field.Store.YES)); + doc.Add(new TextField("text", "test1", Field.Store.YES)); + doc.Add(new TextField("text", "test2", Field.Store.YES)); + doc.Add(new Field("unindexed", "test1", stored)); + doc.Add(new Field("unindexed", "test2", stored)); + doc + .Add(new TextField("unstored", "test1", Field.Store.NO)); + doc + .Add(new TextField("unstored", "test2", Field.Store.NO)); + return doc; } - - [Test] - public virtual void TestFieldSetValueChangeBinary() - { - Field field1 = new Field("field1", new byte[0], Field.Store.YES); - Field field2 = new Field("field2", "", Field.Store.YES, Field.Index.ANALYZED); - Assert.Throws<ArgumentException>(() => field1.SetValue("abc"), "did not hit expected exception"); - Assert.Throws<ArgumentException>(() => field2.SetValue(new byte[0]), "did not hit expected exception"); - } + private void doAssert(Document doc, bool fromIndex) + { + IIndexableField[] keywordFieldValues = doc.GetFields("keyword"); + IIndexableField[] textFieldValues = doc.GetFields("text"); + IIndexableField[] unindexedFieldValues = doc.GetFields("unindexed"); + IIndexableField[] unstoredFieldValues = doc.GetFields("unstored"); + + assertTrue(keywordFieldValues.Length == 2); + assertTrue(textFieldValues.Length == 2); + assertTrue(unindexedFieldValues.Length == 2); + // this test cannot work for documents retrieved from the index + // since unstored fields will obviously not be returned + if (!fromIndex) + { + assertTrue(unstoredFieldValues.Length == 2); + } + + assertTrue(keywordFieldValues[0].StringValue.equals("test1")); + assertTrue(keywordFieldValues[1].StringValue.equals("test2")); + assertTrue(textFieldValues[0].StringValue.equals("test1")); + assertTrue(textFieldValues[1].StringValue.equals("test2")); + assertTrue(unindexedFieldValues[0].StringValue.equals("test1")); + assertTrue(unindexedFieldValues[1].StringValue.equals("test2")); + // this test cannot work for documents retrieved from the index + // since unstored fields will obviously not be returned + if (!fromIndex) + { + assertTrue(unstoredFieldValues[0].StringValue.equals("test1")); + assertTrue(unstoredFieldValues[1].StringValue.equals("test2")); + } + } + +// [Test] +// public void testFieldSetValue() +// { +// +// Field field = new StringField("id", "id1", Field.Store.YES); +// Document doc = new Document(); +// doc.Add(field); +// doc.Add(new StringField("keyword", "test", Field.Store.YES)); +// +// Directory dir = newDirectory(); +// RandomIndexWriter writer = new RandomIndexWriter(random(), dir); +// writer.addDocument(doc); +// field.setStringValue("id2"); +// writer.addDocument(doc); +// field.setStringValue("id3"); +// writer.addDocument(doc); +// +// IndexReader reader = writer.getReader(); +// IndexSearcher searcher = newSearcher(reader); +// +// Query query = new TermQuery(new Term("keyword", "test")); +// +// // ensure that queries return expected results without DateFilter first +// ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; +// assertEquals(3, hits.length); +// int result = 0; +// for (int i = 0; i < 3; i++) +// { +// Document doc2 = searcher.doc(hits[i].doc); +// Field f = (Field) doc2.getField("id"); +// if (f.stringValue().equals("id1")) result |= 1; +// else if (f.stringValue().equals("id2")) result |= 2; +// else if (f.stringValue().equals("id3")) result |= 4; +// else fail("unexpected id field"); +// } +// writer.close(); +// reader.close(); +// dir.close(); +// assertEquals("did not see all IDs", 7, result); +// } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/da25f85a/test/core/Lucene.Net.Test.csproj ---------------------------------------------------------------------- diff --git a/test/core/Lucene.Net.Test.csproj b/test/core/Lucene.Net.Test.csproj index 9b9716d..c5b05d8 100644 --- a/test/core/Lucene.Net.Test.csproj +++ b/test/core/Lucene.Net.Test.csproj @@ -207,6 +207,7 @@ <SubType>Code</SubType> </Compile> <Compile Include="Document\TestDateTools.cs" /> + <Compile Include="Document\TestDocument.cs" /> <Compile Include="SupportClassException.cs" /> <Compile Include="Util\LocalizedTestCase.cs" /> <None Include="App.config" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/da25f85a/test/test-framework/JavaCompatibility/LuceneTestCase.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/JavaCompatibility/LuceneTestCase.cs b/test/test-framework/JavaCompatibility/LuceneTestCase.cs index 7361b7e..17ca894 100644 --- a/test/test-framework/JavaCompatibility/LuceneTestCase.cs +++ b/test/test-framework/JavaCompatibility/LuceneTestCase.cs @@ -54,7 +54,6 @@ namespace Lucene.Net Assert.AreNotSame(unexpected, actual, message); } - protected static void assertEquals(double d1, double d2, double delta) { Assert.AreEqual(d1, d2, delta); @@ -84,5 +83,15 @@ namespace Lucene.Net { Assert.Null(o, msg); } + + protected static void fail() + { + Fail(); + } + + protected static void fail(string message) + { + Fail(message); + } } } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/da25f85a/test/test-framework/JavaCompatibility/SystemTypesHelpers.cs ---------------------------------------------------------------------- diff --git a/test/test-framework/JavaCompatibility/SystemTypesHelpers.cs b/test/test-framework/JavaCompatibility/SystemTypesHelpers.cs index f8f7e4b..7e00d1f 100644 --- a/test/test-framework/JavaCompatibility/SystemTypesHelpers.cs +++ b/test/test-framework/JavaCompatibility/SystemTypesHelpers.cs @@ -1,4 +1,8 @@ -using System.Text; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; namespace Lucene.Net { @@ -30,5 +34,15 @@ namespace Lucene.Net sb.Append(value); return sb; } + + public static sbyte[] getBytes(this string str, string encoding) + { + return (sbyte[])(Array)Encoding.GetEncoding(encoding).GetBytes(str); + } + + public static long size<T>(this IList<T> list) + { + return list.Count; + } } }
