- Starting implementing a file based topology descriptor cache.

- Adding FileExists to AppDataFolder.
- Unit tests.

--HG--
branch : dev
This commit is contained in:
Suha Can
2010-04-14 16:46:14 -07:00
parent f6f73cca9c
commit 8d59e1d6ae
4 changed files with 62 additions and 6 deletions

View File

@@ -1,8 +1,5 @@
using System; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using NUnit.Framework; using NUnit.Framework;
using Orchard.Environment.Configuration; using Orchard.Environment.Configuration;
@@ -91,5 +88,16 @@ this is
a a
test")); test"));
} }
[Test]
public void FileExistsReturnsFalseForNonExistingFile() {
Assert.That(_appDataFolder.FileExists("notexisting"), Is.False);
}
[Test]
public void FileExistsReturnsTrueForExistingFile() {
_appDataFolder.CreateFile("alpha\\foo\\bar.txt", "");
Assert.That(_appDataFolder.FileExists("alpha\\foo\\bar.txt"), Is.True);
}
} }
} }

View File

@@ -11,14 +11,27 @@ namespace Orchard.Tests.Environment.Topology {
[TestFixture] [TestFixture]
public class DefaultTopologyDescriptorCacheTests { public class DefaultTopologyDescriptorCacheTests {
private IContainer _container; private IContainer _container;
private string _tempFolder;
private IAppDataFolder _appDataFolder;
[SetUp] [SetUp]
public void Init() { public void Init() {
_tempFolder = Path.GetTempFileName();
File.Delete(_tempFolder);
Directory.CreateDirectory(_tempFolder);
_appDataFolder = new AppDataFolder();
_appDataFolder.SetBasePath(_tempFolder);
var builder = new ContainerBuilder(); var builder = new ContainerBuilder();
builder.RegisterInstance(_appDataFolder).As<IAppDataFolder>();
builder.RegisterType<DefaultTopologyDescriptorCache>().As<ITopologyDescriptorCache>(); builder.RegisterType<DefaultTopologyDescriptorCache>().As<ITopologyDescriptorCache>();
_container = builder.Build(); _container = builder.Build();
} }
[TearDown]
public void Term() {
Directory.Delete(_tempFolder, true);
}
[Test] [Test]
public void FetchReturnsNullForCacheMiss() { public void FetchReturnsNullForCacheMiss() {
var service = _container.Resolve<ITopologyDescriptorCache>(); var service = _container.Resolve<ITopologyDescriptorCache>();

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Web.Hosting; using System.Web.Hosting;
@@ -16,6 +17,7 @@ namespace Orchard.Environment.Configuration {
void CreateFile(string path, string content); void CreateFile(string path, string content);
string ReadFile(string path); string ReadFile(string path);
void DeleteFile(string path); void DeleteFile(string path);
bool FileExists(string path);
string CreateDirectory(string path); string CreateDirectory(string path);
@@ -51,6 +53,11 @@ namespace Orchard.Environment.Configuration {
File.Delete(Path.Combine(_basePath, path)); File.Delete(Path.Combine(_basePath, path));
} }
public bool FileExists(string path) {
var filePath = Path.Combine(_basePath, path);
return File.Exists(filePath);
}
public IEnumerable<string> ListFiles(string path) { public IEnumerable<string> ListFiles(string path) {
var directoryPath = Path.Combine(_basePath, path); var directoryPath = Path.Combine(_basePath, path);
if (!Directory.Exists(directoryPath)) if (!Directory.Exists(directoryPath))

View File

@@ -1,17 +1,45 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Orchard.Environment.Configuration;
using Orchard.Environment.Topology.Models; using Orchard.Environment.Topology.Models;
using Orchard.Localization;
using Orchard.Logging;
namespace Orchard.Environment.Topology { namespace Orchard.Environment.Topology {
public class DefaultTopologyDescriptorCache : ITopologyDescriptorCache { public class DefaultTopologyDescriptorCache : ITopologyDescriptorCache {
readonly IDictionary<string, ShellTopologyDescriptor> _cache= new Dictionary<string, ShellTopologyDescriptor>(); readonly IDictionary<string, ShellTopologyDescriptor> _cache= new Dictionary<string, ShellTopologyDescriptor>();
private readonly IAppDataFolder _appDataFolder;
private const string TopologyCacheFileName = "cache.dat";
public DefaultTopologyDescriptorCache(IAppDataFolder appDataFolder) {
_appDataFolder = appDataFolder;
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
private Localizer T { get; set; }
#region Implementation of ITopologyDescriptorCache
public ShellTopologyDescriptor Fetch(string name) { public ShellTopologyDescriptor Fetch(string name) {
VerifyCacheFile();
ShellTopologyDescriptor value; ShellTopologyDescriptor value;
return _cache.TryGetValue(name, out value) ? value : null; return _cache.TryGetValue(name, out value) ? value : null;
} }
public void Store(string name, ShellTopologyDescriptor descriptor) { public void Store(string name, ShellTopologyDescriptor descriptor) {
VerifyCacheFile();
_cache[name] = descriptor; _cache[name] = descriptor;
} }
#endregion
private void VerifyCacheFile() {
if (!_appDataFolder.FileExists(TopologyCacheFileName)) {
_appDataFolder.CreateFile(TopologyCacheFileName, String.Empty);
}
}
} }
} }