lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [2/2] lucenenet git commit: Fixing another reflection test
Date Thu, 05 Feb 2015 17:10:29 GMT
Fixing another reflection test


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/5e9250f8
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/5e9250f8
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/5e9250f8

Branch: refs/heads/master
Commit: 5e9250f84a8598c9ed570dc3da23517cd5a5c7e6
Parents: 903d885
Author: Itamar Syn-Hershko <itamar@code972.com>
Authored: Thu Feb 5 19:10:12 2015 +0200
Committer: Itamar Syn-Hershko <itamar@code972.com>
Committed: Thu Feb 5 19:10:12 2015 +0200

----------------------------------------------------------------------
 src/Lucene.Net.Core/Analysis/TokenStream.cs | 42 +++++++++++++-----------
 src/Lucene.Net.Tests/core/TestAssertions.cs | 18 ++++++----
 2 files changed, 34 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/5e9250f8/src/Lucene.Net.Core/Analysis/TokenStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Analysis/TokenStream.cs b/src/Lucene.Net.Core/Analysis/TokenStream.cs
index c1c9d98..5f2aebb 100644
--- a/src/Lucene.Net.Core/Analysis/TokenStream.cs
+++ b/src/Lucene.Net.Core/Analysis/TokenStream.cs
@@ -1,4 +1,7 @@
 using System.Diagnostics;
+using System.Linq;
+using System.Reflection;
+using System.Runtime.CompilerServices;
 using Lucene.Net.Analysis.Tokenattributes;
 using System;
 using Lucene.Net.Documents;
@@ -86,48 +89,49 @@ namespace Lucene.Net.Analysis
         /// <summary>
         /// A TokenStream using the default attribute factory.
         /// </summary>
-        protected internal TokenStream()
+        protected TokenStream()
         {
-            //Debug.Assert(AssertFinal());
+            Debug.Assert(AssertFinal());
         }
 
         /// <summary>
         /// A TokenStream that uses the same attributes as the supplied one.
         /// </summary>
-        protected internal TokenStream(AttributeSource input)
+        protected TokenStream(AttributeSource input)
             : base(input)
         {
-            //Debug.Assert(AssertFinal());
+            Debug.Assert(AssertFinal());
         }
 
         /// <summary>
         /// A TokenStream using the supplied AttributeFactory for creating new <seealso
cref="Attribute"/> instances.
         /// </summary>
-        protected internal TokenStream(AttributeFactory factory)
+        protected TokenStream(AttributeFactory factory)
             : base(factory)
         {
-            //Debug.Assert(AssertFinal());
+            Debug.Assert(AssertFinal());
         }
 
-        /* LUCENENET TO-DO
         private bool AssertFinal()
         {
-          try
-          {
-            Type clazz = this.GetType();
-            if (!clazz.desiredAssertionStatus())
+            var type = this.GetType();
+
+            //if (!type.desiredAssertionStatus()) return true; // not supported in .NET
+
+            var hasCompilerGeneratedAttribute =
+                type.GetCustomAttributes(typeof (CompilerGeneratedAttribute), false).Any();
+            var isAnonymousType = hasCompilerGeneratedAttribute && type.FullName.Contains("AnonymousType");
+
+            var method = type.GetMethod("IncrementToken", BindingFlags.Public | BindingFlags.Instance);
+
+            if (!(isAnonymousType || type.IsSealed || (method != null && method.IsFinal)))
           
             {
-              return true;
+                // Original Java code throws an AssertException via Java's assert, we can't
do this here
+                throw new InvalidOperationException("TokenStream implementation classes or
at least their IncrementToken() implementation must be marked sealed");
             }
-            Debug.Assert(clazz.AnonymousClass || (clazz.Modifiers & (Modifier.FINAL |
Modifier.PRIVATE)) != 0 || Modifier.isFinal(clazz.GetMethod("incrementToken").Modifiers),
"TokenStream implementation classes or at least their IncrementToken() implementation must
be final");
+
             return true;
-          }
-          catch (Exception nsme)
-          {
-            return false;
-          }
         }
-        */
 
         /// <summary>
         /// Consumers (i.e., <seealso cref="IndexWriter"/>) use this method to advance
the stream to

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/5e9250f8/src/Lucene.Net.Tests/core/TestAssertions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/TestAssertions.cs b/src/Lucene.Net.Tests/core/TestAssertions.cs
index 22f9f4e..cf41b27 100644
--- a/src/Lucene.Net.Tests/core/TestAssertions.cs
+++ b/src/Lucene.Net.Tests/core/TestAssertions.cs
@@ -1,3 +1,5 @@
+using System.Diagnostics;
+
 namespace Lucene.Net.Tests
 {
     using NUnit.Framework;
@@ -30,7 +32,7 @@ namespace Lucene.Net.Tests
 
         internal class TestTokenStream1 : TokenStream
         {
-            public override bool IncrementToken()
+            public sealed override bool IncrementToken()
             {
                 return false;
             }
@@ -55,19 +57,21 @@ namespace Lucene.Net.Tests
         [Test]
         public virtual void TestTokenStreams()
         {
-            new TestTokenStream1();
-            new TestTokenStream2();
-            bool doFail = false;
+            // In Java, an AssertionError is expected: TokenStream implementation classes
or at least their incrementToken() implementation must be final
+
+            var a = new TestTokenStream1();
+            var b = new TestTokenStream2();
+            var doFail = false;
             try
             {
-                new TestTokenStream3();
+                var c = new TestTokenStream3();
                 doFail = true;
             }
-            catch (InvalidOperationException e)
+            catch (InvalidOperationException)
             {
                 // expected
             }
-            Assert.IsFalse(doFail, "TestTokenStream3 should fail assertion");
+            assertFalse("TestTokenStream3 should fail assertion", doFail);
         }
     }
 


Mime
View raw message