public void SunnyDay()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("age", "${maxResults}");
            pvs.Add("name", "${name}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();
            CommandLineArgsVariableSource vs1 = new CommandLineArgsVariableSource(
                new string[] { "program.exe", "file.txt", "/name:Aleks Seovic", "/framework:Spring.NET" });
            variableSources.Add(vs1);

            ConfigSectionVariableSource vs2 = new ConfigSectionVariableSource();
            vs2.SectionName = "DaoConfiguration";
            variableSources.Add(vs2);


            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);

            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");
            Assert.AreEqual(1000, tb1.Age);
            Assert.AreEqual("Aleks Seovic", tb1.Name);

        }
 public void AddMessageTest()
 {
     StaticApplicationContext ctx = new StaticApplicationContext();
     ctx.Refresh();
     ctx.AddMessage("code1", CultureInfo.CurrentUICulture, "this is {0}");
     Assert.AreEqual("this is Spring.NET", ctx.GetMessage("code1", "Spring.NET"));
 }
        public void CanCreateFromExplicitConfiguration()
        {
            string SESSIONFACTORY_OBJECTNAME = "SessionFactory";
            string ENTITYINTERCEPTOR_OBJECTNAME = "EntityInterceptor";

            MockRepository mocks = new MockRepository();
            ISessionFactory expectedSessionFactory =  (ISessionFactory) mocks.CreateMock(typeof(ISessionFactory));
            IInterceptor expectedEntityInterceptor = (IInterceptor) mocks.CreateMock(typeof(IInterceptor));
            bool expectedSingleSession = false;
            FlushMode expectedDefaultFlushMode = FlushMode.Auto;

            // create and register context
            StaticApplicationContext appCtx = new StaticApplicationContext();
            appCtx.Name = AbstractApplicationContext.DefaultRootContextName;
            appCtx.ObjectFactory.RegisterSingleton(SESSIONFACTORY_OBJECTNAME, expectedSessionFactory);
            appCtx.ObjectFactory.RegisterSingleton(ENTITYINTERCEPTOR_OBJECTNAME, expectedEntityInterceptor);
            ContextRegistry.Clear();
            ContextRegistry.RegisterContext(appCtx);

            // simulate config section
            string thisTypeName = this.GetType().FullName;
            DictionaryVariableSource variableSource = new DictionaryVariableSource()
                .Add(thisTypeName + ".SessionFactoryObjectName", SESSIONFACTORY_OBJECTNAME)
                .Add(thisTypeName + ".EntityInterceptorObjectName", ENTITYINTERCEPTOR_OBJECTNAME)
                .Add(thisTypeName + ".SingleSession", expectedSingleSession.ToString().ToLower() ) // case insensitive!
                .Add(thisTypeName + ".DefaultFlushMode", expectedDefaultFlushMode.ToString().ToLower() ) // case insensitive!
                ;

            ConfigSectionSessionScopeSettings settings = new ConfigSectionSessionScopeSettings(this.GetType(), variableSource);

            Assert.AreEqual( expectedSessionFactory, settings.SessionFactory );
            Assert.AreEqual( expectedEntityInterceptor, settings.EntityInterceptor );
            Assert.AreEqual( expectedSingleSession, settings.SingleSession );
            Assert.AreEqual( expectedDefaultFlushMode, settings.DefaultFlushMode );
        }
        public void CanCreateWithDefaults()
        {
            string SESSIONFACTORY_OBJECTNAME = ConfigSectionSessionScopeSettings.DEFAULT_SESSION_FACTORY_OBJECT_NAME;

            // setup expected values
            MockRepository mocks = new MockRepository();
            ISessionFactory expectedSessionFactory = mocks.StrictMock<ISessionFactory>();
            IInterceptor expectedEntityInterceptor = null;
            bool expectedSingleSession = SessionScopeSettings.SINGLESESSION_DEFAULT;
            FlushMode expectedDefaultFlushMode = SessionScopeSettings.FLUSHMODE_DEFAULT;

            // create and register context
            StaticApplicationContext appCtx = new StaticApplicationContext();
            appCtx.Name = AbstractApplicationContext.DefaultRootContextName;
            appCtx.ObjectFactory.RegisterSingleton(SESSIONFACTORY_OBJECTNAME, expectedSessionFactory);
            ContextRegistry.Clear();
            ContextRegistry.RegisterContext(appCtx);

            ConfigSectionSessionScopeSettings settings = new ConfigSectionSessionScopeSettings(this.GetType(), (IVariableSource)null);

            Assert.AreEqual(expectedSessionFactory, settings.SessionFactory);
            Assert.AreEqual(expectedEntityInterceptor, settings.EntityInterceptor);
            Assert.AreEqual(expectedSingleSession, settings.SingleSession);
            Assert.AreEqual(expectedDefaultFlushMode, settings.DefaultFlushMode);
        }
		public void Throw_on_get_object_by_wrong_type()
		{
			var ctx = new StaticApplicationContext();
			ctx.ObjectFactory.RegisterSingleton("test", new SimpleType());

			Assert.Throws<ObjectNotOfRequiredTypeException>(() => ctx.GetObject<string>("test"));
		}
		public void Get_named_object()
		{			
			var ctx = new StaticApplicationContext();
			ctx.ObjectFactory.RegisterSingleton("test", new SimpleType());

			Assert.That(ctx.GetObject<SimpleType>("test"), Is.Not.Null);
		}
 public void RegisterObjectPrototype()
 {
     StaticApplicationContext ctx = new StaticApplicationContext();
     ctx.RegisterPrototype("my object", typeof(MockContextAwareObject), null);
     MockContextAwareObject ctx1 = (MockContextAwareObject) ctx.GetObject("my object");
     MockContextAwareObject ctx2 = (MockContextAwareObject) ctx.GetObject("my object");
     Assert.IsTrue(ctx1 != ctx2);
     Assert.IsTrue(!ctx.IsSingleton("my object"));
 }
Esempio n. 8
0
 public void TestAfterPropertiesSet_ApplicationContextJobDataKeySetWithApplicationContext()
 {
     const string objectName = "springJobDetailObject";
     jobDetail.ObjectName = objectName;
     StaticApplicationContext ctx = new StaticApplicationContext();
     jobDetail.ApplicationContext = ctx;
     string key = "applicationContextJobDataKey";
     jobDetail.ApplicationContextJobDataKey = key;
     jobDetail.AfterPropertiesSet();
     Assert.AreSame(ctx, jobDetail.ApplicationContext, "ApplicationContext was not set correctly");
     Assert.AreSame(ctx, jobDetail.JobDataMap[key], "ApplicationContext was not set to job data map");
 }
        public void ThrowsOnMissingVariableSources()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer();

            try
            {
                vphc.PostProcessObjectFactory(ac.ObjectFactory);
                Assert.Fail("Expected ArgumentException not thrown.");
            }
            catch (ArgumentException)
            {
            }
        }
Esempio n. 10
0
        public void Exe1()
        {
            var ctx2 = new StaticApplicationContext();
            //  var ctx = ContextRegistry.GetContext(); // 全局配置

            var ctx = new XmlApplicationContext(System.AppDomain.CurrentDomain.BaseDirectory + "bin/SpringNet/objects.xml"); // 局部配置
            //ctx.RegisterPrototype("Person", typeof(Student), null);
            //ctx.RegisterSingleton("Alice", typeof(Person), null);
            // 注册可以通过xml配置注册
            Person person = ctx.GetObject("Person") as Person;
            Person alice = ctx.GetObject("Alice") as Person;

            var a = 1;
        }
 public void ThrowsOnInvalidVariableSourcesElement()
 {
     StaticApplicationContext ac = new StaticApplicationContext();
     VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer();
     vphc.VariableSources = new ArrayList(new object[] { new object() });
     
     try
     {
         vphc.PostProcessObjectFactory(ac.ObjectFactory);
         Assert.Fail("Expected ArgumentException not thrown.");
     }
     catch (ArgumentException)
     {
     }
 }
        public void ChainedResolution()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("name", "${name}");
            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();
            variableSources.Add(new DictionaryVariableSource(new string[] { "name", "name-value" }));
            variableSources.Add(new DictionaryVariableSource(new string[] { "nickname", "nickname-value" }));
            VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);
            ac.AddObjectFactoryPostProcessor(vphc);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");
            Assert.AreEqual("name-value", tb1.Name);
            Assert.AreEqual("nickname-value", tb1.Nickname);
        }
 public void AddPropertyValue()
 {
     StaticApplicationContext ac = new StaticApplicationContext();
     ac.RegisterSingleton("tb1", typeof(TestObject), new MutablePropertyValues());
     ac.RegisterSingleton("tb2", typeof(TestObject), new MutablePropertyValues());
     MutablePropertyValues pvs = new MutablePropertyValues();
     pvs.Add("Properties", "<spring-config><add key=\"tb1.Age\" value=\"99\"/><add key=\"tb2.Name\" value=\"test\"/></spring-config>");
     ac.RegisterSingleton("configurer1", typeof(PropertyOverrideConfigurer), pvs);
     pvs = new MutablePropertyValues();
     pvs.Add("Properties", "<spring-config><add key=\"tb2.Age\" value=\"99\"/><add key=\"tb2.Name\" value=\"test\"/></spring-config>");
     pvs.Add("order", "0");
     ac.RegisterSingleton("configurer2", typeof(PropertyOverrideConfigurer), pvs);
     ac.Refresh();
     TestObject tb1 = (TestObject)ac.GetObject("tb1");
     TestObject tb2 = (TestObject)ac.GetObject("tb2");
     Assert.AreEqual(99, tb1.Age);
     Assert.AreEqual(99, tb2.Age);
     Assert.AreEqual(null, tb1.Name);
     Assert.AreEqual("test", tb2.Name);
 }
        public void BailsOnUnresolvableVariable()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();
            variableSources.Add(new DictionaryVariableSource(new string[] { }));
            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);
            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);

            try
            {
                ac.Refresh();
                Assert.Fail("something changed wrt VariablePlaceholder resolution");
            }
            catch (ObjectDefinitionStoreException ex)
            {
                Assert.IsTrue(ex.Message.IndexOf("nickname") > -1);
            }
        }
        public void WithUnresolvableEnvironmentProperty()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof (TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("environmentVariableMode", EnvironmentVariableMode.Never);
            ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();
        }
Esempio n. 16
0
 public void TestSchedulerAutoStartsOnContextRefreshedEventByDefault()
 {
     StaticApplicationContext context = new StaticApplicationContext();
     context.RegisterObjectDefinition("scheduler", new RootObjectDefinition(typeof (SchedulerFactoryObject)));
     IScheduler scheduler = (IScheduler) context.GetObject("scheduler", typeof (IScheduler));
     Assert.IsFalse(scheduler.IsStarted);
     context.Refresh();
     Assert.IsTrue(scheduler.IsStarted);
 }
 private static StaticApplicationContext CreateTestContext()
 {
     object testObject;
     StaticApplicationContext appContext = new StaticApplicationContext();
     appContext.RegisterSingleton("testObject", typeof(object), null);
     appContext.RegisterSingleton("testObject1", typeof(object), null);
     appContext.RegisterSingleton("testObject2", typeof(object), null);
     appContext.Refresh();
     testObject = appContext.GetObject("testObject");
     Assert.IsNotNull(testObject);
     return appContext;
 }
        public void OverridePropertyReference()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("Spouse", new RuntimeObjectReference("spouse1"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            ac.RegisterSingleton("spouse1", typeof(TestObject), new MutablePropertyValues());
            ac.RegisterSingleton("spouse2", typeof(TestObject), new MutablePropertyValues());

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"tb1.Spouse\" value=\"spouse2\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyOverrideConfigurer), pvs);

            ac.Refresh();
            TestObject tb1 = (TestObject)ac.GetObject("tb1");
            TestObject spouse2 = (TestObject)ac.GetObject("spouse2");
            Assert.AreEqual(spouse2, tb1.Spouse);
        }
        public void OverridePropertyExpression()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("Age", new ExpressionHolder("26+1"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"tb1.Age\" value=\"26-1\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyOverrideConfigurer), pvs);

            ac.Refresh();
            TestObject tb1 = (TestObject)ac.GetObject("tb1");
            Assert.AreEqual(25, tb1.Age);
        }
Esempio n. 20
0
 public void RemovesContextFromRegistryWhenContextCloses()
 {
     StaticApplicationContext appCtx = new StaticApplicationContext();
     appCtx.Name = "myCtx";
     ContextRegistry.RegisterContext(appCtx);
     Assert.IsTrue(ContextRegistry.IsContextRegistered(appCtx.Name));
     appCtx.Dispose();
     Assert.IsFalse(ContextRegistry.IsContextRegistered(appCtx.Name));
 }
        public void WithOverridingEnvironmentProperty()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof (TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("PROCESSOR_ARCHITECTURE", "G5");
            pvs.Add("properties", nvc);
            pvs.Add("environmentVariableMode", EnvironmentVariableMode.Override);
            ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject) ac["to"];
            Assert.AreEqual(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"),
                to.Touchy);
        }
        public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage)
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            if (hasParentContext)
            {
                StaticApplicationContext parent = new StaticApplicationContext();
                parent.Refresh();
                ac.ParentContext = parent;
            }

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("resourceManagers", resourceManagerList);
            if (useCodeAsDefaultMessage)
            {
                pvs.Add("UseCodeAsDefaultMessage", true);
            }
            ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs);
            ac.Refresh();


            // Localizaiton fallbacks
            GetMessageLocalizationFallbacks(ac);

            // MessageSourceAccessor functionality
            MessageSourceAccessor accessor = new MessageSourceAccessor(ac);

            Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));

            // IMessageSourceResolveable
            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");

            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) };
            Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments));


            // test default message without and with args
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null));
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments));

            /* not supported
             * Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null));
             */

            Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments));

            // test resolvable with default message, without and with args
            resolvable = new DefaultMessageSourceResolvable(null, null, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(null, arguments, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            /* not supported
             *  resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
             *  Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
             */

            resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default");
            Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));


            // test message args
            Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" }));

            /* not supported
             *  Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null));
             */


            /* not supported
             *  Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped"));
             * }
             * else
             * {
             *  Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped"));
             * }
             */


            try
            {
                Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage"));
                if (!useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have thrown NoSuchMessagException");
                }
            }
            catch (NoSuchMessageException e)
            {
                if (useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have returned code as default message.");
                    e.ToString();
                }
            }
        }
        public void WithExpressionProperty()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("age", new ExpressionHolder("${age}"));
            ac.RegisterSingleton("to1", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("age", "'0x7FFFFFFF'");
            pvs.Add("properties", nvc);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to1 = (TestObject)ac.GetObject("to1");;
            Assert.AreEqual(2147483647, to1.Age);
        }
Esempio n. 24
0
        public void TestSchedulerAutoStartupFalse()
        {
            StaticApplicationContext context = new StaticApplicationContext();
            ObjectDefinitionBuilder beanDefinition = ObjectDefinitionBuilder
                .GenericObjectDefinition(typeof(SchedulerFactoryObject))
                .AddPropertyValue("autoStartup", false);

            context.RegisterObjectDefinition("scheduler", beanDefinition.ObjectDefinition);
            IScheduler scheduler = (IScheduler) context.GetObject("scheduler", typeof(IScheduler));

            Assert.IsFalse(scheduler.IsStarted);
            context.Refresh();
            Assert.IsFalse(scheduler.IsStarted);
        }
        public void WithEnvironmentPropertyNotUsed()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof (TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("PROCESSOR_ARCHITECTURE", "G5");
            pvs.Add("properties", nvc);
            ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject) ac["to"];
            Assert.AreEqual("G5", to.Touchy, "Fallback mode is not respecting previously set values.");
        }
 public void WithCircularReference()
 {
     StaticApplicationContext ac = new StaticApplicationContext();
     MutablePropertyValues pvs = new MutablePropertyValues();
     pvs.Add("age", "${age}");
     pvs.Add("name", "name${var}");
     pvs.Add("spouse", new RuntimeObjectReference("${ref}"));
     ac.RegisterSingleton("tb1", typeof (TestObject), pvs);
     pvs = new MutablePropertyValues();
     pvs.Add("age", "${age}");
     pvs.Add("name", "name${age}");
     ac.RegisterSingleton("tb2", typeof (TestObject), pvs);
     pvs = new MutablePropertyValues();
     pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"99\"/><add key=\"var\" value=\"${m}var\"/><add key=\"ref\" value=\"tb2\"/><add key=\"m\" value=\"${var}\"/></spring-config>");
     ac.RegisterSingleton("configurer1", typeof (PropertyPlaceholderConfigurer), pvs);
     pvs = new MutablePropertyValues();
     pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"98\"/></spring-config>");
     pvs.Add("order", "0");
     ac.RegisterSingleton("configurer2", typeof (PropertyPlaceholderConfigurer), pvs);
     ac.Refresh();
 }
        public void SunnyDay()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("age", "${age}");
            RootObjectDefinition def
                = new RootObjectDefinition("${fqn}", new ConstructorArgumentValues(), pvs);
            ac.RegisterObjectDefinition("tb3", def);

            pvs = new MutablePropertyValues();
            pvs.Add("age", "${age}");
            pvs.Add("name", "name${var}${");
            pvs.Add("spouse", new RuntimeObjectReference("${ref}"));
            ac.RegisterSingleton("tb1", typeof (TestObject), pvs);

            ConstructorArgumentValues cas = new ConstructorArgumentValues();
            cas.AddIndexedArgumentValue(1, "${age}");
            cas.AddGenericArgumentValue("${var}name${age}");

            pvs = new MutablePropertyValues();
            ArrayList friends = new ManagedList();
            friends.Add("na${age}me");
            friends.Add(new RuntimeObjectReference("${ref}"));
            pvs.Add("friends", friends);

            ISet someSet = new ManagedSet();
            someSet.Add("na${age}me");
            someSet.Add(new RuntimeObjectReference("${ref}"));
            pvs.Add("someSet", someSet);

            IDictionary someDictionary = new ManagedDictionary();
            someDictionary["key1"] = new RuntimeObjectReference("${ref}");
            someDictionary["key2"] = "${age}name";
            MutablePropertyValues innerPvs = new MutablePropertyValues();
            someDictionary["key3"] = new RootObjectDefinition(typeof (TestObject), innerPvs);
            someDictionary["key4"] = new ChildObjectDefinition("tb1", innerPvs);
            pvs.Add("someMap", someDictionary);

            RootObjectDefinition definition = new RootObjectDefinition(typeof (TestObject), cas, pvs);
            ac.DefaultListableObjectFactory.RegisterObjectDefinition("tb2", definition);

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"98\"/><add key=\"var\" value=\"${m}var\"/><add key=\"ref\" value=\"tb2\"/><add key=\"m\" value=\"my\"/><add key=\"fqn\" value=\"Spring.Objects.TestObject, Spring.Core.Tests\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject) ac.GetObject("tb1");
            TestObject tb2 = (TestObject) ac.GetObject("tb2");
            TestObject tb3 = (TestObject) ac.GetObject("tb3");
            Assert.AreEqual(98, tb1.Age);
            Assert.AreEqual(98, tb2.Age);
            Assert.AreEqual(98, tb3.Age);
            Assert.AreEqual("namemyvar${", tb1.Name);
            Assert.AreEqual("myvarname98", tb2.Name);
            Assert.AreEqual(tb2, tb1.Spouse);
            Assert.AreEqual(2, tb2.Friends.Count);
            IEnumerator ie = tb2.Friends.GetEnumerator();
            ie.MoveNext();
            Assert.AreEqual("na98me", ie.Current);
            ie.MoveNext();
            Assert.AreEqual(tb2, ie.Current);
            Assert.AreEqual(2, tb2.SomeSet.Count);
            Assert.IsTrue(tb2.SomeSet.Contains("na98me"));
            Assert.IsTrue(tb2.SomeSet.Contains(tb2));
            Assert.AreEqual(4, tb2.SomeMap.Count);
            Assert.AreEqual(tb2, tb2.SomeMap["key1"]);
            Assert.AreEqual("98name", tb2.SomeMap["key2"]);
            TestObject inner1 = (TestObject) tb2.SomeMap["key3"];
            TestObject inner2 = (TestObject) tb2.SomeMap["key4"];
            Assert.AreEqual(0, inner1.Age);
            Assert.AreEqual(null, inner1.Name);
            Assert.AreEqual(98, inner2.Age);
            Assert.AreEqual("namemyvar${", inner2.Name);
        }
 public void WithUnresolvablePlaceholder()
 {
     StaticApplicationContext ac = new StaticApplicationContext();
     MutablePropertyValues pvs = new MutablePropertyValues();
     pvs.Add("name", "${ref}");
     ac.RegisterSingleton("tb", typeof (TestObject), pvs);
     ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), null);
     try
     {
         ac.Refresh();
         Assert.Fail("Should have thrown ObjectDefinitionStoreException");
     }
     catch (ObjectDefinitionStoreException ex)
     {
         // expected
         Assert.IsTrue(ex.Message.IndexOf("ref") != -1);
     }
 }
        public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage)
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            if (hasParentContext)
            {
                StaticApplicationContext parent = new StaticApplicationContext();
                parent.Refresh();
                ac.ParentContext = parent;
            }

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("resourceManagers", resourceManagerList);
            if (useCodeAsDefaultMessage)
            {
                pvs.Add("UseCodeAsDefaultMessage", true);
            }
            ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs);
            ac.Refresh();


            // Localizaiton fallbacks
            GetMessageLocalizationFallbacks(ac);

            // MessageSourceAccessor functionality
            MessageSourceAccessor accessor = new MessageSourceAccessor(ac);
            Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));

            // IMessageSourceResolveable
            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");

            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) };
            Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments));


            // test default message without and with args
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null));
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments));

            /* not supported 
            Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null));
             */

            Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments));

            // test resolvable with default message, without and with args
            resolvable = new DefaultMessageSourceResolvable(null, null, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(null, arguments, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            /* not supported 
                resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
                Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            */

            resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default");
            Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));


            // test message args
            Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" }));

            /* not supported 
                Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null));
            */


            /* not supported 
                Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped"));
            }
            else
            {
                Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped"));
            }
            */


            try
            {
                Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage"));
                if (!useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have thrown NoSuchMessagException");
                }
            }
            catch (NoSuchMessageException e)
            {
                if (useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have returned code as default message.");
                    e.ToString();
                }
            }

        }
        public void WithEnvironmentVariableFallback()
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof (TestObject), pvs);

            pvs = new MutablePropertyValues();
            ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject) ac["to"];
            Assert.AreEqual(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"),
                to.Touchy);
        }
Esempio n. 31
0
        public void TestSchedulerFactoryObjectWithApplicationContext()
        {
            TestObject tb = new TestObject("tb", 99);
            StaticApplicationContext ac = new StaticApplicationContext();

            IScheduler scheduler = MockRepository.GenerateMock<IScheduler>();
            SchedulerContext schedulerContext = new SchedulerContext();
            scheduler.Stub(x => x.Context).Return(schedulerContext).Repeat.Times(4);

            SchedulerFactoryObject schedulerFactoryObject = new TestSchedulerFactoryObject(scheduler);
            schedulerFactoryObject.JobFactory = (null);
            IDictionary schedulerContextMap = new Hashtable();
            schedulerContextMap.Add("testObject", tb);
            schedulerFactoryObject.SchedulerContextAsMap = (schedulerContextMap);
            schedulerFactoryObject.ApplicationContext = (ac);
            schedulerFactoryObject.ApplicationContextSchedulerContextKey = ("appCtx");
            try
            {
                schedulerFactoryObject.AfterPropertiesSet();
                schedulerFactoryObject.Start();
                IScheduler returnedScheduler = (IScheduler) schedulerFactoryObject.GetObject();
                Assert.AreEqual(tb, returnedScheduler.Context["testObject"]);
                Assert.AreEqual(ac, returnedScheduler.Context["appCtx"]);
            }
            finally
            {
                schedulerFactoryObject.Dispose();
            }
        }