From 2967add41a0b978e4f467c4e625f0af2303e475d Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 7 Nov 2016 19:12:09 +0100 Subject: [PATCH] Keep structuring tests --- .../TestHelpers => Umbraco.Core}/Settable.cs | 4 +- src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../{ => CoreThings}/AttemptTests.cs | 49 +- .../{ => CoreThings}/CallContextTests.cs | 12 +- .../DelegateExtensionsTests.cs | 14 +- .../EnumerableExtensionsTests.cs | 391 ++++++------- .../{ => CoreThings}/ObjectExtensionsTests.cs | 381 ++++++------ .../{ => CoreThings}/TryConvertToTests.cs | 3 +- .../{ => CoreThings}/UriExtensionsTests.cs | 431 +++++++------- .../{ => CoreThings}/VersionExtensionTests.cs | 2 +- .../{ => CoreThings}/XmlExtensionsTests.cs | 2 +- .../ActionCollectionTests.cs | 2 +- .../CollectionBuildersTests.cs | 2 +- .../LazyCollectionBuilderTests.cs | 2 +- .../PackageActionCollectionTests.cs | 2 +- .../DependencyInjection/ResolverBaseTest.cs | 2 +- .../DependencyInjection/TempTests.cs | 21 - .../XsltExtensionsResolverTests.cs | 2 +- .../{ => Misc}/ApplicationUrlHelperTests.cs | 4 +- .../{ => Misc}/HashCodeCombinerTests.cs | 290 ++++----- src/Umbraco.Tests/{ => Misc}/LibraryTests.cs | 345 ++++++----- .../{ => Misc}/UriUtilityTests.cs | 275 +++++---- .../{ => Misc}/XmlHelperTests.cs | 548 +++++++++--------- src/Umbraco.Tests/Models/ContentTests.cs | 1 + src/Umbraco.Tests/Models/ContentTypeTests.cs | 1 + .../Persistence/DatabaseContextTests.cs | 1 + .../FaultHandling/ConnectionRetryTest.cs | 1 + src/Umbraco.Tests/Plugins/TypeFinderTests.cs | 1 + .../Runtimes/CoreRuntimeTests.cs | 1 + .../Services/ContentServicePerformanceTest.cs | 1 + .../Services/ContentServiceTests.cs | 1 + .../Services/PerformanceTests.cs | 1 + src/Umbraco.Tests/TestHelpers/BaseWebTest.cs | 2 +- .../TestHelpers/DatabaseBehavior.cs | 29 - .../Stubs/TestControllerFactory.cs | 3 +- .../{ => Stubs}/TestFacadeAccessor.cs | 4 +- .../TestIndexCollectionAccessor.cs | 10 +- ...hanceFinder.cs => TestLastChanceFinder.cs} | 22 +- .../TestHelpers/{ => Stubs}/TestProfiler.cs | 2 +- .../Stubs/TestUmbracoContextAccessor.cs | 9 + .../Stubs/TestUmbracoDatabaseAccessor.cs | 9 + .../TestHelpers/TestObjects-Mocks.cs | 1 + src/Umbraco.Tests/TestHelpers/TestObjects.cs | 1 + .../TestHelpers/TestUmbracoContextAccessor.cs | 9 - .../TestUmbracoDatabaseAccessor.cs | 9 - .../TestHelpers/TestWithApplicationBase.cs | 1 + .../TestHelpers/TestWithDatabaseBase.cs | 1 + .../TestHelpers/TestWithSettingsBase.cs | 1 + .../TestHelpers/UmbracoTestAttribute.cs | 1 + src/Umbraco.Tests/{ => Testing}/MockTests.cs | 201 ++++--- src/Umbraco.Tests/{ => Testing}/NUnitTests.cs | 2 +- src/Umbraco.Tests/Umbraco.Tests.csproj | 47 +- ...RenderIndexActionSelectorAttributeTests.cs | 1 + .../Web/Mvc/RenderModelBinderTests.cs | 1 + .../Web/Mvc/SurfaceControllerTests.cs | 1 + .../Web/WebExtensionMethodTests.cs | 1 + 56 files changed, 1541 insertions(+), 1621 deletions(-) rename src/{Umbraco.Tests/TestHelpers => Umbraco.Core}/Settable.cs (97%) rename src/Umbraco.Tests/{ => CoreThings}/AttemptTests.cs (86%) rename src/Umbraco.Tests/{ => CoreThings}/CallContextTests.cs (92%) rename src/Umbraco.Tests/{ => CoreThings}/DelegateExtensionsTests.cs (76%) rename src/Umbraco.Tests/{ => CoreThings}/EnumerableExtensionsTests.cs (62%) rename src/Umbraco.Tests/{ => CoreThings}/ObjectExtensionsTests.cs (82%) rename src/Umbraco.Tests/{ => CoreThings}/TryConvertToTests.cs (98%) rename src/Umbraco.Tests/{ => CoreThings}/UriExtensionsTests.cs (96%) rename src/Umbraco.Tests/{ => CoreThings}/VersionExtensionTests.cs (96%) rename src/Umbraco.Tests/{ => CoreThings}/XmlExtensionsTests.cs (97%) delete mode 100644 src/Umbraco.Tests/DependencyInjection/TempTests.cs rename src/Umbraco.Tests/{ => Misc}/ApplicationUrlHelperTests.cs (99%) rename src/Umbraco.Tests/{ => Misc}/HashCodeCombinerTests.cs (95%) rename src/Umbraco.Tests/{ => Misc}/LibraryTests.cs (93%) rename src/Umbraco.Tests/{ => Misc}/UriUtilityTests.cs (96%) rename src/Umbraco.Tests/{ => Misc}/XmlHelperTests.cs (96%) delete mode 100644 src/Umbraco.Tests/TestHelpers/DatabaseBehavior.cs rename src/Umbraco.Tests/TestHelpers/{ => Stubs}/TestFacadeAccessor.cs (50%) rename src/Umbraco.Tests/TestHelpers/{ => Stubs}/TestIndexCollectionAccessor.cs (50%) rename src/Umbraco.Tests/TestHelpers/Stubs/{FakeLastChanceFinder.cs => TestLastChanceFinder.cs} (69%) rename src/Umbraco.Tests/TestHelpers/{ => Stubs}/TestProfiler.cs (96%) create mode 100644 src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoContextAccessor.cs create mode 100644 src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoDatabaseAccessor.cs delete mode 100644 src/Umbraco.Tests/TestHelpers/TestUmbracoContextAccessor.cs delete mode 100644 src/Umbraco.Tests/TestHelpers/TestUmbracoDatabaseAccessor.cs rename src/Umbraco.Tests/{ => Testing}/MockTests.cs (92%) rename src/Umbraco.Tests/{ => Testing}/NUnitTests.cs (98%) diff --git a/src/Umbraco.Tests/TestHelpers/Settable.cs b/src/Umbraco.Core/Settable.cs similarity index 97% rename from src/Umbraco.Tests/TestHelpers/Settable.cs rename to src/Umbraco.Core/Settable.cs index 71c697f660..ba46543d4d 100644 --- a/src/Umbraco.Tests/TestHelpers/Settable.cs +++ b/src/Umbraco.Core/Settable.cs @@ -1,9 +1,7 @@ using System; -namespace Umbraco.Tests.TestHelpers +namespace Umbraco.Core { - // fixme - belongs to Core? - /// /// Represents a value that can be assigned a value. /// diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 85d09e3b0c..f7a7fb1b28 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -554,6 +554,7 @@ + diff --git a/src/Umbraco.Tests/AttemptTests.cs b/src/Umbraco.Tests/CoreThings/AttemptTests.cs similarity index 86% rename from src/Umbraco.Tests/AttemptTests.cs rename to src/Umbraco.Tests/CoreThings/AttemptTests.cs index 84d33c4351..a004095243 100644 --- a/src/Umbraco.Tests/AttemptTests.cs +++ b/src/Umbraco.Tests/CoreThings/AttemptTests.cs @@ -1,25 +1,24 @@ -using System; -using NUnit.Framework; -using Umbraco.Core; - -namespace Umbraco.Tests -{ - [TestFixture] - public class AttemptTests - { - - [Test] - public void AttemptIf() - { - // just making sure that it is ok to use TryParse as a condition - - int value; - var attempt = Attempt.If(int.TryParse("1234", out value), value); - Assert.IsTrue(attempt.Success); - Assert.AreEqual(1234, attempt.Result); - - attempt = Attempt.If(int.TryParse("12xxx34", out value), value); - Assert.IsFalse(attempt.Success); - } - } -} +using NUnit.Framework; +using Umbraco.Core; + +namespace Umbraco.Tests.CoreThings +{ + [TestFixture] + public class AttemptTests + { + + [Test] + public void AttemptIf() + { + // just making sure that it is ok to use TryParse as a condition + + int value; + var attempt = Attempt.If(int.TryParse("1234", out value), value); + Assert.IsTrue(attempt.Success); + Assert.AreEqual(1234, attempt.Result); + + attempt = Attempt.If(int.TryParse("12xxx34", out value), value); + Assert.IsFalse(attempt.Success); + } + } +} diff --git a/src/Umbraco.Tests/CallContextTests.cs b/src/Umbraco.Tests/CoreThings/CallContextTests.cs similarity index 92% rename from src/Umbraco.Tests/CallContextTests.cs rename to src/Umbraco.Tests/CoreThings/CallContextTests.cs index 0c2c6dd203..6e9aeb2ba9 100644 --- a/src/Umbraco.Tests/CallContextTests.cs +++ b/src/Umbraco.Tests/CoreThings/CallContextTests.cs @@ -2,7 +2,7 @@ using NUnit.Framework; using Umbraco.Core; -namespace Umbraco.Tests +namespace Umbraco.Tests.CoreThings { [TestFixture] public class CallContextTests @@ -35,11 +35,11 @@ namespace Umbraco.Tests SafeCallContext.Clear(); } - //[TearDown] - //public void TearDown() - //{ - // SafeCallContext.Clear(); - //} + [TearDown] + public void TearDown() + { + SafeCallContext.Clear(); + } [Test] public void Test1() diff --git a/src/Umbraco.Tests/DelegateExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/DelegateExtensionsTests.cs similarity index 76% rename from src/Umbraco.Tests/DelegateExtensionsTests.cs rename to src/Umbraco.Tests/CoreThings/DelegateExtensionsTests.cs index 511440c2c7..d7177f3e5a 100644 --- a/src/Umbraco.Tests/DelegateExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/DelegateExtensionsTests.cs @@ -3,16 +3,15 @@ using Lucene.Net.Index; using NUnit.Framework; using Umbraco.Core; -namespace Umbraco.Tests +namespace Umbraco.Tests.CoreThings { [TestFixture] public class DelegateExtensionsTests { - [Test] public void Only_Executes_Specific_Count() { - var maxTries = 5; + const int maxTries = 5; var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { @@ -26,21 +25,14 @@ namespace Umbraco.Tests [Test] public void Quits_On_Success_Count() { - var maxTries = 5; var totalTries = 0; DelegateExtensions.RetryUntilSuccessOrMaxAttempts((currentTry) => { totalTries = currentTry; - if (totalTries == 2) - { - return Attempt.Succeed(); - } - return Attempt.Fail(); + return totalTries == 2 ? Attempt.Succeed() : Attempt.Fail(); }, 5, TimeSpan.FromMilliseconds(10)); Assert.AreEqual(2, totalTries); } - - } } \ No newline at end of file diff --git a/src/Umbraco.Tests/EnumerableExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/EnumerableExtensionsTests.cs similarity index 62% rename from src/Umbraco.Tests/EnumerableExtensionsTests.cs rename to src/Umbraco.Tests/CoreThings/EnumerableExtensionsTests.cs index da69ab9d40..78afae719f 100644 --- a/src/Umbraco.Tests/EnumerableExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/EnumerableExtensionsTests.cs @@ -1,196 +1,197 @@ -using System.Collections.Generic; -using System.Linq; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Tests; -using umbraco.BusinessLogic; - -namespace Umbraco.Tests -{ - [TestFixture] - public class EnumerableExtensionsTests - { - - [Test] - public void Unsorted_Sequence_Equal() - { - var list1 = new[] { 1, 2, 3, 4, 5, 6 }; - var list2 = new[] { 6, 5, 3, 2, 1, 4 }; - var list3 = new[] { 6, 5, 4, 3, 2, 2 }; - - Assert.IsTrue(list1.UnsortedSequenceEqual(list2)); - Assert.IsTrue(list2.UnsortedSequenceEqual(list1)); - Assert.IsFalse(list1.UnsortedSequenceEqual(list3)); - - Assert.IsTrue(((IEnumerable)null).UnsortedSequenceEqual(null)); - Assert.IsFalse(((IEnumerable)null).UnsortedSequenceEqual(list1)); - Assert.IsFalse(list1.UnsortedSequenceEqual(null)); - } - - [Test] - public void Contains_All() - { - var list1 = new[] {1, 2, 3, 4, 5, 6}; - var list2 = new[] {6, 5, 3, 2, 1, 4}; - var list3 = new[] {6, 5, 4, 3}; - - Assert.IsTrue(list1.ContainsAll(list2)); - Assert.IsTrue(list2.ContainsAll(list1)); - Assert.IsTrue(list1.ContainsAll(list3)); - Assert.IsFalse(list3.ContainsAll(list1)); - } - - [Test] - public void Flatten_List_2() - { - var hierarchy = new TestItem() - { - Children = new List() - { - new TestItem(), - new TestItem(), - new TestItem() - } - }; - - var flattened = hierarchy.Children.FlattenList(x => x.Children); - var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children); - - Assert.AreEqual(3, flattened.Count()); - Assert.AreEqual(3, selectRecursive.Count()); - - Assert.IsTrue(flattened.SequenceEqual(selectRecursive)); - } - - [Test] - public void Flatten_List() - { - var hierarchy = new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - { - new TestItem(), - new TestItem() - } - } - } - } - } - }, - new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - } - } - }, - new TestItem() - { - Children = new List() - { - new TestItem() - { - Children = new List() - } - } - } - } - }, - } - }; - - var flattened = hierarchy.Children.FlattenList(x => x.Children); - var selectRecursive = hierarchy.Children.FlattenList(x => x.Children); - - Assert.AreEqual(10, flattened.Count()); - Assert.AreEqual(10, selectRecursive.Count()); - - Assert.IsTrue(flattened.SequenceEqual(selectRecursive)); - } - - private class TestItem - { - public TestItem() - { - Children = Enumerable.Empty(); - } - public IEnumerable Children { get; set; } - } - - [Test] - public void InGroupsOf_ReturnsAllElements() - { - var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; - - var groupsOfTwo = integers.InGroupsOf(2).ToArray(); - - var flattened = groupsOfTwo.SelectMany(x => x).ToArray(); - - Assert.That(groupsOfTwo.Length, Is.EqualTo(5)); - Assert.That(flattened.Length, Is.EqualTo(integers.Length)); - CollectionAssert.AreEquivalent(integers, flattened); - - var groupsOfMassive = integers.InGroupsOf(100).ToArray(); - Assert.That(groupsOfMassive.Length, Is.EqualTo(1)); - flattened = groupsOfMassive.SelectMany(x => x).ToArray(); - Assert.That(flattened.Length, Is.EqualTo(integers.Length)); - CollectionAssert.AreEquivalent(integers, flattened); - } - - [Test] - public void InGroupsOf_CanRepeat() - { - var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; - var inGroupsOf = integers.InGroupsOf(2); - Assert.AreEqual(5, inGroupsOf.Count()); - Assert.AreEqual(5, inGroupsOf.Count()); // again - } - - [TestCase] - public void DistinctBy_ReturnsDistinctElements_AndResetsIteratorCorrectly() - { - // Arrange - var tuple1 = new System.Tuple("fruit", "apple"); - var tuple2 = new System.Tuple("fruit", "orange"); - var tuple3 = new System.Tuple("fruit", "banana"); - var tuple4 = new System.Tuple("fruit", "banana"); // Should be filtered out - var list = new List>() - { - tuple1, - tuple2, - tuple3, - tuple4 - }; - - // Act - var iteratorSource = list.DistinctBy(x => x.Item2); - - // Assert - // First check distinction - Assert.AreEqual(3, iteratorSource.Count()); - - // Check for iterator block mistakes - reset to original query first - iteratorSource = list.DistinctBy(x => x.Item2); - Assert.AreEqual(iteratorSource.Count(), iteratorSource.ToList().Count()); - } - } +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; +using Umbraco.Core; + +namespace Umbraco.Tests.CoreThings +{ + [TestFixture] + public class EnumerableExtensionsTests + { + [Test] + public void Unsorted_Sequence_Equal() + { + var list1 = new[] { 1, 2, 3, 4, 5, 6 }; + var list2 = new[] { 6, 5, 3, 2, 1, 4 }; + var list3 = new[] { 6, 5, 4, 3, 2, 2 }; + + Assert.IsTrue(list1.UnsortedSequenceEqual(list2)); + Assert.IsTrue(list2.UnsortedSequenceEqual(list1)); + Assert.IsFalse(list1.UnsortedSequenceEqual(list3)); + + Assert.IsTrue(((IEnumerable)null).UnsortedSequenceEqual(null)); + Assert.IsFalse(((IEnumerable)null).UnsortedSequenceEqual(list1)); + Assert.IsFalse(list1.UnsortedSequenceEqual(null)); + } + + [Test] + public void Contains_All() + { + var list1 = new[] {1, 2, 3, 4, 5, 6}; + var list2 = new[] {6, 5, 3, 2, 1, 4}; + var list3 = new[] {6, 5, 4, 3}; + + Assert.IsTrue(list1.ContainsAll(list2)); + Assert.IsTrue(list2.ContainsAll(list1)); + Assert.IsTrue(list1.ContainsAll(list3)); + Assert.IsFalse(list3.ContainsAll(list1)); + } + + [Test] + public void Flatten_List_2() + { + var hierarchy = new TestItem + { + Children = new List + { + new TestItem(), + new TestItem(), + new TestItem() + } + }; + +#pragma warning disable CS0618 // Type or member is obsolete + var flattened = hierarchy.Children.FlattenList(x => x.Children); +#pragma warning restore CS0618 // Type or member is obsolete + var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children); + + Assert.AreEqual(3, flattened.Count()); + Assert.AreEqual(3, selectRecursive.Count()); + + Assert.IsTrue(flattened.SequenceEqual(selectRecursive)); + } + + [Test] + public void Flatten_List() + { + var hierarchy = new TestItem + { + Children = new List + { + new TestItem + { + Children = new List + { + new TestItem + { + Children = new List + { + new TestItem + { + Children = new List + { + new TestItem(), + new TestItem() + } + } + } + } + } + }, + new TestItem + { + Children = new List + { + new TestItem + { + Children = new List + { + new TestItem + { + Children = new List() + } + } + }, + new TestItem + { + Children = new List + { + new TestItem + { + Children = new List() + } + } + } + } + } + } + }; + +#pragma warning disable CS0618 // Type or member is obsolete + var flattened = hierarchy.Children.FlattenList(x => x.Children); +#pragma warning restore CS0618 // Type or member is obsolete + var selectRecursive = hierarchy.Children.SelectRecursive(x => x.Children); + + Assert.AreEqual(10, flattened.Count()); + Assert.AreEqual(10, selectRecursive.Count()); + + Assert.IsTrue(flattened.SequenceEqual(selectRecursive)); + } + + private class TestItem + { + public TestItem() + { + Children = Enumerable.Empty(); + } + public IEnumerable Children { get; set; } + } + + [Test] + public void InGroupsOf_ReturnsAllElements() + { + var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + + var groupsOfTwo = integers.InGroupsOf(2).ToArray(); + + var flattened = groupsOfTwo.SelectMany(x => x).ToArray(); + + Assert.That(groupsOfTwo.Length, Is.EqualTo(5)); + Assert.That(flattened.Length, Is.EqualTo(integers.Length)); + CollectionAssert.AreEquivalent(integers, flattened); + + var groupsOfMassive = integers.InGroupsOf(100).ToArray(); + Assert.That(groupsOfMassive.Length, Is.EqualTo(1)); + flattened = groupsOfMassive.SelectMany(x => x).ToArray(); + Assert.That(flattened.Length, Is.EqualTo(integers.Length)); + CollectionAssert.AreEquivalent(integers, flattened); + } + + [Test] + public void InGroupsOf_CanRepeat() + { + var integers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; + var inGroupsOf = integers.InGroupsOf(2); + Assert.AreEqual(5, inGroupsOf.Count()); + Assert.AreEqual(5, inGroupsOf.Count()); // again + } + + [TestCase] + public void DistinctBy_ReturnsDistinctElements_AndResetsIteratorCorrectly() + { + // Arrange + var tuple1 = new System.Tuple("fruit", "apple"); + var tuple2 = new System.Tuple("fruit", "orange"); + var tuple3 = new System.Tuple("fruit", "banana"); + var tuple4 = new System.Tuple("fruit", "banana"); // Should be filtered out + var list = new List>() + { + tuple1, + tuple2, + tuple3, + tuple4 + }; + + // Act + var iteratorSource = list.DistinctBy(x => x.Item2); + + // Assert + // First check distinction + Assert.AreEqual(3, iteratorSource.Count()); + + // Check for iterator block mistakes - reset to original query first + iteratorSource = list.DistinctBy(x => x.Item2); + Assert.AreEqual(iteratorSource.Count(), iteratorSource.ToList().Count()); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/ObjectExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/ObjectExtensionsTests.cs similarity index 82% rename from src/Umbraco.Tests/ObjectExtensionsTests.cs rename to src/Umbraco.Tests/CoreThings/ObjectExtensionsTests.cs index c9d90e342b..403dd421ad 100644 --- a/src/Umbraco.Tests/ObjectExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/ObjectExtensionsTests.cs @@ -1,198 +1,185 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading; -using System.Web.UI.WebControls; -using NUnit.Framework; -using Umbraco.Core; - -namespace Umbraco.Tests -{ - [TestFixture] - public class ObjectExtensionsTests - { - [TestFixtureSetUp] - public void FixtureSetup() - { - } - - [Test] - public void CanParseStringToUnit() - { - var stringUnit = "1234px"; - object objUnit = "1234px"; - var result = stringUnit.TryConvertTo(); - var result2 = objUnit.TryConvertTo(); - var unit = new Unit("1234px"); - Assert.IsTrue(result.Success); - Assert.IsTrue(result2.Success); - Assert.AreEqual(unit, result.Result); - Assert.AreEqual(unit, result2.Result); - } - - [Test] - public void Can_Convert_List_To_Enumerable() - { - var list = new List() {"hello", "world", "awesome"}; - var result = list.TryConvertTo>(); - Assert.IsTrue(result.Success); - Assert.AreEqual(3, result.Result.Count()); - } - - [Test] - public void ObjectExtensions_Object_To_Dictionary() - { - //Arrange - - var obj = new { Key1 = "value1", Key2 = "value2", Key3 = "value3" }; - - //Act - - var d = obj.ToDictionary(); - - //Assert - - Assert.IsTrue(d.Keys.Contains("Key1")); - Assert.IsTrue(d.Keys.Contains("Key2")); - Assert.IsTrue(d.Keys.Contains("Key3")); - Assert.AreEqual(d["Key1"], "value1"); - Assert.AreEqual(d["Key2"], "value2"); - Assert.AreEqual(d["Key3"], "value3"); - } - - [Test] - public void CanConvertIntToNullableInt() - { - var i = 1; - var result = i.TryConvertTo(); - Assert.That(result.Success, Is.True); - } - - [Test] - public void CanConvertNullableIntToInt() - { - int? i = 1; - var result = i.TryConvertTo(); - Assert.That(result.Success, Is.True); - } - - [Test] - public virtual void CanConvertStringToBool() - { - var testCases = new Dictionary - { - {"TRUE", true}, - {"True", true}, - {"true", true}, - {"1", true}, - {"FALSE", false}, - {"False", false}, - {"false", false}, - {"0", false}, - {"", false} - }; - - foreach (var testCase in testCases) - { - var result = testCase.Key.TryConvertTo(); - - Assert.IsTrue(result.Success, testCase.Key); - Assert.AreEqual(testCase.Value, result.Result, testCase.Key); - } - } - - [TestCase("2012-11-10", true)] - [TestCase("2012/11/10", true)] - [TestCase("10/11/2012", true)]// assuming your culture uses DD/MM/YYYY - [TestCase("11/10/2012", false)]// assuming your culture uses DD/MM/YYYY - [TestCase("Sat 10, Nov 2012", true)] - [TestCase("Saturday 10, Nov 2012", true)] - [TestCase("Sat 10, November 2012", true)] - [TestCase("Saturday 10, November 2012", true)] - [TestCase("2012-11-10 13:14:15", true)] - [TestCase("2012-11-10T13:14:15Z", true)] - public virtual void CanConvertStringToDateTime(string date, bool outcome) - { - var dateTime = new DateTime(2012, 11, 10, 13, 14, 15); - - var result = date.TryConvertTo(); - - Assert.IsTrue(result.Success, date); - Assert.AreEqual(DateTime.Equals(dateTime.Date, result.Result.Date), outcome, date); - } - - [Test] - public virtual void CanConvertBlankStringToNullDateTime() - { - var result = "".TryConvertTo(); - Assert.IsTrue(result.Success); - Assert.IsNull(result.Result); - } - - [Test] - public virtual void CanConvertBlankStringToNullBool() - { - var result = "".TryConvertTo(); - Assert.IsTrue(result.Success); - Assert.IsNull(result.Result); - } - - [Test] - public virtual void CanConvertBlankStringToDateTime() - { - var result = "".TryConvertTo(); - Assert.IsTrue(result.Success); - Assert.AreEqual(DateTime.MinValue, result.Result); - } - - [Test] - public virtual void CanConvertObjectToString_Using_ToString_Overload() - { - var result = new MyTestObject().TryConvertTo(); - - Assert.IsTrue(result.Success); - Assert.AreEqual("Hello world", result.Result); - } - - [Test] - public virtual void CanConvertObjectToSameObject() - { - var obj = new MyTestObject(); - var result = obj.TryConvertTo(); - - Assert.AreEqual(obj, result.Result); - } - - private CultureInfo savedCulture; - - /// - /// Run once before each test in derived test fixtures. - /// - [SetUp] - public void TestSetup() - { - savedCulture = Thread.CurrentThread.CurrentCulture; - Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // make sure the dates parse correctly - return; - } - - /// - /// Run once after each test in derived test fixtures. - /// - [TearDown] - public void TestTearDown() - { - Thread.CurrentThread.CurrentCulture = savedCulture; - return; - } - - private class MyTestObject - { - public override string ToString() - { - return "Hello world"; - } - } - } +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Web.UI.WebControls; +using NUnit.Framework; +using Umbraco.Core; + +namespace Umbraco.Tests.CoreThings +{ + [TestFixture] + public class ObjectExtensionsTests + { + private CultureInfo _savedCulture; + + [SetUp] + public void TestSetup() + { + _savedCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // make sure the dates parse correctly + } + + [TearDown] + public void TestTearDown() + { + Thread.CurrentThread.CurrentCulture = _savedCulture; + } + + [Test] + public void CanParseStringToUnit() + { + const string stringUnit = "1234px"; + object objUnit = "1234px"; + var result = stringUnit.TryConvertTo(); + var result2 = objUnit.TryConvertTo(); + var unit = new Unit("1234px"); + Assert.IsTrue(result.Success); + Assert.IsTrue(result2.Success); + Assert.AreEqual(unit, result.Result); + Assert.AreEqual(unit, result2.Result); + } + + [Test] + public void Can_Convert_List_To_Enumerable() + { + var list = new List {"hello", "world", "awesome"}; + var result = list.TryConvertTo>(); + Assert.IsTrue(result.Success); + Assert.AreEqual(3, result.Result.Count()); + } + + [Test] + public void ObjectExtensions_Object_To_Dictionary() + { + //Arrange + + var obj = new { Key1 = "value1", Key2 = "value2", Key3 = "value3" }; + + //Act + + var d = obj.ToDictionary(); + + //Assert + + Assert.IsTrue(d.Keys.Contains("Key1")); + Assert.IsTrue(d.Keys.Contains("Key2")); + Assert.IsTrue(d.Keys.Contains("Key3")); + Assert.AreEqual(d["Key1"], "value1"); + Assert.AreEqual(d["Key2"], "value2"); + Assert.AreEqual(d["Key3"], "value3"); + } + + [Test] + public void CanConvertIntToNullableInt() + { + var i = 1; + var result = i.TryConvertTo(); + Assert.That(result.Success, Is.True); + } + + [Test] + public void CanConvertNullableIntToInt() + { + int? i = 1; + var result = i.TryConvertTo(); + Assert.That(result.Success, Is.True); + } + + [Test] + public virtual void CanConvertStringToBool() + { + var testCases = new Dictionary + { + {"TRUE", true}, + {"True", true}, + {"true", true}, + {"1", true}, + {"FALSE", false}, + {"False", false}, + {"false", false}, + {"0", false}, + {"", false} + }; + + foreach (var testCase in testCases) + { + var result = testCase.Key.TryConvertTo(); + + Assert.IsTrue(result.Success, testCase.Key); + Assert.AreEqual(testCase.Value, result.Result, testCase.Key); + } + } + + [TestCase("2012-11-10", true)] + [TestCase("2012/11/10", true)] + [TestCase("10/11/2012", true)]// assuming your culture uses DD/MM/YYYY + [TestCase("11/10/2012", false)]// assuming your culture uses DD/MM/YYYY + [TestCase("Sat 10, Nov 2012", true)] + [TestCase("Saturday 10, Nov 2012", true)] + [TestCase("Sat 10, November 2012", true)] + [TestCase("Saturday 10, November 2012", true)] + [TestCase("2012-11-10 13:14:15", true)] + [TestCase("2012-11-10T13:14:15Z", true)] + public virtual void CanConvertStringToDateTime(string date, bool outcome) + { + var dateTime = new DateTime(2012, 11, 10, 13, 14, 15); + + var result = date.TryConvertTo(); + + Assert.IsTrue(result.Success, date); + Assert.AreEqual(DateTime.Equals(dateTime.Date, result.Result.Date), outcome, date); + } + + [Test] + public virtual void CanConvertBlankStringToNullDateTime() + { + var result = "".TryConvertTo(); + Assert.IsTrue(result.Success); + Assert.IsNull(result.Result); + } + + [Test] + public virtual void CanConvertBlankStringToNullBool() + { + var result = "".TryConvertTo(); + Assert.IsTrue(result.Success); + Assert.IsNull(result.Result); + } + + [Test] + public virtual void CanConvertBlankStringToDateTime() + { + var result = "".TryConvertTo(); + Assert.IsTrue(result.Success); + Assert.AreEqual(DateTime.MinValue, result.Result); + } + + [Test] + public virtual void CanConvertObjectToString_Using_ToString_Overload() + { + var result = new MyTestObject().TryConvertTo(); + + Assert.IsTrue(result.Success); + Assert.AreEqual("Hello world", result.Result); + } + + [Test] + public virtual void CanConvertObjectToSameObject() + { + var obj = new MyTestObject(); + var result = obj.TryConvertTo(); + + Assert.AreEqual(obj, result.Result); + } + + private class MyTestObject + { + public override string ToString() + { + return "Hello world"; + } + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/TryConvertToTests.cs b/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs similarity index 98% rename from src/Umbraco.Tests/TryConvertToTests.cs rename to src/Umbraco.Tests/CoreThings/TryConvertToTests.cs index 6f1e876c54..0a992db4f6 100644 --- a/src/Umbraco.Tests/TryConvertToTests.cs +++ b/src/Umbraco.Tests/CoreThings/TryConvertToTests.cs @@ -1,12 +1,11 @@ using System; -using LightInject; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Strings; using Umbraco.Tests.TestHelpers; using Umbraco.Core.DI; -namespace Umbraco.Tests +namespace Umbraco.Tests.CoreThings { [TestFixture] public class TryConvertToTests : UmbracoTestBase diff --git a/src/Umbraco.Tests/UriExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs similarity index 96% rename from src/Umbraco.Tests/UriExtensionsTests.cs rename to src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs index 91f727f5f9..2d97faca81 100644 --- a/src/Umbraco.Tests/UriExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/UriExtensionsTests.cs @@ -1,213 +1,218 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.IO; - -namespace Umbraco.Tests -{ - [TestFixture] - public class UriExtensionsTests - { - [TearDown] - public void TearDown() - { - SystemDirectories.Root = ""; - } - - [TestCase("http://www.domain.com/umbraco", "", true)] - [TestCase("http://www.domain.com/Umbraco/", "", true)] - [TestCase("http://www.domain.com/umbraco/default.aspx", "", true)] - [TestCase("http://www.domain.com/umbraco/test/test", "", false)] - [TestCase("http://www.domain.com/umbraco/test/test/test", "", false)] - [TestCase("http://www.domain.com/Umbraco/test/test.aspx", "", true)] - [TestCase("http://www.domain.com/umbraco/test/test.js", "", true)] - [TestCase("http://www.domain.com/umbrac", "", false)] - [TestCase("http://www.domain.com/test", "", false)] - [TestCase("http://www.domain.com/test/umbraco", "", false)] - [TestCase("http://www.domain.com/test/umbraco.aspx", "", false)] - [TestCase("http://www.domain.com/Umbraco/restServices/blah", "", true)] - [TestCase("http://www.domain.com/Umbraco/Backoffice/blah", "", true)] - [TestCase("http://www.domain.com/Umbraco/anything", "", true)] - [TestCase("http://www.domain.com/Umbraco/anything/", "", true)] - [TestCase("http://www.domain.com/Umbraco/surface/blah", "", false)] - [TestCase("http://www.domain.com/umbraco/api/blah", "", false)] - [TestCase("http://www.domain.com/myvdir/umbraco/api/blah", "myvdir", false)] - [TestCase("http://www.domain.com/MyVdir/umbraco/api/blah", "/myvdir", false)] - [TestCase("http://www.domain.com/MyVdir/Umbraco/", "myvdir", true)] - [TestCase("http://www.domain.com/MyVdir/Umbraco/restServices/blah", "/myvdir", true)] - [TestCase("http://www.domain.com/umbraco/webservices/legacyAjaxCalls.asmx/js", "", true)] - [TestCase("http://www.domain.com/umbraco/test/legacyAjaxCalls.ashx?some=query&blah=js", "", true)] - public void Is_Back_Office_Request(string input, string virtualPath, bool expected) - { - SystemDirectories.Root = virtualPath; - - var source = new Uri(input); - Assert.AreEqual(expected, source.IsBackOfficeRequest(virtualPath)); - } - - [TestCase("http://www.domain.com/install", true)] - [TestCase("http://www.domain.com/Install/", true)] - [TestCase("http://www.domain.com/install/default.aspx", true)] - [TestCase("http://www.domain.com/install/test/test", true)] - [TestCase("http://www.domain.com/Install/test/test.aspx", true)] - [TestCase("http://www.domain.com/install/test/test.js", true)] - [TestCase("http://www.domain.com/instal", false)] - [TestCase("http://www.domain.com/umbraco", false)] - [TestCase("http://www.domain.com/umbraco/umbraco", false)] - [TestCase("http://www.domain.com/test/umbraco.aspx", false)] - public void Is_Installer_Request(string input, bool expected) - { - var source = new Uri(input); - Assert.AreEqual(expected, source.IsInstallerRequest()); - } - - [TestCase("http://www.domain.com/foo/bar", "/", "http://www.domain.com/")] - [TestCase("http://www.domain.com/foo/bar#hop", "/", "http://www.domain.com/")] - [TestCase("http://www.domain.com/foo/bar?q=2#hop", "/", "http://www.domain.com/?q=2")] - [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/foo/bar", "/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com/", "/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com", "/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com/foo?q=3", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")] - [TestCase("http://www.domain.com/foo#bang", "/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com/foo?q=3#bang", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")] - public void RewritePath(string input, string path, string expected) - { - var source = new Uri(input); - var output = source.Rewrite(path); - Assert.AreEqual(expected, output.ToString()); - } - - [TestCase("http://www.domain.com/", "path/to/page/", typeof(ArgumentException))] - [TestCase("http://www.domain.com", "path/to/page/", typeof(ArgumentException))] - public void RewritePath_Exceptions(string input, string path, Type exception) - { - var source = new Uri(input); - Assert.Throws(exception, () => - { - var output = source.Rewrite(path); - }); - } - - [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] - [TestCase("http://www.domain.com/foo/bar#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] - [TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] - [TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] - public void RewritePathAndQuery(string input, string path, string query, string expected) - { - var source = new Uri(input); - var output = source.Rewrite(path, query); - Assert.AreEqual(expected, output.ToString()); - } - - [TestCase("http://www.domain.com/", "path/to/page/", "", typeof(ArgumentException))] - [TestCase("http://www.domain.com/", "/path/to/page/", "x=27", typeof(ArgumentException))] - public void RewritePathAndQuery_Exceptions(string input, string path, string query, Type exception) - { - var source = new Uri(input); - Assert.Throws(exception, () => - { - var output = source.Rewrite(path, query); - }); - } - - [TestCase("http://www.domain.com", "/")] - [TestCase("http://www.domain.com/", "/")] - [TestCase("http://www.domain.com/foo", "/foo")] - [TestCase("http://www.domain.com/foo/", "/foo/")] - [TestCase("http://www.domain.com/foo/bar", "/foo/bar")] - [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar%20nix")] - [TestCase("http://www.domain.com/foo/bar?q=7#hop", "/foo/bar")] - [TestCase("/", "/")] - [TestCase("/foo", "/foo")] - [TestCase("/foo/", "/foo/")] - [TestCase("/foo/bar", "/foo/bar")] - [TestCase("/foo/bar?q=7#hop", "/foo/bar")] - [TestCase("/foo%20bar/pof", "/foo%20bar/pof")] - public void GetSafeAbsolutePath(string input, string expected) - { - var source = new Uri(input, UriKind.RelativeOrAbsolute); - var output = source.GetSafeAbsolutePath(); - Assert.AreEqual(expected, output); - } - - [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")] - public void GetAbsolutePathDecoded(string input, string expected) - { - var source = new Uri(input, UriKind.RelativeOrAbsolute); - var output = source.GetAbsolutePathDecoded(); - Assert.AreEqual(expected, output); - } - - [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")] - [TestCase("/foo%20bar/pof", "/foo bar/pof")] - public void GetSafeAbsolutePathDecoded(string input, string expected) - { - var source = new Uri(input, UriKind.RelativeOrAbsolute); - var output = source.GetSafeAbsolutePathDecoded(); - Assert.AreEqual(expected, output); - } - - [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to/")] - [TestCase("http://www.domain.com/", "http://www.domain.com/")] - [TestCase("http://www.domain.com", "http://www.domain.com/")] - [TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to/?q=3")] - public void EndPathWithSlash(string input, string expected) - { - var source = new Uri(input); - var output = source.EndPathWithSlash(); - Assert.AreEqual(expected, output.ToString()); - } - - [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to")] - [TestCase("http://www.domain.com/", "http://www.domain.com/")] - [TestCase("http://www.domain.com", "http://www.domain.com/")] - [TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to?q=3")] - public void TrimPathEndSlash(string input, string expected) - { - var source = new Uri(input); - var output = source.TrimPathEndSlash(); - Assert.AreEqual(expected, output.ToString()); - } - - [TestCase("/foo/bar", "http://www.domain.com", "http://www.domain.com/foo/bar")] - [TestCase("/foo/bar", "http://www.domain.com/dang/dang", "http://www.domain.com/foo/bar")] - [TestCase("/", "http://www.domain.com/dang/dang", "http://www.domain.com/")] - [TestCase("/foo/bar", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar")] - [TestCase("/foo/bar?k=6#yop", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar?k=6")] - public void MakeAbsolute(string input, string reference, string expected) - { - var source = new Uri(input, UriKind.Relative); - var absolute = new Uri(reference); - var output = source.MakeAbsolute(absolute); - Assert.AreEqual(expected, output.ToString()); - } - - [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com", "http://www.domain.com/")] - [TestCase("http://www.domain.com/", "http://www.domain.com/")] - [TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")] - [TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")] - [TestCase("http://www.domain.com:666/path/to/page", "http://www.domain.com/path/to/page")] - [TestCase("http://www.domain.com:666/path/to/page/", "http://www.domain.com/path/to/page/")] - [TestCase("http://www.domain.com:666", "http://www.domain.com/")] - [TestCase("http://www.domain.com:666/", "http://www.domain.com/")] - [TestCase("http://www.domain.com:666/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")] - [TestCase("http://www.domain.com:666/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")] - public void WithoutPort(string input, string expected) - { - var source = new Uri(input); - var output = source.WithoutPort(); - Assert.AreEqual(expected, output.ToString()); - } - } -} +using System; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.IO; + +namespace Umbraco.Tests.CoreThings +{ + [TestFixture] + public class UriExtensionsTests + { + private string _root; + + [SetUp] + public void SetUp() + { + _root = SystemDirectories.Root; + } + + [TearDown] + public void TearDown() + { + SystemDirectories.Root = _root; + } + + [TestCase("http://www.domain.com/umbraco", "", true)] + [TestCase("http://www.domain.com/Umbraco/", "", true)] + [TestCase("http://www.domain.com/umbraco/default.aspx", "", true)] + [TestCase("http://www.domain.com/umbraco/test/test", "", false)] + [TestCase("http://www.domain.com/umbraco/test/test/test", "", false)] + [TestCase("http://www.domain.com/Umbraco/test/test.aspx", "", true)] + [TestCase("http://www.domain.com/umbraco/test/test.js", "", true)] + [TestCase("http://www.domain.com/umbrac", "", false)] + [TestCase("http://www.domain.com/test", "", false)] + [TestCase("http://www.domain.com/test/umbraco", "", false)] + [TestCase("http://www.domain.com/test/umbraco.aspx", "", false)] + [TestCase("http://www.domain.com/Umbraco/restServices/blah", "", true)] + [TestCase("http://www.domain.com/Umbraco/Backoffice/blah", "", true)] + [TestCase("http://www.domain.com/Umbraco/anything", "", true)] + [TestCase("http://www.domain.com/Umbraco/anything/", "", true)] + [TestCase("http://www.domain.com/Umbraco/surface/blah", "", false)] + [TestCase("http://www.domain.com/umbraco/api/blah", "", false)] + [TestCase("http://www.domain.com/myvdir/umbraco/api/blah", "myvdir", false)] + [TestCase("http://www.domain.com/MyVdir/umbraco/api/blah", "/myvdir", false)] + [TestCase("http://www.domain.com/MyVdir/Umbraco/", "myvdir", true)] + [TestCase("http://www.domain.com/MyVdir/Umbraco/restServices/blah", "/myvdir", true)] + [TestCase("http://www.domain.com/umbraco/webservices/legacyAjaxCalls.asmx/js", "", true)] + [TestCase("http://www.domain.com/umbraco/test/legacyAjaxCalls.ashx?some=query&blah=js", "", true)] + public void Is_Back_Office_Request(string input, string virtualPath, bool expected) + { + SystemDirectories.Root = virtualPath; + + var source = new Uri(input); + Assert.AreEqual(expected, source.IsBackOfficeRequest(virtualPath)); + } + + [TestCase("http://www.domain.com/install", true)] + [TestCase("http://www.domain.com/Install/", true)] + [TestCase("http://www.domain.com/install/default.aspx", true)] + [TestCase("http://www.domain.com/install/test/test", true)] + [TestCase("http://www.domain.com/Install/test/test.aspx", true)] + [TestCase("http://www.domain.com/install/test/test.js", true)] + [TestCase("http://www.domain.com/instal", false)] + [TestCase("http://www.domain.com/umbraco", false)] + [TestCase("http://www.domain.com/umbraco/umbraco", false)] + [TestCase("http://www.domain.com/test/umbraco.aspx", false)] + public void Is_Installer_Request(string input, bool expected) + { + var source = new Uri(input); + Assert.AreEqual(expected, source.IsInstallerRequest()); + } + + [TestCase("http://www.domain.com/foo/bar", "/", "http://www.domain.com/")] + [TestCase("http://www.domain.com/foo/bar#hop", "/", "http://www.domain.com/")] + [TestCase("http://www.domain.com/foo/bar?q=2#hop", "/", "http://www.domain.com/?q=2")] + [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/foo/bar", "/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com/", "/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com", "/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com/foo?q=3", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")] + [TestCase("http://www.domain.com/foo#bang", "/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com/foo?q=3#bang", "/path/to/page/", "http://www.domain.com/path/to/page/?q=3")] + public void RewritePath(string input, string path, string expected) + { + var source = new Uri(input); + var output = source.Rewrite(path); + Assert.AreEqual(expected, output.ToString()); + } + + [TestCase("http://www.domain.com/", "path/to/page/", typeof(ArgumentException))] + [TestCase("http://www.domain.com", "path/to/page/", typeof(ArgumentException))] + public void RewritePath_Exceptions(string input, string path, Type exception) + { + var source = new Uri(input); + Assert.Throws(exception, () => + { + var output = source.Rewrite(path); + }); + } + + [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/foo/bar", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] + [TestCase("http://www.domain.com/foo/bar#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] + [TestCase("http://www.domain.com/foo/bar?k=3", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] + [TestCase("http://www.domain.com/foo/bar?k=3#hop", "/path/to/page", "?x=12", "http://www.domain.com/path/to/page?x=12")] + public void RewritePathAndQuery(string input, string path, string query, string expected) + { + var source = new Uri(input); + var output = source.Rewrite(path, query); + Assert.AreEqual(expected, output.ToString()); + } + + [TestCase("http://www.domain.com/", "path/to/page/", "", typeof(ArgumentException))] + [TestCase("http://www.domain.com/", "/path/to/page/", "x=27", typeof(ArgumentException))] + public void RewritePathAndQuery_Exceptions(string input, string path, string query, Type exception) + { + var source = new Uri(input); + Assert.Throws(exception, () => + { + var output = source.Rewrite(path, query); + }); + } + + [TestCase("http://www.domain.com", "/")] + [TestCase("http://www.domain.com/", "/")] + [TestCase("http://www.domain.com/foo", "/foo")] + [TestCase("http://www.domain.com/foo/", "/foo/")] + [TestCase("http://www.domain.com/foo/bar", "/foo/bar")] + [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar%20nix")] + [TestCase("http://www.domain.com/foo/bar?q=7#hop", "/foo/bar")] + [TestCase("/", "/")] + [TestCase("/foo", "/foo")] + [TestCase("/foo/", "/foo/")] + [TestCase("/foo/bar", "/foo/bar")] + [TestCase("/foo/bar?q=7#hop", "/foo/bar")] + [TestCase("/foo%20bar/pof", "/foo%20bar/pof")] + public void GetSafeAbsolutePath(string input, string expected) + { + var source = new Uri(input, UriKind.RelativeOrAbsolute); + var output = source.GetSafeAbsolutePath(); + Assert.AreEqual(expected, output); + } + + [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")] + public void GetAbsolutePathDecoded(string input, string expected) + { + var source = new Uri(input, UriKind.RelativeOrAbsolute); + var output = source.GetAbsolutePathDecoded(); + Assert.AreEqual(expected, output); + } + + [TestCase("http://www.domain.com/foo/bar%20nix", "/foo/bar nix")] + [TestCase("/foo%20bar/pof", "/foo bar/pof")] + public void GetSafeAbsolutePathDecoded(string input, string expected) + { + var source = new Uri(input, UriKind.RelativeOrAbsolute); + var output = source.GetSafeAbsolutePathDecoded(); + Assert.AreEqual(expected, output); + } + + [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to/")] + [TestCase("http://www.domain.com/", "http://www.domain.com/")] + [TestCase("http://www.domain.com", "http://www.domain.com/")] + [TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to/?q=3")] + public void EndPathWithSlash(string input, string expected) + { + var source = new Uri(input); + var output = source.EndPathWithSlash(); + Assert.AreEqual(expected, output.ToString()); + } + + [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/path/to/", "http://www.domain.com/path/to")] + [TestCase("http://www.domain.com/", "http://www.domain.com/")] + [TestCase("http://www.domain.com", "http://www.domain.com/")] + [TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to?q=3")] + public void TrimPathEndSlash(string input, string expected) + { + var source = new Uri(input); + var output = source.TrimPathEndSlash(); + Assert.AreEqual(expected, output.ToString()); + } + + [TestCase("/foo/bar", "http://www.domain.com", "http://www.domain.com/foo/bar")] + [TestCase("/foo/bar", "http://www.domain.com/dang/dang", "http://www.domain.com/foo/bar")] + [TestCase("/", "http://www.domain.com/dang/dang", "http://www.domain.com/")] + [TestCase("/foo/bar", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar")] + [TestCase("/foo/bar?k=6#yop", "http://www.domain.com/dang/dang?q=3#dang", "http://www.domain.com/foo/bar?k=6")] + public void MakeAbsolute(string input, string reference, string expected) + { + var source = new Uri(input, UriKind.Relative); + var absolute = new Uri(reference); + var output = source.MakeAbsolute(absolute); + Assert.AreEqual(expected, output.ToString()); + } + + [TestCase("http://www.domain.com/path/to/page", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com", "http://www.domain.com/")] + [TestCase("http://www.domain.com/", "http://www.domain.com/")] + [TestCase("http://www.domain.com/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")] + [TestCase("http://www.domain.com/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")] + [TestCase("http://www.domain.com:666/path/to/page", "http://www.domain.com/path/to/page")] + [TestCase("http://www.domain.com:666/path/to/page/", "http://www.domain.com/path/to/page/")] + [TestCase("http://www.domain.com:666", "http://www.domain.com/")] + [TestCase("http://www.domain.com:666/", "http://www.domain.com/")] + [TestCase("http://www.domain.com:666/path/to?q=3#yop", "http://www.domain.com/path/to?q=3#yop")] + [TestCase("http://www.domain.com:666/path/to/?q=3#yop", "http://www.domain.com/path/to/?q=3#yop")] + public void WithoutPort(string input, string expected) + { + var source = new Uri(input); + var output = source.WithoutPort(); + Assert.AreEqual(expected, output.ToString()); + } + } +} diff --git a/src/Umbraco.Tests/VersionExtensionTests.cs b/src/Umbraco.Tests/CoreThings/VersionExtensionTests.cs similarity index 96% rename from src/Umbraco.Tests/VersionExtensionTests.cs rename to src/Umbraco.Tests/CoreThings/VersionExtensionTests.cs index ca8fea0393..2f90c3da2e 100644 --- a/src/Umbraco.Tests/VersionExtensionTests.cs +++ b/src/Umbraco.Tests/CoreThings/VersionExtensionTests.cs @@ -2,7 +2,7 @@ using NUnit.Framework; using Umbraco.Core; -namespace Umbraco.Tests +namespace Umbraco.Tests.CoreThings { [TestFixture] public class VersionExtensionTests diff --git a/src/Umbraco.Tests/XmlExtensionsTests.cs b/src/Umbraco.Tests/CoreThings/XmlExtensionsTests.cs similarity index 97% rename from src/Umbraco.Tests/XmlExtensionsTests.cs rename to src/Umbraco.Tests/CoreThings/XmlExtensionsTests.cs index bc4a281b66..b5b1368eb1 100644 --- a/src/Umbraco.Tests/XmlExtensionsTests.cs +++ b/src/Umbraco.Tests/CoreThings/XmlExtensionsTests.cs @@ -3,7 +3,7 @@ using System.Xml.Linq; using NUnit.Framework; using Umbraco.Core; -namespace Umbraco.Tests +namespace Umbraco.Tests.CoreThings { [TestFixture] public class XmlExtensionsTests diff --git a/src/Umbraco.Tests/DependencyInjection/ActionCollectionTests.cs b/src/Umbraco.Tests/DependencyInjection/ActionCollectionTests.cs index 755588b7b8..9451dc07f0 100644 --- a/src/Umbraco.Tests/DependencyInjection/ActionCollectionTests.cs +++ b/src/Umbraco.Tests/DependencyInjection/ActionCollectionTests.cs @@ -4,7 +4,7 @@ using Umbraco.Web; using Umbraco.Web.UI.Pages; using Umbraco.Web._Legacy.Actions; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { [TestFixture] public class ActionCollectionTests : ResolverBaseTest diff --git a/src/Umbraco.Tests/DependencyInjection/CollectionBuildersTests.cs b/src/Umbraco.Tests/DependencyInjection/CollectionBuildersTests.cs index 510f98780a..a5b259bfa3 100644 --- a/src/Umbraco.Tests/DependencyInjection/CollectionBuildersTests.cs +++ b/src/Umbraco.Tests/DependencyInjection/CollectionBuildersTests.cs @@ -5,7 +5,7 @@ using LightInject; using NUnit.Framework; using Umbraco.Core.DI; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { [TestFixture] public class CollectionBuildersTests diff --git a/src/Umbraco.Tests/DependencyInjection/LazyCollectionBuilderTests.cs b/src/Umbraco.Tests/DependencyInjection/LazyCollectionBuilderTests.cs index 6dc5f6ccc9..91988f2b5a 100644 --- a/src/Umbraco.Tests/DependencyInjection/LazyCollectionBuilderTests.cs +++ b/src/Umbraco.Tests/DependencyInjection/LazyCollectionBuilderTests.cs @@ -6,7 +6,7 @@ using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.DI; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { [TestFixture] public class LazyCollectionBuilderTests diff --git a/src/Umbraco.Tests/DependencyInjection/PackageActionCollectionTests.cs b/src/Umbraco.Tests/DependencyInjection/PackageActionCollectionTests.cs index 54ecc89576..d5e7fcd113 100644 --- a/src/Umbraco.Tests/DependencyInjection/PackageActionCollectionTests.cs +++ b/src/Umbraco.Tests/DependencyInjection/PackageActionCollectionTests.cs @@ -7,7 +7,7 @@ using Umbraco.Core.DI; using Umbraco.Core.Plugins; using Umbraco.Core._Legacy.PackageActions; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { [TestFixture] public class PackageActionCollectionTests : ResolverBaseTest diff --git a/src/Umbraco.Tests/DependencyInjection/ResolverBaseTest.cs b/src/Umbraco.Tests/DependencyInjection/ResolverBaseTest.cs index 94b9ffbb65..5d74da325c 100644 --- a/src/Umbraco.Tests/DependencyInjection/ResolverBaseTest.cs +++ b/src/Umbraco.Tests/DependencyInjection/ResolverBaseTest.cs @@ -7,7 +7,7 @@ using Umbraco.Core.DI; using Umbraco.Core.Logging; using Umbraco.Core.Plugins; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { public abstract class ResolverBaseTest // fixme rename, do something! { diff --git a/src/Umbraco.Tests/DependencyInjection/TempTests.cs b/src/Umbraco.Tests/DependencyInjection/TempTests.cs deleted file mode 100644 index d4649bdb20..0000000000 --- a/src/Umbraco.Tests/DependencyInjection/TempTests.cs +++ /dev/null @@ -1,21 +0,0 @@ -using LightInject; -using NUnit.Framework; -using Umbraco.Core.DI; -using Umbraco.Web.Routing; - -namespace Umbraco.Tests.DependencyInjection -{ - [TestFixture] - public class TempTests - { - //[Test] - public void Test() - { - var container = new ServiceContainer(); - container.ConfigureUmbracoCore(); - container.RegisterCollectionBuilder() - .Append(); - var col = container.GetInstance(); - } - } -} diff --git a/src/Umbraco.Tests/DependencyInjection/XsltExtensionsResolverTests.cs b/src/Umbraco.Tests/DependencyInjection/XsltExtensionsResolverTests.cs index a3d1c0b742..1d1d97d704 100644 --- a/src/Umbraco.Tests/DependencyInjection/XsltExtensionsResolverTests.cs +++ b/src/Umbraco.Tests/DependencyInjection/XsltExtensionsResolverTests.cs @@ -4,7 +4,7 @@ using NUnit.Framework; using Umbraco.Core.Macros; using Umbraco.Web; -namespace Umbraco.Tests.DependencyInjection +namespace Umbraco.Tests.DI { [TestFixture] public class XsltExtensionsResolverTests : ResolverBaseTest diff --git a/src/Umbraco.Tests/ApplicationUrlHelperTests.cs b/src/Umbraco.Tests/Misc/ApplicationUrlHelperTests.cs similarity index 99% rename from src/Umbraco.Tests/ApplicationUrlHelperTests.cs rename to src/Umbraco.Tests/Misc/ApplicationUrlHelperTests.cs index 56a0049097..50983b92c9 100644 --- a/src/Umbraco.Tests/ApplicationUrlHelperTests.cs +++ b/src/Umbraco.Tests/Misc/ApplicationUrlHelperTests.cs @@ -6,11 +6,11 @@ using Moq; using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.DI; using Umbraco.Core.Logging; using Umbraco.Core.Sync; -using Umbraco.Core.DI; -namespace Umbraco.Tests +namespace Umbraco.Tests.Misc { [TestFixture] public class ApplicationUrlHelperTests diff --git a/src/Umbraco.Tests/HashCodeCombinerTests.cs b/src/Umbraco.Tests/Misc/HashCodeCombinerTests.cs similarity index 95% rename from src/Umbraco.Tests/HashCodeCombinerTests.cs rename to src/Umbraco.Tests/Misc/HashCodeCombinerTests.cs index 8d1ad021dd..4bd2b687c0 100644 --- a/src/Umbraco.Tests/HashCodeCombinerTests.cs +++ b/src/Umbraco.Tests/Misc/HashCodeCombinerTests.cs @@ -1,145 +1,145 @@ -using System; -using System.IO; -using System.Reflection; -using NUnit.Framework; -using Umbraco.Core; - -namespace Umbraco.Tests -{ - [TestFixture] - public class HashCodeCombinerTests - { - - private DirectoryInfo PrepareFolder() - { - var assDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; - var dir = Directory.CreateDirectory(Path.Combine(assDir.FullName, "HashCombiner", Guid.NewGuid().ToString("N"))); - foreach (var f in dir.GetFiles()) - { - f.Delete(); - } - return dir; - } - - [Test] - public void HashCombiner_Test_String() - { - var combiner1 = new HashCodeCombiner(); - combiner1.AddCaseInsensitiveString("Hello"); - - var combiner2 = new HashCodeCombiner(); - combiner2.AddCaseInsensitiveString("hello"); - - Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - - combiner2.AddCaseInsensitiveString("world"); - - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - } - - [Test] - public void HashCombiner_Test_Int() - { - var combiner1 = new HashCodeCombiner(); - combiner1.AddInt(1234); - - var combiner2 = new HashCodeCombiner(); - combiner2.AddInt(1234); - - Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - - combiner2.AddInt(1); - - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - } - - [Test] - public void HashCombiner_Test_DateTime() - { - var dt = DateTime.Now; - var combiner1 = new HashCodeCombiner(); - combiner1.AddDateTime(dt); - - var combiner2 = new HashCodeCombiner(); - combiner2.AddDateTime(dt); - - Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - - combiner2.AddDateTime(DateTime.Now); - - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - } - - [Test] - public void HashCombiner_Test_File() - { - var dir = PrepareFolder(); - var file1Path = Path.Combine(dir.FullName, "hastest1.txt"); - File.Delete(file1Path); - using (var file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt"))) - { - file1.WriteLine("hello"); - } - var file2Path = Path.Combine(dir.FullName, "hastest2.txt"); - File.Delete(file2Path); - using (var file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt"))) - { - //even though files are the same, the dates are different - file2.WriteLine("hello"); - } - - var combiner1 = new HashCodeCombiner(); - combiner1.AddFile(new FileInfo(file1Path)); - - var combiner2 = new HashCodeCombiner(); - combiner2.AddFile(new FileInfo(file1Path)); - - var combiner3 = new HashCodeCombiner(); - combiner3.AddFile(new FileInfo(file2Path)); - - Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner3.GetCombinedHashCode()); - - combiner2.AddFile(new FileInfo(file2Path)); - - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - } - - [Test] - public void HashCombiner_Test_Folder() - { - var dir = PrepareFolder(); - var file1Path = Path.Combine(dir.FullName, "hastest1.txt"); - File.Delete(file1Path); - using (var file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt"))) - { - file1.WriteLine("hello"); - } - - //first test the whole folder - var combiner1 = new HashCodeCombiner(); - combiner1.AddFolder(dir); - - var combiner2 = new HashCodeCombiner(); - combiner2.AddFolder(dir); - - Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); - - //now add a file to the folder - - var file2Path = Path.Combine(dir.FullName, "hastest2.txt"); - File.Delete(file2Path); - using (var file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt"))) - { - //even though files are the same, the dates are different - file2.WriteLine("hello"); - } - - var combiner3 = new HashCodeCombiner(); - combiner3.AddFolder(dir); - - Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner3.GetCombinedHashCode()); - } - - } -} +using System; +using System.IO; +using System.Reflection; +using NUnit.Framework; +using Umbraco.Core; + +namespace Umbraco.Tests.Misc +{ + [TestFixture] + public class HashCodeCombinerTests + { + + private DirectoryInfo PrepareFolder() + { + var assDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory; + var dir = Directory.CreateDirectory(Path.Combine(assDir.FullName, "HashCombiner", Guid.NewGuid().ToString("N"))); + foreach (var f in dir.GetFiles()) + { + f.Delete(); + } + return dir; + } + + [Test] + public void HashCombiner_Test_String() + { + var combiner1 = new HashCodeCombiner(); + combiner1.AddCaseInsensitiveString("Hello"); + + var combiner2 = new HashCodeCombiner(); + combiner2.AddCaseInsensitiveString("hello"); + + Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + + combiner2.AddCaseInsensitiveString("world"); + + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + } + + [Test] + public void HashCombiner_Test_Int() + { + var combiner1 = new HashCodeCombiner(); + combiner1.AddInt(1234); + + var combiner2 = new HashCodeCombiner(); + combiner2.AddInt(1234); + + Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + + combiner2.AddInt(1); + + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + } + + [Test] + public void HashCombiner_Test_DateTime() + { + var dt = DateTime.Now; + var combiner1 = new HashCodeCombiner(); + combiner1.AddDateTime(dt); + + var combiner2 = new HashCodeCombiner(); + combiner2.AddDateTime(dt); + + Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + + combiner2.AddDateTime(DateTime.Now); + + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + } + + [Test] + public void HashCombiner_Test_File() + { + var dir = PrepareFolder(); + var file1Path = Path.Combine(dir.FullName, "hastest1.txt"); + File.Delete(file1Path); + using (var file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt"))) + { + file1.WriteLine("hello"); + } + var file2Path = Path.Combine(dir.FullName, "hastest2.txt"); + File.Delete(file2Path); + using (var file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt"))) + { + //even though files are the same, the dates are different + file2.WriteLine("hello"); + } + + var combiner1 = new HashCodeCombiner(); + combiner1.AddFile(new FileInfo(file1Path)); + + var combiner2 = new HashCodeCombiner(); + combiner2.AddFile(new FileInfo(file1Path)); + + var combiner3 = new HashCodeCombiner(); + combiner3.AddFile(new FileInfo(file2Path)); + + Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner3.GetCombinedHashCode()); + + combiner2.AddFile(new FileInfo(file2Path)); + + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + } + + [Test] + public void HashCombiner_Test_Folder() + { + var dir = PrepareFolder(); + var file1Path = Path.Combine(dir.FullName, "hastest1.txt"); + File.Delete(file1Path); + using (var file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt"))) + { + file1.WriteLine("hello"); + } + + //first test the whole folder + var combiner1 = new HashCodeCombiner(); + combiner1.AddFolder(dir); + + var combiner2 = new HashCodeCombiner(); + combiner2.AddFolder(dir); + + Assert.AreEqual(combiner1.GetCombinedHashCode(), combiner2.GetCombinedHashCode()); + + //now add a file to the folder + + var file2Path = Path.Combine(dir.FullName, "hastest2.txt"); + File.Delete(file2Path); + using (var file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt"))) + { + //even though files are the same, the dates are different + file2.WriteLine("hello"); + } + + var combiner3 = new HashCodeCombiner(); + combiner3.AddFolder(dir); + + Assert.AreNotEqual(combiner1.GetCombinedHashCode(), combiner3.GetCombinedHashCode()); + } + + } +} diff --git a/src/Umbraco.Tests/LibraryTests.cs b/src/Umbraco.Tests/Misc/LibraryTests.cs similarity index 93% rename from src/Umbraco.Tests/LibraryTests.cs rename to src/Umbraco.Tests/Misc/LibraryTests.cs index e46b7fa830..63f4618101 100644 --- a/src/Umbraco.Tests/LibraryTests.cs +++ b/src/Umbraco.Tests/Misc/LibraryTests.cs @@ -1,176 +1,169 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Text; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Core.Configuration; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.PropertyEditors; -using Umbraco.Tests.PublishedContent; -using Umbraco.Tests.TestHelpers; -using Umbraco.Web; -using Umbraco.Web.PublishedCache; -using Umbraco.Web.PublishedCache.XmlPublishedCache; -using umbraco; -using Umbraco.Core.DI; - -namespace Umbraco.Tests -{ - - /// - /// Tests for the legacy library class - /// - [TestFixture] - public class LibraryTests : BaseWebTest - { - public override void SetUp() - { - base.SetUp(); - - // need to specify a custom callback for unit tests - // AutoPublishedContentTypes generates properties automatically - // when they are requested, but we must declare those that we - // explicitely want to be here... - - var propertyTypes = new[] - { - // AutoPublishedContentType will auto-generate other properties - new PublishedPropertyType("content", 0, "?"), - }; - var type = new AutoPublishedContentType(0, "anything", propertyTypes); - ContentTypesCache.GetPublishedContentTypeByAlias = (alias) => type; - Debug.Print("INIT LIB {0}", - ContentTypesCache.Get(PublishedItemType.Content, "anything") - .PropertyTypes.Count()); - - var umbracoContext = GetUmbracoContext("/test"); - Umbraco.Web.Current.UmbracoContextAccessor.UmbracoContext = umbracoContext; - } - - /// - /// sets up resolvers before resolution is frozen - /// - protected override void MoreSetUp() - { - // required so we can access property.Value - Container.RegisterCollectionBuilder(); - - base.MoreSetUp(); - } - - [Test] - public void Json_To_Xml_Object() - { - var json = "{ id: 1, name: 'hello', children: [{id: 2, name: 'child1'}, {id:3, name: 'child2'}]}"; - var result = library.JsonToXml(json); - Assert.AreEqual(@" - 1 - hello - - 2 - child1 - - - 3 - child2 - -", result.Current.OuterXml); - } - - [Test] - public void Json_To_Xml_Array() - { - var json = "[{id: 2, name: 'child1'}, {id:3, name: 'child2'}]"; - var result = library.JsonToXml(json); - Assert.AreEqual(@" - - 2 - child1 - - - 3 - child2 - -", result.Current.OuterXml); - } - - [Test] - public void Json_To_Xml_Error() - { - var json = "{ id: 1, name: 'hello', children: }"; - var result = library.JsonToXml(json); - Assert.IsTrue(result.Current.OuterXml.StartsWith("")); - } - - [Test] - public void Get_Item_User_Property() - { - var val = library.GetItem(1173, "content"); - var legacyVal = LegacyGetItem(1173, "content"); - Assert.AreEqual(legacyVal, val); - Assert.AreEqual("
This is some content
", val); - } - - [Test] - public void Get_Item_Document_Property() - { - //first test a single static val - var val = library.GetItem(1173, "template"); - var legacyVal = LegacyGetItem(1173, "template"); - Assert.AreEqual(legacyVal, val); - Assert.AreEqual("1234", val); - - //now test them all to see if they all match legacy - foreach(var s in new[]{"id","parentID","level","writerID","template","sortOrder","createDate","updateDate","nodeName","writerName","path"}) - { - val = library.GetItem(1173, s); - legacyVal = LegacyGetItem(1173, s); - Assert.AreEqual(legacyVal, val); - } - } - - [Test] - public void Get_Item_Invalid_Property() - { - var val = library.GetItem(1173, "dontfindme"); - var legacyVal = LegacyGetItem(1173, "dontfindme"); - Assert.AreEqual(legacyVal, val); - Assert.AreEqual("", val); - } - - /// - /// The old method, just using this to make sure we're returning the correct exact data as before. - /// - /// - /// - /// - private string LegacyGetItem(int nodeId, string alias) - { - var cache = UmbracoContext.Current.ContentCache as PublishedContentCache; - if (cache == null) throw new Exception("Unsupported IPublishedContentCache, only the Xml one is supported."); - var umbracoXML = cache.GetXml(UmbracoContext.Current.InPreviewMode); - - string xpath = "./{0}"; - if (umbracoXML.GetElementById(nodeId.ToString()) != null) - if ( - ",id,parentID,level,writerID,template,sortOrder,createDate,updateDate,nodeName,writerName,path," - . - IndexOf("," + alias + ",") > -1) - return umbracoXML.GetElementById(nodeId.ToString()).Attributes.GetNamedItem(alias).Value; - else if ( - umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)) != - null) - return - umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)).ChildNodes[0]. - Value; //.Value + "*"; - else - return string.Empty; - else - return string.Empty; - } - } -} +using System; +using System.Diagnostics; +using System.Linq; +using NUnit.Framework; +using umbraco; +using Umbraco.Core.DI; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Tests.PublishedContent; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; +using Umbraco.Web.PublishedCache.XmlPublishedCache; + +namespace Umbraco.Tests.Misc +{ + + /// + /// Tests for the legacy library class + /// + [TestFixture] + public class LibraryTests : BaseWebTest + { + public override void SetUp() + { + base.SetUp(); + + // need to specify a custom callback for unit tests + // AutoPublishedContentTypes generates properties automatically + // when they are requested, but we must declare those that we + // explicitely want to be here... + + var propertyTypes = new[] + { + // AutoPublishedContentType will auto-generate other properties + new PublishedPropertyType("content", 0, "?"), + }; + var type = new AutoPublishedContentType(0, "anything", propertyTypes); + ContentTypesCache.GetPublishedContentTypeByAlias = (alias) => type; + Debug.Print("INIT LIB {0}", + ContentTypesCache.Get(PublishedItemType.Content, "anything") + .PropertyTypes.Count()); + + var umbracoContext = GetUmbracoContext("/test"); + Umbraco.Web.Current.UmbracoContextAccessor.UmbracoContext = umbracoContext; + } + + /// + /// sets up resolvers before resolution is frozen + /// + protected override void MoreSetUp() + { + // required so we can access property.Value + Container.RegisterCollectionBuilder(); + + base.MoreSetUp(); + } + + [Test] + public void Json_To_Xml_Object() + { + var json = "{ id: 1, name: 'hello', children: [{id: 2, name: 'child1'}, {id:3, name: 'child2'}]}"; + var result = library.JsonToXml(json); + Assert.AreEqual(@" + 1 + hello + + 2 + child1 + + + 3 + child2 + +", result.Current.OuterXml); + } + + [Test] + public void Json_To_Xml_Array() + { + var json = "[{id: 2, name: 'child1'}, {id:3, name: 'child2'}]"; + var result = library.JsonToXml(json); + Assert.AreEqual(@" + + 2 + child1 + + + 3 + child2 + +", result.Current.OuterXml); + } + + [Test] + public void Json_To_Xml_Error() + { + var json = "{ id: 1, name: 'hello', children: }"; + var result = library.JsonToXml(json); + Assert.IsTrue(result.Current.OuterXml.StartsWith("")); + } + + [Test] + public void Get_Item_User_Property() + { + var val = library.GetItem(1173, "content"); + var legacyVal = LegacyGetItem(1173, "content"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("
This is some content
", val); + } + + [Test] + public void Get_Item_Document_Property() + { + //first test a single static val + var val = library.GetItem(1173, "template"); + var legacyVal = LegacyGetItem(1173, "template"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("1234", val); + + //now test them all to see if they all match legacy + foreach(var s in new[]{"id","parentID","level","writerID","template","sortOrder","createDate","updateDate","nodeName","writerName","path"}) + { + val = library.GetItem(1173, s); + legacyVal = LegacyGetItem(1173, s); + Assert.AreEqual(legacyVal, val); + } + } + + [Test] + public void Get_Item_Invalid_Property() + { + var val = library.GetItem(1173, "dontfindme"); + var legacyVal = LegacyGetItem(1173, "dontfindme"); + Assert.AreEqual(legacyVal, val); + Assert.AreEqual("", val); + } + + /// + /// The old method, just using this to make sure we're returning the correct exact data as before. + /// + /// + /// + /// + private string LegacyGetItem(int nodeId, string alias) + { + var cache = UmbracoContext.Current.ContentCache as PublishedContentCache; + if (cache == null) throw new Exception("Unsupported IPublishedContentCache, only the Xml one is supported."); + var umbracoXML = cache.GetXml(UmbracoContext.Current.InPreviewMode); + + string xpath = "./{0}"; + if (umbracoXML.GetElementById(nodeId.ToString()) != null) + if ( + ",id,parentID,level,writerID,template,sortOrder,createDate,updateDate,nodeName,writerName,path," + . + IndexOf("," + alias + ",") > -1) + return umbracoXML.GetElementById(nodeId.ToString()).Attributes.GetNamedItem(alias).Value; + else if ( + umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)) != + null) + return + umbracoXML.GetElementById(nodeId.ToString()).SelectSingleNode(string.Format(xpath, alias)).ChildNodes[0]. + Value; //.Value + "*"; + else + return string.Empty; + else + return string.Empty; + } + } +} diff --git a/src/Umbraco.Tests/UriUtilityTests.cs b/src/Umbraco.Tests/Misc/UriUtilityTests.cs similarity index 96% rename from src/Umbraco.Tests/UriUtilityTests.cs rename to src/Umbraco.Tests/Misc/UriUtilityTests.cs index 835f004948..8eb7ee4311 100644 --- a/src/Umbraco.Tests/UriUtilityTests.cs +++ b/src/Umbraco.Tests/Misc/UriUtilityTests.cs @@ -1,139 +1,138 @@ -using System; -using System.Configuration; -using Moq; -using NUnit.Framework; -using Umbraco.Tests.TestHelpers; -using Umbraco.Web; - -namespace Umbraco.Tests -{ - // fixme - not testing virtual directory! - - [TestFixture] - public class UriUtilityTests - { - - [TearDown] - public void TearDown() - { - SettingsForTests.Reset(); - } - - // test normal urls - [TestCase("http://LocalHost/", "http://localhost/")] - [TestCase("http://LocalHost/?x=y", "http://localhost/?x=y")] - [TestCase("http://LocalHost/Home", "http://localhost/home")] - [TestCase("http://LocalHost/Home?x=y", "http://localhost/home?x=y")] - [TestCase("http://LocalHost/Home/Sub1", "http://localhost/home/sub1")] - [TestCase("http://LocalHost/Home/Sub1?x=y", "http://localhost/home/sub1?x=y")] - - // same with .aspx - [TestCase("http://LocalHost/Home.aspx", "http://localhost/home")] - [TestCase("http://LocalHost/Home.aspx?x=y", "http://localhost/home?x=y")] - [TestCase("http://LocalHost/Home/Sub1.aspx", "http://localhost/home/sub1")] - [TestCase("http://LocalHost/Home/Sub1.aspx?x=y", "http://localhost/home/sub1?x=y")] - - // test that the trailing slash goes but not on hostname - [TestCase("http://LocalHost/", "http://localhost/")] - [TestCase("http://LocalHost/Home/", "http://localhost/home")] - [TestCase("http://LocalHost/Home/?x=y", "http://localhost/home?x=y")] - [TestCase("http://LocalHost/Home/Sub1/", "http://localhost/home/sub1")] - [TestCase("http://LocalHost/Home/Sub1/?x=y", "http://localhost/home/sub1?x=y")] - - // test that default.aspx goes, even with parameters - [TestCase("http://LocalHost/deFault.aspx", "http://localhost/")] - [TestCase("http://LocalHost/deFault.aspx?x=y", "http://localhost/?x=y")] - - // test with inner .aspx - [TestCase("http://Localhost/Home/Sub1.aspx/Sub2", "http://localhost/home/sub1/sub2")] - [TestCase("http://Localhost/Home/Sub1.aspx/Sub2?x=y", "http://localhost/home/sub1/sub2?x=y")] - [TestCase("http://Localhost/Home.aspx/Sub1.aspx/Sub2?x=y", "http://localhost/home/sub1/sub2?x=y")] - [TestCase("http://Localhost/deFault.aspx/Home.aspx/deFault.aspx/Sub1.aspx", "http://localhost/home/default/sub1")] - - public void Uri_To_Umbraco(string sourceUrl, string expectedUrl) - { - UriUtility.SetAppDomainAppVirtualPath("/"); - - var expectedUri = new Uri(expectedUrl); - var sourceUri = new Uri(sourceUrl); - var resultUri = UriUtility.UriToUmbraco(sourceUri); - - Assert.AreEqual(expectedUri.ToString(), resultUri.ToString()); - } - - // test directoryUrl false, trailingSlash false - [TestCase("/", "/", false, false)] - [TestCase("/home", "/home.aspx", false, false)] - [TestCase("/home/sub1", "/home/sub1.aspx", false, false)] - - // test directoryUrl false, trailingSlash true - [TestCase("/", "/", false, true)] - [TestCase("/home", "/home.aspx", false, true)] - [TestCase("/home/sub1", "/home/sub1.aspx", false, true)] - - // test directoryUrl true, trailingSlash false - [TestCase("/", "/", true, false)] - [TestCase("/home", "/home", true, false)] - [TestCase("/home/sub1", "/home/sub1", true, false)] - - // test directoryUrl true, trailingSlash true - [TestCase("/", "/", true, true)] - [TestCase("/home", "/home/", true, true)] - [TestCase("/home/sub1", "/home/sub1/", true, true)] - - public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool directoryUrls, bool trailingSlash) - { - ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", directoryUrls ? "true" : "false"); - - var settings = SettingsForTests.GenerateMockSettings(); - var requestMock = Mock.Get(settings.RequestHandler); - requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash); - SettingsForTests.ConfigureSettings(settings); - - UriUtility.SetAppDomainAppVirtualPath("/"); - - var expectedUri = NewUri(expectedUrl); - var sourceUri = NewUri(sourceUrl); - var resultUri = UriUtility.UriFromUmbraco(sourceUri); - - Assert.AreEqual(expectedUri.ToString(), resultUri.ToString()); - } - - Uri NewUri(string url) - { - return new Uri(url, url.StartsWith("http:") ? UriKind.Absolute : UriKind.Relative); - } - - // - [TestCase("/", "/", "/")] - [TestCase("/", "/foo", "/foo")] - [TestCase("/", "~/foo", "/foo")] - [TestCase("/vdir", "/", "/vdir/")] - [TestCase("/vdir", "/foo", "/vdir/foo")] - [TestCase("/vdir", "/foo/", "/vdir/foo/")] - [TestCase("/vdir", "~/foo", "/vdir/foo")] - - public void Uri_To_Absolute(string virtualPath, string sourceUrl, string expectedUrl) - { - UriUtility.SetAppDomainAppVirtualPath(virtualPath); - var resultUrl = UriUtility.ToAbsolute(sourceUrl); - Assert.AreEqual(expectedUrl, resultUrl); - } - - // - [TestCase("/", "/", "/")] - [TestCase("/", "/foo", "/foo")] - [TestCase("/", "/foo/", "/foo/")] - [TestCase("/vdir", "/vdir", "/")] - [TestCase("/vdir", "/vdir/", "/")] - [TestCase("/vdir", "/vdir/foo", "/foo")] - [TestCase("/vdir", "/vdir/foo/", "/foo/")] - - public void Url_To_App_Relative(string virtualPath, string sourceUrl, string expectedUrl) - { - UriUtility.SetAppDomainAppVirtualPath(virtualPath); - var resultUrl = UriUtility.ToAppRelative(sourceUrl); - Assert.AreEqual(expectedUrl, resultUrl); - } - } +using System; +using System.Configuration; +using Moq; +using NUnit.Framework; +using Umbraco.Tests.TestHelpers; +using Umbraco.Web; + +namespace Umbraco.Tests.Misc +{ + // fixme - not testing virtual directory! + + [TestFixture] + public class UriUtilityTests + { + [TearDown] + public void TearDown() + { + SettingsForTests.Reset(); + } + + // test normal urls + [TestCase("http://LocalHost/", "http://localhost/")] + [TestCase("http://LocalHost/?x=y", "http://localhost/?x=y")] + [TestCase("http://LocalHost/Home", "http://localhost/home")] + [TestCase("http://LocalHost/Home?x=y", "http://localhost/home?x=y")] + [TestCase("http://LocalHost/Home/Sub1", "http://localhost/home/sub1")] + [TestCase("http://LocalHost/Home/Sub1?x=y", "http://localhost/home/sub1?x=y")] + + // same with .aspx + [TestCase("http://LocalHost/Home.aspx", "http://localhost/home")] + [TestCase("http://LocalHost/Home.aspx?x=y", "http://localhost/home?x=y")] + [TestCase("http://LocalHost/Home/Sub1.aspx", "http://localhost/home/sub1")] + [TestCase("http://LocalHost/Home/Sub1.aspx?x=y", "http://localhost/home/sub1?x=y")] + + // test that the trailing slash goes but not on hostname + [TestCase("http://LocalHost/", "http://localhost/")] + [TestCase("http://LocalHost/Home/", "http://localhost/home")] + [TestCase("http://LocalHost/Home/?x=y", "http://localhost/home?x=y")] + [TestCase("http://LocalHost/Home/Sub1/", "http://localhost/home/sub1")] + [TestCase("http://LocalHost/Home/Sub1/?x=y", "http://localhost/home/sub1?x=y")] + + // test that default.aspx goes, even with parameters + [TestCase("http://LocalHost/deFault.aspx", "http://localhost/")] + [TestCase("http://LocalHost/deFault.aspx?x=y", "http://localhost/?x=y")] + + // test with inner .aspx + [TestCase("http://Localhost/Home/Sub1.aspx/Sub2", "http://localhost/home/sub1/sub2")] + [TestCase("http://Localhost/Home/Sub1.aspx/Sub2?x=y", "http://localhost/home/sub1/sub2?x=y")] + [TestCase("http://Localhost/Home.aspx/Sub1.aspx/Sub2?x=y", "http://localhost/home/sub1/sub2?x=y")] + [TestCase("http://Localhost/deFault.aspx/Home.aspx/deFault.aspx/Sub1.aspx", "http://localhost/home/default/sub1")] + + public void Uri_To_Umbraco(string sourceUrl, string expectedUrl) + { + UriUtility.SetAppDomainAppVirtualPath("/"); + + var expectedUri = new Uri(expectedUrl); + var sourceUri = new Uri(sourceUrl); + var resultUri = UriUtility.UriToUmbraco(sourceUri); + + Assert.AreEqual(expectedUri.ToString(), resultUri.ToString()); + } + + // test directoryUrl false, trailingSlash false + [TestCase("/", "/", false, false)] + [TestCase("/home", "/home.aspx", false, false)] + [TestCase("/home/sub1", "/home/sub1.aspx", false, false)] + + // test directoryUrl false, trailingSlash true + [TestCase("/", "/", false, true)] + [TestCase("/home", "/home.aspx", false, true)] + [TestCase("/home/sub1", "/home/sub1.aspx", false, true)] + + // test directoryUrl true, trailingSlash false + [TestCase("/", "/", true, false)] + [TestCase("/home", "/home", true, false)] + [TestCase("/home/sub1", "/home/sub1", true, false)] + + // test directoryUrl true, trailingSlash true + [TestCase("/", "/", true, true)] + [TestCase("/home", "/home/", true, true)] + [TestCase("/home/sub1", "/home/sub1/", true, true)] + + public void Uri_From_Umbraco(string sourceUrl, string expectedUrl, bool directoryUrls, bool trailingSlash) + { + ConfigurationManager.AppSettings.Set("umbracoUseDirectoryUrls", directoryUrls ? "true" : "false"); + + var settings = SettingsForTests.GenerateMockSettings(); + var requestMock = Mock.Get(settings.RequestHandler); + requestMock.Setup(x => x.AddTrailingSlash).Returns(trailingSlash); + SettingsForTests.ConfigureSettings(settings); + + UriUtility.SetAppDomainAppVirtualPath("/"); + + var expectedUri = NewUri(expectedUrl); + var sourceUri = NewUri(sourceUrl); + var resultUri = UriUtility.UriFromUmbraco(sourceUri); + + Assert.AreEqual(expectedUri.ToString(), resultUri.ToString()); + } + + Uri NewUri(string url) + { + return new Uri(url, url.StartsWith("http:") ? UriKind.Absolute : UriKind.Relative); + } + + // + [TestCase("/", "/", "/")] + [TestCase("/", "/foo", "/foo")] + [TestCase("/", "~/foo", "/foo")] + [TestCase("/vdir", "/", "/vdir/")] + [TestCase("/vdir", "/foo", "/vdir/foo")] + [TestCase("/vdir", "/foo/", "/vdir/foo/")] + [TestCase("/vdir", "~/foo", "/vdir/foo")] + + public void Uri_To_Absolute(string virtualPath, string sourceUrl, string expectedUrl) + { + UriUtility.SetAppDomainAppVirtualPath(virtualPath); + var resultUrl = UriUtility.ToAbsolute(sourceUrl); + Assert.AreEqual(expectedUrl, resultUrl); + } + + // + [TestCase("/", "/", "/")] + [TestCase("/", "/foo", "/foo")] + [TestCase("/", "/foo/", "/foo/")] + [TestCase("/vdir", "/vdir", "/")] + [TestCase("/vdir", "/vdir/", "/")] + [TestCase("/vdir", "/vdir/foo", "/foo")] + [TestCase("/vdir", "/vdir/foo/", "/foo/")] + + public void Url_To_App_Relative(string virtualPath, string sourceUrl, string expectedUrl) + { + UriUtility.SetAppDomainAppVirtualPath(virtualPath); + var resultUrl = UriUtility.ToAppRelative(sourceUrl); + Assert.AreEqual(expectedUrl, resultUrl); + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/XmlHelperTests.cs b/src/Umbraco.Tests/Misc/XmlHelperTests.cs similarity index 96% rename from src/Umbraco.Tests/XmlHelperTests.cs rename to src/Umbraco.Tests/Misc/XmlHelperTests.cs index 00a86db800..5df954ebea 100644 --- a/src/Umbraco.Tests/XmlHelperTests.cs +++ b/src/Umbraco.Tests/Misc/XmlHelperTests.cs @@ -1,278 +1,270 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Xml; -using System.Xml.XPath; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Configuration; -using umbraco; -using Umbraco.Core.Configuration.UmbracoSettings; -using Umbraco.Core.Persistence.UnitOfWork; -using Umbraco.Core.Xml; -using Umbraco.Tests.Models; - -namespace Umbraco.Tests -{ - [TestFixture] - public class XmlHelperTests - { - [SetUp] - public void Setup() - { - } - - [NUnit.Framework.Ignore("This is a benchmark test so is ignored by default")] - [Test] - public void Sort_Nodes_Benchmark_Legacy() - { - var xmlContent = GetXmlContent(1); - var xml = new XmlDocument(); - xml.LoadXml(xmlContent); - var original = xml.GetElementById(1173.ToString()); - Assert.IsNotNull(original); - - long totalTime = 0; - var watch = new Stopwatch(); - var iterations = 10000; - - for (var i = 0; i < iterations; i++) - { - //don't measure the time for clone! - var parentNode = original.Clone(); - watch.Start(); - LegacySortNodes(ref parentNode); - watch.Stop(); - totalTime += watch.ElapsedMilliseconds; - watch.Reset(); - - //do assertions just to make sure it is working properly. - var currSort = 0; - foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) - { - Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); - currSort++; - } - - //ensure the parent node's properties still exist first - Assert.AreEqual("content", parentNode.ChildNodes[0].Name); - Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); - //then the child nodes should come straight after - Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); - } - - Debug.WriteLine("Total time for " + iterations + " iterations is " + totalTime); - } - - [NUnit.Framework.Ignore("This is a benchmark test so is ignored by default")] - [Test] - public void Sort_Nodes_Benchmark_New() - { - var xmlContent = GetXmlContent(1); - var xml = new XmlDocument(); - xml.LoadXml(xmlContent); - var original = xml.GetElementById(1173.ToString()); - Assert.IsNotNull(original); - - long totalTime = 0; - var watch = new Stopwatch(); - var iterations = 10000; - - for (var i = 0; i < iterations; i++) - { - //don't measure the time for clone! - var parentNode = (XmlElement)original.Clone(); - watch.Start(); - XmlHelper.SortNodes( - parentNode, - "./* [@id]", - x => x.AttributeValue("sortOrder")); - watch.Stop(); - totalTime += watch.ElapsedMilliseconds; - watch.Reset(); - - //do assertions just to make sure it is working properly. - var currSort = 0; - foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) - { - Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); - currSort++; - } - //ensure the parent node's properties still exist first - Assert.AreEqual("content", parentNode.ChildNodes[0].Name); - Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); - //then the child nodes should come straight after - Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); - - } - - Debug.WriteLine("Total time for " + iterations + " iterations is " + totalTime); - } - - - [Test] - public void Sort_Nodes() - { - var xmlContent = GetXmlContent(1); - var xml = new XmlDocument(); - xml.LoadXml(xmlContent); - var original = xml.GetElementById(1173.ToString()); - Assert.IsNotNull(original); - - var parentNode = (XmlElement)original.Clone(); - - XmlHelper.SortNodes( - parentNode, - "./* [@id]", - x => x.AttributeValue("sortOrder")); - - //do assertions just to make sure it is working properly. - var currSort = 0; - foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) - { - Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); - currSort++; - } - //ensure the parent node's properties still exist first - Assert.AreEqual("content", parentNode.ChildNodes[0].Name); - Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); - //then the child nodes should come straight after - Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); - } - - /// - /// This was the logic to sort before and now lives here just to show the benchmarks tests above. - /// - /// - private static void LegacySortNodes(ref XmlNode parentNode) - { - XmlNode n = parentNode.CloneNode(true); - - // remove all children from original node - string xpath = "./* [@id]"; - foreach (XmlNode child in parentNode.SelectNodes(xpath)) - parentNode.RemoveChild(child); - - - XPathNavigator nav = n.CreateNavigator(); - XPathExpression expr = nav.Compile(xpath); - expr.AddSort("@sortOrder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number); - XPathNodeIterator iterator = nav.Select(expr); - while (iterator.MoveNext()) - parentNode.AppendChild( - ((IHasXmlNode)iterator.Current).GetNode()); - } - - /// - /// returns xml with a reverse sort order - /// - /// - /// - private string GetXmlContent(int templateId) - { - return @" - - - - -]> - - - - - 1 - - This is some content]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; - } - - } -} +using System.Diagnostics; +using System.Linq; +using System.Xml; +using System.Xml.XPath; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Xml; + +namespace Umbraco.Tests.Misc +{ + [TestFixture] + public class XmlHelperTests + { + [SetUp] + public void Setup() + { + } + + [Ignore("This is a benchmark test so is ignored by default")] + [Test] + public void Sort_Nodes_Benchmark_Legacy() + { + var xmlContent = GetXmlContent(1); + var xml = new XmlDocument(); + xml.LoadXml(xmlContent); + var original = xml.GetElementById(1173.ToString()); + Assert.IsNotNull(original); + + long totalTime = 0; + var watch = new Stopwatch(); + var iterations = 10000; + + for (var i = 0; i < iterations; i++) + { + //don't measure the time for clone! + var parentNode = original.Clone(); + watch.Start(); + LegacySortNodes(ref parentNode); + watch.Stop(); + totalTime += watch.ElapsedMilliseconds; + watch.Reset(); + + //do assertions just to make sure it is working properly. + var currSort = 0; + foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) + { + Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); + currSort++; + } + + //ensure the parent node's properties still exist first + Assert.AreEqual("content", parentNode.ChildNodes[0].Name); + Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); + //then the child nodes should come straight after + Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); + } + + Debug.WriteLine("Total time for " + iterations + " iterations is " + totalTime); + } + + [Ignore("This is a benchmark test so is ignored by default")] + [Test] + public void Sort_Nodes_Benchmark_New() + { + var xmlContent = GetXmlContent(1); + var xml = new XmlDocument(); + xml.LoadXml(xmlContent); + var original = xml.GetElementById(1173.ToString()); + Assert.IsNotNull(original); + + long totalTime = 0; + var watch = new Stopwatch(); + var iterations = 10000; + + for (var i = 0; i < iterations; i++) + { + //don't measure the time for clone! + var parentNode = (XmlElement)original.Clone(); + watch.Start(); + XmlHelper.SortNodes( + parentNode, + "./* [@id]", + x => x.AttributeValue("sortOrder")); + watch.Stop(); + totalTime += watch.ElapsedMilliseconds; + watch.Reset(); + + //do assertions just to make sure it is working properly. + var currSort = 0; + foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) + { + Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); + currSort++; + } + //ensure the parent node's properties still exist first + Assert.AreEqual("content", parentNode.ChildNodes[0].Name); + Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); + //then the child nodes should come straight after + Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); + + } + + Debug.WriteLine("Total time for " + iterations + " iterations is " + totalTime); + } + + + [Test] + public void Sort_Nodes() + { + var xmlContent = GetXmlContent(1); + var xml = new XmlDocument(); + xml.LoadXml(xmlContent); + var original = xml.GetElementById(1173.ToString()); + Assert.IsNotNull(original); + + var parentNode = (XmlElement)original.Clone(); + + XmlHelper.SortNodes( + parentNode, + "./* [@id]", + x => x.AttributeValue("sortOrder")); + + //do assertions just to make sure it is working properly. + var currSort = 0; + foreach (var child in parentNode.SelectNodes("./* [@id]").Cast()) + { + Assert.AreEqual(currSort, int.Parse(child.Attributes["sortOrder"].Value)); + currSort++; + } + //ensure the parent node's properties still exist first + Assert.AreEqual("content", parentNode.ChildNodes[0].Name); + Assert.AreEqual("umbracoUrlAlias", parentNode.ChildNodes[1].Name); + //then the child nodes should come straight after + Assert.IsTrue(parentNode.ChildNodes[2].Attributes["id"] != null); + } + + /// + /// This was the logic to sort before and now lives here just to show the benchmarks tests above. + /// + /// + private static void LegacySortNodes(ref XmlNode parentNode) + { + XmlNode n = parentNode.CloneNode(true); + + // remove all children from original node + string xpath = "./* [@id]"; + foreach (XmlNode child in parentNode.SelectNodes(xpath)) + parentNode.RemoveChild(child); + + + XPathNavigator nav = n.CreateNavigator(); + XPathExpression expr = nav.Compile(xpath); + expr.AddSort("@sortOrder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number); + XPathNodeIterator iterator = nav.Select(expr); + while (iterator.MoveNext()) + parentNode.AppendChild( + ((IHasXmlNode)iterator.Current).GetNode()); + } + + /// + /// returns xml with a reverse sort order + /// + /// + /// + private string GetXmlContent(int templateId) + { + return @" + + + + +]> + + + + + 1 + + This is some content]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +"; + } + } +} diff --git a/src/Umbraco.Tests/Models/ContentTests.cs b/src/Umbraco.Tests/Models/ContentTests.cs index 8279fd7cb9..c335bbfdde 100644 --- a/src/Umbraco.Tests/Models/ContentTests.cs +++ b/src/Umbraco.Tests/Models/ContentTests.cs @@ -20,6 +20,7 @@ using Umbraco.Core.Serialization; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Models { diff --git a/src/Umbraco.Tests/Models/ContentTypeTests.cs b/src/Umbraco.Tests/Models/ContentTypeTests.cs index 79eef89a57..7006fdd722 100644 --- a/src/Umbraco.Tests/Models/ContentTypeTests.cs +++ b/src/Umbraco.Tests/Models/ContentTypeTests.cs @@ -9,6 +9,7 @@ using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Serialization; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Models { diff --git a/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs b/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs index aebe87e40c..40b84102f2 100644 --- a/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs +++ b/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs @@ -14,6 +14,7 @@ using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Core.Profiling; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Persistence { diff --git a/src/Umbraco.Tests/Persistence/FaultHandling/ConnectionRetryTest.cs b/src/Umbraco.Tests/Persistence/FaultHandling/ConnectionRetryTest.cs index 97c23004ae..5c92c9d7f4 100644 --- a/src/Umbraco.Tests/Persistence/FaultHandling/ConnectionRetryTest.cs +++ b/src/Umbraco.Tests/Persistence/FaultHandling/ConnectionRetryTest.cs @@ -8,6 +8,7 @@ using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Persistence.FaultHandling { diff --git a/src/Umbraco.Tests/Plugins/TypeFinderTests.cs b/src/Umbraco.Tests/Plugins/TypeFinderTests.cs index 2ce4725aa2..1811ea5a57 100644 --- a/src/Umbraco.Tests/Plugins/TypeFinderTests.cs +++ b/src/Umbraco.Tests/Plugins/TypeFinderTests.cs @@ -22,6 +22,7 @@ using umbraco.uicontrols; using Umbraco.Core.Logging; using Umbraco.Core.Plugins; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.Models.Trees; using Umbraco.Web.Trees; diff --git a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs index 6b1c66c53a..31fe2b7351 100644 --- a/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs +++ b/src/Umbraco.Tests/Runtimes/CoreRuntimeTests.cs @@ -12,6 +12,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using UmbracoExamine; namespace Umbraco.Tests.Runtimes diff --git a/src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs b/src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs index 70caaa40b4..15d160fe33 100644 --- a/src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs +++ b/src/Umbraco.Tests/Services/ContentServicePerformanceTest.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Models.Rdbms; using Umbraco.Core.Persistence.Repositories; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.TestHelpers.Stubs; using Current = Umbraco.Web.Current; namespace Umbraco.Tests.Services diff --git a/src/Umbraco.Tests/Services/ContentServiceTests.cs b/src/Umbraco.Tests/Services/ContentServiceTests.cs index 904d00f3cc..1ab7d2c5f5 100644 --- a/src/Umbraco.Tests/Services/ContentServiceTests.cs +++ b/src/Umbraco.Tests/Services/ContentServiceTests.cs @@ -19,6 +19,7 @@ using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Core.DI; using Umbraco.Core.Events; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Services { diff --git a/src/Umbraco.Tests/Services/PerformanceTests.cs b/src/Umbraco.Tests/Services/PerformanceTests.cs index d968358c14..e8fbfd7105 100644 --- a/src/Umbraco.Tests/Services/PerformanceTests.cs +++ b/src/Umbraco.Tests/Services/PerformanceTests.cs @@ -20,6 +20,7 @@ using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.Services { diff --git a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs index b75e2fb49a..c63c6b218b 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseWebTest.cs @@ -66,7 +66,7 @@ namespace Umbraco.Tests.TestHelpers return new FacadeRouter( TestObjects.GetUmbracoSettings().WebRouting, contentFinders ?? new ContentFinderCollection(Enumerable.Empty()), - new FakeLastChanceFinder(), + new TestLastChanceFinder(), container?.TryGetInstance() ?? new ServiceContext(), new ProfilingLogger(Mock.Of(), Mock.Of())); } diff --git a/src/Umbraco.Tests/TestHelpers/DatabaseBehavior.cs b/src/Umbraco.Tests/TestHelpers/DatabaseBehavior.cs deleted file mode 100644 index 80d351d7fb..0000000000 --- a/src/Umbraco.Tests/TestHelpers/DatabaseBehavior.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Umbraco.Tests.TestHelpers -{ - /// - /// The behavior used to control how the database is handled for test fixtures inheriting from BaseDatabaseFactoryTest - /// - public enum DatabaseBehavior - { - /// - /// A database is not required whatsoever for the fixture - /// - NoDatabasePerFixture, - - /// - /// For each test a new database file and schema will be created - /// - NewDbFileAndSchemaPerTest, - - /// - /// Creates a new database file and schema for the whole fixture, each test will use the pre-existing one - /// - NewDbFileAndSchemaPerFixture, - - /// - /// For each test a new database file without a schema - /// - EmptyDbFilePerTest, - - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs index 1b22be3d3c..c0968ebb11 100644 --- a/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestControllerFactory.cs @@ -5,7 +5,6 @@ using System.Reflection; using System.Web.Mvc; using System.Web.Routing; using System.Web.SessionState; -using NUnit.Framework; using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Plugins; @@ -59,7 +58,7 @@ namespace Umbraco.Tests.TestHelpers.Stubs return null; } - public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) + public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) { return SessionStateBehavior.Disabled; } diff --git a/src/Umbraco.Tests/TestHelpers/TestFacadeAccessor.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestFacadeAccessor.cs similarity index 50% rename from src/Umbraco.Tests/TestHelpers/TestFacadeAccessor.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/TestFacadeAccessor.cs index 34ab185e71..46ebe9a47f 100644 --- a/src/Umbraco.Tests/TestHelpers/TestFacadeAccessor.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestFacadeAccessor.cs @@ -1,8 +1,8 @@ using Umbraco.Web.PublishedCache; -namespace Umbraco.Tests.TestHelpers +namespace Umbraco.Tests.TestHelpers.Stubs { - class TestFacadeAccessor : IFacadeAccessor + public class TestFacadeAccessor : IFacadeAccessor { public IFacade Facade { get; set; } } diff --git a/src/Umbraco.Tests/TestHelpers/TestIndexCollectionAccessor.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestIndexCollectionAccessor.cs similarity index 50% rename from src/Umbraco.Tests/TestHelpers/TestIndexCollectionAccessor.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/TestIndexCollectionAccessor.cs index 4d45f83ac6..3d554275b5 100644 --- a/src/Umbraco.Tests/TestHelpers/TestIndexCollectionAccessor.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestIndexCollectionAccessor.cs @@ -2,16 +2,10 @@ using System.Collections.Generic; using Examine; using UmbracoExamine; -namespace Umbraco.Tests.TestHelpers +namespace Umbraco.Tests.TestHelpers.Stubs { - /// - /// An empty index collection accessor class for testing - /// public class TestIndexCollectionAccessor : IExamineIndexCollectionAccessor { - public IReadOnlyDictionary Indexes - { - get { return new Dictionary(); } - } + public IReadOnlyDictionary Indexes => new Dictionary(); } } \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestLastChanceFinder.cs similarity index 69% rename from src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/TestLastChanceFinder.cs index 83cd02f1f5..14d7325c8d 100644 --- a/src/Umbraco.Tests/TestHelpers/Stubs/FakeLastChanceFinder.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestLastChanceFinder.cs @@ -1,12 +1,12 @@ -using Umbraco.Web.Routing; - -namespace Umbraco.Tests.TestHelpers.Stubs -{ - internal class FakeLastChanceFinder : IContentLastChanceFinder - { - public bool TryFindContent(PublishedContentRequest frequest) - { - return false; - } - } +using Umbraco.Web.Routing; + +namespace Umbraco.Tests.TestHelpers.Stubs +{ + internal class TestLastChanceFinder : IContentLastChanceFinder + { + public bool TryFindContent(PublishedContentRequest frequest) + { + return false; + } + } } \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/TestProfiler.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestProfiler.cs similarity index 96% rename from src/Umbraco.Tests/TestHelpers/TestProfiler.cs rename to src/Umbraco.Tests/TestHelpers/Stubs/TestProfiler.cs index 841f99e260..13334d54cf 100644 --- a/src/Umbraco.Tests/TestHelpers/TestProfiler.cs +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestProfiler.cs @@ -3,7 +3,7 @@ using StackExchange.Profiling; using StackExchange.Profiling.SqlFormatters; using Umbraco.Core.Logging; -namespace Umbraco.Tests.TestHelpers +namespace Umbraco.Tests.TestHelpers.Stubs { public class TestProfiler : IProfiler { diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoContextAccessor.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoContextAccessor.cs new file mode 100644 index 0000000000..dedba75cbc --- /dev/null +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoContextAccessor.cs @@ -0,0 +1,9 @@ +using Umbraco.Web; + +namespace Umbraco.Tests.TestHelpers.Stubs +{ + public class TestUmbracoContextAccessor : IUmbracoContextAccessor + { + public UmbracoContext UmbracoContext { get; set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoDatabaseAccessor.cs b/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoDatabaseAccessor.cs new file mode 100644 index 0000000000..216467a4bc --- /dev/null +++ b/src/Umbraco.Tests/TestHelpers/Stubs/TestUmbracoDatabaseAccessor.cs @@ -0,0 +1,9 @@ +using Umbraco.Core.Persistence; + +namespace Umbraco.Tests.TestHelpers.Stubs +{ + public class TestUmbracoDatabaseAccessor : IUmbracoDatabaseAccessor + { + public UmbracoDatabase UmbracoDatabase { get; set; } + } +} diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs index 2b6b150ef0..730c934c42 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects-Mocks.cs @@ -9,6 +9,7 @@ using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.PublishedCache; using Umbraco.Web.Routing; diff --git a/src/Umbraco.Tests/TestHelpers/TestObjects.cs b/src/Umbraco.Tests/TestHelpers/TestObjects.cs index 68a6cc3874..2e9439498b 100644 --- a/src/Umbraco.Tests/TestHelpers/TestObjects.cs +++ b/src/Umbraco.Tests/TestHelpers/TestObjects.cs @@ -16,6 +16,7 @@ using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Services; using Umbraco.Core.Strings; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web.Services; namespace Umbraco.Tests.TestHelpers diff --git a/src/Umbraco.Tests/TestHelpers/TestUmbracoContextAccessor.cs b/src/Umbraco.Tests/TestHelpers/TestUmbracoContextAccessor.cs deleted file mode 100644 index 57f2a5a576..0000000000 --- a/src/Umbraco.Tests/TestHelpers/TestUmbracoContextAccessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Umbraco.Web; - -namespace Umbraco.Tests -{ - internal class TestUmbracoContextAccessor : IUmbracoContextAccessor - { - public UmbracoContext UmbracoContext { get; set; } - } -} \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/TestUmbracoDatabaseAccessor.cs b/src/Umbraco.Tests/TestHelpers/TestUmbracoDatabaseAccessor.cs deleted file mode 100644 index a37c950415..0000000000 --- a/src/Umbraco.Tests/TestHelpers/TestUmbracoDatabaseAccessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Umbraco.Core.Persistence; - -namespace Umbraco.Tests.TestHelpers -{ - internal class TestUmbracoDatabaseAccessor : IUmbracoDatabaseAccessor - { - public UmbracoDatabase UmbracoDatabase { get; set; } - } -} diff --git a/src/Umbraco.Tests/TestHelpers/TestWithApplicationBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithApplicationBase.cs index 1a80ef46ad..6136d442e4 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithApplicationBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithApplicationBase.cs @@ -16,6 +16,7 @@ using Umbraco.Core.Events; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Plugins; using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web.Services; using UmbracoExamine; diff --git a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs index 75b64d4811..23c318c041 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs @@ -31,6 +31,7 @@ using File = System.IO.File; using Umbraco.Core.DI; using Umbraco.Core.Events; using Umbraco.Core.Strings; +using Umbraco.Tests.TestHelpers.Stubs; namespace Umbraco.Tests.TestHelpers { diff --git a/src/Umbraco.Tests/TestHelpers/TestWithSettingsBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithSettingsBase.cs index a1fb6e791c..d0d01cd10e 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithSettingsBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithSettingsBase.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using Umbraco.Tests.TestHelpers.Stubs; using Current = Umbraco.Web.Current; namespace Umbraco.Tests.TestHelpers diff --git a/src/Umbraco.Tests/TestHelpers/UmbracoTestAttribute.cs b/src/Umbraco.Tests/TestHelpers/UmbracoTestAttribute.cs index b18b3d1a3e..879e169d67 100644 --- a/src/Umbraco.Tests/TestHelpers/UmbracoTestAttribute.cs +++ b/src/Umbraco.Tests/TestHelpers/UmbracoTestAttribute.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Reflection; +using Umbraco.Core; namespace Umbraco.Tests.TestHelpers { diff --git a/src/Umbraco.Tests/MockTests.cs b/src/Umbraco.Tests/Testing/MockTests.cs similarity index 92% rename from src/Umbraco.Tests/MockTests.cs rename to src/Umbraco.Tests/Testing/MockTests.cs index 3f3d61b741..a584d0a44b 100644 --- a/src/Umbraco.Tests/MockTests.cs +++ b/src/Umbraco.Tests/Testing/MockTests.cs @@ -1,103 +1,98 @@ -using System; -using System.Linq; -using System.Web; -using System.Web.Security; -using LightInject; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Configuration.UmbracoSettings; -using Umbraco.Core.Logging; -using Umbraco.Core.Dictionary; -using Umbraco.Core.Services; -using Moq; -using Umbraco.Core.Cache; -using Umbraco.Core.DI; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Tests.TestHelpers; -using Umbraco.Web; -using Umbraco.Web.PublishedCache; -using Umbraco.Web.Routing; -using Umbraco.Web.Security; -using Current = Umbraco.Web.Current; - -namespace Umbraco.Tests -{ - [TestFixture] - public class MockTests : UmbracoTestBase - { - public override void SetUp() - { - base.SetUp(); - - Current.UmbracoContextAccessor = new TestUmbracoContextAccessor(); - } - - [Test] - public void Can_Mock_Service_Context() - { - // ReSharper disable once UnusedVariable - var serviceContext = TestObjects.GetServiceContextMock(); - Assert.Pass(); - } - - [Test] - public void Can_Mock_Database_Context() - { - var databaseFactory = TestObjects.GetDatabaseFactoryMock(); - var logger = Mock.Of(); - var runtimeState = Mock.Of(); - var migrationEntryService = Mock.Of(); - - // ReSharper disable once UnusedVariable - var databaseContext = new DatabaseContext(databaseFactory, logger, runtimeState, migrationEntryService); - Assert.Pass(); - } - - [Test] - public void Can_Mock_Umbraco_Context() - { - var umbracoContext = TestObjects.GetUmbracoContextMock(Current.UmbracoContextAccessor); - Assert.AreEqual(umbracoContext, UmbracoContext.Current); - } - - [Test] - public void Can_Mock_Umbraco_Helper() - { - var umbracoContext = TestObjects.GetUmbracoContextMock(); - - // unless we can inject them in MembershipHelper, we need need this - Container.Register(_ => Mock.Of()); - Container.Register(_ => Mock.Of()); - Container.Register(_ => CacheHelper.CreateDisabledCacheHelper()); - Container.Register(); - - // ReSharper disable once UnusedVariable - var helper = new UmbracoHelper(umbracoContext, - Mock.Of(), - Mock.Of(), - Mock.Of(), - Mock.Of(), - Mock.Of(), - Mock.Of(), - new MembershipHelper(umbracoContext, Mock.Of(), Mock.Of()), - new ServiceContext(), - CacheHelper.CreateDisabledCacheHelper()); - Assert.Pass(); - } - - [Test] - public void Can_Mock_UrlProvider() - { - var umbracoContext = TestObjects.GetUmbracoContextMock(); - - var urlProviderMock = new Mock(); - urlProviderMock.Setup(provider => provider.GetUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) - .Returns("/hello/world/1234"); - var urlProvider = urlProviderMock.Object; - - var theUrlProvider = new UrlProvider(umbracoContext, new [] { urlProvider }); - - Assert.AreEqual("/hello/world/1234", theUrlProvider.GetUrl(1234)); - } - } -} +using System; +using System.Web.Security; +using Moq; +using NUnit.Framework; +using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.Dictionary; +using Umbraco.Core.Logging; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.Services; +using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; +using Umbraco.Web; +using Umbraco.Web.Routing; +using Umbraco.Web.Security; +using Current = Umbraco.Web.Current; + +namespace Umbraco.Tests.Testing +{ + [TestFixture] + public class MockTests : UmbracoTestBase + { + public override void SetUp() + { + base.SetUp(); + + Current.UmbracoContextAccessor = new TestUmbracoContextAccessor(); + } + + [Test] + public void Can_Mock_Service_Context() + { + // ReSharper disable once UnusedVariable + var serviceContext = TestObjects.GetServiceContextMock(); + Assert.Pass(); + } + + [Test] + public void Can_Mock_Database_Context() + { + var databaseFactory = TestObjects.GetDatabaseFactoryMock(); + var logger = Mock.Of(); + var runtimeState = Mock.Of(); + var migrationEntryService = Mock.Of(); + + // ReSharper disable once UnusedVariable + var databaseContext = new DatabaseContext(databaseFactory, logger, runtimeState, migrationEntryService); + Assert.Pass(); + } + + [Test] + public void Can_Mock_Umbraco_Context() + { + var umbracoContext = TestObjects.GetUmbracoContextMock(Current.UmbracoContextAccessor); + Assert.AreEqual(umbracoContext, UmbracoContext.Current); + } + + [Test] + public void Can_Mock_Umbraco_Helper() + { + var umbracoContext = TestObjects.GetUmbracoContextMock(); + + // unless we can inject them in MembershipHelper, we need need this + Container.Register(_ => Mock.Of()); + Container.Register(_ => Mock.Of()); + Container.Register(_ => CacheHelper.CreateDisabledCacheHelper()); + Container.Register(); + + // ReSharper disable once UnusedVariable + var helper = new UmbracoHelper(umbracoContext, + Mock.Of(), + Mock.Of(), + Mock.Of(), + Mock.Of(), + Mock.Of(), + Mock.Of(), + new MembershipHelper(umbracoContext, Mock.Of(), Mock.Of()), + new ServiceContext(), + CacheHelper.CreateDisabledCacheHelper()); + Assert.Pass(); + } + + [Test] + public void Can_Mock_UrlProvider() + { + var umbracoContext = TestObjects.GetUmbracoContextMock(); + + var urlProviderMock = new Mock(); + urlProviderMock.Setup(provider => provider.GetUrl(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns("/hello/world/1234"); + var urlProvider = urlProviderMock.Object; + + var theUrlProvider = new UrlProvider(umbracoContext, new [] { urlProvider }); + + Assert.AreEqual("/hello/world/1234", theUrlProvider.GetUrl(1234)); + } + } +} diff --git a/src/Umbraco.Tests/NUnitTests.cs b/src/Umbraco.Tests/Testing/NUnitTests.cs similarity index 98% rename from src/Umbraco.Tests/NUnitTests.cs rename to src/Umbraco.Tests/Testing/NUnitTests.cs index 2d7b345409..bc8f660723 100644 --- a/src/Umbraco.Tests/NUnitTests.cs +++ b/src/Umbraco.Tests/Testing/NUnitTests.cs @@ -1,6 +1,6 @@ using NUnit.Framework; -namespace Umbraco.Tests +namespace Umbraco.Tests.Testing { // these 4 test classes validate that our test class pattern *should* be: // - test base classes *not* marked as [TestFixture] but having a virtual SetUp diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 88dc716b2c..cd0addfbf4 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -227,38 +227,36 @@ - + - - + - - - - + + + - - + + - + @@ -268,7 +266,7 @@ - + @@ -298,7 +296,7 @@ - + @@ -394,7 +392,7 @@ - + @@ -452,10 +450,10 @@ - + - + @@ -494,7 +492,7 @@ - + True True @@ -502,7 +500,7 @@ - + @@ -530,7 +528,6 @@ - @@ -564,14 +561,14 @@ - - + + - + - + @@ -580,10 +577,10 @@ - + - - + + diff --git a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs index 68c1aada49..f95cf09499 100644 --- a/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/RenderIndexActionSelectorAttributeTests.cs @@ -14,6 +14,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Profiling; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.Models; using Umbraco.Web.Mvc; diff --git a/src/Umbraco.Tests/Web/Mvc/RenderModelBinderTests.cs b/src/Umbraco.Tests/Web/Mvc/RenderModelBinderTests.cs index 9c16d6dbb6..f57aec0776 100644 --- a/src/Umbraco.Tests/Web/Mvc/RenderModelBinderTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/RenderModelBinderTests.cs @@ -7,6 +7,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web.Models; using Umbraco.Web.Mvc; using Current = Umbraco.Web.Current; diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs index e614d8d285..690a3a709e 100644 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs @@ -12,6 +12,7 @@ using Umbraco.Core.Dictionary; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache; diff --git a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs index 4f2256d15b..0de68f6a38 100644 --- a/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs +++ b/src/Umbraco.Tests/Web/WebExtensionMethodTests.cs @@ -7,6 +7,7 @@ using Moq; using NUnit.Framework; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Tests.TestHelpers; +using Umbraco.Tests.TestHelpers.Stubs; using Umbraco.Web; using Umbraco.Web.Mvc; using Umbraco.Web.PublishedCache;