Switched logging from file to inmemory for tests

This commit is contained in:
Suhas Joshi 2015-01-18 11:33:25 -08:00
Родитель dd4ad28e1b
Коммит 5d06b0d353
9 изменённых файлов: 62 добавлений и 138 удалений

Просмотреть файл

@ -7,7 +7,7 @@ using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
{
public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>,IDisposable
public class InMemoryEFUserStoreTest : UserManagerTestBase<IdentityUser, IdentityRole>
{
protected override object CreateTestContext()
{
@ -24,10 +24,5 @@ namespace Microsoft.AspNet.Identity.EntityFramework.InMemory.Test
var store = new RoleStore<IdentityRole, InMemoryContext>((InMemoryContext)context);
services.AddInstance<IRoleStore<IdentityRole>>(store);
}
public void Dispose()
{
loggerFactory.Dispose();
}
}
}

Просмотреть файл

@ -34,7 +34,6 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test
public void DropDatabaseDone()
{
DropDb();
loggerFactory.Dispose();
}
public void DropDb()

Просмотреть файл

@ -7,7 +7,7 @@ using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Identity.InMemory.Test
{
public class InMemoryStoreTest : UserManagerTestBase<InMemoryUser, IdentityRole>, IDisposable
public class InMemoryStoreTest : UserManagerTestBase<InMemoryUser, IdentityRole>
{
protected override object CreateTestContext()
{
@ -23,10 +23,5 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
{
services.AddSingleton<IRoleStore<IdentityRole>, InMemoryRoleStore<IdentityRole>>();
}
public void Dispose()
{
loggerFactory.Dispose();
}
}
}

Просмотреть файл

@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using Microsoft.Framework.Logging;
using Xunit;
@ -60,15 +59,11 @@ namespace Microsoft.AspNet.Identity.Test
}
private static void VerifySuccessLog(ILogger logger, string className, string methodName, string id, string userOrRole = "user")
{
if (logger is TestFileLogger)
TestLogger testlogger = logger as TestLogger;
if (testlogger != null)
{
var fileLogger = logger as TestFileLogger;
string expected = string.Format("{0} for {1}: {2} : Success", methodName, userOrRole, id);
lock (TestFileLogger.FileLock)
{
Assert.True(File.ReadAllText(fileLogger.FileName).Contains(expected));
}
Assert.True(testlogger.LogMessages.Contains(expected));
}
else
{
@ -78,13 +73,10 @@ namespace Microsoft.AspNet.Identity.Test
public static void VerifyLogMessage(ILogger logger, string expectedLog)
{
if (logger is TestFileLogger)
TestLogger testlogger = logger as TestLogger;
if (testlogger != null)
{
var fileLogger = logger as TestFileLogger;
lock (TestFileLogger.FileLock)
{
Assert.True(File.ReadAllText(fileLogger.FileName).Contains(expectedLog));
}
Assert.True(testlogger.LogMessages.Contains(expectedLog));
}
else
{
@ -94,16 +86,13 @@ namespace Microsoft.AspNet.Identity.Test
private static void VerifyFailureLog(ILogger logger, string className, string methodName, string userId, string userOrRole = "user", params IdentityError[] errors)
{
if (logger is TestFileLogger)
TestLogger testlogger = logger as TestLogger;
if (testlogger != null)
{
var fileLogger = logger as TestFileLogger;
errors = errors ?? new IdentityError[] { new IdentityError() };
string expected = string.Format("{0} for {1}: {2} : Failed : {3}", methodName, userOrRole, userId, string.Join(",", errors.Select(x => x.Code).ToList()));
lock (TestFileLogger.FileLock)
{
Assert.True(File.ReadAllText(fileLogger.FileName).Contains(expected));
}
Assert.True(testlogger.LogMessages.Contains(expected));
}
else
{

Просмотреть файл

@ -1,48 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Identity.Test
{
public class TestFileLogger : ILogger
{
public string FileName { get; set; }
public static object FileLock { get; private set; } = new object();
public TestFileLogger(string name)
{
var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "IdentityTests");
Directory.CreateDirectory(directory);
FileName = Path.Combine(directory, (name + DateTime.Now.Ticks + "log.txt"));
lock (FileLock)
{
if (!File.Exists(FileName))
{
File.Create(FileName).Close();
}
}
}
public IDisposable BeginScope(object state)
{
throw new NotImplementedException();
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
lock (FileLock)
{
File.AppendAllLines(FileName, new string[] { state.ToString() });
}
}
}
}

Просмотреть файл

@ -1,55 +0,0 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Identity.Test
{
public class TestFileLoggerFactory : ILoggerFactory, IDisposable
{
private static Dictionary<string, ILogger> _loggers;
static TestFileLoggerFactory()
{
_loggers = new Dictionary<string, ILogger>();
}
public void AddProvider(ILoggerProvider provider)
{
}
public ILogger Create(string name)
{
if (!_loggers.ContainsKey(name))
{
try
{
_loggers.Add(name, new TestFileLogger(name));
}
catch (ArgumentException ex)
{
// Silently skip if there is already a logger with that key
}
}
return _loggers[name];
}
public void Dispose()
{
Parallel.ForEach(_loggers.Values, l =>
{
if(l is TestFileLogger)
{
var logger = l as TestFileLogger;
File.Delete(logger.FileName);
}
});
}
}
}

29
test/Shared/TestLogger.cs Normal file
Просмотреть файл

@ -0,0 +1,29 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Identity.Test
{
public class TestLogger : ILogger
{
public IList<string> LogMessages { get; private set; } = new List<string>();
public IDisposable BeginScope(object state)
{
throw new NotImplementedException();
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
LogMessages.Add(state.ToString());
}
}
}

Просмотреть файл

@ -0,0 +1,20 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Identity.Test
{
public class TestLoggerFactory : ILoggerFactory
{
public void AddProvider(ILoggerProvider provider)
{
}
public ILogger Create(string name)
{
return new TestLogger();
}
}
}

Просмотреть файл

@ -26,11 +26,11 @@ namespace Microsoft.AspNet.Identity.Test
where TRole : IdentityRole<TKey>, new()
where TKey : IEquatable<TKey>
{
protected TestFileLoggerFactory loggerFactory;
protected TestLoggerFactory loggerFactory;
public UserManagerTestBase()
{
loggerFactory = new TestFileLoggerFactory();
loggerFactory = new TestLoggerFactory();
}
protected virtual void SetupIdentityServices(IServiceCollection services, object context = null)