Adding AppDataFolder service

Factoring references to Server.MapPath and HostingEnvironment.MapPath out of components
Building testing around setup package to isolate database deadlock

--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-02-13 19:25:03 -08:00
parent ec5450ac2d
commit 6baef5ca02
11 changed files with 291 additions and 45 deletions

View File

@@ -89,6 +89,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Roles\Controllers\AdminControllerTests.cs" />
<Compile Include="Roles\Services\RoleServiceTests.cs" />
<Compile Include="Setup\SetupControllerTests.cs" />
<Compile Include="Values.cs" />
<Compile Include="Users\Controllers\AdminControllerTests.cs" />
<Compile Include="Users\Services\MembershipServiceTests.cs" />
@@ -113,6 +114,10 @@
<Project>{D10AD48F-407D-4DB5-A328-173EC7CB010F}</Project>
<Name>Orchard.Roles</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Setup\Orchard.Setup.csproj">
<Project>{8C7FCBC2-E6E1-405E-BFB5-D8D9E67A09C4}</Project>
<Name>Orchard.Setup</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Modules\Orchard.Users\Orchard.Users.csproj">
<Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project>
<Name>Orchard.Users</Name>

View File

@@ -0,0 +1,110 @@
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Builder;
using JetBrains.Annotations;
using Moq;
using NUnit.Framework;
using Orchard.Data.Migrations;
using Orchard.Environment;
using Orchard.Environment.Configuration;
using Orchard.Setup.Controllers;
using Orchard.Setup.ViewModels;
using Orchard.UI.Notify;
namespace Orchard.Tests.Modules.Setup {
[TestFixture, Ignore("this can't be made to work")]
public class SetupControllerTests {
private string _tempFolder;
private IContainer _container;
[SetUp]
public void Init() {
_tempFolder = Path.GetTempFileName();
File.Delete(_tempFolder);
Directory.CreateDirectory(_tempFolder);
var hostContainer = OrchardStarter.CreateHostContainer(builder => {
builder.Register(new ControllerBuilder());
builder.Register(new ViewEngineCollection { new WebFormViewEngine() });
builder.Register(new RouteCollection());
builder.Register(new ModelBinderDictionary());
});
hostContainer.Resolve<IAppDataFolder>().SetBasePath(_tempFolder);
var host = (DefaultOrchardHost)hostContainer.Resolve<IOrchardHost>();
_container = host.CreateShellContainer();
_container.Build(builder => {
builder.Register<SetupController>();
});
//var builder = new ContainerBuilder();
//builder.Register<SetupController>();
//builder.Register<Notifier>().As<INotifier>();
//builder.Register<DefaultOrchardHost>().As<IOrchardHost>();
//builder.Register<DatabaseMigrationManager>().As<IDatabaseMigrationManager>();
//builder.Register<ShellSettingsLoader>().As<IShellSettingsLoader>();
//builder.Register<TestAppDataFolder>().As<IAppDataFolder>();
//_container = builder.Build();
}
private string GetMessages() {
var notifier = _container.Resolve<INotifier>();
return notifier.List().Aggregate("", (a, b) => a + b.Message.ToString());
}
private SetupViewModel GetTestSetupModel() {
return new SetupViewModel {
AdminUsername = "test1",
AdminPassword = "test2",
DatabaseOptions = true,
SiteName = "test3"
};
}
[Test]
public void IndexNormallyReturnsWithDefaultAdminUsername() {
var controller = _container.Resolve<SetupController>();
var result = controller.Index(null);
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.TypeOf<ViewResult>());
var viewResult = (ViewResult)result;
Assert.That(viewResult.ViewData.Model, Is.TypeOf<SetupViewModel>());
var model2 = (SetupViewModel)viewResult.ViewData.Model;
Assert.That(model2.AdminUsername, Is.EqualTo("admin"));
}
[Test]
public void SetupShouldCreateShellSettingsFile() {
var model = GetTestSetupModel();
var controller = _container.Resolve<SetupController>();
var result = controller.IndexPOST(model);
Assert.That(GetMessages(), Is.StringContaining("Setup succeeded"));
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.TypeOf<RedirectResult>());
Assert.That(File.Exists(Path.Combine(_tempFolder, "Sites\\default.txt")));
}
[Test]
public void BuiltinDatabaseShouldCreateSQLiteFile() {
var model = GetTestSetupModel();
var controller = _container.Resolve<SetupController>();
var result = controller.IndexPOST(model);
Assert.That(GetMessages(), Is.StringContaining("Setup succeeded"));
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.TypeOf<RedirectResult>());
Assert.That(File.Exists(Path.Combine(_tempFolder, "Sites\\default\\orchard.db")));
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Orchard.Environment.Configuration;
namespace Orchard.Tests.Environment.Configuration {
[TestFixture]
public class AppDataFolderTests {
private string _tempFolder;
private IAppDataFolder _appDataFolder;
[SetUp]
public void Init() {
_tempFolder = Path.GetTempFileName();
File.Delete(_tempFolder);
Directory.CreateDirectory(Path.Combine(_tempFolder, "alpha"));
File.WriteAllText(Path.Combine(_tempFolder, "alpha\\beta.txt"), "beta-content");
File.WriteAllText(Path.Combine(_tempFolder, "alpha\\gamma.txt"), "gamma-content");
_appDataFolder = new AppDataFolder();
_appDataFolder.SetBasePath(_tempFolder);
}
[TearDown]
public void Term() {
Directory.Delete(_tempFolder, true);
}
[Test]
public void ListFilesShouldContainSubPathAndFileName() {
var files = _appDataFolder.ListFiles("alpha");
Assert.That(files.Count(), Is.EqualTo(2));
Assert.That(files, Has.Some.EqualTo("alpha\\beta.txt"));
Assert.That(files, Has.Some.EqualTo("alpha\\gamma.txt"));
}
[Test]
public void NonExistantFolderShouldListAsEmptyCollection() {
var files = _appDataFolder.ListFiles("delta");
Assert.That(files.Count(), Is.EqualTo(0));
}
[Test]
public void PhysicalPathAddsToBasePathAndDoesNotNeedToExist() {
var physicalPath = _appDataFolder.MapPath("delta\\epsilon.txt");
Assert.That(physicalPath, Is.EqualTo(Path.Combine(_tempFolder, "delta\\epsilon.txt")));
}
}
}

View File

@@ -138,6 +138,7 @@
<Compile Include="Data\Migrations\DatabaseMigrationManagerTests.cs" />
<Compile Include="Data\RepositoryTests.cs" />
<Compile Include="Data\StubLocator.cs" />
<Compile Include="Environment\Configuration\AppDataFolderTests.cs" />
<Compile Include="Environment\DefaultCompositionStrategyTests.cs" />
<Compile Include="Environment\DefaultOrchardHostTests.cs" />
<Compile Include="Environment\DefaultOrchardShellTests.cs" />

View File

@@ -18,19 +18,19 @@ using Orchard.UI.Notify;
namespace Orchard.Setup.Controllers {
public class SetupController : Controller {
private readonly INotifier _notifier;
private readonly IDatabaseMigrationManager _databaseMigrationManager;
private readonly IOrchardHost _orchardHost;
private readonly IShellSettingsLoader _shellSettingsLoader;
private readonly IAppDataFolder _appDataFolder;
public SetupController(
INotifier notifier,
IDatabaseMigrationManager databaseMigrationManager,
IOrchardHost orchardHost,
IShellSettingsLoader shellSettingsLoader) {
IOrchardHost orchardHost,
IShellSettingsLoader shellSettingsLoader,
IAppDataFolder appDataFolder) {
_notifier = notifier;
_databaseMigrationManager = databaseMigrationManager;
_orchardHost = orchardHost;
_shellSettingsLoader = shellSettingsLoader;
_appDataFolder = appDataFolder;
T = NullLocalizer.Instance;
}
@@ -38,7 +38,7 @@ namespace Orchard.Setup.Controllers {
public ActionResult Index(SetupViewModel model) {
string message = "";
if(!CanWriteTo(Server.MapPath("~/App_Data"), out message)) {
if (!CanWriteTo(out message)) {
_notifier.Error(
T(
"Hey, it looks like I can't write to the App_Data folder in the root of this application and that's where I need to save some of the information you're about to enter.\r\n\r\nPlease give me (the machine account this application is running under) write access to App_Data so I can get this app all set up for you.\r\n\r\nThanks!\r\n\r\n----\r\n{0}",
@@ -60,11 +60,11 @@ namespace Orchard.Setup.Controllers {
try {
var shellSettings = new ShellSettings {
Name = "default",
DataProvider = model.DatabaseOptions ? "SQLite" : "SqlServer",
DataConnectionString = model.DatabaseConnectionString
};
Name = "default",
DataProvider = model.DatabaseOptions ? "SQLite" : "SqlServer",
DataConnectionString = model.DatabaseConnectionString
};
// creating a standalone environment.
// in theory this environment can be used to resolve any normal components by interface, and those
// components will exist entirely in isolation - no crossover between the safemode container currently in effect
@@ -74,7 +74,6 @@ namespace Orchard.Setup.Controllers {
var sessionFactoryHolder = finiteEnvironment.Resolve<ISessionFactoryHolder>();
sessionFactoryHolder.UpdateSchema();
var contentManager = finiteEnvironment.Resolve<IContentManager>();
// create superuser
var membershipService = finiteEnvironment.Resolve<IMembershipService>();
@@ -83,6 +82,7 @@ namespace Orchard.Setup.Controllers {
String.Empty, String.Empty, String.Empty,
true));
/*
// set site name and settings
var siteService = finiteEnvironment.Resolve<ISiteService>();
var siteSettings = siteService.GetSiteSettings().As<SiteSettings>();
@@ -91,6 +91,9 @@ namespace Orchard.Setup.Controllers {
siteSettings.Record.SuperUser = model.AdminUsername;
siteSettings.Record.PageTitleSeparator = " - ";
var contentManager = finiteEnvironment.Resolve<IContentManager>();
// create home page as a CMS page
var page = contentManager.Create("page");
page.As<BodyAspect>().Text = "Welcome to Orchard";
@@ -102,6 +105,7 @@ namespace Orchard.Setup.Controllers {
var authenticationService = finiteEnvironment.Resolve<IAuthenticationService>();
authenticationService.SignIn(user, true);
*/
}
catch {
finiteEnvironment.Resolve<ITransactionManager>().Cancel();
@@ -124,17 +128,15 @@ namespace Orchard.Setup.Controllers {
}
}
static bool CanWriteTo(string path, out string message) {
bool CanWriteTo(out string message) {
try {
var systemCheckPath = Path.Combine(path, "_systemcheck.txt");
System.IO.File.WriteAllText(systemCheckPath, "Communicator check one two one two");
System.IO.File.AppendAllText(systemCheckPath, "\r\nThis is Bones McCoy on a line to Sulu");
System.IO.File.Delete(systemCheckPath);
_appDataFolder.CreateFile("_systemcheck.txt", "Communicator check one two one two");
_appDataFolder.DeleteFile("_systemcheck.txt");
message = "";
return true;
} catch (Exception ex) {
}
catch (Exception ex) {
message = ex.Message.Replace("_systemcheck.txt", "");
return false;
}

View File

@@ -15,16 +15,19 @@ namespace Orchard.Data {
private readonly IShellSettings _shellSettings;
private readonly ICompositionStrategy _compositionStrategy;
private readonly IDatabaseMigrationManager _databaseMigrationManager;
private readonly IAppDataFolder _appDataFolder;
private ISessionFactory _sessionFactory;
public SessionFactoryHolder(
IShellSettings shellSettings,
ICompositionStrategy compositionStrategy,
IDatabaseMigrationManager databaseMigrationManager) {
IDatabaseMigrationManager databaseMigrationManager,
IAppDataFolder appDataFolder) {
_shellSettings = shellSettings;
_compositionStrategy = compositionStrategy;
_databaseMigrationManager = databaseMigrationManager;
_appDataFolder = appDataFolder;
}
@@ -34,7 +37,7 @@ namespace Orchard.Data {
}
public ISessionFactory GetSessionFactory() {
lock(this) {
lock (this) {
if (_sessionFactory == null) {
_sessionFactory = BuildSessionFactory();
}
@@ -49,8 +52,8 @@ namespace Orchard.Data {
private IDatabaseCoordinator GetDatabaseCoordinator() {
var sitesPath = HostingEnvironment.MapPath("~/App_Data/Sites");
var shellPath = Path.Combine(sitesPath, _shellSettings.Name);
var shellPath = _appDataFolder.CreateDirectory(Path.Combine("Sites", _shellSettings.Name));
return _databaseMigrationManager.CreateCoordinator(_shellSettings.DataProvider, shellPath, _shellSettings.DataConnectionString);
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
namespace Orchard.Environment.Configuration {
/// <summary>
/// Abstraction of App_Data folder
/// Expected to work on physical filesystem, but decouples core
/// system from web hosting apis
/// </summary>
public interface IAppDataFolder {
IEnumerable<string> ListFiles(string path);
void CreateFile(string path, string content);
void DeleteFile(string path);
string CreateDirectory(string path);
/// <summary>
/// May be called to initialize component when not in a hosting environment
/// app domain
/// </summary>
void SetBasePath(string basePath);
string MapPath(string path);
}
public class AppDataFolder : IAppDataFolder {
protected string _basePath;
public AppDataFolder() {
_basePath = HostingEnvironment.MapPath("~/App_Data");
}
public void CreateFile(string path, string content) {
File.WriteAllText(Path.Combine(_basePath, path), content);
}
public void DeleteFile(string path) {
File.Delete(Path.Combine(_basePath, path));
}
public IEnumerable<string> ListFiles(string path) {
var directoryPath = Path.Combine(_basePath, path);
if (!Directory.Exists(directoryPath))
return Enumerable.Empty<string>();
var files = Directory.GetFiles(directoryPath);
return files.Select(file => {
var fileName = Path.GetFileName(file);
return Path.Combine(path, fileName);
});
}
public string CreateDirectory(string path) {
var directory = Path.Combine(_basePath, path);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
return directory;
}
public void SetBasePath(string basePath) {
_basePath = basePath;
}
public string MapPath(string path) {
return Path.Combine(_basePath, path);
}
}
}

View File

@@ -14,9 +14,11 @@ namespace Orchard.Environment.Configuration {
}
public class ShellSettingsLoader : IShellSettingsLoader {
private readonly IAppDataFolder _appDataFolder;
Localizer T { get; set; }
public ShellSettingsLoader() {
public ShellSettingsLoader(IAppDataFolder appDataFolder) {
_appDataFolder = appDataFolder;
T = NullLocalizer.Instance;
}
@@ -30,31 +32,24 @@ namespace Orchard.Environment.Configuration {
if (string.IsNullOrEmpty(settings.Name))
throw new ArgumentException(T("Settings \"Name\" is not set.").ToString());
var sitesPath = HostingEnvironment.MapPath("~/App_Data/Sites");
if (string.IsNullOrEmpty(sitesPath))
throw new ArgumentException(T("Can't determine the path on the server to save settings. Looking for something like \"~/App_Data/Sites\".").ToString());
if (!Directory.Exists(sitesPath))
Directory.CreateDirectory(sitesPath);
var filePath = Path.Combine(sitesPath, string.Format("{0}.txt", settings.Name));
File.WriteAllText(filePath, ComposeSettings(settings));
var filePath = Path.Combine("Sites", settings.Name + ".txt");
_appDataFolder.CreateFile(filePath, ComposeSettings(settings));
}
static IEnumerable<IShellSettings> LoadSettings() {
IEnumerable<IShellSettings> LoadSettings() {
foreach (var yamlDocument in LoadFiles()) {
yield return ParseSettings(yamlDocument);
}
}
static IEnumerable<YamlDocument> LoadFiles() {
var sitesPath = HostingEnvironment.MapPath("~/App_Data/Sites");
if (sitesPath != null && Directory.Exists(sitesPath)) {
foreach (var settingsFilePath in Directory.GetFiles(sitesPath, "*.txt")) {
var yamlStream = YamlParser.Load(settingsFilePath);
yield return yamlStream.Documents.Single();
}
IEnumerable<YamlDocument> LoadFiles() {
var filePaths = _appDataFolder.ListFiles("Sites")
.Where(path => path.EndsWith(".txt", StringComparison.InvariantCultureIgnoreCase));
foreach (var filePath in filePaths) {
var yamlStream = YamlParser.Load(_appDataFolder.MapPath(filePath));
yield return yamlStream.Documents.Single();
}
}
@@ -65,10 +60,10 @@ namespace Orchard.Environment.Configuration {
.ToDictionary(x => ((Scalar)x.Key).Text, x => x.Value);
return new ShellSettings {
Name = GetValue(fields, "Name"),
DataProvider = GetValue(fields, "DataProvider"),
DataConnectionString = GetValue(fields, "DataConnectionString")
};
Name = GetValue(fields, "Name"),
DataProvider = GetValue(fields, "DataProvider"),
DataConnectionString = GetValue(fields, "DataConnectionString")
};
}
static string GetValue(

View File

@@ -36,6 +36,7 @@ namespace Orchard.Environment {
get { return _current; }
}
void IOrchardHost.Initialize() {
ViewEngines.Engines.Insert(0, LayoutViewEngine.CreateShim());
_controllerBuilder.SetControllerFactory(new OrchardControllerFactory());

View File

@@ -22,6 +22,7 @@ namespace Orchard.Environment {
builder.Register<DefaultOrchardHost>().As<IOrchardHost>().SingletonScoped();
builder.Register<DefaultCompositionStrategy>().As<ICompositionStrategy>().SingletonScoped();
builder.Register<DefaultShellContainerFactory>().As<IShellContainerFactory>().SingletonScoped();
builder.Register<AppDataFolder>().As<IAppDataFolder>().SingletonScoped();
builder.Register<ShellSettingsLoader>().As<IShellSettingsLoader>().SingletonScoped();
builder.Register<SafeModeShellContainerFactory>().As<IShellContainerFactory>().SingletonScoped();

View File

@@ -140,6 +140,7 @@
<Compile Include="Data\Migrations\SQLiteDatabaseCoordinator.cs" />
<Compile Include="Data\Migrations\SqlServerDatabaseCoordinator.cs" />
<Compile Include="Data\SessionFactoryHolder.cs" />
<Compile Include="Environment\Configuration\AppDataFolder.cs" />
<Compile Include="Environment\Configuration\ShellSettingsLoader.cs" />
<Compile Include="Environment\ExtensibleInterceptionModule.cs" />
<Compile Include="Environment\ShellBuilders\DefaultShellContainerFactory.cs" />