Skip to content

Commit

Permalink
Merge pull request #4824 from berkarslan-xo/bugfix/Issue-4823
Browse files Browse the repository at this point in the history
Custom Analyzer types generate System.MissingMethodException
  • Loading branch information
valadas authored Sep 9, 2021
2 parents 1ac6ef9 + ad085f3 commit df67ceb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
7 changes: 6 additions & 1 deletion DNN Platform/Library/Framework/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,15 @@ public static Type CreateType(string TypeName, string CacheKey, bool UseCache, b
}

public static object CreateInstance(Type Type)
{
return CreateInstance(Type, null);
}

public static object CreateInstance(Type Type, object[] args)
{
if (Type != null)
{
return Type.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, null, null);
return Type.InvokeMember(string.Empty, BindingFlags.CreateInstance, null, null, args, null);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,17 @@ public Analyzer GetCustomAnalyzer()
try
{
var analyzerType = Reflection.CreateType(customAnalyzerType);
analyzer = Reflection.CreateInstance(analyzerType) as Analyzer;

// If parameterless ctor exists, use that; if not, pass the Lucene Version.
if (analyzerType?.GetConstructor(Type.EmptyTypes) != null)
{
analyzer = Reflection.CreateInstance(analyzerType) as Analyzer;
}
else if (analyzerType?.GetConstructor(new Type[] { typeof(Lucene.Net.Util.Version) }) != null)
{
analyzer = Reflection.CreateInstance(analyzerType, new object[] { Constants.LuceneVersion }) as Analyzer;
}

if (analyzer == null)
{
throw new ArgumentException(string.Format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
<Compile Include="Entities\Tabs\TabChangeTrackerTests.cs" />
<Compile Include="Entities\Urls\AdvancedUrlRewriterTests.cs" />
<Compile Include="Entities\Urls\FriendlyUrlControllerTests.cs" />
<Compile Include="Framework\ReflectionTests.cs" />
<Compile Include="Framework\ServicesFrameworkTests.cs" />
<Compile Include="Framework\JavaScriptLibraries\JavaScriptTests.cs" />
<Compile Include="Providers\Builders\FolderInfoBuilder.cs" />
Expand All @@ -205,6 +206,7 @@
<Compile Include="Services\Localization\LocalizationTests.cs" />
<Compile Include="Services\Mobile\PreviewProfileControllerTests.cs" />
<Compile Include="Services\Mobile\RedirectionControllerTests.cs" />
<Compile Include="Services\Search\Internals\LuceneControllerImplTests.cs" />
<Compile Include="Services\Tokens\TokenReplaceTests.cs" />
<Compile Include="Services\Tokens\PropertyAccessTests.cs" />
<Compile Include="FileSystemUtilsTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace DotNetNuke.Tests.Core.Framework
{
using System.Text;
using DotNetNuke.Framework;
using NUnit.Framework;

public class ReflectionTests
{
[Test]
public void CreateInstance_WithArgs_WorksCorrectly()
{
// Arrange
var typeToCreate = typeof(StringBuilder);
var argToPass = new object[] { "one" };

// Act
var result = (StringBuilder)Reflection.CreateInstance(typeToCreate, argToPass);

// Assert
Assert.AreEqual("one", result.ToString());
}

[Test]
public void CreateInstance_WithoutArgs_WorksCorrectly()
{
// Arrange
var typeToCreate = typeof(StringBuilder);

// Act
var result = (StringBuilder)Reflection.CreateInstance(typeToCreate);

// Assert
Assert.AreEqual(string.Empty, result.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace DotNetNuke.Tests.Core.Services.Search.Internals
{
using System.Data;
using DotNetNuke.Abstractions;
using DotNetNuke.Abstractions.Application;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search.Internals;
using DotNetNuke.Tests.Utilities.Mocks;
using Lucene.Net.Analysis.Cz;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;

public class LuceneControllerImplTests
{
[Test]
public void GetCustomAnalyzer_WithTheProvidedAnalyzer_ReturnsTheAnalyzerCorrectly()
{
// Arrange
const string HostSettingsTableName = "HostSettings";
const string SettingNameColumnName = "SettingName";
const string SettingValueColumnName = "SettingValue";
const string SettingIsSecureColumnName = "SettingIsSecure";
const string CustomAnalyzerCacheKeyName = "Search_CustomAnalyzer";
const string CzechAnalyzerTypeName = "Lucene.Net.Analysis.Cz.CzechAnalyzer, Lucene.Net.Contrib.Analyzers";
var services = new ServiceCollection();
var mockData = MockComponentProvider.CreateDataProvider();
var hostSettings = new DataTable(HostSettingsTableName);
var nameCol = hostSettings.Columns.Add(SettingNameColumnName);
hostSettings.Columns.Add(SettingValueColumnName);
hostSettings.Columns.Add(SettingIsSecureColumnName);
hostSettings.PrimaryKey = new[] { nameCol };
hostSettings.Rows.Add(CustomAnalyzerCacheKeyName, CzechAnalyzerTypeName, true);
mockData.Setup(c => c.GetHostSettings()).Returns(hostSettings.CreateDataReader());
var mockedApplicationStatusInfo = new Mock<IApplicationStatusInfo>();
mockedApplicationStatusInfo.Setup(s => s.Status).Returns(UpgradeStatus.Install);
mockedApplicationStatusInfo.Setup(s => s.ApplicationMapPath).Returns(string.Empty);
services.AddTransient(container => Mock.Of<IHostSettingsService>());
services.AddTransient(container => mockedApplicationStatusInfo.Object);
services.AddTransient(container => Mock.Of<INavigationManager>());
Globals.DependencyProvider = services.BuildServiceProvider();
MockComponentProvider.CreateDataCacheProvider();
DataCache.ClearCache();
var luceneController = new LuceneControllerImpl();

// Act
var analyzer = luceneController.GetCustomAnalyzer();

// Assert
Assert.IsInstanceOf<CzechAnalyzer>(analyzer);
}
}
}

0 comments on commit df67ceb

Please sign in to comment.