Skip to content

Commit b64a4c0

Browse files
committed
NH-3807 - Configure Log4Net for tests independently of any app.config.
Manually load section from NHibernate.Test.dll.config because autoloading doesn't work with dotnet test running.
1 parent 427bbbc commit b64a4c0

File tree

13 files changed

+137
-97
lines changed

13 files changed

+137
-97
lines changed

src/NHibernate.Test/App.config

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,53 +50,10 @@
5050
<property name="show_sql">false</property>
5151
<property name="command_timeout">444</property>
5252

53-
</session-factory>
53+
</session-factory>
5454
</hibernate-configuration>
5555

56-
<log4net debug="false">
57-
58-
<!-- Appenders -->
59-
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
60-
<layout type="log4net.Layout.PatternLayout, log4net">
61-
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
62-
</layout>
63-
</appender>
64-
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
65-
<layout type="log4net.Layout.PatternLayout, log4net">
66-
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
67-
</layout>
68-
</appender>
69-
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender, log4net" >
70-
<param name="File" value="log.txt" />
71-
<param name="AppendToFile" value="false" />
72-
<param name="RollingStyle" value="Date" />
73-
<param name="DatePattern" value="yyyy.MM.dd" />
74-
<param name="StaticLogFileName" value="true" />
75-
<layout type="log4net.Layout.PatternLayout, log4net">
76-
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
77-
</layout>
78-
</appender>
79-
80-
<root>
81-
<priority value="WARN" />
82-
<appender-ref ref="console" />
83-
</root>
84-
85-
<!-- Loggers -->
86-
<logger name="NHibernate.Hql.Ast.ANTLR">
87-
<priority value="OFF" />
88-
</logger>
89-
<logger name="NHibernate.SQL">
90-
<level value="OFF" />
91-
</logger>
92-
<logger name="NHibernate.AdoNet.AbstractBatcher">
93-
<level value="OFF" />
94-
</logger>
95-
<logger name="NHibernate.Tool.hbm2ddl.SchemaExport">
96-
<level value="ERROR" />
97-
</logger>
98-
99-
</log4net>
100-
101-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup>
56+
<startup>
57+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
58+
</startup>
10259
</configuration>

src/NHibernate.Test/CfgTest/ConfigurationFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void InvalidXmlInHbmFile()
236236
Configuration cfg = new Configuration();
237237
try
238238
{
239-
cfg.Configure();
239+
cfg.Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
240240
cfg.AddXmlFile("invalid.hbm.xml");
241241
}
242242
catch (HibernateException)

src/NHibernate.Test/CfgTest/ConfigurationSchemaFixture.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public void InvalidConfig()
2525
}
2626

2727
[Test]
28+
#if NETCOREAPP2_0
29+
[Ignore("This platform does not support automatically locating app.config from unit tests.")]
30+
#endif
2831
public void FromAppConfigTest()
2932
{
3033
IHibernateConfiguration hc = ConfigurationManager.GetSection("hibernate-configuration") as IHibernateConfiguration;
@@ -33,10 +36,19 @@ public void FromAppConfigTest()
3336
Assert.AreEqual("NHibernate.Test", hc.SessionFactory.Name);
3437
}
3538

39+
[Test]
40+
public void FromConfigForExePathTest()
41+
{
42+
IHibernateConfiguration hc = TestsContext.GetTestAssemblyHibernateConfiguration();
43+
Assert.That(hc.ByteCodeProviderType, Is.EqualTo("lcg"));
44+
Assert.IsTrue(hc.UseReflectionOptimizer);
45+
Assert.AreEqual("NHibernate.Test", hc.SessionFactory.Name);
46+
}
47+
3648
[Test]
3749
public void IgnoreSystemOutOfAppConfig()
3850
{
39-
IHibernateConfiguration hc = ConfigurationManager.GetSection("hibernate-configuration") as IHibernateConfiguration;
51+
IHibernateConfiguration hc = TestsContext.GetTestAssemblyHibernateConfiguration();
4052
string xml =
4153
@"<?xml version='1.0' encoding='utf-8' ?>
4254
<hibernate-configuration xmlns='urn:nhibernate-configuration-2.2'>

src/NHibernate.Test/CfgTest/DefaultFlushModeFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void CanSetDefaultFlushModeThroughXmlConfiguration()
3434
[Test]
3535
public void CanSetDefaultFlushModeThroughStandardConfiguration()
3636
{
37-
var cfg = new Configuration().Configure();
37+
var cfg = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
3838
cfg.Properties[Environment.DefaultFlushMode] = FlushMode.Always.ToString();
3939

4040
using (var sessionFactory = cfg.BuildSessionFactory())
@@ -60,7 +60,7 @@ public void CanSetDefaultFlushModeThroughStandardConfiguration()
6060
public void CanSetDefaultFlushModeThroughLoquaciousConfiguration()
6161
{
6262
var cfg = new Configuration()
63-
.Configure();
63+
.Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
6464

6565
cfg
6666
.SessionFactory()
@@ -74,7 +74,7 @@ public void CanSetDefaultFlushModeThroughLoquaciousConfiguration()
7474
}
7575
}
7676

77-
cfg.Configure()
77+
cfg.Configure(TestsContext.GetTestAssemblyHibernateConfiguration())
7878
.SessionFactory()
7979
.DefaultFlushMode(FlushMode.Commit);
8080

src/NHibernate.Test/CfgTest/Loquacious/EntityCacheConfigurationFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class EntityCacheConfigurationFixture
1212
[Test]
1313
public void ConfigureCacheOfClass()
1414
{
15-
Configuration configure = new Configuration().Configure();
15+
Configuration configure = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
1616
configure.AddResource("NHibernate.Test.CfgTest.Loquacious.EntityToCache.hbm.xml", GetType().Assembly);
1717

1818
configure.EntityCache<EntityToCache>(ce =>
@@ -30,7 +30,7 @@ public void ConfigureCacheOfClass()
3030
[Test]
3131
public void ConfigureCacheOfCollection()
3232
{
33-
Configuration configure = new Configuration().Configure();
33+
Configuration configure = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
3434
configure.AddResource("NHibernate.Test.CfgTest.Loquacious.EntityToCache.hbm.xml", GetType().Assembly);
3535

3636
configure.EntityCache<EntityToCache>(ce =>
@@ -54,7 +54,7 @@ public void ConfigureCacheOfCollection()
5454
[Test]
5555
public void ConfigureCacheOfCollectionWithOutEntity()
5656
{
57-
Configuration configure = new Configuration().Configure();
57+
Configuration configure = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
5858
configure.AddResource("NHibernate.Test.CfgTest.Loquacious.EntityToCache.hbm.xml", GetType().Assembly);
5959

6060
configure.EntityCache<EntityToCache>(ce => ce.Collection(e => e.Elements, cc =>
@@ -71,7 +71,7 @@ public void ConfigureCacheOfCollectionWithOutEntity()
7171
[Test]
7272
public void NotAllowRelatedCollections()
7373
{
74-
Configuration configure = new Configuration().Configure();
74+
Configuration configure = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
7575
configure.AddResource("NHibernate.Test.CfgTest.Loquacious.EntityToCache.hbm.xml", GetType().Assembly);
7676

7777
var exception =

src/NHibernate.Test/ConnectionStringTest/NamedConnectionStringFixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public void ConnectionStringInSettingsOverrideNamedConnectionSTring()
3333
}
3434

3535
[Test]
36+
#if NETCOREAPP2_0
37+
[Ignore("This platform does not support named connection strings from unit tests.")]
38+
#endif
3639
public void CanGetNamedConnectionStringFromConfiguration()
3740
{
3841
Dictionary<string, string> settings = new Dictionary<string, string>();

src/NHibernate.Test/MappingByCode/IntegrationTests/NH3667/MapFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void TestMapElementElement()
4444
[Test]
4545
public void TestMapEntityEntity()
4646
{
47-
var cfg = new Configuration().Configure();
47+
var cfg = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
4848
var mapper = new ModelMapper();
4949

5050
mapper.Class<ClassWithMapEntityEntity>(
@@ -123,7 +123,7 @@ public void TestMapEntityElement()
123123
[Test]
124124
public void TestMapElementEntity()
125125
{
126-
var cfg = new Configuration().Configure();
126+
var cfg = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
127127
var mapper = new ModelMapper();
128128

129129
mapper.Class<ClassWithMapElementEntity>(

src/NHibernate.Test/NHSpecificTest/NH1587/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Bug()
1717
if (TestConfigurationHelper.hibernateConfigFile != null)
1818
cfg.Configure(TestConfigurationHelper.hibernateConfigFile);
1919
cfg.AddResource("NHibernate.Test.NHSpecificTest.NH1587.Mappings.hbm.xml", GetType().Assembly);
20-
cfg.Configure();
20+
cfg.Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
2121

2222
bool useOptimizer= false;
2323
using (var ls = new LogSpy("NHibernate.Tuple.Entity.PocoEntityTuplizer"))

src/NHibernate.Test/NHSpecificTest/NH1867/AddMappingTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public class B
4242
[Test]
4343
public void NestedWithinNonGeneric()
4444
{
45-
var configuration = new Configuration().Configure();
45+
var configuration = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
4646
configuration.AddXml(string.Format(mappingTemplate, typeof(A).FullName, typeof(A.B).FullName));
4747
}
4848

4949
[Test]
5050
public void NestedWithinGeneric()
5151
{
52-
var configuration = new Configuration().Configure();
52+
var configuration = new Configuration().Configure(TestsContext.GetTestAssemblyHibernateConfiguration());
5353
configuration.AddXml(string.Format(mappingTemplate, typeof(A<int>).FullName, typeof(A<int>.B).FullName));
5454
}
5555
}

src/NHibernate.Test/TestsContext.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Configuration;
2+
using System.IO;
3+
using System.Xml;
4+
5+
using NHibernate.Cfg.ConfigurationSchema;
6+
using NUnit.Framework;
7+
using log4net.Repository.Hierarchy;
8+
9+
namespace NHibernate.Test
10+
{
11+
[SetUpFixture]
12+
public class TestsContext
13+
{
14+
[OneTimeSetUp]
15+
public void RunBeforeAnyTests()
16+
{
17+
HibernateConfiguration config = GetTestAssemblyHibernateConfiguration();
18+
NHibernate.Cfg.Environment.InitializeGlobalProperties(config);
19+
20+
ConfigureLog4Net();
21+
}
22+
23+
public static HibernateConfiguration GetTestAssemblyHibernateConfiguration()
24+
{
25+
HibernateConfiguration config;
26+
string assemblyPath = Path.Combine(TestContext.CurrentContext.TestDirectory, @"NHibernate.Test.dll");
27+
var configuration = ConfigurationManager.OpenExeConfiguration(assemblyPath);
28+
ConfigurationSection configSection = configuration.GetSection(CfgXmlHelper.CfgSectionName);
29+
30+
using (XmlTextReader reader = new XmlTextReader(configSection.SectionInformation.GetRawXml(), XmlNodeType.Document, null))
31+
{
32+
config = new HibernateConfiguration(reader);
33+
}
34+
35+
return config;
36+
}
37+
38+
private static void ConfigureLog4Net()
39+
{
40+
var hierarchy = (Hierarchy)log4net.LogManager.GetRepository(typeof(TestsContext).Assembly);
41+
42+
var consoleAppender = new log4net.Appender.ConsoleAppender()
43+
{
44+
Layout = new log4net.Layout.PatternLayout("%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"),
45+
};
46+
47+
((Logger)hierarchy.GetLogger("NHibernate.Hql.Ast.ANTLR")).Level = log4net.Core.Level.Off;
48+
((Logger)hierarchy.GetLogger("NHibernate.SQL")).Level = log4net.Core.Level.Off;
49+
((Logger)hierarchy.GetLogger("NHibernate.AdoNet.AbstractBatcher")).Level = log4net.Core.Level.Off;
50+
((Logger)hierarchy.GetLogger("NHibernate.Tool.hbm2ddl.SchemaExport")).Level = log4net.Core.Level.Error;
51+
hierarchy.Root.Level = log4net.Core.Level.Warn;
52+
hierarchy.Root.AddAppender(consoleAppender);
53+
hierarchy.Configured = true;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)