This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit bef69cf0a36d193373442a0173b3a9a1bf23fa56 Author: Shad Storhaug <[email protected]> AuthorDate: Tue Dec 7 23:14:53 2021 +0700 BREAKING: Removed serializable support for most types. Commented old patch tests for LUCENENET-170 and LUCENENET-338, since they are no longer applicable. Added tests for serialization. Closes #275, closes LUCENENET-574. --- .../Compound/Hyphenation/HyphenationTree.cs | 3 - .../Language/Bm/Rule.cs | 5 + .../VectorHighlight/ScoreOrderFragmentsBuilder.cs | 6 + src/Lucene.Net.Memory/MemoryIndex.Info.cs | 5 +- src/Lucene.Net.Memory/MemoryIndex.cs | 6 - .../Function/DocValues/DocTermsIndexDocValues.cs | 2 +- src/Lucene.Net.QueryParser/Classic/Token.cs | 6 +- .../Flexible/Standard/Parser/Token.cs | 2 + .../Surround/Parser/Token.cs | 5 +- .../Prefix/Tree/SpatialPrefixTree.cs | 3 - src/Lucene.Net.Spatial/Query/SpatialOperation.cs | 3 - .../Spell/SuggestWordScoreComparator.cs | 6 + src/Lucene.Net.Suggest/Suggest/Lookup.cs | 5 + .../Support/ExceptionSerializationTestBase.cs | 25 --- .../Support/Util/LuceneTestCase.Serialization.cs | 98 +++++++++ src/Lucene.Net.Tests/Support/TestOldPatches.cs | 158 ++++++++------- src/Lucene.Net.Tests/Support/TestSerialization.cs | 223 +++++++++++---------- src/Lucene.Net.Tests/Support/Util/TestLongsRef.cs | 75 +++++++ src/Lucene.Net.Tests/Util/TestBytesRef.cs | 25 ++- src/Lucene.Net.Tests/Util/TestCharsRef.cs | 23 +++ src/Lucene.Net.Tests/Util/TestIntsRef.cs | 24 +++ src/Lucene.Net.Tests/Util/TestLongBitSet.cs | 27 +++ src/Lucene.Net.Tests/Util/TestPriorityQueue.cs | 36 +++- src/Lucene.Net/Index/IndexWriterConfig.cs | 3 - src/Lucene.Net/Index/LiveIndexWriterConfig.cs | 3 - src/Lucene.Net/Index/Term.cs | 3 - src/Lucene.Net/Index/TermContext.cs | 5 +- src/Lucene.Net/Search/BooleanClause.cs | 3 - src/Lucene.Net/Search/BooleanQuery.cs | 3 - src/Lucene.Net/Search/FieldCacheRangeFilter.cs | 24 --- .../Search/MaxNonCompetitiveBoostAttributeImpl.cs | 5 +- src/Lucene.Net/Search/Query.cs | 3 - src/Lucene.Net/Search/TermQuery.cs | 3 - .../Support/Index/IndexOptionsComparer.cs | 9 +- src/Lucene.Net/Util/BytesRef.cs | 10 + src/Lucene.Net/Util/CharsRef.cs | 11 +- src/Lucene.Net/Util/LongBitSet.cs | 4 +- src/Lucene.Net/Util/PriorityQueue.cs | 4 +- 38 files changed, 566 insertions(+), 298 deletions(-) diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/HyphenationTree.cs b/src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/HyphenationTree.cs index 3b3882b..5652274 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/HyphenationTree.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Compound/Hyphenation/HyphenationTree.cs @@ -52,9 +52,6 @@ namespace Lucene.Net.Analysis.Compound.Hyphenation /// <summary> /// Temporary map to store interletter values on pattern loading. /// </summary> -#if FEATURE_SERIALIZABLE - [NonSerialized] -#endif private TernaryTree ivalues; public HyphenationTree() diff --git a/src/Lucene.Net.Analysis.Phonetic/Language/Bm/Rule.cs b/src/Lucene.Net.Analysis.Phonetic/Language/Bm/Rule.cs index 3dbd4bd..a41df9e 100644 --- a/src/Lucene.Net.Analysis.Phonetic/Language/Bm/Rule.cs +++ b/src/Lucene.Net.Analysis.Phonetic/Language/Bm/Rule.cs @@ -940,6 +940,11 @@ namespace Lucene.Net.Analysis.Phonetic.Language.Bm public sealed class Phoneme : IPhonemeExpr { + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif private class PhonemeComparer : IComparer<Phoneme> { public int Compare(Phoneme o1, Phoneme o2) diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/ScoreOrderFragmentsBuilder.cs b/src/Lucene.Net.Highlighter/VectorHighlight/ScoreOrderFragmentsBuilder.cs index 82819a3..164031d 100644 --- a/src/Lucene.Net.Highlighter/VectorHighlight/ScoreOrderFragmentsBuilder.cs +++ b/src/Lucene.Net.Highlighter/VectorHighlight/ScoreOrderFragmentsBuilder.cs @@ -1,4 +1,5 @@ using Lucene.Net.Util; +using System; using System.Collections.Generic; using WeightedFragInfo = Lucene.Net.Search.VectorHighlight.FieldFragList.WeightedFragInfo; @@ -67,6 +68,11 @@ namespace Lucene.Net.Search.VectorHighlight /// <see cref="IComparer{T}"/> for <see cref="WeightedFragInfo"/> by boost, breaking ties /// by offset. /// </summary> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif public class ScoreComparer : IComparer<WeightedFragInfo> { private ScoreComparer() { } // Singleton only diff --git a/src/Lucene.Net.Memory/MemoryIndex.Info.cs b/src/Lucene.Net.Memory/MemoryIndex.Info.cs index 3ca4ff7..97d11a3 100644 --- a/src/Lucene.Net.Memory/MemoryIndex.Info.cs +++ b/src/Lucene.Net.Memory/MemoryIndex.Info.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Util; +using Lucene.Net.Util; using System; namespace Lucene.Net.Index.Memory @@ -38,9 +38,6 @@ namespace Lucene.Net.Index.Memory /// <summary> /// Terms sorted ascending by term text; computed on demand /// </summary> -#if FEATURE_SERIALIZABLE - [NonSerialized] -#endif internal int[] sortedTerms; /// <summary> diff --git a/src/Lucene.Net.Memory/MemoryIndex.cs b/src/Lucene.Net.Memory/MemoryIndex.cs index bbad2e0..bb9d1d2 100644 --- a/src/Lucene.Net.Memory/MemoryIndex.cs +++ b/src/Lucene.Net.Memory/MemoryIndex.cs @@ -155,9 +155,6 @@ namespace Lucene.Net.Index.Memory /// /// </para> /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public partial class MemoryIndex { /// <summary> @@ -167,9 +164,6 @@ namespace Lucene.Net.Index.Memory /// <summary> /// fields sorted ascending by fieldName; lazily computed on demand </summary> -#if FEATURE_SERIALIZABLE - [NonSerialized] -#endif private KeyValuePair<string, Info>[] sortedFields; private readonly bool storeOffsets; diff --git a/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs b/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs index f209463..a9a1cb8 100644 --- a/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs +++ b/src/Lucene.Net.Queries/Function/DocValues/DocTermsIndexDocValues.cs @@ -164,7 +164,7 @@ namespace Lucene.Net.Queries.Function.DocValues // LUCENENET: It is no longer good practice to use binary serialization. // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 #if FEATURE_SERIALIZABLE_EXCEPTIONS - [Serializable] + [Serializable] #endif public sealed class DocTermsIndexException : Exception, IRuntimeException // LUCENENET specific: Added IRuntimeException for identification of the Java superclass in .NET { diff --git a/src/Lucene.Net.QueryParser/Classic/Token.cs b/src/Lucene.Net.QueryParser/Classic/Token.cs index 51e6ac6..15b65e1 100644 --- a/src/Lucene.Net.QueryParser/Classic/Token.cs +++ b/src/Lucene.Net.QueryParser/Classic/Token.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace Lucene.Net.QueryParsers.Classic { @@ -18,10 +18,12 @@ namespace Lucene.Net.QueryParsers.Classic * See the License for the specific language governing permissions and * limitations under the License. */ - + /// <summary> /// Describes the input token stream. /// </summary> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 #if FEATURE_SERIALIZABLE [Serializable] #endif diff --git a/src/Lucene.Net.QueryParser/Flexible/Standard/Parser/Token.cs b/src/Lucene.Net.QueryParser/Flexible/Standard/Parser/Token.cs index 12463a7..b450513 100644 --- a/src/Lucene.Net.QueryParser/Flexible/Standard/Parser/Token.cs +++ b/src/Lucene.Net.QueryParser/Flexible/Standard/Parser/Token.cs @@ -22,6 +22,8 @@ namespace Lucene.Net.QueryParsers.Flexible.Standard.Parser /// <summary> /// Describes the input token stream. /// </summary> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 #if FEATURE_SERIALIZABLE [Serializable] #endif diff --git a/src/Lucene.Net.QueryParser/Surround/Parser/Token.cs b/src/Lucene.Net.QueryParser/Surround/Parser/Token.cs index 438998c..0bb297a 100644 --- a/src/Lucene.Net.QueryParser/Surround/Parser/Token.cs +++ b/src/Lucene.Net.QueryParser/Surround/Parser/Token.cs @@ -18,16 +18,17 @@ namespace Lucene.Net.QueryParsers.Surround.Parser * See the License for the specific language governing permissions and * limitations under the License. */ - + /// <summary> /// Describes the input token stream. /// </summary> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 #if FEATURE_SERIALIZABLE [Serializable] #endif public class Token { - /// <summary> /// An integer that describes the kind of this token. This numbering /// system is determined by JavaCCParser, and a table of these numbers is diff --git a/src/Lucene.Net.Spatial/Prefix/Tree/SpatialPrefixTree.cs b/src/Lucene.Net.Spatial/Prefix/Tree/SpatialPrefixTree.cs index 99cafb7..c1d6a86 100644 --- a/src/Lucene.Net.Spatial/Prefix/Tree/SpatialPrefixTree.cs +++ b/src/Lucene.Net.Spatial/Prefix/Tree/SpatialPrefixTree.cs @@ -104,9 +104,6 @@ namespace Lucene.Net.Spatial.Prefix.Tree return Math.Sqrt(width * width + height * height); } -#if FEATURE_SERIALIZABLE - [NonSerialized] -#endif private Cell worldCell;//cached /// <summary>Returns the level 0 cell which encompasses all spatial data.</summary> diff --git a/src/Lucene.Net.Spatial/Query/SpatialOperation.cs b/src/Lucene.Net.Spatial/Query/SpatialOperation.cs index d9e9f04..9e7404f 100644 --- a/src/Lucene.Net.Spatial/Query/SpatialOperation.cs +++ b/src/Lucene.Net.Spatial/Query/SpatialOperation.cs @@ -33,9 +33,6 @@ namespace Lucene.Net.Spatial.Queries /// </para> /// @lucene.experimental /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public abstract class SpatialOperation { // Private registry diff --git a/src/Lucene.Net.Suggest/Spell/SuggestWordScoreComparator.cs b/src/Lucene.Net.Suggest/Spell/SuggestWordScoreComparator.cs index eb27c5b..0d9c6cf 100644 --- a/src/Lucene.Net.Suggest/Spell/SuggestWordScoreComparator.cs +++ b/src/Lucene.Net.Suggest/Spell/SuggestWordScoreComparator.cs @@ -1,4 +1,5 @@ using J2N.Text; +using System; using System.Collections.Generic; namespace Lucene.Net.Search.Spell @@ -23,6 +24,11 @@ namespace Lucene.Net.Search.Spell /// <summary> /// Score first, then frequency /// </summary> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif public class SuggestWordScoreComparer : IComparer<SuggestWord> { diff --git a/src/Lucene.Net.Suggest/Suggest/Lookup.cs b/src/Lucene.Net.Suggest/Suggest/Lookup.cs index 43ec5a3..27440d0 100644 --- a/src/Lucene.Net.Suggest/Suggest/Lookup.cs +++ b/src/Lucene.Net.Suggest/Suggest/Lookup.cs @@ -129,6 +129,11 @@ namespace Lucene.Net.Search.Suggest /// </summary> public static readonly IComparer<string> CHARSEQUENCE_COMPARER = new CharSequenceComparer(); + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif private class CharSequenceComparer : IComparer<string> { diff --git a/src/Lucene.Net.TestFramework/Support/ExceptionSerializationTestBase.cs b/src/Lucene.Net.TestFramework/Support/ExceptionSerializationTestBase.cs index 1c81999..c5c0596 100644 --- a/src/Lucene.Net.TestFramework/Support/ExceptionSerializationTestBase.cs +++ b/src/Lucene.Net.TestFramework/Support/ExceptionSerializationTestBase.cs @@ -1,10 +1,7 @@ #if FEATURE_SERIALIZABLE using System; using System.Globalization; -using System.IO; using System.Reflection; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; using Assert = Lucene.Net.TestFramework.Assert; namespace Lucene.Net.Util @@ -38,28 +35,6 @@ namespace Lucene.Net.Util { #endif - protected static bool TypeCanSerialize<T>(T exception) - { - T clone; - - try - { -#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - var binaryFormatter = new BinaryFormatter(); - using var serializationStream = new MemoryStream(); - binaryFormatter.Serialize(serializationStream, exception); - serializationStream.Seek(0, SeekOrigin.Begin); - clone = (T)binaryFormatter.Deserialize(serializationStream); -#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - } - catch (SerializationException) - { - return false; - } - - return true; - } - protected static object TryInstantiate(Type type) { object instance = null; diff --git a/src/Lucene.Net.TestFramework/Support/Util/LuceneTestCase.Serialization.cs b/src/Lucene.Net.TestFramework/Support/Util/LuceneTestCase.Serialization.cs new file mode 100644 index 0000000..922d544 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Support/Util/LuceneTestCase.Serialization.cs @@ -0,0 +1,98 @@ +#if FEATURE_SERIALIZABLE +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Lucene.Net.Util +{ + /* + * 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. + */ + + public abstract partial class LuceneTestCase + { +#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) + /// <summary> + /// Serializes <paramref name="source"/> using <see cref="BinaryFormatter"/> + /// and returns a <see cref="MemoryStream"/> with the result of the serialzation. + /// This method is not meant to scale, it is only for quick serialization verification + /// in tests. + /// </summary> + /// <param name="source">The object to serialize. It must be serializable.</param> + /// <returns>A <see cref="MemoryStream"/> containing the serialized result.</returns> + internal static MemoryStream Serialize(object source) + { + IFormatter formatter = new BinaryFormatter(); + MemoryStream stream = new MemoryStream(); + formatter.Serialize(stream, source); + return stream; + } + + /// <summary> + /// Deserializes the given <paramref name="stream"/> into an object instance + /// of type <typeparamref name="T"/>. + /// </summary> + /// <typeparam name="T">The type to cast to after serialization.</typeparam> + /// <param name="stream">The stream to deserialize.</param> + /// <returns>A deserialized object instance.</returns> + internal static T Deserialize<T>(Stream stream) + { + IFormatter formatter = new BinaryFormatter(); + stream.Position = 0; + return (T)formatter.Deserialize(stream); + } +#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) + + /// <summary> + /// Uses <see cref="BinaryFormatter"/> and a <see cref="MemoryStream"/> + /// to serialize and then deserialize <paramref name="source"/>. If the + /// operation succeeds it means that serialization didn't fail, but extra + /// checks should be made on the members of the object to ensure they survived + /// the trip. + /// </summary> + /// <typeparam name="T">The type of <paramref name="source"/>.</typeparam> + /// <param name="source">An object to clone. It must be serializable.</param> + /// <returns>The deserialized object instance (a copy of <paramref name="source"/>).</returns> + internal static T Clone<T>(T source) + { + return Deserialize<T>(Serialize(source)); + } + + /// <summary> + /// Does a check to ensure a type can serialize and deserialize. + /// <para/> + /// NOTE: This only checks errors. It does not verify that the members survived + /// intact. To make that check, use <see cref="Clone{T}(T)"/> instead. + /// </summary> + /// <typeparam name="T">The type of <paramref name="source"/>.</typeparam> + /// <param name="source">An object to serialize. It must be serializable.</param> + /// <returns><c>true</c> if the serialization operation was successful; otherwise <c>false</c>.</returns> + internal static bool TypeCanSerialize<T>(T source) + { + try + { + Clone(source); + } + catch (SerializationException) + { + return false; + } + + return true; + } + } +} +#endif \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Support/TestOldPatches.cs b/src/Lucene.Net.Tests/Support/TestOldPatches.cs index 26b9c38..8398cae 100644 --- a/src/Lucene.Net.Tests/Support/TestOldPatches.cs +++ b/src/Lucene.Net.Tests/Support/TestOldPatches.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Analysis; +using Lucene.Net.Analysis; using Lucene.Net.Index; using Lucene.Net.Search; using Lucene.Net.Util; @@ -51,88 +51,92 @@ namespace Lucene.Net.Support //} //------------------------------------------- -#if FEATURE_SERIALIZABLE - [Test] - [Description("LUCENENET-170")] - public void Test_Util_Parameter() - { - Lucene.Net.Search.BooleanQuery queryPreSerialized = new Lucene.Net.Search.BooleanQuery(); - queryPreSerialized.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("country", "Russia")), Occur.MUST); - queryPreSerialized.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("country", "France")), Occur.MUST); - - //now serialize it - System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); -#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - serializer.Serialize(memoryStream, queryPreSerialized); - - //now deserialize - memoryStream.Seek(0, System.IO.SeekOrigin.Begin); - Lucene.Net.Search.BooleanQuery queryPostSerialized = (Lucene.Net.Search.BooleanQuery)serializer.Deserialize(memoryStream); -#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - memoryStream.Close(); - - Assert.AreEqual(queryPreSerialized, queryPostSerialized, "See the issue: LUCENENET-170"); - } -#endif + // LUCENENENET: Microsoft no longer considers it good practice to use binary serialization + // in new applications. Therefore, we are no longer marking queries serializable + // (It isn't serializable in Lucene 4.8.0 anymore anyway). + // See: https://github.com/dotnet/corefx/issues/23584 + //#if FEATURE_SERIALIZABLE + // [Test] + // [Description("LUCENENET-170")] + // public void Test_Util_Parameter() + // { + // Lucene.Net.Search.BooleanQuery queryPreSerialized = new Lucene.Net.Search.BooleanQuery(); + // queryPreSerialized.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("country", "Russia")), Occur.MUST); + // queryPreSerialized.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("country", "France")), Occur.MUST); + + // //now serialize it + // System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + // System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); + //#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) + // serializer.Serialize(memoryStream, queryPreSerialized); + + // //now deserialize + // memoryStream.Seek(0, System.IO.SeekOrigin.Begin); + // Lucene.Net.Search.BooleanQuery queryPostSerialized = (Lucene.Net.Search.BooleanQuery)serializer.Deserialize(memoryStream); + //#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) + // memoryStream.Close(); + + // Assert.AreEqual(queryPreSerialized, queryPostSerialized, "See the issue: LUCENENET-170"); + // } + //#endif // LUCENENENET: Microsoft no longer considers it good practice to use binary serialization // in new applications. Therefore, we are no longer marking RAMDirectory serializable // (It isn't serializable in Lucene 4.8.0 anymore anyway). // See: https://github.com/dotnet/corefx/issues/23584 -// //------------------------------------------- -//#if FEATURE_SERIALIZABLE -// [Test] -// [Description("LUCENENET-174")] -// public void Test_Store_RAMDirectory() -// { -// Lucene.Net.Store.RAMDirectory ramDIR = new Lucene.Net.Store.RAMDirectory(); - -// //Index 1 Doc -// Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); -// var conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); -// Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(ramDIR, conf /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED*/); -// Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); -// doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); -// wr.AddDocument(doc); -// wr.Dispose(); - -// //now serialize it -// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); -// System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); -// serializer.Serialize(memoryStream, ramDIR); - -// //Close DIR -// ramDIR.Dispose(); -// ramDIR = null; - -// //now deserialize -// memoryStream.Seek(0, System.IO.SeekOrigin.Begin); -// Lucene.Net.Store.RAMDirectory ramDIR2 = (Lucene.Net.Store.RAMDirectory)serializer.Deserialize(memoryStream); - -// //Add 1 more doc -// Lucene.Net.Analysis.Analyzer analyzer2 = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); -// var conf2 = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); -// wr = new Lucene.Net.Index.IndexWriter(ramDIR2, conf2 /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED*/); -// doc = new Lucene.Net.Documents.Document(); -// doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); -// wr.AddDocument(doc); -// wr.Dispose(); - -// Lucene.Net.Search.TopDocs topDocs; -// //Search -// using (var reader = DirectoryReader.Open(ramDIR2)) -// { -// Lucene.Net.Search.IndexSearcher s = new Lucene.Net.Search.IndexSearcher(reader); -// Lucene.Net.QueryParsers.Classic.QueryParser qp = new Lucene.Net.QueryParsers.Classic.QueryParser(Version.LUCENE_CURRENT, "field1", new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_CURRENT)); -// Lucene.Net.Search.Query q = qp.Parse("value1"); -// topDocs = s.Search(q, 100); -// } - -// Assert.AreEqual(topDocs.TotalHits, 2, "See the issue: LUCENENET-174"); -// } -//#endif + // //------------------------------------------- + //#if FEATURE_SERIALIZABLE + // [Test] + // [Description("LUCENENET-174")] + // public void Test_Store_RAMDirectory() + // { + // Lucene.Net.Store.RAMDirectory ramDIR = new Lucene.Net.Store.RAMDirectory(); + + // //Index 1 Doc + // Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); + // var conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); + // Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(ramDIR, conf /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED*/); + // Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document(); + // doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); + // wr.AddDocument(doc); + // wr.Dispose(); + + // //now serialize it + // System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + // System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); + // serializer.Serialize(memoryStream, ramDIR); + + // //Close DIR + // ramDIR.Dispose(); + // ramDIR = null; + + // //now deserialize + // memoryStream.Seek(0, System.IO.SeekOrigin.Begin); + // Lucene.Net.Store.RAMDirectory ramDIR2 = (Lucene.Net.Store.RAMDirectory)serializer.Deserialize(memoryStream); + + // //Add 1 more doc + // Lucene.Net.Analysis.Analyzer analyzer2 = new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(Version.LUCENE_CURRENT); + // var conf2 = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); + // wr = new Lucene.Net.Index.IndexWriter(ramDIR2, conf2 /*new Lucene.Net.Analysis.WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.UNLIMITED*/); + // doc = new Lucene.Net.Documents.Document(); + // doc.Add(new Lucene.Net.Documents.Field("field1", "value1 value11", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED)); + // wr.AddDocument(doc); + // wr.Dispose(); + + // Lucene.Net.Search.TopDocs topDocs; + // //Search + // using (var reader = DirectoryReader.Open(ramDIR2)) + // { + // Lucene.Net.Search.IndexSearcher s = new Lucene.Net.Search.IndexSearcher(reader); + // Lucene.Net.QueryParsers.Classic.QueryParser qp = new Lucene.Net.QueryParsers.Classic.QueryParser(Version.LUCENE_CURRENT, "field1", new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_CURRENT)); + // Lucene.Net.Search.Query q = qp.Parse("value1"); + // topDocs = s.Search(q, 100); + // } + + // Assert.AreEqual(topDocs.TotalHits, 2, "See the issue: LUCENENET-174"); + // } + //#endif //------------------------------------------- diff --git a/src/Lucene.Net.Tests/Support/TestSerialization.cs b/src/Lucene.Net.Tests/Support/TestSerialization.cs index a61e561..4cc821d 100644 --- a/src/Lucene.Net.Tests/Support/TestSerialization.cs +++ b/src/Lucene.Net.Tests/Support/TestSerialization.cs @@ -1,109 +1,114 @@ -#if FEATURE_SERIALIZABLE -using Lucene.Net.Attributes; -using Lucene.Net.Index; -using Lucene.Net.Search; -using Lucene.Net.Util; -using NUnit.Framework; -using Assert = Lucene.Net.TestFramework.Assert; - -#pragma warning disable 612, 618 -namespace Lucene.Net.Support -{ - /* - * 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. - */ - - [SuppressCodecs("Lucene3x")] // Suppress non-writable codecs - [TestFixture] - public class TestSerialization : LuceneTestCase - { - Lucene.Net.Store.RAMDirectory dir = null; - - [SetUp] - public void Setup() - { - dir = new Lucene.Net.Store.RAMDirectory(); - Index(); - } - - void Index() - { - var conf = new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(LuceneVersion.LUCENE_CURRENT)); - Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(dir, conf/*new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(LuceneVersion.LUCENE_CURRENT), Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED*/); - - Lucene.Net.Documents.Document doc = null; - Lucene.Net.Documents.Field f = null; - - doc = new Lucene.Net.Documents.Document(); - f = new Lucene.Net.Documents.Field("field", "a b c d", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); - doc.Add(f); - wr.AddDocument(doc); - - doc = new Lucene.Net.Documents.Document(); - f = new Lucene.Net.Documents.Field("field", "a b a d", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); - doc.Add(f); - wr.AddDocument(doc); - - doc = new Lucene.Net.Documents.Document(); - f = new Lucene.Net.Documents.Field("field", "a b e f", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); - doc.Add(f); - wr.AddDocument(doc); - - doc = new Lucene.Net.Documents.Document(); - f = new Lucene.Net.Documents.Field("field", "x y z", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); - doc.Add(f); - wr.AddDocument(doc); - - wr.Dispose(); - } - - - [Test, LuceneNetSpecific] - [Description("LUCENENET-338 (also see LUCENENET-170)")] - public void TestBooleanQuerySerialization() - { - Lucene.Net.Search.BooleanQuery lucQuery = new Lucene.Net.Search.BooleanQuery(); - - lucQuery.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field", "x")), Occur.MUST); - - System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - System.IO.MemoryStream ms = new System.IO.MemoryStream(); -#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - bf.Serialize(ms, lucQuery); - ms.Seek(0, System.IO.SeekOrigin.Begin); - Lucene.Net.Search.BooleanQuery lucQuery2 = (Lucene.Net.Search.BooleanQuery)bf.Deserialize(ms); -#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) - ms.Close(); - - Assert.AreEqual(lucQuery, lucQuery2, "Error in serialization"); - - using var reader = DirectoryReader.Open(dir); - //Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir, true); - Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(reader); - - int hitCount = searcher.Search(lucQuery, 20).TotalHits; - - //searcher.Close(); - searcher = new Lucene.Net.Search.IndexSearcher(reader); - - int hitCount2 = searcher.Search(lucQuery2, 20).TotalHits; - - Assert.AreEqual(hitCount, hitCount2, "Error in serialization - different hit counts"); - } - } -} -#pragma warning restore 612, 618 -#endif \ No newline at end of file +// LUCENENENET: Microsoft no longer considers it good practice to use binary serialization +// in new applications. Therefore, we are no longer marking queries serializable +// (It isn't serializable in Lucene 4.8.0 anymore anyway). +// See: https://github.com/dotnet/corefx/issues/23584 + +//#if FEATURE_SERIALIZABLE +//using Lucene.Net.Attributes; +//using Lucene.Net.Index; +//using Lucene.Net.Search; +//using Lucene.Net.Util; +//using NUnit.Framework; +//using Assert = Lucene.Net.TestFramework.Assert; + +//#pragma warning disable 612, 618 +//namespace Lucene.Net.Support +//{ +// /* +// * 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. +// */ + +// [SuppressCodecs("Lucene3x")] // Suppress non-writable codecs +// [TestFixture] +// public class TestSerialization : LuceneTestCase +// { +// Lucene.Net.Store.RAMDirectory dir = null; + +// [SetUp] +// public void Setup() +// { +// dir = new Lucene.Net.Store.RAMDirectory(); +// Index(); +// } + +// void Index() +// { +// var conf = new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(LuceneVersion.LUCENE_CURRENT)); +// Lucene.Net.Index.IndexWriter wr = new Lucene.Net.Index.IndexWriter(dir, conf/*new Lucene.Net.Analysis.Core.WhitespaceAnalyzer(LuceneVersion.LUCENE_CURRENT), Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED*/); + +// Lucene.Net.Documents.Document doc = null; +// Lucene.Net.Documents.Field f = null; + +// doc = new Lucene.Net.Documents.Document(); +// f = new Lucene.Net.Documents.Field("field", "a b c d", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); +// doc.Add(f); +// wr.AddDocument(doc); + +// doc = new Lucene.Net.Documents.Document(); +// f = new Lucene.Net.Documents.Field("field", "a b a d", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); +// doc.Add(f); +// wr.AddDocument(doc); + +// doc = new Lucene.Net.Documents.Document(); +// f = new Lucene.Net.Documents.Field("field", "a b e f", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); +// doc.Add(f); +// wr.AddDocument(doc); + +// doc = new Lucene.Net.Documents.Document(); +// f = new Lucene.Net.Documents.Field("field", "x y z", Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED); +// doc.Add(f); +// wr.AddDocument(doc); + +// wr.Dispose(); +// } + + +// [Test, LuceneNetSpecific] +// [Description("LUCENENET-338 (also see LUCENENET-170)")] +// public void TestBooleanQuerySerialization() +// { +// Lucene.Net.Search.BooleanQuery lucQuery = new Lucene.Net.Search.BooleanQuery(); + +// lucQuery.Add(new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field", "x")), Occur.MUST); + +// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); +// System.IO.MemoryStream ms = new System.IO.MemoryStream(); +//#pragma warning disable SYSLIB0011 // Type or member is obsolete (BinaryFormatter) +// bf.Serialize(ms, lucQuery); +// ms.Seek(0, System.IO.SeekOrigin.Begin); +// Lucene.Net.Search.BooleanQuery lucQuery2 = (Lucene.Net.Search.BooleanQuery)bf.Deserialize(ms); +//#pragma warning restore SYSLIB0011 // Type or member is obsolete (BinaryFormatter) +// ms.Close(); + +// Assert.AreEqual(lucQuery, lucQuery2, "Error in serialization"); + +// using var reader = DirectoryReader.Open(dir); +// //Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir, true); +// Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(reader); + +// int hitCount = searcher.Search(lucQuery, 20).TotalHits; + +// //searcher.Close(); +// searcher = new Lucene.Net.Search.IndexSearcher(reader); + +// int hitCount2 = searcher.Search(lucQuery2, 20).TotalHits; + +// Assert.AreEqual(hitCount, hitCount2, "Error in serialization - different hit counts"); +// } +// } +//} +//#pragma warning restore 612, 618 +//#endif \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Support/Util/TestLongsRef.cs b/src/Lucene.Net.Tests/Support/Util/TestLongsRef.cs new file mode 100644 index 0000000..c45541b --- /dev/null +++ b/src/Lucene.Net.Tests/Support/Util/TestLongsRef.cs @@ -0,0 +1,75 @@ +using Lucene.Net.Attributes; +using NUnit.Framework; +using Assert = Lucene.Net.TestFramework.Assert; + +namespace Lucene.Net.Util +{ + /* + * 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. + */ + + // LUCENENET: These tests didn't exist in Lucene 4.8.0. Just copying the TestIntsRef tests so we can test the same things. + [TestFixture] + public class TestLongsRef : LuceneTestCase + { + [Test, LuceneNetSpecific] + public virtual void TestEmpty() + { + Int64sRef i = new Int64sRef(); + Assert.AreEqual(Int64sRef.EMPTY_INT64S, i.Int64s); + Assert.AreEqual(0, i.Offset); + Assert.AreEqual(0, i.Length); + } + + [Test, LuceneNetSpecific] + public virtual void TestFromLongs() + { + long[] ints = new long[] { 1, 2, 3, 4 }; + Int64sRef i = new Int64sRef(ints, 0, 4); + Assert.AreEqual(ints, i.Int64s); + Assert.AreEqual(0, i.Offset); + Assert.AreEqual(4, i.Length); + + Int64sRef i2 = new Int64sRef(ints, 1, 3); + Assert.AreEqual(new Int64sRef(new long[] { 2, 3, 4 }, 0, 3), i2); + + Assert.IsFalse(i.Equals(i2)); + } + +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + var longs = new long[] { 5, 10, 15, 20, 25, 30, 35, 40 }; + + var longsRef = new Int64sRef(longs, 3, 4); + + Assert.AreEqual(4, longsRef.Length); + Assert.AreSame(longs, longsRef.Int64s); + Assert.AreEqual(longs, longsRef.Int64s); + Assert.AreEqual(3, longsRef.Offset); + + var clone = Clone(longsRef); + + Assert.AreEqual(4, clone.Length); + Assert.AreNotSame(longs, clone.Int64s); + Assert.AreEqual(longs, clone.Int64s); + Assert.AreEqual(3, clone.Offset); + } +#endif + } +} \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Util/TestBytesRef.cs b/src/Lucene.Net.Tests/Util/TestBytesRef.cs index 3da8ac5..309ee15 100644 --- a/src/Lucene.Net.Tests/Util/TestBytesRef.cs +++ b/src/Lucene.Net.Tests/Util/TestBytesRef.cs @@ -1,4 +1,4 @@ -using J2N.Text; +using J2N.Text; using Lucene.Net.Attributes; using NUnit.Framework; using Assert = Lucene.Net.TestFramework.Assert; @@ -96,5 +96,28 @@ namespace Lucene.Net.Util b.CopyBytes(new BytesRef("bcde")); Assert.AreEqual("bcde", b.Utf8ToString()); } + +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + byte[] bytes = new byte[] { 44, 66, 77, 33, 99, 13, 74, 26 }; + + var bytesRef = new BytesRef(bytes, 2, 4); + + Assert.AreEqual(4, bytesRef.Length); + Assert.AreSame(bytes, bytesRef.Bytes); + Assert.AreEqual(bytes, bytesRef.Bytes); + Assert.AreEqual(2, bytesRef.Offset); + + var clone = Clone(bytesRef); + + Assert.AreEqual(4, clone.Length); + Assert.AreNotSame(bytes, clone.Bytes); + Assert.AreEqual(bytes, clone.Bytes); + Assert.AreEqual(2, clone.Offset); + } +#endif } } \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Util/TestCharsRef.cs b/src/Lucene.Net.Tests/Util/TestCharsRef.cs index 1246e38..1865313 100644 --- a/src/Lucene.Net.Tests/Util/TestCharsRef.cs +++ b/src/Lucene.Net.Tests/Util/TestCharsRef.cs @@ -247,5 +247,28 @@ namespace Lucene.Net.Util // expected exception } } + +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + var chars = "The quick brown fox jumped over the lazy dog.".ToCharArray(); + + var charsRef = new CharsRef(chars, 8, 10); + + Assert.AreEqual(10, charsRef.Length); + Assert.AreSame(chars, charsRef.Chars); + Assert.AreEqual(chars, charsRef.Chars); + Assert.AreEqual(8, charsRef.Offset); + + var clone = Clone(charsRef); + + Assert.AreEqual(10, clone.Length); + Assert.AreNotSame(chars, clone.Chars); + Assert.AreEqual(chars, clone.Chars); + Assert.AreEqual(8, clone.Offset); + } +#endif } } \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Util/TestIntsRef.cs b/src/Lucene.Net.Tests/Util/TestIntsRef.cs index 6e79c59..35c5c00 100644 --- a/src/Lucene.Net.Tests/Util/TestIntsRef.cs +++ b/src/Lucene.Net.Tests/Util/TestIntsRef.cs @@ -1,3 +1,4 @@ +using Lucene.Net.Attributes; using NUnit.Framework; using Assert = Lucene.Net.TestFramework.Assert; @@ -46,5 +47,28 @@ namespace Lucene.Net.Util Assert.IsFalse(i.Equals(i2)); } + +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + var ints = new int[] { 5, 10, 15, 20, 25, 30, 35, 40 }; + + var intsRef = new Int32sRef(ints, 3, 4); + + Assert.AreEqual(4, intsRef.Length); + Assert.AreSame(ints, intsRef.Int32s); + Assert.AreEqual(ints, intsRef.Int32s); + Assert.AreEqual(3, intsRef.Offset); + + var clone = Clone(intsRef); + + Assert.AreEqual(4, clone.Length); + Assert.AreNotSame(ints, clone.Int32s); + Assert.AreEqual(ints, clone.Int32s); + Assert.AreEqual(3, clone.Offset); + } +#endif } } \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Util/TestLongBitSet.cs b/src/Lucene.Net.Tests/Util/TestLongBitSet.cs index badd258..109f7cc 100644 --- a/src/Lucene.Net.Tests/Util/TestLongBitSet.cs +++ b/src/Lucene.Net.Tests/Util/TestLongBitSet.cs @@ -446,5 +446,32 @@ namespace Lucene.Net.Util Assert.IsTrue(bits.Get(1)); Assert.IsFalse(newBits.Get(1)); } + +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + var numWords = Int64BitSet.Bits2words(10); + var expected = new long[numWords]; + for (int i = 0; i < numWords; i++) + expected[i] = i; + + var set = new Int64BitSet(expected, 10); + + + Assert.AreEqual(expected, set.bits); + Assert.AreSame(expected, set.bits); + Assert.AreEqual(numWords, set.numWords); + Assert.AreEqual(10L, set.Length); + + var clone = Clone(set); + + Assert.AreEqual(expected, clone.bits); + Assert.AreNotSame(expected, clone.bits); + Assert.AreEqual(numWords, clone.numWords); + Assert.AreEqual(10L, clone.Length); + } +#endif } } \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs b/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs index f6b8d60..2c6add0 100644 --- a/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs +++ b/src/Lucene.Net.Tests/Util/TestPriorityQueue.cs @@ -2,6 +2,11 @@ using NUnit.Framework; using System; using System.Collections.Generic; +using System.IO; +#if FEATURE_SERIALIZABLE +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +#endif using Assert = Lucene.Net.TestFramework.Assert; using Console = Lucene.Net.Util.SystemConsole; @@ -29,6 +34,9 @@ namespace Lucene.Net.Util { private static readonly int MAX_PQ_SIZE = ArrayUtil.MAX_ARRAY_LENGTH - 1; +#if FEATURE_SERIALIZABLE + [Serializable] +#endif private class IntegerQueue : PriorityQueue<int?> { public IntegerQueue(int count) @@ -647,6 +655,32 @@ namespace Lucene.Net.Util pq.Clear(); } - #endregion +#if FEATURE_SERIALIZABLE + + [Test, LuceneNetSpecific] + public void TestSerialization() + { + var queue = new IntegerQueue(10); + var expected = new int?[11]; + + for (int i = 0; i < 10; i++) + { + queue.Add(i); + expected[i + 1] = i; + } + + Assert.AreEqual(10, queue.maxSize); + Assert.AreEqual(expected, queue.heap); + Assert.AreEqual(10, queue.Count); + + var clone = Clone(queue); + + Assert.AreEqual(10, clone.maxSize); + Assert.AreEqual(expected, clone.heap); + Assert.AreEqual(10, clone.Count); + } +#endif + +#endregion } } \ No newline at end of file diff --git a/src/Lucene.Net/Index/IndexWriterConfig.cs b/src/Lucene.Net/Index/IndexWriterConfig.cs index 44d473c..538c68c 100644 --- a/src/Lucene.Net/Index/IndexWriterConfig.cs +++ b/src/Lucene.Net/Index/IndexWriterConfig.cs @@ -63,9 +63,6 @@ namespace Lucene.Net.Index /// @since 3.1 /// </summary> /// <seealso cref="IndexWriter.Config"/> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public sealed class IndexWriterConfig : LiveIndexWriterConfig // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation { // LUCENENET specific: De-nested OpenMode enum from this class to prevent naming conflict diff --git a/src/Lucene.Net/Index/LiveIndexWriterConfig.cs b/src/Lucene.Net/Index/LiveIndexWriterConfig.cs index d052dd4..ffa8973 100644 --- a/src/Lucene.Net/Index/LiveIndexWriterConfig.cs +++ b/src/Lucene.Net/Index/LiveIndexWriterConfig.cs @@ -35,9 +35,6 @@ namespace Lucene.Net.Index /// /// @since 4.0 /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public class LiveIndexWriterConfig { private readonly Analyzer analyzer; diff --git a/src/Lucene.Net/Index/Term.cs b/src/Lucene.Net/Index/Term.cs index 98d792a..e9f2a91 100644 --- a/src/Lucene.Net/Index/Term.cs +++ b/src/Lucene.Net/Index/Term.cs @@ -31,9 +31,6 @@ namespace Lucene.Net.Index /// Note that terms may represent more than words from text fields, but also /// things like dates, email addresses, urls, etc. /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public sealed class Term : IComparable<Term>, IEquatable<Term> // LUCENENET specific - class implements IEquatable<T> { /// <summary> diff --git a/src/Lucene.Net/Index/TermContext.cs b/src/Lucene.Net/Index/TermContext.cs index 1e95e19..d7470b0 100644 --- a/src/Lucene.Net/Index/TermContext.cs +++ b/src/Lucene.Net/Index/TermContext.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Diagnostics; +using Lucene.Net.Diagnostics; using Lucene.Net.Support; using System; @@ -32,9 +32,6 @@ namespace Lucene.Net.Index /// <para/> /// @lucene.experimental /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public sealed class TermContext { /// <summary> diff --git a/src/Lucene.Net/Search/BooleanClause.cs b/src/Lucene.Net/Search/BooleanClause.cs index 621f1ca..7b6a67c 100644 --- a/src/Lucene.Net/Search/BooleanClause.cs +++ b/src/Lucene.Net/Search/BooleanClause.cs @@ -21,9 +21,6 @@ namespace Lucene.Net.Search /// <summary> /// A clause in a <see cref="BooleanQuery"/>. </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public class BooleanClause : IEquatable<BooleanClause> { // LUCENENET specific - de-nested Occur from BooleanClause in order to prevent diff --git a/src/Lucene.Net/Search/BooleanQuery.cs b/src/Lucene.Net/Search/BooleanQuery.cs index 13c412e..865ec3d 100644 --- a/src/Lucene.Net/Search/BooleanQuery.cs +++ b/src/Lucene.Net/Search/BooleanQuery.cs @@ -65,9 +65,6 @@ namespace Lucene.Net.Search /// }; /// </code> /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public class BooleanQuery : Query, IEnumerable<BooleanClause> { private static int maxClauseCount = 1024; diff --git a/src/Lucene.Net/Search/FieldCacheRangeFilter.cs b/src/Lucene.Net/Search/FieldCacheRangeFilter.cs index 5d38226..65c339e 100644 --- a/src/Lucene.Net/Search/FieldCacheRangeFilter.cs +++ b/src/Lucene.Net/Search/FieldCacheRangeFilter.cs @@ -61,9 +61,6 @@ namespace Lucene.Net.Search public static class FieldCacheRangeFilter { -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousStringFieldCacheRangeFilter : FieldCacheRangeFilter<string> { internal AnonymousStringFieldCacheRangeFilter(string field, string lowerVal, string upperVal, bool includeLower, bool includeUpper) @@ -132,9 +129,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousBytesRefFieldCacheRangeFilter : FieldCacheRangeFilter<BytesRef> { internal AnonymousBytesRefFieldCacheRangeFilter(string field, BytesRef lowerVal, BytesRef upperVal, bool includeLower, bool includeUpper) @@ -203,9 +197,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousSbyteFieldCacheRangeFilter : FieldCacheRangeFilter<sbyte?> { internal AnonymousSbyteFieldCacheRangeFilter(string field, FieldCache.IParser parser, sbyte? lowerVal, sbyte? upperVal, bool includeLower, bool includeUpper) @@ -255,9 +246,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousInt16FieldCacheRangeFilter : FieldCacheRangeFilter<short?> { internal AnonymousInt16FieldCacheRangeFilter(string field, FieldCache.IParser parser, short? lowerVal, short? upperVal, bool includeLower, bool includeUpper) @@ -308,9 +296,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousInt32FieldCacheRangeFilter : FieldCacheRangeFilter<int?> { internal AnonymousInt32FieldCacheRangeFilter(string field, FieldCache.IParser parser, int? lowerVal, int? upperVal, bool includeLower, bool includeUpper) @@ -358,9 +343,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousInt64FieldCacheRangeFilter : FieldCacheRangeFilter<long?> { internal AnonymousInt64FieldCacheRangeFilter(string field, FieldCache.IParser parser, long? lowerVal, long? upperVal, bool includeLower, bool includeUpper) @@ -408,9 +390,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousSingleFieldCacheRangeFilter : FieldCacheRangeFilter<float?> { internal AnonymousSingleFieldCacheRangeFilter(string field, FieldCache.IParser parser, float? lowerVal, float? upperVal, bool includeLower, bool includeUpper) @@ -463,9 +442,6 @@ namespace Lucene.Net.Search } } -#if FEATURE_SERIALIZABLE - [Serializable] -#endif private class AnonymousDoubleFieldCacheRangeFilter : FieldCacheRangeFilter<double?> { internal AnonymousDoubleFieldCacheRangeFilter(string field, FieldCache.IParser parser, double? lowerVal, double? upperVal, bool includeLower, bool includeUpper) diff --git a/src/Lucene.Net/Search/MaxNonCompetitiveBoostAttributeImpl.cs b/src/Lucene.Net/Search/MaxNonCompetitiveBoostAttributeImpl.cs index 19ce211..4359769 100644 --- a/src/Lucene.Net/Search/MaxNonCompetitiveBoostAttributeImpl.cs +++ b/src/Lucene.Net/Search/MaxNonCompetitiveBoostAttributeImpl.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace Lucene.Net.Search { @@ -28,9 +28,6 @@ namespace Lucene.Net.Search /// <para/> /// @lucene.internal /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public sealed class MaxNonCompetitiveBoostAttribute : Attribute, IMaxNonCompetitiveBoostAttribute { private float maxNonCompetitiveBoost = float.NegativeInfinity; diff --git a/src/Lucene.Net/Search/Query.cs b/src/Lucene.Net/Search/Query.cs index 47759c7..82cb20a 100644 --- a/src/Lucene.Net/Search/Query.cs +++ b/src/Lucene.Net/Search/Query.cs @@ -44,9 +44,6 @@ namespace Lucene.Net.Search /// <para/>See also the family of Span Queries (<see cref="Lucene.Net.Search.Spans"/>) /// and additional queries available in the <a href="{@docRoot}/../queries/overview-summary.html">Queries module</a> /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public abstract class Query // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation { // LUCENENET NOTE: We can't set the default boost in the constructor because the diff --git a/src/Lucene.Net/Search/TermQuery.cs b/src/Lucene.Net/Search/TermQuery.cs index f3469d4..be71eb1 100644 --- a/src/Lucene.Net/Search/TermQuery.cs +++ b/src/Lucene.Net/Search/TermQuery.cs @@ -41,9 +41,6 @@ namespace Lucene.Net.Search /// A <see cref="Query"/> that matches documents containing a term. /// this may be combined with other terms with a <see cref="BooleanQuery"/>. /// </summary> -#if FEATURE_SERIALIZABLE - [Serializable] -#endif public class TermQuery : Query { private readonly Term term; diff --git a/src/Lucene.Net/Support/Index/IndexOptionsComparer.cs b/src/Lucene.Net/Support/Index/IndexOptionsComparer.cs index 4f97e6c..6dc3e9a 100644 --- a/src/Lucene.Net/Support/Index/IndexOptionsComparer.cs +++ b/src/Lucene.Net/Support/Index/IndexOptionsComparer.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace Lucene.Net.Index @@ -27,6 +28,12 @@ namespace Lucene.Net.Index /// this class was created as a more performant alternative than calling <c>CompareTo()</c> on <see cref="IndexOptions"/>. /// </summary> // See: GH-376 + + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif public sealed class IndexOptionsComparer : IComparer<IndexOptions> { private IndexOptionsComparer() { } // No instance diff --git a/src/Lucene.Net/Util/BytesRef.cs b/src/Lucene.Net/Util/BytesRef.cs index 87bb54e..07bdbc2 100644 --- a/src/Lucene.Net/Util/BytesRef.cs +++ b/src/Lucene.Net/Util/BytesRef.cs @@ -399,6 +399,11 @@ namespace Lucene.Net.Util } } + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif internal class Utf8SortedAsUnicodeComparer : IComparer<BytesRef> { public static Utf8SortedAsUnicodeComparer Instance = new Utf8SortedAsUnicodeComparer(); @@ -435,6 +440,11 @@ namespace Lucene.Net.Util /// @deprecated this comparer is only a transition mechanism [Obsolete("this comparer is only a transition mechanism")] + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif internal class Utf8SortedAsUtf16Comparer : IComparer<BytesRef> { // Only singleton diff --git a/src/Lucene.Net/Util/CharsRef.cs b/src/Lucene.Net/Util/CharsRef.cs index f24deaf..98f3a37 100644 --- a/src/Lucene.Net/Util/CharsRef.cs +++ b/src/Lucene.Net/Util/CharsRef.cs @@ -315,7 +315,7 @@ namespace Lucene.Net.Util /// @deprecated this comparer is only a transition mechanism [Obsolete("this comparer is only a transition mechanism")] - private static readonly IComparer<CharsRef> utf16SortedAsUTF8SortOrder = new Utf16SortedAsUTF8Comparer(); + private static readonly IComparer<CharsRef> utf16SortedAsUTF8SortOrder = new Utf16SortedAsUtf8Comparer(); /// @deprecated this comparer is only a transition mechanism [Obsolete("this comparer is only a transition mechanism")] @@ -323,10 +323,15 @@ namespace Lucene.Net.Util /// @deprecated this comparer is only a transition mechanism [Obsolete("this comparer is only a transition mechanism")] - private class Utf16SortedAsUTF8Comparer : IComparer<CharsRef> + // LUCENENET: It is no longer good practice to use binary serialization. + // See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568 +#if FEATURE_SERIALIZABLE + [Serializable] +#endif + private class Utf16SortedAsUtf8Comparer : IComparer<CharsRef> { // Only singleton - internal Utf16SortedAsUTF8Comparer() + internal Utf16SortedAsUtf8Comparer() { } diff --git a/src/Lucene.Net/Util/LongBitSet.cs b/src/Lucene.Net/Util/LongBitSet.cs index ba8b9fa..491ff84 100644 --- a/src/Lucene.Net/Util/LongBitSet.cs +++ b/src/Lucene.Net/Util/LongBitSet.cs @@ -37,9 +37,9 @@ namespace Lucene.Net.Util #endif public sealed class Int64BitSet { - private readonly long[] bits; + internal readonly long[] bits; // LUCENENET: Internal for testing private readonly long numBits; - private readonly int numWords; + internal readonly int numWords; // LUCENENET: Internal for testing /// <summary> /// If the given <see cref="Int64BitSet"/> is large enough to hold diff --git a/src/Lucene.Net/Util/PriorityQueue.cs b/src/Lucene.Net/Util/PriorityQueue.cs index e03768c..9912fd7 100644 --- a/src/Lucene.Net/Util/PriorityQueue.cs +++ b/src/Lucene.Net/Util/PriorityQueue.cs @@ -43,8 +43,8 @@ namespace Lucene.Net.Util public abstract class PriorityQueue<T> { private int size = 0; - private readonly int maxSize; - private readonly T[] heap; + internal readonly int maxSize; // LUCENENET: Internal for testing + internal readonly T[] heap; // LUCENENET: Internal for testing protected PriorityQueue(int maxSize) // LUCENENET specific - made protected instead of public : this(maxSize, true)
