Skip to content

WIP - Replace IObjectsFactory with IServiceProvider interface #1793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 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
82 changes: 82 additions & 0 deletions src/NHibernate.Test/Async/CfgTest/SettingsFactoryFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using NHibernate.AdoNet;
using NHibernate.Bytecode;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Connection;
using NHibernate.Exceptions;
using NHibernate.Hql;
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;
using NHibernate.Transaction;
using NSubstitute;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CfgTest
{
using System.Threading.Tasks;
[TestFixture]
public class SettingsFactoryFixtureAsync
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should not be generated as there is nothing async, it is a bug related to async generator that I need to fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I released a new version of the async generator (0.8.2.10) that fixes the issue of generating non async methods.

{

[Test]
public async Task InvalidRegisteredServicesAsync()
{
await (InvalidRegisteredServiceAsync<IBatcherFactory>());
await (InvalidRegisteredServiceAsync<ICacheProvider>());
await (InvalidRegisteredServiceAsync<IConnectionProvider>());
await (InvalidRegisteredServiceAsync<Dialect.Dialect>());
await (InvalidRegisteredServiceAsync<ILinqToHqlGeneratorsRegistry>());
await (InvalidRegisteredServiceAsync<IQueryCacheFactory>());
await (InvalidRegisteredServiceAsync<IQueryModelRewriterFactory>());
await (InvalidRegisteredServiceAsync<IQueryTranslatorFactory>());
await (InvalidRegisteredServiceAsync<ITransactionFactory>());
}

private Task InvalidRegisteredServiceAsync<TService>()
{
try
{
var originalSp = Environment.ServiceProvider;

var sp = new DefaultServiceProvider();
sp.Register<TService>(() => throw new InvalidOperationException());

Environment.ServiceProvider = sp;

var properties = new Dictionary<string, string>
{
{Environment.UseQueryCache, "true"}
};
if (typeof(TService) != typeof(Dialect.Dialect))
{
properties.Add(Environment.Dialect, typeof(Dialect.PostgreSQL83Dialect).FullName);
}

Assert.Throws<HibernateException>(
() => new SettingsFactory().BuildSettings(properties),
$"HibernateException should be thrown for service {typeof(TService)}");

Environment.ServiceProvider = originalSp;
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
}
79 changes: 0 additions & 79 deletions src/NHibernate.Test/Bytecode/ActivatorObjectFactoryFixture.cs

This file was deleted.

75 changes: 75 additions & 0 deletions src/NHibernate.Test/Bytecode/DefaultServiceProviderFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using NHibernate.Bytecode;
using NUnit.Framework;

namespace NHibernate.Test.Bytecode
{
[TestFixture]
public class DefaultServiceProviderFixture
{
public class WithOutPublicParameterLessCtor
{
public string Something { get; set; }
protected WithOutPublicParameterLessCtor() { }

public WithOutPublicParameterLessCtor(string something)
{
Something = something;
}
}

public class PublicParameterLessCtor
{
}

protected virtual IServiceProvider GetServiceProvider()
{
return new DefaultServiceProvider();
}

[Test]
public void CreateInstanceDefCtor()
{
var sp = GetServiceProvider();
Assert.Throws<ArgumentNullException>(() => sp.GetService(null));
Assert.Throws<MissingMethodException>(() => sp.GetService(typeof(WithOutPublicParameterLessCtor)));
var instance = sp.GetService(typeof(PublicParameterLessCtor));
Assert.That(instance, Is.Not.Null);
Assert.That(instance, Is.InstanceOf<PublicParameterLessCtor>());
}


[Test]
public void RegisterService()
{
var sp = new DefaultServiceProvider();

Assert.That(sp.GetService(typeof(IInterceptor)), Is.Null);

sp.Register<IInterceptor, EmptyInterceptor>();
var instance = sp.GetService(typeof(IInterceptor));
Assert.That(instance, Is.Not.Null);
Assert.That(instance, Is.InstanceOf<EmptyInterceptor>());

Assert.Throws<InvalidOperationException>(() => sp.Register<IInterceptor, EmptyInterceptor>(), "service should not be registered twice.");
Assert.Throws<InvalidOperationException>(() => sp.Register<Dialect.Dialect, Dialect.Dialect>(), "non concrete implementation type should not be permitted.");
Assert.Throws<InvalidOperationException>(() => sp.Register(typeof(Dialect.Dialect), typeof(EmptyInterceptor)), "concrete implementation type should derive from service type.");
}

[Test]
public void RegisterServiceCreator()
{
var sp = new DefaultServiceProvider();

Assert.That(sp.GetService(typeof(IInterceptor)), Is.Null);

sp.Register<IInterceptor>(() => new EmptyInterceptor());
var instance = sp.GetService(typeof(IInterceptor));
Assert.That(instance, Is.Not.Null);
Assert.That(instance, Is.InstanceOf<EmptyInterceptor>());

Assert.Throws<InvalidOperationException>(() => sp.Register<IInterceptor>(() => new EmptyInterceptor()), "service should not be registered twice.");
}

}
}
8 changes: 4 additions & 4 deletions src/NHibernate.Test/CfgTest/ConfigurationSchemaFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public void IgnoreSystemOutOfAppConfig()
}

[Test]
public void ObjectsFactory()
public void ServiceProvider()
{
Assume.That(TestsContext.ExecutingWithVsTest, Is.False);

var xml =
@"<?xml version='1.0' encoding='utf-8' ?>
<hibernate-configuration xmlns='urn:nhibernate-configuration-2.2'>
<objects-factory type='test'/>
<service-provider type='test'/>
<session-factory>
</session-factory>
</hibernate-configuration>";
Expand All @@ -89,11 +89,11 @@ public void ObjectsFactory()
using (var xtr = new XmlTextReader(xml, XmlNodeType.Document, null))
{
hc = new HibernateConfiguration(xtr);
Assert.That(hc.ObjectsFactoryType, Is.Null);
Assert.That(hc.ServiceProviderType, Is.Null);
}

hc = HibernateConfiguration.FromAppConfig(xml);
Assert.That(hc.ObjectsFactoryType, Is.EqualTo("test"));
Assert.That(hc.ServiceProviderType, Is.EqualTo("test"));
}

[Test]
Expand Down
66 changes: 0 additions & 66 deletions src/NHibernate.Test/CfgTest/CustomObjectsFactoryTest.cs

This file was deleted.

56 changes: 56 additions & 0 deletions src/NHibernate.Test/CfgTest/CustomServiceProviderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using NHibernate.Bytecode;
using NUnit.Framework;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Test.CfgTest
{
[TestFixture]
public class CustomServiceProviderTest
{
private class MyServiceProvider : IServiceProvider
{
public object GetService(System.Type serviceType)
{
throw new NotImplementedException();
}
}
private class InvalidServiceProvider
{
}
private class InvalidNoCtorServiceProvider : MyServiceProvider
{
public InvalidNoCtorServiceProvider(string pizza) {}
}

[Test]
public void WhenNoShortCutUsedThenCanBuildServiceProvider()
{
var properties = new Dictionary<string, string> { { Environment.PropertyBytecodeProvider, typeof(MyServiceProvider).AssemblyQualifiedName } };
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.Nothing);
}

[Test]
public void WhenNoShortCutUsedThenCanBuildInstanceOfConfiguredServiceProvider()
{
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(MyServiceProvider).AssemblyQualifiedName } };
Assert.That(Environment.BuildServiceProvider(properties), Is.InstanceOf<MyServiceProvider>());
}

[Test]
public void WhenInvalidThenThrow()
{
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(InvalidServiceProvider).AssemblyQualifiedName } };
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.TypeOf<HibernateServiceProviderException>());
}

[Test]
public void WhenNoDefaultCtorThenThrow()
{
var properties = new Dictionary<string, string> { { Environment.PropertyServiceProvider, typeof(InvalidNoCtorServiceProvider).AssemblyQualifiedName } };
Assert.That(() => Environment.BuildServiceProvider(properties), Throws.TypeOf<HibernateServiceProviderException>()
.And.InnerException.Message.Contains("constructor was not found"));
}
}
}
Loading