Skip to content

Add generic job support for assemblyQualifiedNameWithoutVersion method #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using Xunit;

namespace Horarium.Test
{
public class AssemblyQualifiedNameWithoutVersionTests
{
[Fact]
public void GetName_ForNonGenericJob_ShouldRemoveVersion()
{
var jobName = new SecondNonGenericJob().GetType().AssemblyQualifiedNameWithoutVersion();

var type = Type.GetType(jobName, true);
var instance = Activator.CreateInstance(type);

Assert.NotNull(instance);
Assert.IsType<SecondNonGenericJob>(instance);
Assert.Equal("Horarium.Test.SecondNonGenericJob, Horarium.Test", jobName);
}

[Fact]
public void GetName_ForGenericJobWithTwoArguments_ShouldRemoveVersionForAllTypes()
{
var jobName = new GenericJobWithTwoArguments<SecondNonGenericJob, FirstNonGenericJob>().GetType().AssemblyQualifiedNameWithoutVersion();

var type = Type.GetType(jobName, true);
var instance = Activator.CreateInstance(type);

Assert.NotNull(instance);
Assert.IsType<GenericJobWithTwoArguments<SecondNonGenericJob, FirstNonGenericJob>>(instance);
Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.SecondNonGenericJob, Horarium.Test], [Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test", jobName);
}

[Fact]
public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldRemoveVersionForAllTypes()
{
var jobName = new GenericJobWithTwoArguments<GenericJob<FirstNonGenericJob>, GenericJob<SecondNonGenericJob>>()
.GetType()
.AssemblyQualifiedNameWithoutVersion();

var type = Type.GetType(jobName, true);
var instance = Activator.CreateInstance(type);

Assert.NotNull(instance);
Assert.IsType<GenericJobWithTwoArguments<GenericJob<FirstNonGenericJob>, GenericJob<SecondNonGenericJob>>>(instance);
// Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.GenericJob`1[[Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName);
johnstoletov marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void GetName_ForGenericJobWithSingleGenericArgument_ShouldRemoveVersionForAllTypes()
{
var jobName = new GenericJob<SecondNonGenericJob>().GetType().AssemblyQualifiedNameWithoutVersion();

var type = Type.GetType(jobName, true);
var instance = Activator.CreateInstance(type);

Assert.NotNull(instance);
Assert.IsType<GenericJob<SecondNonGenericJob>>(instance);
Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test", jobName);
}

[Fact]
public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldRemoveVersionForAllTypes()
{
var jobName = new GenericJob<GenericJob<SecondNonGenericJob>>().GetType().AssemblyQualifiedNameWithoutVersion();

var type = Type.GetType(jobName, true);
var instance = Activator.CreateInstance(type);

Assert.NotNull(instance);
Assert.IsType<GenericJob<GenericJob<SecondNonGenericJob>>>(instance);
Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName);
}
}
}
4 changes: 4 additions & 0 deletions src/Horarium.Test/TypeName/FirstNonGenericJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Horarium.Test
{
public class FirstNonGenericJob { }
}
4 changes: 4 additions & 0 deletions src/Horarium.Test/TypeName/GenericJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Horarium.Test
{
public class GenericJob<TFirst> { }
}
4 changes: 4 additions & 0 deletions src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Horarium.Test
{
public class GenericJobWithTwoArguments<TFirst, TSecond> { }
}
4 changes: 4 additions & 0 deletions src/Horarium.Test/TypeName/SecondNonGenericJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Horarium.Test
{
public class SecondNonGenericJob { }
}
29 changes: 25 additions & 4 deletions src/Horarium/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Cronos;
using Newtonsoft.Json;

[assembly:InternalsVisibleTo("Horarium.Test")]
namespace Horarium
{
internal static class Utils
Expand All @@ -19,14 +22,32 @@ public static object FromJson(this string json, Type type, JsonSerializerSetting

public static string AssemblyQualifiedNameWithoutVersion(this Type type)
{
string retValue = type.FullName + ", " + type.GetTypeInfo().Assembly.GetName().Name;
return retValue;
if (!type.IsGenericType)
{
return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}";
}

if (string.IsNullOrWhiteSpace(type.FullName))
{
throw new ArgumentException($"Не удалось получить имя тип {type}");
johnstoletov marked this conversation as resolved.
Show resolved Hide resolved
}

var genericArguments = type
.GetGenericArguments()
.Select(typeArgument => $"[{typeArgument.AssemblyQualifiedNameWithoutVersion()}]")
.ToArray();

var genericPart = string.Join(", ", genericArguments);

var bracketIndex = type.FullName.IndexOf("[", StringComparison.Ordinal);

return $"{type.FullName.Substring(0, bracketIndex)}[{genericPart}], {type.GetTypeInfo().Assembly.GetName().Name}";
}

public static DateTime? ParseAndGetNextOccurrence(string cron)
{
var expression = CronExpression.Parse(cron, CronFormat.IncludeSeconds);

return expression.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Local);
}
}
Expand Down