Skip to content

Commit

Permalink
improved configuration file loading code in Test project
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Nov 5, 2012
1 parent 06278a0 commit be2ed6c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Test/BootstrapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public void ClearBootstrap()
private IConfigurationSource CreateBootstrap(string configFile)
{
var fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(@"Config", configFile);

var filePath = Path.Combine(@"Config", configFile);
fileMap.ExeConfigFilename = filePath;
Setup.LoadConfigFileFromResource(filePath);
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var configSource = config.GetSection("superSocket") as IConfigurationSource;

Expand Down
32 changes: 8 additions & 24 deletions Test/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,23 @@

namespace SuperSocket.Test
{
[SetUpFixture]
public class Setup
public static class Setup
{
[SetUp]
public void RunBeforeAllTest()
public static void LoadConfigFileFromResource(string filePath)
{
Console.WriteLine("RunBeforeAllTest");

//Extract all resource
var currentAssembly = typeof(Setup).Assembly;
var assemblyName = currentAssembly.GetName().Name;
var names = currentAssembly.GetManifestResourceNames();

foreach (var name in names)
var resourceName = currentAssembly.GetName().Name + "." + filePath.Replace(Path.DirectorySeparatorChar, '.');
using (var stream = currentAssembly.GetManifestResourceStream(resourceName))
{
var fileName = name.Substring(assemblyName.Length + 1);
var fileNameSegments = fileName.Split('.');
var filePath = string.Join(Path.DirectorySeparatorChar.ToString(), fileNameSegments, 0, fileNameSegments.Length -1) + "." + fileNameSegments[fileNameSegments.Length - 1];

if(File.Exists(filePath))
continue;

var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);

var dir = Path.GetDirectoryName(filePath);

if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
Directory.CreateDirectory(dir);

using(var stream = currentAssembly.GetManifestResourceStream(name))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
File.WriteAllBytes(filePath, buffer);
}
File.WriteAllBytes(filePath, buffer);
}
}

Expand Down

0 comments on commit be2ed6c

Please sign in to comment.