Permalink
Browse files

Added "WhatTimeIsIt" sample

  • Loading branch information...
1 parent 92787b3 commit 94c7550a61d1f68567268548e9d5f15e8abe44a1 @SteveSanderson committed Mar 9, 2010
Showing with 849 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. BIN Samples/ExternalAssemblies/Deleporter.dll
  3. BIN Samples/ExternalAssemblies/Moq.dll
  4. BIN Samples/ExternalAssemblies/Ninject.dll
  5. BIN Samples/ExternalAssemblies/nunit.framework.dll
  6. +27 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/Infrastructure/NinjectControllerFactoryUtils.cs
  7. +31 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/Infrastructure/TestFixtureBase.cs
  8. +28 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/Infrastructure/TidyupUtils.cs
  9. +36 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/Properties/AssemblyInfo.cs
  10. +42 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/SimpleTest.cs
  11. +76 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.IntegrationTest/WhatTimeIsIt.IntegrationTest.csproj
  12. +26 −0 Samples/WhatTimeIsIt/WhatTimeIsIt.sln
  13. +19 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Controllers/HomeController.cs
  14. +1 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Global.asax
  15. +29 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Global.asax.cs
  16. +26 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/NinjectControllerFactory.cs
  17. +35 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Properties/AssemblyInfo.cs
  18. +9 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Services/IDateProvider.cs
  19. +9 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Services/RealDateProvider.cs
  20. +23 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Views/Home/Index.aspx
  21. +35 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Views/Web.config
  22. +30 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Web.Debug.config
  23. +31 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Web.Release.config
  24. +205 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/Web.config
  25. +124 −0 Samples/WhatTimeIsIt/WhatTimeIsIt/WhatTimeIsIt.csproj
View
@@ -0,0 +1,7 @@
+*.suo
+*.user
+bin
+obj
+*.pdb
+_ReSharper*
+*.ReSharper.user
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,27 @@
+using System.Linq;
+using System.Web.Mvc;
+
+namespace WhatTimeIsIt.IntegrationTest.Infrastructure
+{
+ public static class NinjectControllerFactoryUtils
+ {
+ public static void TemporarilyReplaceBinding<TService>(TService implementation)
+ {
+ var controllerFactory = (NinjectControllerFactory)ControllerBuilder.Current.GetControllerFactory();
+ var kernel = controllerFactory.Kernel;
+
+ // Remove existing bindings and replace with new one
+ var originalBindings = kernel.GetBindings(typeof (TService)).ToList();
+ foreach (var originalBinding in originalBindings)
+ kernel.RemoveBinding(originalBinding);
+ var replacementBinding = kernel.Bind<TService>().ToConstant(implementation).Binding;
+
+ // Clear up by doing the reverse
+ TidyupUtils.AddTidyupTask(() => {
+ kernel.RemoveBinding(replacementBinding);
+ foreach (var originalBinding in originalBindings)
+ kernel.AddBinding(originalBinding);
+ });
+ }
+ }
+}
@@ -0,0 +1,31 @@
+using System;
+using System.Net;
+using System.Text.RegularExpressions;
+using DeleporterCore.Client;
+using NUnit.Framework;
+
+namespace WhatTimeIsIt.IntegrationTest.Infrastructure
+{
+ public class TestFixtureBase
+ {
+ protected static string GetPageHtml(string url)
+ {
+ return new WebClient().DownloadString("http://localhost:8081" + url);
+ }
+
+ protected static T ParseSimpleDomElement<T>(string html, string elementId)
+ {
+ var match = Regex.Match(html, @"<.*? id=['""]" + elementId + @"['""].*?>(.*?)</.*?>", RegexOptions.Singleline);
+ string elementText = match.Success ? match.Groups[1].Captures[0].Value : null;
+ return (T)Convert.ChangeType(elementText, typeof(T));
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ // Runs any tidyup tasks in both the local and remote appdomains
+ TidyupUtils.PerformTidyup();
+ Deleporter.Run(TidyupUtils.PerformTidyup);
+ }
+ }
+}
@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+
+namespace WhatTimeIsIt.IntegrationTest.Infrastructure
+{
+ /// <summary>
+ /// Since you can't usually run multiple integration tests in parallel, this approach to tidying up makes no attempt to
+ /// manage separate collections of tasks for concurrently running tests. If your test suite does support parallelization
+ /// to some extent, you might need to enhance this class.
+ /// </summary>
+ public static class TidyupUtils
+ {
+ private static readonly List<Action> _tasks = new List<Action>();
+
+ public static void AddTidyupTask(Action task) { _tasks.Add(task); }
+
+ public static void PerformTidyup()
+ {
+ try {
+ foreach (var task in _tasks)
+ task();
+ }
+ finally {
+ _tasks.Clear();
+ }
+ }
+ }
+}
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("WhatTimeIsIt.IntegrationTest")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("WhatTimeIsIt.IntegrationTest")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("bbc6f734-d519-4535-a2e3-da56f2fe7618")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,42 @@
+using System;
+using DeleporterCore.Client;
+using Moq;
+using NUnit.Framework;
+using WhatTimeIsIt.IntegrationTest.Infrastructure;
+using WhatTimeIsIt.Services;
+
+namespace WhatTimeIsIt.IntegrationTest
+{
+ /// <summary>
+ /// Of course, in a real application, you'd be using a browser automation tool such as WatiN or Selenium to automate the UI.
+ /// In this example, I'm directly issuing HTTP requests and parsing the results just so you can run it without needing to set up any browser automation tool.
+ /// </summary>
+ [TestFixture]
+ public class SimpleTest : TestFixtureBase
+ {
+ [Test]
+ public void DisplaysCurrentYear()
+ {
+ var html = GetPageHtml("/");
+ var displayedDate = ParseSimpleDomElement<DateTime>(html, "date");
+ Assert.AreEqual(DateTime.Now.Year, displayedDate.Year);
+ }
+
+ [Test]
+ public void DisplaysSpecialMessageIfWebServerHasSomehowGoneBackInTime()
+ {
+ // Inject a mock IDateProvider, setting the clock back to 1975
+ var dateToSimulate = new DateTime(1975, 1, 1);
+ Deleporter.Run(() => {
+ var mockDateProvider = new Mock<IDateProvider>();
+ mockDateProvider.Setup(x => x.CurrentDate).Returns(dateToSimulate);
+ NinjectControllerFactoryUtils.TemporarilyReplaceBinding(mockDateProvider.Object);
+ });
+
+ // Now see what it displays
+ var html = GetPageHtml("/");
+ var message = ParseSimpleDomElement<string>(html, "extraInfo");
+ StringAssert.Contains("The world wide web hasn't been invented yet", message);
+ }
+ }
+}
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{8969EB8E-4B39-4277-B23A-99ABF114C950}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>WhatTimeIsIt.IntegrationTest</RootNamespace>
+ <AssemblyName>WhatTimeIsIt.IntegrationTest</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Deleporter">
+ <HintPath>..\..\ExternalAssemblies\Deleporter.dll</HintPath>
+ </Reference>
+ <Reference Include="Moq">
+ <HintPath>..\..\ExternalAssemblies\Moq.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject">
+ <HintPath>..\..\ExternalAssemblies\Ninject.dll</HintPath>
+ </Reference>
+ <Reference Include="nunit.framework, Version=2.5.3.9345, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\ExternalAssemblies\nunit.framework.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Infrastructure\NinjectControllerFactoryUtils.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SimpleTest.cs" />
+ <Compile Include="Infrastructure\TidyupUtils.cs" />
+ <Compile Include="Infrastructure\TestFixtureBase.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\WhatTimeIsIt\WhatTimeIsIt.csproj">
+ <Project>{7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}</Project>
+ <Name>WhatTimeIsIt</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,26 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhatTimeIsIt", "WhatTimeIsIt\WhatTimeIsIt.csproj", "{7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhatTimeIsIt.IntegrationTest", "WhatTimeIsIt.IntegrationTest\WhatTimeIsIt.IntegrationTest.csproj", "{8969EB8E-4B39-4277-B23A-99ABF114C950}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7CA4AC8A-5459-4456-874F-95E4ADD5C4EA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {8969EB8E-4B39-4277-B23A-99ABF114C950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8969EB8E-4B39-4277-B23A-99ABF114C950}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8969EB8E-4B39-4277-B23A-99ABF114C950}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8969EB8E-4B39-4277-B23A-99ABF114C950}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,19 @@
+using System.Web.Mvc;
+using WhatTimeIsIt.Services;
+
+namespace WhatTimeIsIt.Controllers
+{
+ public class HomeController : Controller
+ {
+ private readonly IDateProvider _dateProvider;
+ public HomeController(IDateProvider dateProvider)
+ {
+ _dateProvider = dateProvider;
+ }
+
+ public ActionResult Index()
+ {
+ return View(_dateProvider.CurrentDate);
+ }
+ }
+}
@@ -0,0 +1 @@
+<%@ Application Codebehind="Global.asax.cs" Inherits="WhatTimeIsIt.MvcApplication" Language="C#" %>
@@ -0,0 +1,29 @@
+using System.Web;
+using System.Web.Mvc;
+using System.Web.Routing;
+
+namespace WhatTimeIsIt
+{
+ public class MvcApplication : HttpApplication
+ {
+ public static void RegisterRoutes(RouteCollection routes)
+ {
+ routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
+
+ routes.MapRoute(
+ "Default", // Route name
+ "{controller}/{action}/{id}", // URL with parameters
+ new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
+ );
+
+ }
+
+ protected void Application_Start()
+ {
+ AreaRegistration.RegisterAllAreas();
+
+ RegisterRoutes(RouteTable.Routes);
+ ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
+ }
+ }
+}
@@ -0,0 +1,26 @@
+using System;
+using System.Web.Mvc;
+using Ninject;
+using Ninject.Modules;
+using WhatTimeIsIt.Services;
+
+namespace WhatTimeIsIt
+{
+ public class NinjectControllerFactory : DefaultControllerFactory
+ {
+ public readonly IKernel Kernel = new StandardKernel(new ServiceModule());
+
+ protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
+ {
+ return controllerType != null ? (IController) Kernel.Get(controllerType) : null;
+ }
+
+ private class ServiceModule : NinjectModule
+ {
+ public override void Load()
+ {
+ Bind<IDateProvider>().To<RealDateProvider>();
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 94c7550

Please sign in to comment.