Skip to content

Commit

Permalink
Added CustomLogger and removed log4net, added FileConfiguration that …
Browse files Browse the repository at this point in the history
…is passed to the repository constructor, instead of passing a file name to the method. This is done to accomodate one of our custom apps.
  • Loading branch information
shashankshetty committed Jul 14, 2009
1 parent 356833e commit c22cb93
Show file tree
Hide file tree
Showing 39 changed files with 450 additions and 28,732 deletions.
Binary file removed lib/XDDNDK2.dll
Binary file not shown.
Binary file removed lib/log4net.dll
Binary file not shown.
28,655 changes: 0 additions & 28,655 deletions lib/log4net.xml

This file was deleted.

Binary file not shown.
File renamed without changes.
9 changes: 7 additions & 2 deletions src/ExcelMapper.Tests/ExcelMapper.Tests.csproj
Expand Up @@ -93,13 +93,18 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Excel\Users.xls">
<None Include="Excel\UsersXls.xls">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Excel\Users.xlsx">
<None Include="Excel\UsersXlsx.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="RunTimCodeGeneratorLoggingConfig.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
80 changes: 65 additions & 15 deletions src/ExcelMapper.Tests/ExcelToDTOMapperTests.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

using ExcelMapper.Configuration;
using ExcelMapper.Repository;
Expand All @@ -24,6 +25,7 @@ public class When_asked_to_convert_excel_worksheets_to_DTO
private IRepository _excelRepository;
private IClassGenerator _classGenerator;
private IAssemblyGenerator _assemblyGenerator;
private IFileConfiguration _fileConfiguration;
private IExcelToDTOMapper _excelToDtoMapper;

private List<string> _excelFiles;
Expand All @@ -37,11 +39,12 @@ public void SetUp()
_excelRepository = MockRepository.GenerateMock<IRepository>();
_classGenerator = MockRepository.GenerateMock<IClassGenerator>();
_assemblyGenerator = MockRepository.GenerateMock<IAssemblyGenerator>();
_excelToDtoMapper = new ExcelToDTOMapper(_excelRepository, _classGenerator, _assemblyGenerator);
_fileConfiguration = MockRepository.GenerateMock<IFileConfiguration>();
_excelToDtoMapper = new ExcelToDTOMapper(_excelRepository, _classGenerator, _assemblyGenerator, _fileConfiguration);

_excelFiles = new List<string>
{
"TestExcel"
"TestExcel.xls"
};
_workSheetNames = new List<string>
{
Expand All @@ -57,9 +60,9 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
{
AddProperties();

_excelRepository.Expect(x => x.GetWorkSheetNames("")).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("", "")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("", "")).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);

Expand All @@ -69,9 +72,9 @@ public void Should_create_one_dto_for_each_of_the_worksheet()
[Test]
public void Should_not_create_dto_if_there_are_no_properties()
{
_excelRepository.Expect(x => x.GetWorkSheetNames("")).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("", "")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("", "")).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("")).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(_classAttributes1)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(true);

Expand All @@ -83,9 +86,9 @@ public void Should_not_create_an_assembly_if_the_compile_fails()
{
AddProperties();

_excelRepository.Expect(x => x.GetWorkSheetNames("")).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes("", _workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes("", _workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
_excelRepository.Expect(x => x.GetWorkSheetNames()).IgnoreArguments().Return(_workSheetNames);
_excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes1);
_excelRepository.Expect(x => x.GetClassAttributes(_workSheetNames[0])).IgnoreArguments().Return(_classAttributes2);
_classGenerator.Expect(x => x.Create(null)).IgnoreArguments();
_assemblyGenerator.Expect(x => x.Compile(null, null)).IgnoreArguments().Return(false);

Expand Down Expand Up @@ -117,14 +120,17 @@ public void SetUp()
[TearDown]
public void TearDown()
{
File.Delete(_assemblyName);

FileInfo[] files = GetCurrentDirectoryCSFiles();

foreach (var file in files)
{
File.Delete(file.Name);
}

//if (Directory.Exists(TestData.LogsDirectory))
//{
// Directory.Delete(TestData.LogsDirectory, true);
//}
}

private static FileInfo[] GetCurrentDirectoryCSFiles()
Expand All @@ -137,13 +143,57 @@ public void Should_create_an_assembly_of_dto_corresponding_to_worksheets_in_the_
{
List<string> excelFiles = new List<string>
{
TestData.UsersXlsx
"Test.xlsx"
};
Assert.IsTrue(_excelToDtoMapper.Run(TestData.AssemblyName, excelFiles));

Assert.IsTrue(_excelToDtoMapper.Run(TestData.AssemblyName, excelFiles));
Assert.IsTrue(File.Exists(_assemblyName));
Assert.IsTrue(GetCurrentDirectoryCSFiles().Length > 0);
}

[Test]
public void Should_create_an_assembly_of_dto_corresponding_to_worksheets_in_the_excel_with_given_namespace_appended_to_filename()
{
List<string> excelFiles = new List<string>
{
TestData.UsersXlsx
};
bool assemblyCompiled = _excelToDtoMapper.Run(TestData.AssemblyName, excelFiles);

if (!assemblyCompiled)
{
Assert.Fail("Assembly failed to compile");
}
Assembly assembly = Assembly.LoadFile(Path.GetFullPath(_assemblyName));
Type[] types = assembly.GetTypes();
Assert.AreEqual(TestData.AssemblyName + "." + "UsersXlsx", types[0].Namespace);
}

[Test]
public void Should_create_an_assembly_of_dto_with_identical_classes_in_two_different_namespaces()
{
List<string> excelFiles = new List<string>
{
TestData.UsersXls,
TestData.UsersXlsx
};
bool assemblyCompiled = _excelToDtoMapper.Run(TestData.AssemblyName, excelFiles);

if (!assemblyCompiled)
{
Assert.Fail("Assembly failed to compile");
}
Assembly assembly = Assembly.LoadFile(Path.GetFullPath(_assemblyName));
Type[] types = assembly.GetTypes();

Console.WriteLine(types.Length);
Console.WriteLine(types[0].Name);
Console.WriteLine(types[1].Name);
Console.WriteLine(types[2].Name);
Assert.IsTrue(types.Length == 2);
Assert.AreEqual(TestData.AssemblyName + "." + "UsersXlsx", types[0].Namespace);
Assert.AreEqual(TestData.AssemblyName + "." + "UsersXls", types[1].Namespace);
}
}
}
}
Expand Up @@ -16,22 +16,22 @@ public class When_asked_for_a_connection
{
private string _file;
private IConnectionString _connectionString;
private IConnection _connection;
private IConnectionBuilder _connectionBuilder;

[SetUp]
public void SetUp()
{
_file = TestData.UsersXlsx;
_connectionString = MockRepository.GenerateMock<IConnectionString>();
_connection = new global::ExcelMapper.Repository.Connection.Connection(_connectionString);
_connectionBuilder = new global::ExcelMapper.Repository.Connection.ConnectionBuilder(_connectionString);
}

[Test]
public void Should_open_a_new_OleDbConnection()
{
_connectionString.Expect(x => x.Get(_file)).Return(TestData.UsersXlsConnectionString);

using (OleDbConnection connection = _connection.GetConnection(_file))
using (OleDbConnection connection = _connectionBuilder.GetConnection(_file))
{
Assert.IsInstanceOfType(typeof(OleDbConnection), connection);
Assert.AreEqual(ConnectionState.Open, connection.State);
Expand Down
20 changes: 14 additions & 6 deletions src/ExcelMapper.Tests/Repository/ExcelRepositoryTests.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;

using ExcelMapper.Configuration;
using ExcelMapper.Repository;
using ExcelMapper.Repository.Connection;
using ExcelMapper.Tests.DTO;
Expand All @@ -20,6 +21,7 @@ public class ExcelRepositoryTestsBase
{
private IConnectionString _connectionString;
protected IRepository _excelRepository;
protected IFileConfiguration _fileConfiguration;

protected string _xlsxFile;
protected string _xlsFile;
Expand All @@ -29,7 +31,8 @@ public class ExcelRepositoryTestsBase
public void SetUp()
{
_connectionString = MockRepository.GenerateMock<IConnectionString>();
_excelRepository = new ExcelRepository(new ExcelMapper.Repository.Connection.Connection(_connectionString));
_fileConfiguration = MockRepository.GenerateMock<IFileConfiguration>();
_excelRepository = new ExcelRepository(new ConnectionBuilder(_connectionString), _fileConfiguration);

_xlsxFile = TestData.UsersXlsx;
_xlsFile = TestData.UsersXls;
Expand All @@ -46,7 +49,8 @@ public class When_given_an_Excel_File : ExcelRepositoryTestsBase
[Test]
public void Should_give_the_list_of_WorkSheets_in_the_excel()
{
List<string> workSheets = _excelRepository.GetWorkSheetNames(_xlsxFile).ToList();
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
List<string> workSheets = _excelRepository.GetWorkSheetNames().ToList();
Assert.IsTrue(workSheets.Count == 3);
Assert.IsTrue(workSheets.Exists(x => x.Equals(String.Format("{0}$", _workSheetName))));
}
Expand All @@ -58,7 +62,8 @@ public class When_given_a_WorkSheet : ExcelRepositoryTestsBase
[Test]
public void Should_return_Class_Properties_object_that_includes_all_the_properties_in_the_class()
{
ClassAttributes classAttributes = _excelRepository.GetClassAttributes(_xlsxFile, String.Format("{0}$", _workSheetName));
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
ClassAttributes classAttributes = _excelRepository.GetClassAttributes(String.Format("{0}$", _workSheetName));
Assert.IsNotNull(classAttributes);
Assert.AreEqual(_workSheetName, classAttributes.Name);
Assert.IsTrue(classAttributes.Properties.Count == 4);
Expand All @@ -71,8 +76,9 @@ public class When_given_a_excel_file_name_and_worksheet_name : ExcelRepositoryTe
[Test]
public void Should_map_the_excel_columns_to_the_given_dto_object()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
User expectedUser = TestData.GetUsers(_xlsxFile, _workSheetName).FirstOrDefault();
User actualUser = _excelRepository.Get<User>(_xlsxFile, _workSheetName).FirstOrDefault();
User actualUser = _excelRepository.Get<User>(_workSheetName).FirstOrDefault();

Assert.IsNotNull(actualUser);

Expand All @@ -85,17 +91,19 @@ public void Should_map_the_excel_columns_to_the_given_dto_object()
[Test]
public void Should_return_dto_objects_for_each_row_in_the_Xlsx_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
List<User> expectedCourts = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
List<User> actualCourts = _excelRepository.Get<User>(_xlsxFile, _workSheetName).ToList();
List<User> actualCourts = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualCourts);
Assert.AreEqual(expectedCourts.Count, actualCourts.Count);
}

[Test]
public void Should_return_dto_objects_for_each_row_in_the_Xls_worksheet()
{
_fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
List<User> expectedSuits = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
List<User> actualSuits = _excelRepository.Get<User>(_xlsFile, _workSheetName).ToList();
List<User> actualSuits = _excelRepository.Get<User>(_workSheetName).ToList();
Assert.IsNotNull(actualSuits);
Assert.AreEqual(expectedSuits.Count, actualSuits.Count);
}
Expand Down
23 changes: 23 additions & 0 deletions src/ExcelMapper.Tests/RunTimCodeGeneratorLoggingConfig.xml
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread](%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="Logs/RunTimeCodeGenerator.log" />
<appendToFile value="true" />
<maximumFileSize value="1000KB" />
<maxSizeRollBackups value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="WARN" />
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
11 changes: 6 additions & 5 deletions src/ExcelMapper.Tests/TestData.cs
Expand Up @@ -10,15 +10,16 @@ namespace ExcelMapper.Tests
{
public class TestData
{
public static string UsersXlsx = @"Excel\Users.xlsx";
public static string UsersXls = @"Excel\Users.xls";
public static string UsersXlsxConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Excel\\Users.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
public static string UsersXlsConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Excel\\Users.xls;Extended Properties=\"Excel 8.0;HDR=YES;\"";
public static string UsersXlsx = @"Excel\UsersXlsx.xlsx";
public static string UsersXls = @"Excel\UsersXls.xls";
public static string UsersXlsxConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Excel\\UsersXlsx.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
public static string UsersXlsConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Excel\\UsersXls.xls;Extended Properties=\"Excel 8.0;HDR=YES;\"";
public static string AssemblyName = "TestAssembly";
public static string LogsDirectory = "Logs";

public static IEnumerable<User> GetUsers(string file, string workSheet)
{
using (OleDbConnection connection = new Connection(new ConnectionString()).GetConnection(file))
using (OleDbConnection connection = new ConnectionBuilder(new ConnectionString()).GetConnection(file))
{
using (OleDbCommand command = connection.CreateCommand())
{
Expand Down
4 changes: 3 additions & 1 deletion src/ExcelMapper/Configuration/ExcelMapperRegistry.cs
Expand Up @@ -10,7 +10,9 @@ public ExcelMapperRegistry()
{
ForRequestedType<IRepository>()
.TheDefaultIsConcreteType<ExcelRepository>();

ForRequestedType<IFileConfiguration>()
.AsSingletons()
.TheDefaultIsConcreteType<FileConfiguration>();
Scan(s =>
{
s.AssemblyContainingType<ExcelMapperRegistry>();
Expand Down
12 changes: 12 additions & 0 deletions src/ExcelMapper/Configuration/FileConfiguration.cs
@@ -0,0 +1,12 @@
namespace ExcelMapper.Configuration
{
public interface IFileConfiguration
{
string FileName { get; set; }
}

public class FileConfiguration : IFileConfiguration
{
public string FileName { get; set; }
}
}
8 changes: 6 additions & 2 deletions src/ExcelMapper/ExcelMapper.csproj
Expand Up @@ -71,14 +71,15 @@
<ItemGroup>
<Compile Include="Configuration\BootStrapper.cs" />
<Compile Include="Configuration\ExcelMapperRegistry.cs" />
<Compile Include="Configuration\FileConfiguration.cs" />
<Compile Include="Configuration\StructureMapServiceLocator.cs" />
<Compile Include="Configuration\ExcelMapperServiceLocator.cs" />
<Compile Include="IExcelToDTOMapper.cs" />
<Compile Include="ExcelToDTOMapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repository\Connection\Connection.cs" />
<Compile Include="Repository\Connection\ConnectionBuilder.cs" />
<Compile Include="Repository\Connection\ConnectionString.cs" />
<Compile Include="Repository\Connection\IConnection.cs" />
<Compile Include="Repository\Connection\IConnectionBuilder.cs" />
<Compile Include="Repository\Connection\IConnectionString.cs" />
<Compile Include="Repository\ExcelRepository.cs" />
<Compile Include="Repository\Extensions\DataReaderExtensions.cs" />
Expand All @@ -90,6 +91,9 @@
<Name>RunTimeCodeGenerator</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit c22cb93

Please sign in to comment.