lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [2/4] lucenenet git commit: Refactored NamedSPILoader and Codec class tests.
Date Thu, 07 Apr 2016 18:06:24 GMT
Refactored NamedSPILoader and Codec class tests.


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

Branch: refs/heads/master
Commit: 7b8664729879f1a9d728f3db2eaad95e2f70e4a2
Parents: c6d7bec
Author: Jozef Izso <jozef.izso@gmail.com>
Authored: Fri Mar 11 11:53:08 2016 +0100
Committer: Jozef Izso <jozef.izso@gmail.com>
Committed: Fri Mar 11 21:46:13 2016 +0100

----------------------------------------------------------------------
 src/Lucene.Net.Core/Codecs/Codec.cs             |  4 +-
 src/Lucene.Net.Core/Util/NamedSPILoader.cs      |  6 +-
 src/Lucene.Net.Tests/Lucene.Net.Tests.csproj    |  1 +
 src/Lucene.Net.Tests/core/Codecs/TestCodec.cs   | 48 ++++++++++++
 .../core/Util/TestNamedSPILoader.cs             | 78 +++++++++++++++-----
 5 files changed, 114 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b866472/src/Lucene.Net.Core/Codecs/Codec.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Codecs/Codec.cs b/src/Lucene.Net.Core/Codecs/Codec.cs
index 8af65d1..1f53bb5 100644
--- a/src/Lucene.Net.Core/Codecs/Codec.cs
+++ b/src/Lucene.Net.Core/Codecs/Codec.cs
@@ -108,7 +108,7 @@ namespace Lucene.Net.Codecs
         {
             if (Loader == null)
             {
-                throw new InvalidOperationException("You called Codec.forName() before all
Codecs could be initialized. " + "this likely happens if you call it from a Codec's ctor.");
+                throw new InvalidOperationException("You called Codec.ForName() before all
Codecs could be initialized. This likely happens if you call it from a Codec's ctor.");
             }
             return Loader.Lookup(name);
         }
@@ -119,7 +119,7 @@ namespace Lucene.Net.Codecs
         {
             if (Loader == null)
             {
-                throw new InvalidOperationException("You called Codec.availableCodecs() before
all Codecs could be initialized. " + "this likely happens if you call it from a Codec's ctor.");
+                throw new InvalidOperationException("You called Codec.AvailableCodecs() before
all Codecs could be initialized. This likely happens if you call it from a Codec's ctor.");
             }
             return Loader.AvailableServices();
         }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b866472/src/Lucene.Net.Core/Util/NamedSPILoader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/NamedSPILoader.cs b/src/Lucene.Net.Core/Util/NamedSPILoader.cs
index bc1a4cb..ad262d8 100644
--- a/src/Lucene.Net.Core/Util/NamedSPILoader.cs
+++ b/src/Lucene.Net.Core/Util/NamedSPILoader.cs
@@ -1,5 +1,6 @@
 using Lucene.Net.Support;
 using System;
+using System.Linq;
 using System.Collections.Generic;
 
 namespace Lucene.Net.Util
@@ -115,7 +116,10 @@ namespace Lucene.Net.Util
             {
                 return service;
             }
-            throw new System.ArgumentException("A SPI class of type " + Clazz.Name + " with
name '" + name + "' does not exist. " + "You need to add the corresponding JAR file supporting
this SPI to your classpath." + "The current classpath supports the following names: " + AvailableServices());
+            var availableServices = string.Join(", ", AvailableServices());
+            throw new ArgumentException("An SPI class of type " + Clazz.Name + " with name
'" + name + "' does not exist. "
+                + "You need to reference the corresponding assembly that contains the class.
"
+                + "The current NamedSPILoader supports the following names: " + availableServices);
         }
 
         public ISet<string> AvailableServices()

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b866472/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
index 6be5a97..53d1d26 100644
--- a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
+++ b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
@@ -154,6 +154,7 @@
     <Compile Include="core\Codecs\Perfield\TestPerFieldDocValuesFormat.cs" />
     <Compile Include="core\Codecs\Perfield\TestPerFieldPostingsFormat.cs" />
     <Compile Include="core\Codecs\Perfield\TestPerFieldPostingsFormat2.cs" />
+    <Compile Include="core\Codecs\TestCodec.cs" />
     <Compile Include="core\Document\TestBinaryDocument.cs" />
     <Compile Include="core\Document\TestDateTools.cs" />
     <Compile Include="core\Document\TestDocument.cs" />

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b866472/src/Lucene.Net.Tests/core/Codecs/TestCodec.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Codecs/TestCodec.cs b/src/Lucene.Net.Tests/core/Codecs/TestCodec.cs
new file mode 100644
index 0000000..f8d005f
--- /dev/null
+++ b/src/Lucene.Net.Tests/core/Codecs/TestCodec.cs
@@ -0,0 +1,48 @@
+using System;
+using NUnit.Framework;
+using System.Collections.Generic;
+
+namespace Lucene.Net.Codecs
+{
+    /*
+     * 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.
+     */
+
+    [TestFixture]
+    public class TestCodec
+    {
+        [Test]
+        public void TestLookup()
+        {
+            Codec codec = Codec.ForName("Lucene46");
+            Assert.AreEqual("Lucene46", codec.Name);
+        }
+
+        // we want an exception if its not found.
+        [Test]
+        public void TestBogusLookup()
+        {
+            Assert.Throws<ArgumentException>(() => Codec.ForName("dskfdskfsdfksdfdsf"));
+        }
+
+        [Test]
+        public void TestAvailableServices()
+        {
+            ISet<string> codecs = Codec.AvailableCodecs();
+            Assert.IsTrue(codecs.Contains("Lucene46"));
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/7b866472/src/Lucene.Net.Tests/core/Util/TestNamedSPILoader.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestNamedSPILoader.cs b/src/Lucene.Net.Tests/core/Util/TestNamedSPILoader.cs
index 5d42841..0dcec35 100644
--- a/src/Lucene.Net.Tests/core/Util/TestNamedSPILoader.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestNamedSPILoader.cs
@@ -1,10 +1,8 @@
+using System;
 using NUnit.Framework;
-using System.Collections.Generic;
 
 namespace Lucene.Net.Util
 {
-    using Codec = Lucene.Net.Codecs.Codec;
-
     /*
      * Licensed to the Apache Software Foundation (ASF) under one or more
      * contributor license agreements.  See the NOTICE file distributed with
@@ -22,37 +20,77 @@ namespace Lucene.Net.Util
      * limitations under the License.
      */
 
-    // TODO: maybe we should test this with mocks, but its easy
-    // enough to test the basics via Codec
     [TestFixture]
     public class TestNamedSPILoader : LuceneTestCase
     {
         [Test]
-        public virtual void TestLookup()
+        public void Lookup_ExistingService_ReturnServiceByName()
         {
-            Codec codec = Codec.ForName("Lucene46");
-            Assert.AreEqual("Lucene46", codec.Name);
+            // Arrange
+            var loader = CreateNamedSPILoaderForCodecClass();
+
+            // Act
+            var codec = loader.Lookup("CustomCodec1");
+
+            // Assert
+            Assert.IsInstanceOf<CustomCodec1>(codec);
+            Assert.AreEqual("CustomCodec1", codec.Name);
         }
 
-        // we want an exception if its not found.
         [Test]
-        public virtual void TestBogusLookup()
+        public void Lookup_NonexistingService_ThrowsException()
         {
-            try
-            {
-                Codec.ForName("dskfdskfsdfksdfdsf");
-                Assert.Fail();
-            }
-            catch (System.ArgumentException expected)
+            // Arrange
+            var loader = CreateNamedSPILoaderForCodecClass();
+
+            // Act
+            var actualException = Assert.Throws<ArgumentException>(() => loader.Lookup("NonexistingCodecName"));
+
+            // Assert
+            const string expectedMessage = "An SPI class of type CustomCodec with name 'NonexistingCodecName'
does not exist. "
+                + "You need to reference the corresponding assembly that contains the class.
"
+                + "The current NamedSPILoader supports the following names: CustomCodec1,
CustomCodec2";
+            Assert.AreEqual(expectedMessage, actualException.Message);
+        }
+
+        [Test]
+        public void AvailableServices_LoaderWithServices_ReturnsListOfNamesOfRegisteredServices()
+        {
+            // Arrange
+            var expectedServices = new [] { "CustomCodec1", "CustomCodec2" };
+            var loader = CreateNamedSPILoaderForCodecClass();
+
+            // Act
+            var actualServices = loader.AvailableServices();
+
+            // Assert
+            Assert.IsNotNull(actualServices);
+            CollectionAssert.IsNotEmpty(actualServices);
+            CollectionAssert.AreEqual(expectedServices, actualServices);
+        }
+
+        private static NamedSPILoader<CustomCodec> CreateNamedSPILoaderForCodecClass()
+        {
+            return new NamedSPILoader<CustomCodec>(typeof(CustomCodec));
+        }
+
+        private abstract class CustomCodec : NamedSPILoader<CustomCodec>.NamedSPI
+        {
+            public string Name
             {
+                get
+                {
+                    return this.GetType().Name;
+                }
             }
         }
 
-        [Test]
-        public virtual void TestAvailableServices()
+        private class CustomCodec1 : CustomCodec
+        {
+        }
+
+        private class CustomCodec2 : CustomCodec
         {
-            ISet<string> codecs = Codec.AvailableCodecs();
-            Assert.IsTrue(codecs.Contains("Lucene46"));
         }
     }
 }
\ No newline at end of file


Mime
View raw message