// /* ============================================================================ // Copyright 2014 Hewlett Packard // // 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. // ============================================================================ */ using System; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Openstack.Common.ServiceLocation; namespace Openstack.Test.ServiceLocation { [TestClass] public class ServiceLocatorTests { public interface ITestEchoService { string Echo(string msg); } public class TestEchoService : ITestEchoService { public string Echo(string msg) { return msg; } } public class TestReverseEchoService : ITestEchoService { public string Echo(string msg) { return new string(msg.Reverse().ToArray()); } } internal class TestServiceLocator : IServiceLocator { public T Locate() { throw new NotImplementedException(); } } internal class TestServiceManager : IServiceLocationRuntimeManager { public void RegisterServiceInstance(TService instance) { throw new NotImplementedException(); } public void RegisterServiceInstance(Type type, object instance) { throw new NotImplementedException(); } public void RegisterServiceType(Type type) { throw new NotImplementedException(); } public void RegisterServiceType() where TConcretion : class, TInterface { throw new NotImplementedException(); } public void RegisterServiceType(Type type, Type registrationValue) { throw new NotImplementedException(); } } internal class TestServiceOverrideManager : IServiceLocationOverrideManager { public void RegisterServiceInstance(TService instance) { throw new NotImplementedException(); } public void RegisterServiceInstance(Type type, object instance) { throw new NotImplementedException(); } public void RegisterServiceType(Type type) { throw new NotImplementedException(); } public void RegisterServiceType() where TConcretion : class, TInterface { throw new NotImplementedException(); } public void RegisterServiceType(Type type, Type registrationValue) { throw new NotImplementedException(); } } [TestInitialize] public void Initialize() { ServiceLocator.Reset(); } [TestCleanup] public void TestCleanup() { ServiceLocator.Reset(); } [TestMethod] public void CanRegisterAndLocateAService() { var myServiceInstance = new TestEchoService(); var manager = ServiceLocator.Instance.Locate(); Assert.IsNotNull(manager); manager.RegisterServiceInstance(myServiceInstance); var service = ServiceLocator.Instance.Locate(); Assert.IsNotNull(service); Assert.AreEqual("Works", service.Echo("Works")); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotLocateAServiceThatHasNotBeenRegistered() { ServiceLocator.Instance.Locate(); } [TestMethod] public void CanOverrideAndLocateAService() { var echoServiceInstance = new TestEchoService(); var reverseEchoServiceInstance = new TestReverseEchoService(); var runtimeManager = ServiceLocator.Instance.Locate(); var overrrideManager = ServiceLocator.Instance.Locate(); Assert.IsNotNull(runtimeManager); Assert.IsNotNull(overrrideManager); runtimeManager.RegisterServiceInstance(echoServiceInstance); overrrideManager.RegisterServiceInstance(reverseEchoServiceInstance); var service = ServiceLocator.Instance.Locate(); Assert.IsNotNull(service); Assert.IsInstanceOfType(service, typeof(TestReverseEchoService)); Assert.AreEqual("skroW", service.Echo("Works")); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterNullService() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance(null); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterNullType() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance(null,new TestEchoService()); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterNonInterface() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance("Hello!"); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterAServiceLocator() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance(new TestServiceLocator()); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterAServiceManager() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance(new TestServiceManager()); } [TestMethod] [ExpectedException(typeof(InvalidOperationException))] public void CannotRegisterAnOverrideManager() { var runtimeManager = ServiceLocator.Instance.Locate(); runtimeManager.RegisterServiceInstance(new TestServiceOverrideManager()); } } }