From 08b64a2a4a925eb3bf266d82559d28a75dff6448 Mon Sep 17 00:00:00 2001 From: harshgMSFT Date: Tue, 17 Jun 2014 19:26:46 -0700 Subject: [PATCH] InlineConstraint Functional Test, removing the dependency on a physical file. --- .../InlineConstraintTests.cs | 39 ++++++++----------- ...Microsoft.AspNet.Mvc.FunctionalTests.kproj | 1 - .../config/InlineConstraintTestsConfig.json | 10 ----- .../project.json | 3 +- .../DefaultCommandLineArgumentBuilder.cs | 23 +++++++++++ .../ICommandLineArgumentProvider.cs | 14 +++++++ .../InlineConstraintsWebSite.kproj | 2 + .../InlineConstraintsWebSite/Startup.cs | 9 ++++- 8 files changed, 65 insertions(+), 36 deletions(-) delete mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/config/InlineConstraintTestsConfig.json create mode 100644 test/WebSites/InlineConstraintsWebSite/DefaultCommandLineArgumentBuilder.cs create mode 100644 test/WebSites/InlineConstraintsWebSite/ICommandLineArgumentProvider.cs diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs index 8ea1f2ed1f..d706de8900 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/InlineConstraintTests.cs @@ -2,8 +2,10 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.IO; using System.Reflection; +using System.Text; using System.Threading.Tasks; using InlineConstraints; using Microsoft.AspNet.Builder; @@ -22,31 +24,23 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests { private readonly IServiceProvider _provider; private readonly Action _app = new Startup().Configure; - private readonly string _jsonConfigFilePath; - private readonly Configuration _config = new Configuration(); + public InlineConstraintTests() { _provider = TestHelper.CreateServices("InlineConstraintsWebSite"); - - // TODO: Hardcoding the config file path for now. Update it to read it from args. - _jsonConfigFilePath = @"config\InlineConstraintTestsConfig.json"; - _config.AddJsonFile(_jsonConfigFilePath); - - Environment.SetEnvironmentVariable("AppConfigPath", _jsonConfigFilePath); + _provider = new ServiceCollection() + .AddScoped() + .BuildServiceProvider(_provider); } [Fact] public async Task RoutingToANonExistantArea_WithExistConstraint_RoutesToCorrectAction() { - // Arrange - var source = new JsonConfigurationSource(_jsonConfigFilePath); - - // Add the exists inline constraint. - _config.Set("TemplateCollection:areaRoute:TemplateValue", - @"{area:exists}/{controller=Home}/{action=Index}"); - _config.Set("TemplateCollection:actionAsMethod:TemplateValue", - @"{controller=Home}/{action=Index}"); - _config.Commit(); + var svc = _provider.GetService(); + svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue="+ + "{area:exists}/{controller=Home}/{action=Index}"); + svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue="+ + "{controller=Home}/{action=Index}"); var server = TestServer.Create(_provider, _app); var client = server.Handler; @@ -64,12 +58,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests public async Task RoutingToANonExistantArea_WithoutExistConstraint_RoutesToIncorrectAction() { // Arrange - _config.Set("TemplateCollection:areaRoute:TemplateValue", - @"{area}/{controller=Home}/{action=Index}"); - _config.Set("TemplateCollection:actionAsMethod:TemplateValue", - @"{controller=Home}/{action=Index}"); - - _config.Commit(); + var svc = _provider.GetService(); + svc.AddArgument("--TemplateCollection:areaRoute:TemplateValue="+ + "{area}/{controller=Home}/{action=Index}"); + svc.AddArgument("--TemplateCollection:actionAsMethod:TemplateValue"+ + "={controller=Home}/{action=Index}"); var server = TestServer.Create(_provider, _app); var client = server.Handler; diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj index 37f8a4a27b..030e6bfb80 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Microsoft.AspNet.Mvc.FunctionalTests.kproj @@ -27,7 +27,6 @@ - diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/config/InlineConstraintTestsConfig.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/config/InlineConstraintTestsConfig.json deleted file mode 100644 index 95d271a2d8..0000000000 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/config/InlineConstraintTestsConfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "TemplateCollection": { - "areaRoute": { - "TemplateValue": "" - }, - "actionAsMethod": { - "TemplateValue": "" - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index cee07bd612..5b695e4e8f 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -12,7 +12,8 @@ "xunit.assert": "2.0.0-aspnet-*", "xunit.core": "2.0.0-aspnet-*", "Xunit.KRunner": "0.1-alpha-*", - "Microsoft.Framework.ConfigurationModel.Json": "0.1-alpha-*" + "Microsoft.Framework.ConfigurationModel.Json": "0.1-alpha-*", + "Microsoft.Framework.DependencyInjection": "0.1-alpha-*" }, "commands": { "test": "Xunit.KRunner" diff --git a/test/WebSites/InlineConstraintsWebSite/DefaultCommandLineArgumentBuilder.cs b/test/WebSites/InlineConstraintsWebSite/DefaultCommandLineArgumentBuilder.cs new file mode 100644 index 0000000000..5d1badf786 --- /dev/null +++ b/test/WebSites/InlineConstraintsWebSite/DefaultCommandLineArgumentBuilder.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; + +namespace InlineConstraints +{ + public class DefaultCommandLineArgumentBuilder : ICommandLineArgumentBuilder + { + private readonly List _args = new List(); + + public void AddArgument(string arg) + { + _args.Add(arg); + } + + public IEnumerable Build() + { + return _args; + } + } +} \ No newline at end of file diff --git a/test/WebSites/InlineConstraintsWebSite/ICommandLineArgumentProvider.cs b/test/WebSites/InlineConstraintsWebSite/ICommandLineArgumentProvider.cs new file mode 100644 index 0000000000..88ea0c64bd --- /dev/null +++ b/test/WebSites/InlineConstraintsWebSite/ICommandLineArgumentProvider.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; + +namespace InlineConstraints +{ + public interface ICommandLineArgumentBuilder + { + void AddArgument(string arg); + + IEnumerable Build(); + } +} \ No newline at end of file diff --git a/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj b/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj index d695d4c847..e851782a03 100644 --- a/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj +++ b/test/WebSites/InlineConstraintsWebSite/InlineConstraintsWebSite.kproj @@ -33,6 +33,8 @@ + + diff --git a/test/WebSites/InlineConstraintsWebSite/Startup.cs b/test/WebSites/InlineConstraintsWebSite/Startup.cs index ff420c5177..ca8accbc27 100644 --- a/test/WebSites/InlineConstraintsWebSite/Startup.cs +++ b/test/WebSites/InlineConstraintsWebSite/Startup.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Routing; @@ -13,6 +14,7 @@ namespace InlineConstraints public class Startup { public Action RouteCollectionProvider { get; set; } + public void Configure(IBuilder app) { // Set up application services @@ -28,12 +30,17 @@ namespace InlineConstraints var config = new Configuration(); config.AddEnvironmentVariables(); - + var commandLineBuilder = app.ApplicationServices.GetService(); string appConfigPath; if (config.TryGet("AppConfigPath", out appConfigPath)) { config.AddJsonFile(appConfigPath); } + else if (commandLineBuilder != null) + { + var args = commandLineBuilder.Build(); + config.AddCommandLine(args.ToArray()); + } else { var basePath = app.ApplicationServices.GetService().ApplicationBasePath;