diff --git a/Spring.Net.2010.sln b/Spring.Net.2010.sln index 00f4fb73..0925182d 100644 --- a/Spring.Net.2010.sln +++ b/Spring.Net.2010.sln @@ -78,6 +78,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Data.NHibernate31.20 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Data.NHibernate31.Tests.2010", "test\Spring\Spring.Data.NHibernate31.Tests\Spring.Data.NHibernate31.Tests.2010.csproj", "{867628D8-DFCC-4D2A-A786-EB0D97DBC3DC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Web.Mvc3.2010", "src\Spring\Spring.Web.Mvc3\Spring.Web.Mvc3.2010.csproj", "{904C97E0-3667-4D12-A55F-6CC2E6F68A0A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Web.Mvc3.Tests.2010", "test\Spring\Spring.Web.Mvc3.Tests\Spring.Web.Mvc3.Tests.2010.csproj", "{1E8E7C0A-4474-44F6-AA67-FABB40FA9784}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|.NET = Debug|.NET @@ -458,6 +462,26 @@ Global {867628D8-DFCC-4D2A-A786-EB0D97DBC3DC}.Release|Any CPU.Build.0 = Release|Any CPU {867628D8-DFCC-4D2A-A786-EB0D97DBC3DC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {867628D8-DFCC-4D2A-A786-EB0D97DBC3DC}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Debug|.NET.ActiveCfg = Debug|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Release|.NET.ActiveCfg = Release|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Release|Any CPU.Build.0 = Release|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Debug|.NET.ActiveCfg = Debug|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Release|.NET.ActiveCfg = Release|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Release|Any CPU.Build.0 = Release|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784}.Release|Mixed Platforms.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Spring/Spring.Web.Mvc/SpringControllerFactory.cs b/src/Spring/Spring.Web.Mvc/SpringControllerFactory.cs index 42bdfd93..efaf6e7f 100644 --- a/src/Spring/Spring.Web.Mvc/SpringControllerFactory.cs +++ b/src/Spring/Spring.Web.Mvc/SpringControllerFactory.cs @@ -116,7 +116,7 @@ namespace Spring.Web.Mvc var controllers = ApplicationContext.GetObjectsOfType(controllerType); if (controllers.Count > 0) { - controller = (IController)controllers.Cast().First().Value; + controller = (IController)controllers.Cast().First().Value; } } else diff --git a/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContext.cs b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContext.cs new file mode 100644 index 00000000..bfb6cf4b --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContext.cs @@ -0,0 +1,148 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Spring.Context.Support; +using Spring.Core.IO; + +namespace Spring.Context.Support +{ + /// + /// Application Context for ASP.NET MVC Applications + /// + public class MvcApplicationContext : AbstractXmlApplicationContext + { + private readonly string[] _configurationLocations; + + private readonly IResource[] _configurationResources; + + /// + /// Create a new MvcApplicationContext, loading the definitions + /// from the given XML resource. + /// + /// The application context name. + /// Flag specifying whether to make this context case sensitive or not. + /// Names of configuration resources. + public MvcApplicationContext(string name, bool caseSensitive, params string[] configurationLocations) + : this(new MvcApplicationContextArgs(name, null, configurationLocations, null, caseSensitive)) + { + } + + /// + /// Create a new MvcApplicationContext with the given parent, + /// loading the definitions from the given XML resources. + /// + /// The application context name. + /// Flag specifying whether to make this context case sensitive or not. + /// The parent context. + /// Names of configuration resources. + public MvcApplicationContext(string name, bool caseSensitive, IApplicationContext parentContext, + params string[] configurationLocations) + : this(new MvcApplicationContextArgs(name, parentContext, configurationLocations, null, caseSensitive)) + { } + + /// + /// Initializes a new instance of the class. + /// + /// The args. + public MvcApplicationContext(MvcApplicationContextArgs args) + : base(args.Name, args.CaseSensitive, args.ParentContext) + { + _configurationLocations = args.ConfigurationLocations; + _configurationResources = args.ConfigurationResources; + + Refresh(); + + if (log.IsDebugEnabled) + { + log.Debug("created instance " + this.ToString()); + } + } + + /// + /// Create a new MvcApplicationContext, loading the definitions + /// from the given XML resource. + /// + /// The application context name. + /// Flag specifying whether to make this context case sensitive or not. + /// Names of configuration resources. + /// Configuration resources. + public MvcApplicationContext(string name, bool caseSensitive, string[] configurationLocations, IResource[] configurationResources) + : this(new MvcApplicationContextArgs(name, null, configurationLocations, configurationResources, caseSensitive)) + { + } + + /// + /// Create a new MvcApplicationContext, loading the definitions + /// from the given XML resource. + /// + /// Names of configuration resources. + public MvcApplicationContext(params string[] configurationLocations) + : this(new MvcApplicationContextArgs(string.Empty, null, configurationLocations, null, false)) + { + } + + /// + /// An array of resource locations, referring to the XML object + /// definition files that this context is to be built with. + /// + /// + /// + ///

+ /// Examples of the format of the various strings that would be + /// returned by accessing this property can be found in the overview + /// documentation of with the + /// class. + ///

+ ///
+ /// + /// An array of resource locations, or if none. + /// + protected override string[] ConfigurationLocations + { + get { return _configurationLocations; } + } + + /// + /// An array of resources that this context is to be built with. + /// + /// + /// + ///

+ /// Examples of the format of the various strings that would be + /// returned by accessing this property can be found in the overview + /// documentation of with the + /// class. + ///

+ ///
+ /// + /// An array of s, or if none. + /// + protected override IResource[] ConfigurationResources + { + get { return _configurationResources; } + } + + } +} diff --git a/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContextArgs.cs b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContextArgs.cs new file mode 100644 index 00000000..d8828dea --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcApplicationContextArgs.cs @@ -0,0 +1,76 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + + +using System; +using Spring.Core.IO; + +namespace Spring.Context.Support +{ + /// + /// Encapsulates arguments to the class. + /// + public class MvcApplicationContextArgs : AbstractXmlApplicationContextArgs + { + + private const bool DEFAULT_CASESENSITIVE = false; + private const bool DEFAULT_REFRESH = false; + + /// + /// Initializes a new instance of the MvcApplicationContextArgs class. + /// + public MvcApplicationContextArgs() + { + CaseSensitive = DEFAULT_CASESENSITIVE; + } + + /// + /// Initializes a new instance of the MvcApplicationContextArgs class. + /// + /// The name. + /// The parent context. + /// The configuration locations. + /// The configuration resources. + public MvcApplicationContextArgs(string name, IApplicationContext parentContext, string[] configurationLocations, IResource[] configurationResources) + : this(name, parentContext, configurationLocations, configurationResources, DEFAULT_CASESENSITIVE) + { } + + + /// + /// Initializes a new instance of the MvcApplicationContextArgs class. + /// + /// The name. + /// The parent context. + /// The configuration locations. + /// The configuration resources. + /// if set to true [case sensitive]. + public MvcApplicationContextArgs(string name, IApplicationContext parentContext, string[] configurationLocations, IResource[] configurationResources, bool caseSensitive) + { + Name = name; + ParentContext = parentContext; + ConfigurationLocations = configurationLocations; + ConfigurationResources = configurationResources; + CaseSensitive = caseSensitive; + Refresh = DEFAULT_REFRESH; + } + + + } +} diff --git a/src/Spring/Spring.Web.Mvc3/Context/Support/MvcContextHandler.cs b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcContextHandler.cs new file mode 100644 index 00000000..cfadc4bb --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/Context/Support/MvcContextHandler.cs @@ -0,0 +1,63 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Spring.Context.Support; + +namespace Spring.Context.Support +{ + /// + /// Context Handler for ASP.NET MVC Applications + /// + public class MvcContextHandler : ContextHandler + { + + /// + /// The of + /// created if no type attribute is specified on a context element. + /// + /// + protected override Type DefaultApplicationContextType + { + get { return typeof(MvcApplicationContext); } + } + + + /// + /// Get the context's case-sensitivity to use if none is specified + /// + /// + /// + ///

+ /// Derived handlers may override this property to change their default case-sensitivity. + ///

+ ///

+ /// Defaults to 'true'. + ///

+ ///
+ protected override bool DefaultCaseSensitivity + { + get { return false; } + } + } +} diff --git a/src/Spring/Spring.Web.Mvc3/Spring.Web.Mvc3.2010.csproj b/src/Spring/Spring.Web.Mvc3/Spring.Web.Mvc3.2010.csproj new file mode 100644 index 00000000..3f687926 --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/Spring.Web.Mvc3.2010.csproj @@ -0,0 +1,80 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A} + Library + Properties + Spring.Web.Mvc + Spring.Web.Mvc3 + v4.0 + 512 + + + + true + full + false + ..\..\..\build\VS.Net.2010\Spring.Web.Mvc3\Debug\ + TRACE;DEBUG;NET_3_5;NET_4_0 + prompt + 4 + true + Spring.Web.Mvc3.xml + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\..\lib\Net\3.5\Common.Logging.dll + True + + + + + + + + + + + + + + + CommonAssemblyInfo.cs + + + + + + + + + + {710961A3-0DF4-49E4-A26E-F5B9C044AC84} + Spring.Core.2010 + True + + + + + + + + \ No newline at end of file diff --git a/src/Spring/Spring.Web.Mvc3/SpringMvcApplication.cs b/src/Spring/Spring.Web.Mvc3/SpringMvcApplication.cs new file mode 100644 index 00000000..be1d7851 --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/SpringMvcApplication.cs @@ -0,0 +1,79 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Web; +using System.Web.Mvc; +using Spring.Objects.Factory; +using Spring.Core.IO; +using Spring.Objects.Factory.Xml; +using System.Web.Routing; +using Spring.Context.Support; + +namespace Spring.Web.Mvc +{ + /// + /// Spring.NET-specific HttpApplication for ASP.NET MVC integration. + /// + public abstract class SpringMvcApplication : HttpApplication + { + /// + /// Executes custom initialization code after all event handler modules have been added. + /// + public override void Init() + { + base.Init(); + + //the Spring HTTP Module won't have built the context for us until now so we have to delay until the init + ConfigureApplicationContext(); + RegisterResolver(); + } + + /// + /// Configures the instance. + /// + /// + /// You must override this method in a derived class to control the manner in which the + /// is configured. + /// + protected virtual void ConfigureApplicationContext() + { + + } + + + /// + /// Registers the DependencyResolver implementation with the MVC runtime. + /// + /// You must override this method in a derived class to control the manner in which the + /// is registered. + /// + /// + public virtual void RegisterResolver() + { + DependencyResolver.SetResolver(new SpringMvcDependencyResolver(ContextRegistry.GetContext())); + } + + } +} diff --git a/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs b/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs new file mode 100644 index 00000000..c70b6159 --- /dev/null +++ b/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Web.Mvc; +using Spring.Context; +using Spring.Context.Support; + +namespace Spring.Web.Mvc +{ + /// + /// Spring-based implementation of the interface. + /// + public class SpringMvcDependencyResolver : IDependencyResolver + { + + private IApplicationContext _context; + + /// + /// Initializes a new instance of the class. + /// + /// The context. + public SpringMvcDependencyResolver(IApplicationContext context) + { + _context = context; + } + + + /// + /// Gets the application context. + /// + /// The application context. + public IApplicationContext ApplicationContext + { + get + { + if (_context == null || _context.Name != ApplicationContextName) + { + if (string.IsNullOrEmpty(ApplicationContextName)) + { + _context = ContextRegistry.GetContext(); + } + else + { + _context = ContextRegistry.GetContext(ApplicationContextName); + } + } + + return _context; + } + } + + /// + /// Gets or sets the name of the application context. + /// + /// + /// Defaults to using the root (default) Application Context. + /// + /// The name of the application context. + public static string ApplicationContextName { get; set; } + + + /// + /// Resolves singly registered services that support arbitrary object creation. + /// + /// The type of the requested service or object. + /// The requested service or object. + public object GetService(Type serviceType) + { + object service = null; + + if (serviceType != null) + { + var services = ApplicationContext.GetObjectsOfType(serviceType); + if (services.Count > 0) + { + service = services.Cast().First().Value; + } + } + + return service; + } + + /// + /// Resolves multiply registered services. + /// + /// The type of the requested services. + /// The requested services. + public IEnumerable GetServices(Type serviceType) + { + var services = ApplicationContext.GetObjectsOfType(serviceType); + return services.Cast(); + } + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/ControllerFactoryTestExtension.cs b/test/Spring/Spring.Web.Mvc3.Tests/ControllerFactoryTestExtension.cs new file mode 100644 index 00000000..42d792b4 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/ControllerFactoryTestExtension.cs @@ -0,0 +1,65 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using System.Web; +using System.Web.Routing; +using Spring.Core.IO; +using Spring.Objects.Factory; +using Spring.Objects.Factory.Xml; +using System.Web.Mvc; +using Spring.Web.Mvc.Tests.Controllers; +using Spring.Context.Support; +using System.Reflection; + +namespace Spring.Web.Mvc.Tests +{ + public static class ControllerFactoryTestExtension + { + private static readonly PropertyInfo _typeCacheProperty; + private static readonly FieldInfo _cacheField; + + static ControllerFactoryTestExtension() + { + _typeCacheProperty = typeof(DefaultControllerFactory).GetProperty("ControllerTypeCache", BindingFlags.Instance | BindingFlags.NonPublic); + _cacheField = _typeCacheProperty.PropertyType.GetField("_cache", BindingFlags.NonPublic | BindingFlags.Instance); + } + + /// + /// Replaces the cache field of a the DefaultControllerFactory's ControllerTypeCache. + /// This ensures that only the specified controller types will be searched when instantiating a controller. + /// As the ControllerTypeCache is internal, this uses some reflection hackery. + /// + public static void InitializeWithControllerTypes(this IControllerFactory factory, params Type[] controllerTypes) + { + var cache = controllerTypes + .GroupBy(t => t.Name.Substring(0, t.Name.Length - "Controller".Length), StringComparer.OrdinalIgnoreCase) + .ToDictionary(g => g.Key, g => g.ToLookup(t => t.Namespace ?? string.Empty, StringComparer.OrdinalIgnoreCase), StringComparer.OrdinalIgnoreCase); + + var buildManager = _typeCacheProperty.GetValue(factory, null); + _cacheField.SetValue(buildManager, cache); + } + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/Controllers/FirstContainerRegisteredController.cs b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/FirstContainerRegisteredController.cs new file mode 100644 index 00000000..d11f1366 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/FirstContainerRegisteredController.cs @@ -0,0 +1,31 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System; +using System.Web; +using System.Web.Mvc; + +namespace Spring.Web.Mvc.Tests.Controllers +{ + public class FirstContainerRegisteredController : Controller + { + public string TestValue { get; set; } + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NamedContextController.cs b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NamedContextController.cs new file mode 100644 index 00000000..444aa022 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NamedContextController.cs @@ -0,0 +1,31 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System; +using System.Web; +using System.Web.Mvc; + +namespace Spring.Web.Mvc.Tests.Controllers +{ + public class NamedContextController : Controller + { + + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NotInContainerController.cs b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NotInContainerController.cs new file mode 100644 index 00000000..f804f564 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/NotInContainerController.cs @@ -0,0 +1,31 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System; +using System.Web; +using System.Web.Mvc; + +namespace Spring.Web.Mvc.Tests.Controllers +{ + public class NotInContainerController : Controller + { + + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/Controllers/SecondContainerRegisteredController.cs b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/SecondContainerRegisteredController.cs new file mode 100644 index 00000000..b27fef00 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/Controllers/SecondContainerRegisteredController.cs @@ -0,0 +1,32 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + + +using System; +using System.Web; +using System.Web.Mvc; + +namespace Spring.Web.Mvc.Tests.Controllers +{ + public class SecondContainerRegisteredController : Controller + { + + } +} diff --git a/test/Spring/Spring.Web.Mvc3.Tests/Spring.Web.Mvc3.Tests.2010.csproj b/test/Spring/Spring.Web.Mvc3.Tests/Spring.Web.Mvc3.Tests.2010.csproj new file mode 100644 index 00000000..f515a699 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/Spring.Web.Mvc3.Tests.2010.csproj @@ -0,0 +1,94 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {1E8E7C0A-4474-44F6-AA67-FABB40FA9784} + Library + Properties + Spring.Web.Mvc3.Tests + Spring.Web.Mvc3.Tests + v4.0 + 512 + + + + true + full + false + ..\..\..\build\VS.Net.2010\Spring.Web.Mvc3.Tests\Debug\ + TRACE;DEBUG;NET_3_5;NET_4_0 + prompt + 4 + true + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\..\lib\Net\2.0\nunit.framework.dll + True + + + + + + + + + + + + + + + CommonAssemblyInfo.cs + + + + + + + + + + + {710961A3-0DF4-49E4-A26E-F5B9C044AC84} + Spring.Core.2010 + True + + + {904C97E0-3667-4D12-A55F-6CC2E6F68A0A} + Spring.Web.Mvc3.2010 + + + + + Always + + + + + Always + + + + + + + + \ No newline at end of file diff --git a/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs b/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs new file mode 100644 index 00000000..f26ef3f6 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs @@ -0,0 +1,116 @@ +#region License + +/* + * Copyright © 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using NUnit.Framework; +using Spring.Context.Support; +using Spring.Web.Mvc.Tests.Controllers; + +namespace Spring.Web.Mvc.Tests +{ +// internal class MockContext : HttpContextBase +// { +// } + + [TestFixture] + public class SpringMvcDependencyResolverTests + { + #region Setup/Teardown + + [SetUp] + public void _TestSetup() + { + ContextRegistry.Clear(); + _context = new MvcApplicationContext("file://objects.xml"); + _mvcNamedContext = new MvcApplicationContext("named", false, "file://namedContextObjects.xml"); + + ContextRegistry.RegisterContext(_context); + ContextRegistry.RegisterContext(_mvcNamedContext); + + _resolver = new SpringMvcDependencyResolver(_context); + + SpringMvcDependencyResolver.ApplicationContextName = string.Empty; + } + + #endregion + + private MvcApplicationContext _context; + private MvcApplicationContext _mvcNamedContext; + private SpringMvcDependencyResolver _resolver; + + [Test] + public void CanUseNamedContextToResolveType() + { + SpringMvcDependencyResolver.ApplicationContextName = "named"; + var service = _resolver.GetService(typeof (NamedContextController)); + + Assert.NotNull(service); + } + + [Test] + public void CanUseUnnamedContextToResolveType() + { + Assert.That(SpringMvcDependencyResolver.ApplicationContextName, Is.Empty, "PRECONDITION: Resolver has named-context improperly set!"); + + var service = _resolver.GetService(typeof (FirstContainerRegisteredController)); + Assert.NotNull(service); + } + + [Test] + public void RequestForNullTypeReturnsNull() + { + var service = _resolver.GetService(null); + Assert.That(service, Is.Null); + } + + [Test] + public void CanReturnCollectionOfTypes() + { + var services = _resolver.GetServices(typeof(FirstContainerRegisteredController)); + Assert.That(services.Count(), Is.EqualTo(2)); + } + + [Test] + public void RequestForSingleInstanceOfMultiplyRegisteredTypeReturnsFirstDefinition() + { + var service = (FirstContainerRegisteredController)_resolver.GetService(typeof(FirstContainerRegisteredController)); + Assert.That(service.TestValue, Is.EqualTo("First Definition of Type")); + } + + [Test] + public void RequestForSingleUndefinedTypeReturnsNull() + { + var service = _resolver.GetService(typeof(NotInContainerController)); + Assert.That(service, Is.Null); + } + + [Test] + public void RequestForCollectionOfUndefinedTypesReturnsEmptyCollection() + { + var services = _resolver.GetServices(typeof(NotInContainerController)); + Assert.That(services, Is.Empty); + } + + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Web.Mvc3.Tests/namedContextObjects.xml b/test/Spring/Spring.Web.Mvc3.Tests/namedContextObjects.xml new file mode 100644 index 00000000..a8f11e48 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/namedContextObjects.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/test/Spring/Spring.Web.Mvc3.Tests/objects.xml b/test/Spring/Spring.Web.Mvc3.Tests/objects.xml new file mode 100644 index 00000000..bfd59553 --- /dev/null +++ b/test/Spring/Spring.Web.Mvc3.Tests/objects.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + +