Got ApplicationTests working with new unit test project.
Had to fix the ApplicationTree and Application classes to lazily call the Cache method (which is no renamed to EnsureCache) so that we could set the config location in the unit tests. This should also improve performance a teeny bit and allow for better error handling if required.
This commit is contained in:
@@ -41,3 +41,4 @@ _BuildOutput/*
|
||||
build/UmbracoCms.AllBinaries.zip
|
||||
build/UmbracoCms.WebPI.zip
|
||||
build/UmbracoCms.zip
|
||||
src/Umbraco.Tests/config/applications.config
|
||||
|
||||
@@ -18,3 +18,5 @@ using System.Runtime.InteropServices;
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("04436b0a-1dc6-4ee1-9d96-4c04f1a9f429")]
|
||||
|
||||
[assembly: InternalsVisibleTo("Umbraco.Tests")]
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
***********************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlServerCe;
|
||||
using System.Linq;
|
||||
using System.Xml;
|
||||
using System.Diagnostics;
|
||||
using umbraco.DataLayer;
|
||||
@@ -44,6 +46,44 @@ namespace SqlCE4Umbraco
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Most likely only will be used for unit tests but will remove all tables from the database
|
||||
/// </summary>
|
||||
internal void ClearDatabase()
|
||||
{
|
||||
var localConnection = new SqlCeConnection(ConnectionString);
|
||||
var dbFile = localConnection.Database;
|
||||
if (System.IO.File.Exists(dbFile))
|
||||
{
|
||||
var tables = new List<string>();
|
||||
using (var reader = ExecuteReader("select table_name from information_schema.tables where TABLE_TYPE <> 'VIEW'"))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
tables.Add(reader.GetString("TABLE_NAME"));
|
||||
}
|
||||
}
|
||||
|
||||
while(tables.Any())
|
||||
{
|
||||
for (var i = 0; i < tables.Count; i++)
|
||||
{
|
||||
var dropTable = "DROP TABLE " + tables[i];
|
||||
|
||||
try
|
||||
{
|
||||
ExecuteNonQuery(dropTable);
|
||||
tables.Remove(tables[i]);
|
||||
}
|
||||
catch (SqlHelperException ex)
|
||||
{
|
||||
//this will occur because there is no cascade option, so we just wanna try the next one
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new parameter for use with this specific implementation of ISqlHelper.
|
||||
/// </summary>
|
||||
|
||||
+9
-36
@@ -1,49 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using umbraco;
|
||||
using NUnit.Framework;
|
||||
using umbraco.BusinessLogic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using umbraco.DataLayer;
|
||||
using System.Linq;
|
||||
|
||||
namespace Umbraco.LegacyTests
|
||||
namespace Umbraco.Tests.BusinessLogic
|
||||
{
|
||||
[TestClass()]
|
||||
public abstract class BaseTest
|
||||
{
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf");
|
||||
|
||||
var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
var installer = dataHelper.Utility.CreateInstaller();
|
||||
if (installer.CanConnect)
|
||||
{
|
||||
installer.Install();
|
||||
}
|
||||
|
||||
Application.Apps = new List<Application>()
|
||||
{
|
||||
new Application("content", "content", "content", 0)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
///This is a test class for ApplicationTest and is intended
|
||||
///to contain all ApplicationTest Unit Tests
|
||||
///</summary>
|
||||
[TestClass()]
|
||||
[TestFixture()]
|
||||
public class ApplicationTest : BaseTest
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Create a new application and delete it
|
||||
///</summary>
|
||||
[TestMethod()]
|
||||
[Test()]
|
||||
public void Application_Make_New()
|
||||
{
|
||||
var name = Guid.NewGuid().ToString("N");
|
||||
@@ -62,7 +35,7 @@ namespace Umbraco.LegacyTests
|
||||
/// Creates a new user, assigns the user to existing application,
|
||||
/// then deletes the user
|
||||
/// </summary>
|
||||
[TestMethod()]
|
||||
[Test()]
|
||||
public void Application_Create_New_User_Assign_Application_And_Delete_User()
|
||||
{
|
||||
var name = Guid.NewGuid().ToString("N");
|
||||
@@ -89,7 +62,7 @@ namespace Umbraco.LegacyTests
|
||||
/// <summary>
|
||||
/// create a new application and assigne an new user and deletes the application making sure the assignments are removed
|
||||
/// </summary>
|
||||
[TestMethod()]
|
||||
[Test()]
|
||||
public void Application_Make_New_Assign_User_And_Delete()
|
||||
{
|
||||
var name = Guid.NewGuid().ToString("N");
|
||||
@@ -107,13 +80,13 @@ namespace Umbraco.LegacyTests
|
||||
//assign the app
|
||||
user.addApplication(app.alias);
|
||||
//ensure it's added
|
||||
Assert.AreEqual<int>(1, user.Applications.Where(x => x.alias == app.alias).Count());
|
||||
Assert.AreEqual(1, user.Applications.Count(x => x.alias == app.alias));
|
||||
|
||||
//delete the app
|
||||
app.Delete();
|
||||
|
||||
//make sure the assigned applications are gone
|
||||
Assert.AreEqual<int>(0, user.Applications.Where(x => x.alias == name).Count());
|
||||
Assert.AreEqual(0, user.Applications.Count(x => x.alias == name));
|
||||
}
|
||||
|
||||
#region Tests to write
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using NUnit.Framework;
|
||||
using SqlCE4Umbraco;
|
||||
using umbraco.BusinessLogic;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.IO;
|
||||
using GlobalSettings = umbraco.GlobalSettings;
|
||||
|
||||
namespace Umbraco.Tests.BusinessLogic
|
||||
{
|
||||
[TestFixture]
|
||||
public abstract class BaseTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Removes any resources that were used for the test
|
||||
/// </summary>
|
||||
[TearDown]
|
||||
public void Dispose()
|
||||
{
|
||||
ClearDatabase();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures everything is setup to allow for unit tests to execute for each test
|
||||
/// </summary>
|
||||
[SetUp]
|
||||
public void Initialize()
|
||||
{
|
||||
InitializeDatabase();
|
||||
InitializeApps();
|
||||
InitializeAppConfigFile();
|
||||
InitializeTreeConfigFile();
|
||||
}
|
||||
|
||||
private void ClearDatabase()
|
||||
{
|
||||
var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN) as SqlCEHelper;
|
||||
if (dataHelper == null)
|
||||
throw new InvalidOperationException("The sql helper for unit tests must be of type SqlCEHelper, check the ensure the connection string used for this test is set to use SQLCE");
|
||||
dataHelper.ClearDatabase();
|
||||
}
|
||||
|
||||
private void InitializeDatabase()
|
||||
{
|
||||
ConfigurationManager.AppSettings.Set("umbracoDbDSN", @"datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf");
|
||||
|
||||
var dataHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
var installer = dataHelper.Utility.CreateInstaller();
|
||||
if (installer.CanConnect)
|
||||
{
|
||||
installer.Install();
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeApps()
|
||||
{
|
||||
Application.Apps = new List<Application>()
|
||||
{
|
||||
new Application("content", "content", "content", 0)
|
||||
};
|
||||
}
|
||||
|
||||
private void InitializeAppConfigFile()
|
||||
{
|
||||
Application.AppConfigFilePath = IOHelper.MapPath(SystemDirectories.Config + "/" + Application.AppConfigFileName, false);
|
||||
}
|
||||
|
||||
private void InitializeTreeConfigFile()
|
||||
{
|
||||
ApplicationTree.TreeConfigFilePath = IOHelper.MapPath(SystemDirectories.Config + "/" + ApplicationTree.TreeConfigFileName, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,11 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework">
|
||||
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
@@ -40,9 +44,32 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BusinessLogic\ApplicationTest.cs" />
|
||||
<Compile Include="BusinessLogic\BaseTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SQLCE4Umbraco\SqlCE4Umbraco.csproj">
|
||||
<Project>{5BA5425F-27A7-4677-865E-82246498AA2E}</Project>
|
||||
<Name>SqlCE4Umbraco</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\umbraco.businesslogic\umbraco.businesslogic.csproj">
|
||||
<Project>{E469A9CE-1BEC-423F-AC44-713CD72457EA}</Project>
|
||||
<Name>umbraco.businesslogic</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\umbraco.datalayer\umbraco.datalayer.csproj">
|
||||
<Project>{C7CB79F0-1C97-4B33-BFA7-00731B579AE2}</Project>
|
||||
<Name>umbraco.datalayer</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>xcopy "$(ProjectDir)"..\..\lib\SQLCE4\amd64\*.* "$(TargetDir)amd64\" /Y /F /E /D
|
||||
xcopy "$(ProjectDir)"..\..\lib\SQLCE4\x86\*.* "$(TargetDir)x86\" /Y /F /E /D</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
<!-- 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.
|
||||
<Target Name="BeforeBuild">
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="NUnit" version="2.6.0.12054" targetFramework="net40" />
|
||||
</packages>
|
||||
Binary file not shown.
Binary file not shown.
+10845
File diff suppressed because it is too large
Load Diff
+15
@@ -0,0 +1,15 @@
|
||||
Copyright © 2002-2012 Charlie Poole
|
||||
Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
|
||||
Copyright © 2000-2002 Philip A. Craig
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment (see the following) in the product documentation is required.
|
||||
|
||||
Portions Copyright © 2002-2012 Charlie Poole or Copyright © 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov or Copyright © 2000-2002 Philip A. Craig
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<repositories>
|
||||
<repository path="..\Umbraco.Tests\packages.config" />
|
||||
</repositories>
|
||||
@@ -9,8 +9,6 @@ using System.Web;
|
||||
using System.Xml.Linq;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.IO;
|
||||
using umbraco.interfaces;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using System.Runtime.CompilerServices;
|
||||
using umbraco.businesslogic;
|
||||
|
||||
@@ -23,12 +21,29 @@ namespace umbraco.BusinessLogic
|
||||
{
|
||||
private static ISqlHelper _sqlHelper;
|
||||
|
||||
private const string CACHE_KEY = "ApplicationCache";
|
||||
private const string CacheKey = "ApplicationCache";
|
||||
internal const string AppConfigFileName = "applications.config";
|
||||
private static string _appConfig;
|
||||
private static readonly object Locker = new object();
|
||||
|
||||
private static readonly string _appConfig =
|
||||
IOHelper.MapPath(SystemDirectories.Config + "/applications.config");
|
||||
|
||||
private static readonly object m_Locker = new object();
|
||||
/// <summary>
|
||||
/// gets/sets the application.config file path
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The setter is generally only going to be used in unit tests, otherwise it will attempt to resolve it using the IOHelper.MapPath
|
||||
/// </remarks>
|
||||
internal static string AppConfigFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_appConfig))
|
||||
{
|
||||
_appConfig = IOHelper.MapPath(SystemDirectories.Config + "/" + AppConfigFileName);
|
||||
}
|
||||
return _appConfig;
|
||||
}
|
||||
set { _appConfig = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cache storage for all applications
|
||||
@@ -36,15 +51,15 @@ namespace umbraco.BusinessLogic
|
||||
internal static List<Application> Apps
|
||||
{
|
||||
get
|
||||
{
|
||||
//ensure cache exists
|
||||
if (HttpRuntime.Cache[CACHE_KEY] == null)
|
||||
ReCache();
|
||||
return HttpRuntime.Cache[CACHE_KEY] as List<Application>;
|
||||
{
|
||||
//Whenever this is accessed, we need to ensure the cache exists!
|
||||
EnsureCache();
|
||||
|
||||
return HttpRuntime.Cache[CacheKey] as List<Application>;
|
||||
}
|
||||
set
|
||||
{
|
||||
HttpRuntime.Cache.Insert(CACHE_KEY, value);
|
||||
HttpRuntime.Cache.Insert(CacheKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,14 +89,7 @@ namespace umbraco.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A static constructor that will cache all application trees
|
||||
/// </summary>
|
||||
static Application()
|
||||
{
|
||||
//RegisterIApplications();
|
||||
Cache();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Application"/> class.
|
||||
@@ -273,22 +281,22 @@ namespace umbraco.BusinessLogic
|
||||
/// </summary>
|
||||
private static void ReCache()
|
||||
{
|
||||
HttpRuntime.Cache.Remove(CACHE_KEY);
|
||||
Cache();
|
||||
HttpRuntime.Cache.Remove(CacheKey);
|
||||
EnsureCache();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read all Application data and store it in cache.
|
||||
/// </summary>
|
||||
private static void Cache()
|
||||
private static void EnsureCache()
|
||||
{
|
||||
//don't query the database is the cache is not null
|
||||
if (HttpRuntime.Cache[CACHE_KEY] != null)
|
||||
if (HttpRuntime.Cache[CacheKey] != null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
List<Application> tmp = new List<Application>();
|
||||
var tmp = new List<Application>();
|
||||
|
||||
//using (IRecordsReader dr =
|
||||
// SqlHelper.ExecuteReader("Select appAlias, appIcon, appName from umbracoApp"))
|
||||
@@ -324,16 +332,18 @@ namespace umbraco.BusinessLogic
|
||||
//installer is run and there is no database or connection string defined.
|
||||
//the reason this method may get called during the installation is that the
|
||||
//SqlHelper of this class is shared amongst everything "Application" wide.
|
||||
|
||||
//TODO: Perhaps we should log something here??
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal static void LoadXml(Action<XDocument> callback, bool saveAfterCallback)
|
||||
{
|
||||
lock (m_Locker)
|
||||
lock (Locker)
|
||||
{
|
||||
var doc = File.Exists(_appConfig)
|
||||
? XDocument.Load(_appConfig)
|
||||
var doc = File.Exists(AppConfigFilePath)
|
||||
? XDocument.Load(AppConfigFilePath)
|
||||
: XDocument.Parse("<?xml version=\"1.0\"?><applications />");
|
||||
|
||||
if (doc.Root != null)
|
||||
@@ -342,7 +352,10 @@ namespace umbraco.BusinessLogic
|
||||
|
||||
if (saveAfterCallback)
|
||||
{
|
||||
doc.Save(_appConfig);
|
||||
//ensure the folder is created!
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(AppConfigFilePath));
|
||||
|
||||
doc.Save(AppConfigFilePath);
|
||||
|
||||
ReCache();
|
||||
}
|
||||
@@ -350,73 +363,4 @@ namespace umbraco.BusinessLogic
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum DefaultApps
|
||||
{
|
||||
content,
|
||||
media,
|
||||
users,
|
||||
settings,
|
||||
developer,
|
||||
member,
|
||||
translation
|
||||
}
|
||||
|
||||
public class ApplicationRegistrar : ApplicationStartupHandler
|
||||
{
|
||||
private ISqlHelper _sqlHelper;
|
||||
protected ISqlHelper SqlHelper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sqlHelper == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return _sqlHelper;
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationRegistrar()
|
||||
{
|
||||
// Load all Applications by attribute and add them to the XML config
|
||||
var types = TypeFinder.FindClassesOfType<IApplication>()
|
||||
.Where(x => x.GetCustomAttributes(typeof(ApplicationAttribute), false).Any());
|
||||
|
||||
var attrs = types.Select(x => (ApplicationAttribute)x.GetCustomAttributes(typeof(ApplicationAttribute), false).Single())
|
||||
.Where(x => Application.getByAlias(x.Alias) == null);
|
||||
|
||||
var allAliases = Application.getAll().Select(x => x.alias).Concat(attrs.Select(x => x.Alias));
|
||||
var inString = "'" + string.Join("','", allAliases) + "'";
|
||||
|
||||
Application.LoadXml(doc =>
|
||||
{
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("alias", attr.Alias),
|
||||
new XAttribute("name", attr.Name),
|
||||
new XAttribute("icon", attr.Icon),
|
||||
new XAttribute("sortOrder", attr.SortOrder)));
|
||||
}
|
||||
|
||||
var dbApps = SqlHelper.ExecuteReader("SELECT * FROM umbracoApp WHERE appAlias NOT IN ("+ inString +")");
|
||||
while (dbApps.Read())
|
||||
{
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("alias", dbApps.GetString("appAlias")),
|
||||
new XAttribute("name", dbApps.GetString("appName")),
|
||||
new XAttribute("icon", dbApps.GetString("appIcon")),
|
||||
new XAttribute("sortOrder", dbApps.GetByte("sortOrder"))));
|
||||
}
|
||||
|
||||
}, true);
|
||||
|
||||
//SqlHelper.ExecuteNonQuery("DELETE FROM umbracoApp");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.businesslogic;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace umbraco.BusinessLogic
|
||||
{
|
||||
public class ApplicationRegistrar : ApplicationStartupHandler
|
||||
{
|
||||
private ISqlHelper _sqlHelper;
|
||||
protected ISqlHelper SqlHelper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sqlHelper == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return _sqlHelper;
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationRegistrar()
|
||||
{
|
||||
// Load all Applications by attribute and add them to the XML config
|
||||
var types = TypeFinder.FindClassesOfType<IApplication>()
|
||||
.Where(x => x.GetCustomAttributes(typeof(ApplicationAttribute), false).Any());
|
||||
|
||||
var attrs = types.Select(x => (ApplicationAttribute)x.GetCustomAttributes(typeof(ApplicationAttribute), false).Single())
|
||||
.Where(x => Application.getByAlias(x.Alias) == null);
|
||||
|
||||
var allAliases = Application.getAll().Select(x => x.alias).Concat(attrs.Select(x => x.Alias));
|
||||
var inString = "'" + string.Join("','", allAliases) + "'";
|
||||
|
||||
Application.LoadXml(doc =>
|
||||
{
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("alias", attr.Alias),
|
||||
new XAttribute("name", attr.Name),
|
||||
new XAttribute("icon", attr.Icon),
|
||||
new XAttribute("sortOrder", attr.SortOrder)));
|
||||
}
|
||||
|
||||
var dbApps = SqlHelper.ExecuteReader("SELECT * FROM umbracoApp WHERE appAlias NOT IN ("+ inString +")");
|
||||
while (dbApps.Read())
|
||||
{
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("alias", dbApps.GetString("appAlias")),
|
||||
new XAttribute("name", dbApps.GetString("appName")),
|
||||
new XAttribute("icon", dbApps.GetString("appIcon")),
|
||||
new XAttribute("sortOrder", dbApps.GetByte("sortOrder"))));
|
||||
}
|
||||
|
||||
}, true);
|
||||
|
||||
//SqlHelper.ExecuteNonQuery("DELETE FROM umbracoApp");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Xml.Linq;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.IO;
|
||||
using umbraco.businesslogic;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace umbraco.BusinessLogic
|
||||
{
|
||||
@@ -21,12 +18,29 @@ namespace umbraco.BusinessLogic
|
||||
public class ApplicationTree
|
||||
{
|
||||
|
||||
private const string CACHE_KEY = "ApplicationTreeCache";
|
||||
private const string CacheKey = "ApplicationTreeCache";
|
||||
internal const string TreeConfigFileName = "trees.config";
|
||||
private static string _treeConfig;
|
||||
private static readonly object Locker = new object();
|
||||
|
||||
private static readonly string _appTreeConfig =
|
||||
IOHelper.MapPath(SystemDirectories.Config + "/trees.config");
|
||||
|
||||
private static readonly object m_Locker = new object();
|
||||
/// <summary>
|
||||
/// gets/sets the trees.config file path
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The setter is generally only going to be used in unit tests, otherwise it will attempt to resolve it using the IOHelper.MapPath
|
||||
/// </remarks>
|
||||
internal static string TreeConfigFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_treeConfig))
|
||||
{
|
||||
_treeConfig = IOHelper.MapPath(SystemDirectories.Config + "/" + TreeConfigFileName);
|
||||
}
|
||||
return _treeConfig;
|
||||
}
|
||||
set { _treeConfig = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cache storage for all application trees
|
||||
@@ -36,13 +50,12 @@ namespace umbraco.BusinessLogic
|
||||
get
|
||||
{
|
||||
//ensure cache exists
|
||||
if (HttpRuntime.Cache[CACHE_KEY] == null)
|
||||
ReCache();
|
||||
return HttpRuntime.Cache[CACHE_KEY] as List<ApplicationTree>;
|
||||
EnsureCache();
|
||||
return HttpRuntime.Cache[CacheKey] as List<ApplicationTree>;
|
||||
}
|
||||
set
|
||||
{
|
||||
HttpRuntime.Cache.Insert(CACHE_KEY, value);
|
||||
HttpRuntime.Cache.Insert(CacheKey, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,15 +186,7 @@ namespace umbraco.BusinessLogic
|
||||
{
|
||||
get { return _action; }
|
||||
set { _action = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A static constructor that will cache all application trees
|
||||
/// </summary>
|
||||
static ApplicationTree()
|
||||
{
|
||||
Cache();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ApplicationTree"/> class.
|
||||
@@ -393,23 +398,24 @@ namespace umbraco.BusinessLogic
|
||||
/// </summary>
|
||||
private static void ReCache()
|
||||
{
|
||||
HttpRuntime.Cache.Remove(CACHE_KEY);
|
||||
Cache();
|
||||
HttpRuntime.Cache.Remove(CacheKey);
|
||||
EnsureCache();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read all ApplicationTree data and store it in cache.
|
||||
/// </summary>
|
||||
private static void Cache()
|
||||
private static void EnsureCache()
|
||||
{
|
||||
//don't query the database if the cache is not null
|
||||
if (HttpRuntime.Cache[CACHE_KEY] == null)
|
||||
if (HttpRuntime.Cache[CacheKey] != null)
|
||||
return;
|
||||
|
||||
lock (Locker)
|
||||
{
|
||||
lock (m_Locker)
|
||||
if (HttpRuntime.Cache[CacheKey] == null)
|
||||
{
|
||||
if (HttpRuntime.Cache[CACHE_KEY] == null)
|
||||
{
|
||||
List<ApplicationTree> list = new List<ApplicationTree>();
|
||||
var list = new List<ApplicationTree>();
|
||||
|
||||
// using (IRecordsReader dr = SqlHelper.ExecuteReader(@"Select treeSilent, treeInitialize, treeSortOrder, appAlias, treeAlias, treeTitle, treeIconClosed,
|
||||
// treeIconOpen, treeHandlerAssembly, treeHandlerType, action from umbracoAppTree order by treeSortOrder"))
|
||||
@@ -433,42 +439,40 @@ namespace umbraco.BusinessLogic
|
||||
// }
|
||||
// }
|
||||
|
||||
LoadXml(doc =>
|
||||
LoadXml(doc =>
|
||||
{
|
||||
foreach (var addElement in doc.Root.Elements("add").OrderBy(x =>
|
||||
{
|
||||
var sortOrderAttr = x.Attribute("sortOrder");
|
||||
return sortOrderAttr != null ? Convert.ToInt32(sortOrderAttr.Value) : 0;
|
||||
}))
|
||||
{
|
||||
var sortOrderAttr = x.Attribute("sortOrder");
|
||||
return sortOrderAttr != null ? Convert.ToInt32(sortOrderAttr.Value) : 0;
|
||||
}))
|
||||
{
|
||||
list.Add(new ApplicationTree(
|
||||
addElement.Attribute("silent") != null ? Convert.ToBoolean(addElement.Attribute("silent").Value) : false,
|
||||
addElement.Attribute("initialize") != null ? Convert.ToBoolean(addElement.Attribute("initialize").Value) : true,
|
||||
addElement.Attribute("sortOrder") != null ? Convert.ToByte(addElement.Attribute("sortOrder").Value) : (byte)0,
|
||||
addElement.Attribute("application").Value,
|
||||
addElement.Attribute("alias").Value,
|
||||
addElement.Attribute("title").Value,
|
||||
addElement.Attribute("iconClosed").Value,
|
||||
addElement.Attribute("iconOpen").Value,
|
||||
addElement.Attribute("assembly").Value,
|
||||
addElement.Attribute("type").Value,
|
||||
addElement.Attribute("action") != null ? addElement.Attribute("action").Value : ""));
|
||||
addElement.Attribute("silent") != null ? Convert.ToBoolean(addElement.Attribute("silent").Value) : false,
|
||||
addElement.Attribute("initialize") != null ? Convert.ToBoolean(addElement.Attribute("initialize").Value) : true,
|
||||
addElement.Attribute("sortOrder") != null ? Convert.ToByte(addElement.Attribute("sortOrder").Value) : (byte)0,
|
||||
addElement.Attribute("application").Value,
|
||||
addElement.Attribute("alias").Value,
|
||||
addElement.Attribute("title").Value,
|
||||
addElement.Attribute("iconClosed").Value,
|
||||
addElement.Attribute("iconOpen").Value,
|
||||
addElement.Attribute("assembly").Value,
|
||||
addElement.Attribute("type").Value,
|
||||
addElement.Attribute("action") != null ? addElement.Attribute("action").Value : ""));
|
||||
}
|
||||
}, false);
|
||||
|
||||
AppTrees = list;
|
||||
}
|
||||
AppTrees = list;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal static void LoadXml(Action<XDocument> callback, bool saveAfterCallback)
|
||||
{
|
||||
lock (m_Locker)
|
||||
lock (Locker)
|
||||
{
|
||||
var doc = File.Exists(_appTreeConfig)
|
||||
? XDocument.Load(_appTreeConfig)
|
||||
var doc = File.Exists(TreeConfigFileName)
|
||||
? XDocument.Load(TreeConfigFileName)
|
||||
: XDocument.Parse("<?xml version=\"1.0\"?><trees />");
|
||||
if (doc.Root != null)
|
||||
{
|
||||
@@ -476,7 +480,7 @@ namespace umbraco.BusinessLogic
|
||||
|
||||
if (saveAfterCallback)
|
||||
{
|
||||
doc.Save(_appTreeConfig);
|
||||
doc.Save(TreeConfigFileName);
|
||||
|
||||
ReCache();
|
||||
}
|
||||
@@ -484,86 +488,4 @@ namespace umbraco.BusinessLogic
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ApplicationTreeRegistrar : ApplicationStartupHandler
|
||||
{
|
||||
private ISqlHelper _sqlHelper;
|
||||
protected ISqlHelper SqlHelper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sqlHelper == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return _sqlHelper;
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationTreeRegistrar()
|
||||
{
|
||||
// Load all Applications by attribute and add them to the XML config
|
||||
var types = TypeFinder.FindClassesOfType<ITree>()
|
||||
.Where(x => x.GetCustomAttributes(typeof(TreeAttribute), false).Any());
|
||||
|
||||
var items = types.Select(x => new Tuple<Type, TreeAttribute>(x,
|
||||
(TreeAttribute)x.GetCustomAttributes(typeof(TreeAttribute), false).Single()))
|
||||
.Where(x => ApplicationTree.getByAlias(x.Item2.Alias) == null);
|
||||
|
||||
var allAliases = ApplicationTree.getAll().Select(x => x.Alias).Concat(items.Select(x => x.Item2.Alias));
|
||||
var inString = "'" + string.Join("','", allAliases) + "'";
|
||||
|
||||
ApplicationTree.LoadXml(doc =>
|
||||
{
|
||||
foreach (var tuple in items)
|
||||
{
|
||||
var type = tuple.Item1;
|
||||
var attr = tuple.Item2;
|
||||
|
||||
var typeParts = type.AssemblyQualifiedName.Split(',');
|
||||
var assemblyName = typeParts[1].Trim();
|
||||
var typeName = typeParts[0].Substring(assemblyName.Length + 1).Trim();
|
||||
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("silent", attr.Silent),
|
||||
new XAttribute("initialize", attr.Initialize),
|
||||
new XAttribute("sortOrder", attr.SortOrder),
|
||||
new XAttribute("alias", attr.Alias),
|
||||
new XAttribute("application", attr.ApplicationAlias),
|
||||
new XAttribute("title", attr.Title),
|
||||
new XAttribute("iconClosed", attr.IconClosed),
|
||||
new XAttribute("iconOpen", attr.IconOpen),
|
||||
new XAttribute("assembly", assemblyName),
|
||||
new XAttribute("type", typeName),
|
||||
new XAttribute("action", attr.Action)));
|
||||
}
|
||||
|
||||
var dbTrees = SqlHelper.ExecuteReader("SELECT * FROM umbracoAppTree WHERE treeAlias NOT IN (" + inString + ")");
|
||||
while(dbTrees.Read())
|
||||
{
|
||||
var action = dbTrees.GetString("action");
|
||||
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("silent", dbTrees.GetBoolean("treeSilent")),
|
||||
new XAttribute("initialize", dbTrees.GetBoolean("treeInitialize")),
|
||||
new XAttribute("sortOrder", dbTrees.GetByte("treeSortOrder")),
|
||||
new XAttribute("alias", dbTrees.GetString("treeAlias")),
|
||||
new XAttribute("application", dbTrees.GetString("appAlias")),
|
||||
new XAttribute("title", dbTrees.GetString("treeTitle")),
|
||||
new XAttribute("iconClosed", dbTrees.GetString("treeIconClosed")),
|
||||
new XAttribute("iconOpen", dbTrees.GetString("treeIconOpen")),
|
||||
new XAttribute("assembly", dbTrees.GetString("treeHandlerAssembly")),
|
||||
new XAttribute("type", dbTrees.GetString("treeHandlerType")),
|
||||
new XAttribute("action", string.IsNullOrEmpty(action) ? "" : action)));
|
||||
}
|
||||
|
||||
}, true);
|
||||
|
||||
//SqlHelper.ExecuteNonQuery("DELETE FROM umbracoAppTree");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using umbraco.BusinessLogic.Utils;
|
||||
using umbraco.DataLayer;
|
||||
using umbraco.businesslogic;
|
||||
using umbraco.interfaces;
|
||||
|
||||
namespace umbraco.BusinessLogic
|
||||
{
|
||||
public class ApplicationTreeRegistrar : ApplicationStartupHandler
|
||||
{
|
||||
private ISqlHelper _sqlHelper;
|
||||
protected ISqlHelper SqlHelper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sqlHelper == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return _sqlHelper;
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationTreeRegistrar()
|
||||
{
|
||||
// Load all Applications by attribute and add them to the XML config
|
||||
var types = TypeFinder.FindClassesOfType<ITree>()
|
||||
.Where(x => x.GetCustomAttributes(typeof(TreeAttribute), false).Any());
|
||||
|
||||
var items = types.Select(x => new Tuple<Type, TreeAttribute>(x,
|
||||
(TreeAttribute)x.GetCustomAttributes(typeof(TreeAttribute), false).Single()))
|
||||
.Where(x => ApplicationTree.getByAlias(x.Item2.Alias) == null);
|
||||
|
||||
var allAliases = ApplicationTree.getAll().Select(x => x.Alias).Concat(items.Select(x => x.Item2.Alias));
|
||||
var inString = "'" + string.Join("','", allAliases) + "'";
|
||||
|
||||
ApplicationTree.LoadXml(doc =>
|
||||
{
|
||||
foreach (var tuple in items)
|
||||
{
|
||||
var type = tuple.Item1;
|
||||
var attr = tuple.Item2;
|
||||
|
||||
var typeParts = type.AssemblyQualifiedName.Split(',');
|
||||
var assemblyName = typeParts[1].Trim();
|
||||
var typeName = typeParts[0].Substring(assemblyName.Length + 1).Trim();
|
||||
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("silent", attr.Silent),
|
||||
new XAttribute("initialize", attr.Initialize),
|
||||
new XAttribute("sortOrder", attr.SortOrder),
|
||||
new XAttribute("alias", attr.Alias),
|
||||
new XAttribute("application", attr.ApplicationAlias),
|
||||
new XAttribute("title", attr.Title),
|
||||
new XAttribute("iconClosed", attr.IconClosed),
|
||||
new XAttribute("iconOpen", attr.IconOpen),
|
||||
new XAttribute("assembly", assemblyName),
|
||||
new XAttribute("type", typeName),
|
||||
new XAttribute("action", attr.Action)));
|
||||
}
|
||||
|
||||
var dbTrees = SqlHelper.ExecuteReader("SELECT * FROM umbracoAppTree WHERE treeAlias NOT IN (" + inString + ")");
|
||||
while(dbTrees.Read())
|
||||
{
|
||||
var action = dbTrees.GetString("action");
|
||||
|
||||
doc.Root.Add(new XElement("add",
|
||||
new XAttribute("silent", dbTrees.GetBoolean("treeSilent")),
|
||||
new XAttribute("initialize", dbTrees.GetBoolean("treeInitialize")),
|
||||
new XAttribute("sortOrder", dbTrees.GetByte("treeSortOrder")),
|
||||
new XAttribute("alias", dbTrees.GetString("treeAlias")),
|
||||
new XAttribute("application", dbTrees.GetString("appAlias")),
|
||||
new XAttribute("title", dbTrees.GetString("treeTitle")),
|
||||
new XAttribute("iconClosed", dbTrees.GetString("treeIconClosed")),
|
||||
new XAttribute("iconOpen", dbTrees.GetString("treeIconOpen")),
|
||||
new XAttribute("assembly", dbTrees.GetString("treeHandlerAssembly")),
|
||||
new XAttribute("type", dbTrees.GetString("treeHandlerType")),
|
||||
new XAttribute("action", string.IsNullOrEmpty(action) ? "" : action)));
|
||||
}
|
||||
|
||||
}, true);
|
||||
|
||||
//SqlHelper.ExecuteNonQuery("DELETE FROM umbracoAppTree");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace umbraco.BusinessLogic
|
||||
{
|
||||
public enum DefaultApps
|
||||
{
|
||||
content,
|
||||
media,
|
||||
users,
|
||||
settings,
|
||||
developer,
|
||||
member,
|
||||
translation
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,5 @@ using System.Runtime.CompilerServices;
|
||||
[assembly: AssemblyProduct("")]
|
||||
|
||||
|
||||
[assembly: InternalsVisibleTo("Umbraco.Tests")]
|
||||
[assembly: InternalsVisibleTo("Umbraco.LegacyTests")]
|
||||
@@ -140,8 +140,11 @@
|
||||
<Compile Include="ApplicationAttribute.cs" />
|
||||
<Compile Include="ApplicationBase.cs" />
|
||||
<Compile Include="ApplicationDefinitions.cs" />
|
||||
<Compile Include="ApplicationRegistrar.cs" />
|
||||
<Compile Include="ApplicationStartupHandler.cs" />
|
||||
<Compile Include="ApplicationTree.cs" />
|
||||
<Compile Include="ApplicationTreeRegistrar.cs" />
|
||||
<Compile Include="DefaultApps.cs" />
|
||||
<Compile Include="TreeAttribute.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
|
||||
@@ -121,7 +121,6 @@
|
||||
<Compile Include="..\..\src\SolutionInfo.cs">
|
||||
<Link>Properties\SolutionInfo.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="ApplicationTest.cs" />
|
||||
<Compile Include="ApplicationTreeTest.cs" />
|
||||
<Compile Include="DataTypeDefinitionTest.cs" />
|
||||
<Compile Include="DictionaryTest.cs" />
|
||||
|
||||
Reference in New Issue
Block a user