Ejemplo n.º 1
0
            public override void Setup()
            {
                string objectXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " +
                                   "  <objects xmlns=\"http://www.springframework.net\" " +
                                   "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                                   "    xsi:schemaLocation=\"http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd\"> " +
                                   "    <object id=\"SimpleDependency\" singleton=\"false\" type=\"MvcContrib.UnitTests.IoC.SimpleDependency\"/> " +
                                   "    <object id=\"NestedDependency\" singleton=\"false\" type=\"MvcContrib.UnitTests.IoC.NestedDependency\" > " +
                                   "      <constructor-arg> " +
                                   "        <object type=\"MvcContrib.UnitTests.IoC.SimpleDependency\" /> " +
                                   "      </constructor-arg> " +
                                   "    </object> " +
                                   "  </objects>";
                Stream         stream   = new MemoryStream(ASCIIEncoding.Default.GetBytes(objectXml));
                IResource      resource = new InputStreamResource(stream, "In memory xml");
                IObjectFactory factory  = new XmlObjectFactory(resource);

                _dependencyResolver = new SpringDependencyResolver(factory);
            }
Ejemplo n.º 2
0
        public void CanAddThrowsAdviceWithoutAdvisor()
        {
            IObjectFactory f = new XmlObjectFactory(new ReadOnlyXmlTestResource("throwsAdvice.xml", GetType()));

            ThrowsAdviceInterceptorTests.MyThrowsHandler th =
                (ThrowsAdviceInterceptorTests.MyThrowsHandler)f.GetObject("throwsAdvice");
            CountingBeforeAdvice cba = (CountingBeforeAdvice)f.GetObject("countingBeforeAdvice");

            Assert.AreEqual(0, cba.GetCalls());
            Assert.AreEqual(0, th.GetCalls());
            ThrowsAdviceInterceptorTests.IEcho echo = (ThrowsAdviceInterceptorTests.IEcho)f.GetObject("throwsAdvised");
            echo.A = 12;
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(0, th.GetCalls());
            Exception expected = new Exception();

            try
            {
                echo.EchoException(1, expected);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual(expected, ex);
            }
            // No throws handler method: count should still be 0
            Assert.AreEqual(0, th.GetCalls());

            // Handler knows how to handle this exception
            expected = new HttpException();
            try
            {
                echo.EchoException(1, expected);
                Assert.Fail();
            }
            catch (HttpException ex)
            {
                Assert.AreEqual(expected, ex);
            }
            // One match
            Assert.AreEqual(1, th.GetCalls("HttpException"));
        }
        public void Setup()
        {
            string objectXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> " +
                               "  <objects xmlns=\"http://www.springframework.net\" " +
                               "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                               "    xsi:schemaLocation=\"http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd\"> " +
                               "    <object id=\"SimpleController\" singleton=\"false\" type=\"" + typeof(SpringSimpleController).FullName + "\"/> " +
                               "    <object id=\"DisposableController\" singleton=\"false\" type=\"" + typeof(SpringDisposableController).FullName + "\"/> " +
                               "    <object id=\"TestAreaSimpleController\" singleton=\"false\" type=\"" + typeof(TestArea.SpringSimpleController).FullName + "\"/>" +
                               "  </objects>";

            var stream  = new MemoryStream(Encoding.Default.GetBytes(objectXml));
            var factory = new XmlObjectFactory(new InputStreamResource(stream, "In memory xml"));

            SpringControllerFactory.Configure(factory);

            _factory = new SpringControllerFactory();

            _context = new RequestContext(MvcMockHelpers.DynamicHttpContextBase(), new RouteData());
        }
Ejemplo n.º 4
0
        public void ISBNValidatorTests()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <v:validator id='isbnValidator' test='#this' type='Spring.Validation.Validators.ISBNValidator, Spring.Core'>
                    <v:message id='error.airportCode.dummy' providers='summary' when='false'/>
                </v:validator>
            </objects>";

            MemoryStream stream   = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource    resource = new InputStreamResource(stream, "isbnValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object           obj           = objectFactory.GetObject("isbnValidator");

            Assert.IsTrue(obj is IValidator);
            IValidator validator = obj as IValidator;

            Assert.IsTrue(validator.Validate("978-1-905158-79-9", new ValidationErrors()));
        }
Ejemplo n.º 5
0
        public void GetObjectTypeWithDirectTarget()
        {
            IObjectFactory bf = new XmlObjectFactory(
                new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml",
                                            GetType()));

            // We have a counting before advice here
            CountingBeforeAdvice cba = (CountingBeforeAdvice)bf.GetObject("countingBeforeAdvice");

            Assert.AreEqual(0, cba.GetCalls());

            ITestObject tb = (ITestObject)bf.GetObject("directTarget");

            Assert.IsTrue(tb.Name.Equals("Adam"));
            Assert.AreEqual(1, cba.GetCalls());

            ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&directTarget");

            Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type");
        }
Ejemplo n.º 6
0
        public void DoesntCallContextRegistryForLocalObjectFactoryReferences()
        {
            string xml = string.Format(
                @"<?xml version='1.0' encoding='UTF-8' ?>
<objects xmlns='http://www.springframework.net'>
	<object id='foo' type='{0}'>
        <property name='MyField' expression='@(theObject)' />
    </object>
</objects>"
                , typeof(MyTestObject).AssemblyQualifiedName
                );

            XmlObjectFactory of        = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));
            object           theObject = new object();

            of.RegisterSingleton("theObject", theObject);

            MyTestObject to = (MyTestObject)of.GetObject("foo");

            Assert.AreSame(theObject, to.MyField);
        }
Ejemplo n.º 7
0
        public void WithNullCardType()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <v:validator id='ccValidator' test='#this' type='Oragon.Spring.Validation.Validators.CreditCardValidator, Oragon.Spring.Core'>
                  <v:message id='error.airportCode.dummy' providers='summary' when='false'/>
                </v:validator>
            </objects>";

            MemoryStream stream   = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource    resource = new InputStreamResource(stream, "ccValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object           obj           = objectFactory.GetObject("ccValidator");

            Assert.IsTrue(obj is CreditCardValidator);
            CreditCardValidator validator = obj as CreditCardValidator;

            Assert.IsNull(validator.CardType);
            Assert.Throws <ArgumentException>(() => validator.Validate("378282246310005", new ValidationErrors()));
        }
        public void SetExcludeFiltersCorrectly()
        {
            string xml      = @"<?xml version='1.0' encoding='UTF-8' ?>
                            <objects xmlns='http://www.springframework.net' xmlns:mongo='http://www.springframework.net/mongo'>  
                                <mongo:convention-profile id='Profile1'
	                                default-value='Spring.Data.MongoDb.Config.DefaultValueConvention, Spring.Data.MongoDb.Tests'>
                                    <mongo:exclude-filter type='regex' expression='.*Exclude' />
                                </mongo:convention-profile>
                            </objects>";
            var    factory  = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));
            var    profile1 = factory.GetObject <ConventionProfile>("Profile1");

            var lookupProfile = BsonClassMap.LookupConventions(typeof(MyEntityInclude));

            Assert.That(lookupProfile, Is.Not.Null);
            Assert.That(lookupProfile, Is.EqualTo(profile1));

            lookupProfile = BsonClassMap.LookupConventions(typeof(MyEntityExclude));
            Assert.That(lookupProfile, Is.Not.SameAs(profile1));
            Assert.That(lookupProfile.DefaultValueConvention, Is.Not.TypeOf <DefaultValueConvention>());
        }
        public void PrototypeAndSingletonBehaveDifferently()
        {
            int               initialCount = 10;
            IObjectFactory    of           = new XmlObjectFactory(new ReadOnlyXmlTestResource("prototypeTargetSourceTests.xml", GetType()));
            ISideEffectObject singleton    = (ISideEffectObject)of.GetObject("singleton");

            Assert.AreEqual(initialCount, singleton.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount + 1, singleton.Count);

            ISideEffectObject prototype = (ISideEffectObject)of.GetObject("prototype");

            Assert.AreEqual(initialCount, prototype.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount, prototype.Count);

            ISideEffectObject prototypeByName = (ISideEffectObject)of.GetObject("prototypeByName");

            Assert.AreEqual(initialCount, prototypeByName.Count);
            singleton.doWork();
            Assert.AreEqual(initialCount, prototypeByName.Count);
        }
        public void CanBeConfiguredToIncludeOrExcludePathsForEvents()
        {
            string           simple = "Data/Xml/watcher-simple.xml";
            XmlObjectFactory f      = new XmlObjectFactory(new FileSystemResource(simple));

            f.RegisterSingleton(DefaultApplicationWatcherFactory.InjectedApplicationName, application);
            watcher = f["watcher"] as FileSystemApplicationWatcher;
            Assert.IsNotNull(watcher,
                             String.Format("test file [{0}] should define a file sistem resource!", simple));
            Assert.AreEqual(1, watcher.Excludes.Count);
            Assert.AreEqual(1, watcher.Includes.Count);

            watcher.StartWatching(dispatcher);

            // propagated
            string subDir = Path.Combine(appFullPath, "foo");

            Directory.CreateDirectory(subDir);
            using (File.Create(Path.Combine(subDir, "foo.bar"))) {}
            eventLatch.Acquire();
            Assert.IsFalse(dispatched);
        }
Ejemplo n.º 11
0
        public void SetsUpMongoDbFactoryUsingAMongoConnectStringWithoutCredentials()
        {
            string xml     = @"<?xml version='1.0' encoding='UTF-8' ?>
                           <objects xmlns='http://www.springframework.net' xmlns:mongo='http://www.springframework.net/mongo'>  
                             <mongo:db-factory url='mongodb://localhost/database' />
                           </objects>";
            var    factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));

            IObjectDefinition         definition           = factory.GetObjectDefinition("MongoDatabaseFactory");
            ConstructorArgumentValues constructorArguments = definition.ConstructorArgumentValues;

            Assert.That(constructorArguments.ArgumentCount, Is.EqualTo(1));

            var argument = constructorArguments.GetArgumentValue(0, typeof(MongoUrl));

            Assert.That(argument, Is.Not.Null);

            var dbFactory = factory.GetObject <IMongoDatabaseFactory>("MongoDatabaseFactory");
            var db        = dbFactory.GetDatabase();

            Assert.That(db.Name, Is.EqualTo("database"));
        }
Ejemplo n.º 12
0
        public void ConfigureContainerWithSauceUsingLinqToXml()
        {
            XNamespace xmlns = @"http://www.springframework.net";
            var        xml   =
                new XElement(xmlns + "objects",
                             new XElement(xmlns + "object", new XAttribute("id", "Sauce"), new XAttribute("type", "SauceBéarnaise")));

            using (var stream = new MemoryStream())
            {
                using (var xw = XmlWriter.Create(stream))
                {
                    xml.Save(xw);
                    xw.Flush();
                    stream.Position = 0;
                }
                var resource = new InputStreamResource(stream, "");
                var context  = new XmlObjectFactory(resource);

                var sauce = context.GetObject("Sauce");
                Assert.IsAssignableFrom <SauceBéarnaise>(sauce);
            }
        }
        public void WithAppend()
        {
            string           resourceName = "Spring/Objects/Factory/Config/PPC-SPRNET-55.xml";
            XmlObjectFactory ctx          = new XmlObjectFactory(new FileSystemResource(resourceName));

            Assert.IsNotNull(ctx);
            IObjectFactoryPostProcessor processor = (IObjectFactoryPostProcessor)ctx["appendConfigurer"];

            processor.PostProcessObjectFactory(ctx);
            TestObjectDAO to = (TestObjectDAO)ctx["appendDao"];

            Assert.AreEqual(testConnectionString + "," + testConnectionStringTwo, to.DbConnection.ConnectionString);
            Assert.AreEqual(1000, to.MaxResults);

            ctx       = new XmlObjectFactory(new FileSystemResource(resourceName));
            processor = (IObjectFactoryPostProcessor)ctx["noAppendConfigurer"];
            processor.PostProcessObjectFactory(ctx);

            to = (TestObjectDAO)ctx["noAppendDao"];
            Assert.AreEqual(testConnectionStringTwo, to.DbConnection.ConnectionString);
            Assert.AreEqual(1000, to.MaxResults);
        }
Ejemplo n.º 14
0
        public void TestIncompatibleTxAtts()
        {
            var resourceName =
                @"assembly://Spring.Messaging.Amqp.Rabbit.Tests/Spring.Messaging.Amqp.Rabbit.Tests.Config/"
                + typeof(ListenerContainerParserTests).Name + "-fail-context.xml";
            var resource = new AssemblyResource(resourceName);

            Assert.Throws <ObjectDefinitionStoreException>(
                () =>
            {
                try
                {
                    var result = new XmlObjectFactory(resource);
                    Assert.Fail("Expected an exception.");
                }
                catch (Exception ex)
                {
                    Assert.That(ex.InnerException.Message, Is.StringStarting("Listener Container - cannot set channel-transacted with acknowledge='NONE'"));
                    throw;
                }
            },
                "Expected an exception.");
        }
        public void Serialization()
        {
            IObjectFactory iof = new XmlObjectFactory(new ReadOnlyXmlTestResource("RegularExpressionSetterTests.xml", GetType()));
            IPerson        p   = (IPerson)iof.GetObject("SerializableSettersAdvised");
            // Interceptor behind regexp advisor
            NopInterceptor nop = (NopInterceptor)iof.GetObject("NopInterceptor");

            Assert.AreEqual(0, nop.Count);

            int newAge = 12;

            // Not advised
            Assert.AreEqual(0, p.GetAge());
            Assert.AreEqual(0, nop.Count);

            // This is proxied
            p.SetAge(newAge);
            Assert.AreEqual(1, nop.Count);
            p.SetAge(newAge);
            Assert.AreEqual(newAge, p.GetAge());
            // Only setter fired
            Assert.AreEqual(2, nop.Count);

            // Serialize and continue...

#if !NETCOREAPP // deep chains for Type serialization problems, not worth the effort at the moment
            p = (IPerson)SerializationTestUtils.SerializeAndDeserialize(p);
            Assert.AreEqual(newAge, p.GetAge());
#endif
            // Remembers count, but we need to get a new reference to nop...
            nop = (SerializableNopInterceptor)((IAdvised)p).Advisors[0].Advice;
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual("SerializableSettersAdvised", p.GetName());
            p.SetAge(newAge + 1);
            Assert.AreEqual(3, nop.Count);
            Assert.AreEqual(newAge + 1, p.GetAge());
        }
        public void IfNoIdUseDefault()
        {
            string xml     = @"<?xml version='1.0' encoding='UTF-8' ?>
                            <objects xmlns='http://www.springframework.net' xmlns:mongo='http://www.springframework.net/mongo'>  
                                <mongo:convention-profile />
                            </objects>";
            var    factory = new XmlObjectFactory(new StringResource(xml, Encoding.UTF8));

            Assert.That(factory.GetObjectDefinitionNames(), Contains.Item("MongoConventionProfile"));

            var conventionProfile = factory.GetObject("MongoConventionProfile") as ConventionProfile;

            Assert.That(conventionProfile, Is.Not.Null);
            Assert.That(conventionProfile.DefaultValueConvention, Is.TypeOf <NullDefaultValueConvention>());
            Assert.That(conventionProfile.ElementNameConvention, Is.TypeOf <MemberNameElementNameConvention>());
            Assert.That(conventionProfile.ExtraElementsMemberConvention, Is.TypeOf <NamedExtraElementsMemberConvention>());
            Assert.That(conventionProfile.IdGeneratorConvention, Is.TypeOf <LookupIdGeneratorConvention>());
            Assert.That(conventionProfile.IdMemberConvention, Is.TypeOf <NamedIdMemberConvention>());
            Assert.That(conventionProfile.IgnoreExtraElementsConvention, Is.TypeOf <NeverIgnoreExtraElementsConvention>());
            Assert.That(conventionProfile.IgnoreIfDefaultConvention, Is.TypeOf <NeverIgnoreIfDefaultConvention>());
            Assert.That(conventionProfile.IgnoreIfNullConvention, Is.TypeOf <NeverIgnoreIfNullConvention>());
            Assert.That(conventionProfile.MemberFinderConvention, Is.TypeOf <PublicMemberFinderConvention>());
            Assert.That(conventionProfile.SerializationOptionsConvention, Is.TypeOf <NullSerializationOptionsConvention>());
        }
Ejemplo n.º 17
0
        private XmlObjectFactory LoadContext()
        {
            XmlObjectFactory objectFactory = null;

            try
            {
                // Resource file name template: <class-name>-<contextIndex>-context.xml
                var resourceName = @"assembly://Spring.Messaging.Amqp.Rabbit.Tests/Spring.Messaging.Amqp.Rabbit.Tests.Config/" + typeof(AdminParserTests).Name + "-" + this.contextIndex + "-context.xml";
                Logger.Info("Resource Name: " + resourceName);
                var resource = new AssemblyResource(resourceName);
                objectFactory = new XmlObjectFactory(resource);
                if (!this.validContext)
                {
                    Assert.Fail("Context " + resource + " suppose to fail");
                }
            }
            catch (Exception e)
            {
                if (e is ObjectDefinitionParsingException || e is ObjectDefinitionStoreException)
                {
                    if (this.validContext)
                    {
                        // Context expected to be valid - throw an exception up
                        throw e;
                    }

                    Logger.Warn("Failure was expected", e);
                }
                else
                {
                    throw;
                }
            }

            return(objectFactory);
        }
 public void SetUp()
 {
     objectFactory = new XmlObjectFactory(new ReadOnlyXmlTestResource("simplePoolTests.xml", GetType()));
 }
Ejemplo n.º 19
0
 public void SetUp()
 {
     this.ObjectFactory = new XmlObjectFactory(
         new ReadOnlyXmlTestResource("threadLocalTests.xml", GetType()));
     log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 }
Ejemplo n.º 20
0
 public void Init()
 {
     //_container = ContextRegistry.GetContext();
     _objectFactory = ParserTestsHelper.LoadMessagingConfig();
     _channel       = _objectFactory.GetObject <IChannel>(ChannelName) as RedisChannel;
 }
Ejemplo n.º 21
0
 public void Init()
 {
     _objectFactory = ParserTestsHelper.LoadMessagingConfig();
 }