コード例 #1
0
        public void Get_Resolver_DoesNot_Throw_Exception_If_ProcessResource_Was_Called()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.config", Path.GetTempPath());

            PropertiesInterpreter interpreter = new PropertiesInterpreter(path);
            WindsorContainer      container   = new WindsorContainer();

            interpreter.ProcessResource(interpreter.Source, container.Kernel.ConfigurationStore, container.Kernel);

            ConfigurationProcessingException actual = null;
            IPropertyResolver resolver = null;

            // act
            try
            {
                resolver = interpreter.Resolver;
            }
            catch (ConfigurationProcessingException e)
            {
                actual = e;
            }

            // assert
            Assert.IsNull(actual);
            Assert.IsNotNull(resolver);
        }
コード例 #2
0
        //[Platform(Exclude = "Linux")]
        public void RelativePathSubDependencyResolver_Resolves_RelativePaths_As_Expected_When_No_PropertiesResolver()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "relpath-castle.config", m_tempPath);

            const string connString = "server=localhost;user=sa";

            WindsorContainer container = new WindsorContainer(path);

            container.Kernel.Resolver.AddSubResolver(new RelativePathSubDependencyResolver(container.Kernel));

            // act
            RelPathTestClass obj = container.Resolve <RelPathTestClass>();

            // assert
            Assert.IsNotNull(obj);
            Assert.AreEqual(m_getFullPath(@"..\etc\config.ini"), obj.PathParam);
            Assert.AreEqual(3, obj.PathArrParam.Length);
            Assert.AreEqual(m_getFullPath(@"..\etc\config1.ini"), obj.PathArrParam[0]);

            if (!PlatformHelper.IsUnix())
            {
                Assert.AreEqual(@"C:\temp.ini", obj.PathArrParam[1]);
            }

            Assert.AreEqual(m_getFullPath(@"..\etc\second.ini"), obj.PathArrParam[2]);
            Assert.AreEqual(connString, obj.MySqlConnection.ConnectionString);
        }
コード例 #3
0
        public void Get_Resolver_Throws_Exception_If_ProcessResource_Was_Not_Called()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.config", Path.GetTempPath());

            PropertiesInterpreter interpreter = new PropertiesInterpreter(path);

            ConfigurationProcessingException expected =
                new ConfigurationProcessingException("Properties file has not been processed yet. Have you missed calling PropertiesInterpreter.ProcessResource(IResource,IConfigurationStore,IKernel)");
            ConfigurationProcessingException actual = null;

            // act
            try
            {
                IPropertyResolver resolver = interpreter.Resolver;
            }
            catch (ConfigurationProcessingException e)
            {
                actual = e;
            }

            // assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(expected.Message, actual.Message);
        }
コード例 #4
0
        public void Get_Resolver_DoesNot_Throws_Exception_Once_SubSystem_Is_Initialised()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.config", Path.GetTempPath());

            PropertiesSubSystem subSystem = new PropertiesSubSystem(path);
            WindsorContainer    container = new WindsorContainer();

            container.Kernel.AddSubSystem(PropertiesSubSystem.SubSystemKey, subSystem);

            KernelException   actual   = null;
            IPropertyResolver resolver = null;

            // act
            try
            {
                resolver = subSystem.Resolver;
            }
            catch (KernelException e)
            {
                actual = e;
            }

            // assert
            Assert.IsNull(actual);
            Assert.IsNotNull(resolver);
        }
コード例 #5
0
        public void Get_Resolver_Throws_Exception_If_SubSystem_Not_Initialised()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "invalid-castle.config", Path.GetTempPath());
            PropertiesSubSystem subSystem = new PropertiesSubSystem(path);

            const string      expectedMessagePrefix = "Error processing node castle";
            string            actualMessage         = string.Empty;
            IPropertyResolver resolver = null;

            // act
            try
            {
                resolver = subSystem.Resolver;
                WindsorContainer container = new WindsorContainer();
                container.Kernel.AddSubSystem(PropertiesSubSystem.SubSystemKey, subSystem);
            }
            catch (ConfigurationProcessingException e)
            {
                actualMessage = e.Message;
            }

            // assert
            Assert.IsNull(resolver);
            Assert.IsTrue(actualMessage.StartsWith(expectedMessagePrefix));
        }
コード例 #6
0
        public void SubSystem_From_Json_File_Initialises_Properly()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.json", Path.GetTempPath());

            PropertiesSubSystem subsystem = new PropertiesSubSystem(path);
            WindsorContainer    container = new WindsorContainer();

            // act
            subsystem.Init((IKernelInternal)container.Kernel);

            // assert
            Assert.AreEqual("mihir", subsystem.Resolver.GetValue("name"));
            CollectionAssert.AreEqual(new[] { "chess", "cricket" }, subsystem.Resolver.GetValue <string[]>("hobbies"));

            IDictionary <string, string> extrasStuffMixed = subsystem.Resolver.GetValue <Dictionary <string, string> >("extraStuffMixed");

            Assert.AreEqual("31", extrasStuffMixed["age"]);
            Assert.AreEqual("male", extrasStuffMixed["sex"]);

            IDictionary <string, double> extrasStuffFloats = subsystem.Resolver.GetValue <Dictionary <string, double> >("extraStuffFloats");

            Assert.AreEqual(4.0, extrasStuffFloats["precision"]);
            Assert.AreEqual(2.56, extrasStuffFloats["start"]);
        }
コード例 #7
0
        public void GetValue_Simple_Works_As_Expected()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.config", Path.GetTempPath());

            PropertiesSubSystem subSystem = new PropertiesSubSystem(path);
            WindsorContainer    container = new WindsorContainer();

            container.Kernel.AddSubSystem(PropertiesSubSystem.SubSystemKey, subSystem);

            IConversionManager manager = (IConversionManager)container.Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);

            manager.Add(new PersonTypeConverter());

            IPropertyResolver resolver = subSystem.Resolver;

            // act
            string strParam = resolver.GetValue("strParam");
            double dblParam = resolver.GetValue <double>("dblParam");

            string[] arrParam = resolver.GetValue <string[]>("arrParam");
            IDictionary <string, string> dictParam = resolver.GetValue <IDictionary <string, string> >("dictParam");
            List <double> listParam = resolver.GetValue <List <double> >("listParam");

            Person[] personArr = resolver.GetValue <Person[]>("personArr");

            // assert
            Assert.AreEqual("this is string", strParam);
            Assert.AreEqual(20.12, dblParam);
            Assert.AreEqual("arr item 1", arrParam[0]);
            Assert.AreEqual(strParam, arrParam[1]);
            Assert.AreEqual(@"..\etc\config.ini", arrParam[2]);
            Assert.IsTrue(dictParam.ContainsKey("key1"));
            Assert.IsTrue(dictParam.ContainsKey("key2"));
            Assert.AreEqual("value 1", dictParam["key1"]);
            Assert.AreEqual("value 2", dictParam["key2"]);
            Assert.AreEqual(21.2, listParam[0]);
            Assert.AreEqual(3, listParam[1]);
            Assert.AreEqual(dblParam, listParam[2]);
            Assert.AreEqual("Mihir", personArr[0].Name);
            Assert.AreEqual(30, personArr[0].PersonAge);
            Assert.AreEqual("Sneha", personArr[1].Name);
            Assert.AreEqual(33, personArr[1].PersonAge);

            // act
            Dependency[] dependencies = resolver.TryGetDependencies <TestClass>("strParam", "arrParam", "refParam", "dictParam", "listParam", "personArr");

            container.Register(Component.For <TestClass>().DependsOn(dependencies));

            // assert
            Assert.IsNotNull(container.Resolve <TestClass>());
        }
コード例 #8
0
        public void Ctor_Test()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "castle.config", Path.GetTempPath());

            // act
            PropertyResolvingWindsorContainer container = new PropertyResolvingWindsorContainer(path);

            // assert
            Assert.IsInstanceOf <PropertiesInterpreter>(container.Interpreter);

            // get properties subsystem from kernel
            PropertiesSubSystem subsystem = container.Kernel.GetSubSystem <PropertiesSubSystem>(PropertiesSubSystem.SubSystemKey);

            // assert
            Assert.IsNotNull(subsystem);
            Assert.AreEqual("this is string", subsystem.Resolver.GetValue("strParam"));
        }
コード例 #9
0
        public void RelativePathSubDependencyResolver_Resolves_RelativePaths_As_Expected_With_PropertiesResolver()
        {
            // arrange
            string path = EmbeddedResourceUtil.ExportToPath("Castle.Windsor.Extensions.Test.data", "relpath-castle-with-propertiesresolver.config", m_tempPath);

            const string connString = "server=localhost;user=sa";

            PropertiesSubSystem subSystem = new PropertiesSubSystem(path);
            WindsorContainer    container = new WindsorContainer();

            container.Kernel.AddSubSystem(PropertiesSubSystem.SubSystemKey, subSystem);
            container.Kernel.Resolver.AddSubResolver(new RelativePathSubDependencyResolver(container.Kernel));

            container.Register(Component
                               .For <SqlConnection>()
                               .DependsOn(Dependency.OnValue("connectionString", subSystem.Resolver.GetValue("dbConnectionString"))));

            container.Register(Component
                               .For <RelPathTestClass>()
                               .DependsOn(
                                   subSystem.Resolver.GetDependency <string>("pathParam"),
                                   subSystem.Resolver.GetDependency <string[]>("pathArrParam"),
                                   Dependency.OnComponent("mySqlConnection", typeof(SqlConnection))
                                   ));

            // act
            RelPathTestClass obj = container.Resolve <RelPathTestClass>();

            // assert
            Assert.IsNotNull(obj);
            Assert.AreEqual(m_getFullPath(@"..\etc\config.ini"), obj.PathParam);
            Assert.AreEqual(3, obj.PathArrParam.Length);
            Assert.AreEqual(m_getFullPath(@"..\etc\config1.ini"), obj.PathArrParam[0]);

            if (!PlatformHelper.IsUnix())
            {
                Assert.AreEqual(@"C:\temp.ini", obj.PathArrParam[1]);
            }

            Assert.AreEqual(m_getFullPath(@"..\etc\second.ini"), obj.PathArrParam[2]);
            Assert.AreEqual(connString, obj.MySqlConnection.ConnectionString);
        }