Esempio n. 1
0
        public void ReadStreamMultipleTimes()
        {
            FileInfo file   = null;
            Stream   stream = null;

            try
            {
                file   = new FileInfo("ReadStreamMultipleTimes");
                stream = file.Create();
                // attempting to read this stream twice is an error...
                InputStreamResource res = new InputStreamResource(stream, "A temporary resource.");
                Stream streamOne        = res.InputStream;
                Stream streamTwo;
                Assert.Throws <InvalidOperationException>(() => streamTwo = res.InputStream); // should bail here
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (file != null &&
                        file.Exists)
                    {
                        file.Delete();
                    }
                }
                catch {}
            }
        }
 public void Instantiation () 
 {
     FileInfo file = null;
     Stream stream = null;
     try 
     {
         file = new FileInfo ("Instantiation");
         stream = file.Create ();
         InputStreamResource res = new InputStreamResource (stream, "A temporary resource.");
         Assert.IsTrue (res.IsOpen);
         Assert.IsTrue (res.Exists);
         Assert.IsNotNull (res.InputStream);
     } 
     finally 
     {
         try 
         {
             if (stream != null) 
             {
                 stream.Close ();
             }
             if (file != null
                 && file.Exists) 
             {
                 file.Delete ();
             }
         } 
         catch {}
     }
 }
Esempio n. 3
0
        public void Instantiation()
        {
            FileInfo file   = null;
            Stream   stream = null;

            try
            {
                file   = new FileInfo("Instantiation");
                stream = file.Create();
                InputStreamResource res = new InputStreamResource(stream, "A temporary resource.");
                Assert.IsTrue(res.IsOpen);
                Assert.IsTrue(res.Exists);
                Assert.IsNotNull(res.InputStream);
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (file != null &&
                        file.Exists)
                    {
                        file.Delete();
                    }
                }
                catch {}
            }
        }
        public void TestDifferentCollectionTypes()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
          <v:group id='validatePerson' when='T(Spring.Objects.TestObject) == #this.GetType()'>
            <v:required id ='req' when='true' test='Name'/>
            <v:regex id ='reg' test='Name'>
              <v:property name='Expression' value='[a-z]*\s[a-z]*'/>
              <v:property name='Options' value='IgnoreCase'/>
              <v:message id='reg1' providers='regularni' when='true'>
                 <v:param value='#this.ToString()'/> 
              </v:message>
            </v:regex>
          </v:group>  
           <v:collection id='collectionValidator' validate-all='true'>
                <v:ref name='validatePerson'/>
           </v:collection>
           </objects>";

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

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            CollectionValidator validator = (CollectionValidator) objectFactory.GetObject("collectionValidator");
            
            IList listPersons = new ArrayList();
            IDictionary dictPersons = new Hashtable();
            ISet setPersons = new ListSet();  

            listPersons.Add(new TestObject("DAMJAN Tomic", 24));
            listPersons.Add(new TestObject("Goran Milosavljevic", 24));
            listPersons.Add(new TestObject("Ivan CIKIC", 28));

            dictPersons.Add(1, listPersons[0]);
            dictPersons.Add(2, listPersons[1]);
            dictPersons.Add(3, listPersons[2]);

            setPersons.AddAll(listPersons);
            IValidationErrors ve = new ValidationErrors();

            Assert.IsTrue(validator.Validate(listPersons, ve));                        
            Assert.IsTrue(ve.IsEmpty);                                    
            Assert.IsTrue(validator.Validate(dictPersons, ve));
            Assert.IsTrue(ve.IsEmpty);
            Assert.IsTrue(validator.Validate(setPersons, ve));
            Assert.IsTrue(ve.IsEmpty);
        }
        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='urlValidator' test='#this' type='Spring.Validation.Validators.UrlValidator, Spring.Core'/>
            </objects>";
            
            MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
            IResource resource = new InputStreamResource(stream, "urlValidator");

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
            object obj = objectFactory.GetObject("urlValidator");
            
            Assert.IsTrue(obj is IValidator);
            IValidator validator = obj as IValidator;
            Assert.IsTrue(validator.Validate("http://www.springframework.net", new ValidationErrors()));
        }
 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);
 }
        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()));
        }        
        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='Spring.Validation.Validators.CreditCardValidator, 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.IsTrue(validator.Validate("378282246310005", new ValidationErrors()));
        }
Esempio n. 9
0
            public void Setup()
            {
                //still investigating configuring objects without using xml for unit test

                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=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringSimpleController\"/> " +
                                   "    <object id=\"DependencyController\" singleton=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringDependencyController\" > " +
                                   "      <constructor-arg> " +
                                   "        <object type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+StubDependency\" /> " +
                                   "      </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);
                SpringControllerFactory.Configure(factory);
            }
        public void NotAccessibleInterfaceProxying()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
                <objects xmlns='http://www.springframework.net'>
                <object id='MyProxy' type='Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop'>
                    <property name='ProxyInterfaces'>
                        <list>
                            <value>Spring.Aop.Framework.HelperInterface2, Spring.Aop.Tests</value>
                        </list>
                    </property>
                    <property name='TargetName' value='MyTarget' />
                    <property name='InterceptorNames'>
                        <list>
                            <value>MyInterceptor</value>
                        </list>
                    </property>
                </object>
                <object id='MyTarget' type='Spring.Aop.Framework.HelperClassForNotAccessibleInterfaceProxyingTest, Spring.Aop.Tests'/>
                <object id='MyInterceptor' type='Spring.Aop.Interceptor.NopInterceptor'/>
               </objects>";

                MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
                IResource resource = new InputStreamResource(stream, "Test ProxyFactoryObject");
                XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);

                HelperInterface2 hc = (HelperInterface2)objectFactory.GetObject("MyProxy");
                Console.WriteLine(hc.SecondDoSomething());
        }
 public void ReadStreamMultipleTimes () 
 {
     FileInfo file = null;
     Stream stream = null;
     try 
     {
         file = new FileInfo ("ReadStreamMultipleTimes");
         stream = file.Create ();
         // attempting to read this stream twice is an error...
         InputStreamResource res = new InputStreamResource (stream, "A temporary resource.");
         Stream streamOne = res.InputStream;
         Stream streamTwo = res.InputStream; // should bail here
     } 
     finally 
     {
         try 
         {
             if (stream != null) 
             {
                 stream.Close ();
             }
             if (file != null
                 && file.Exists) 
             {
                 file.Delete ();
             }
         } 
         catch {}
     }
 }
        public void ErrorTests()
        {
            const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
                <object id='ccAmex' type='Spring.Validation.Validators.Amex, Spring.Core'/>
                <v:validator id='ccValidator' test='Creditcard' type='Spring.Validation.Validators.CreditCardValidator, Spring.Core'>
                    <v:property name='CardType' ref='ccAmex'/>
                    <v:message id='errorKey' providers='validationSummary' />
                </v:validator>
            </objects>";

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

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);

            IValidationErrors errors = new ValidationErrors();
            Contact contact = new Contact();
            IValidator validator = (IValidator)objectFactory.GetObject("ccValidator");

            contact.Creditcard = "378282246310";
            bool result = validator.Validate(contact, errors);
           
            Assert.IsNotNull(errors.GetErrors("validationSummary"));
            Assert.IsFalse(result);
        }    
        public void TestNestingCollectionValidatorWithXMLDescription()
        {     
              const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
            <objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
              <v:group id='validatePerson' when='T(Spring.Objects.TestObject) == #this.GetType()'>
                <v:required id ='req' when='true' test='Name'/>
                <v:regex id ='reg' test='Name'>
                  <v:property name='Expression' value='[a-z]*\s[a-z]*'/>
                  <v:property name='Options' value='IgnoreCase'/>
                  <v:message id='reg1' providers='regExpr' when='true'>
                     <v:param value='#this.ToString()'/> 
                  </v:message>
                </v:regex>
              </v:group>  
           
              <v:group id='validator'>
                  <v:collection id='collectionValidator' validate-all='true' context='Members' include-element-errors='true'>
                      <v:message id='coll1' providers='membersCollection' when='true'>
                          <v:param value='#this.ToString()'/> 
                      </v:message>
                      <v:ref name='validatePerson'/>
                  </v:collection>     
              </v:group>  
           </objects>";

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

            XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);                        
                     
            ValidatorGroup validator = (ValidatorGroup) objectFactory.GetObject("validator");
            Society soc = new Society();
                         
            soc.Members.Add(new TestObject("Damjan Tomic", 24));
            soc.Members.Add(new TestObject("Goran Milosavljevic", 24));
            soc.Members.Add(new TestObject("Ivan Cikic", 28));
           
            IValidationErrors err1 = new ValidationErrors();
            
            Assert.IsTrue(validator.Validate(soc, err1));
            
            soc.Members.Add(new TestObject("foo", 30));
            soc.Members.Add(new TestObject("bar", 30));
            Assert.IsFalse(validator.Validate(soc, err1));
             
        }                           
Esempio n. 14
0
        public void ContainerLoadsXmlFromRawStream()
        {
            var xml = "<objects xmlns=\"http://www.springframework.net\"><object id=\"Sauce\" type=\"SauceBéarnaise\" /></objects>";
            using (var stream = new MemoryStream())
            {
                using (var sw = new StreamWriter(stream))
                {
                    sw.Write(xml);
                    sw.Flush();
                    stream.Position = 0;
                    var resource = new InputStreamResource(stream, "");
                    var context = new XmlObjectFactory(resource);

                    var sauce = context.GetObject("Sauce");
                    Assert.IsAssignableFrom<SauceBéarnaise>(sauce);
                }
            }
        }
Esempio n. 15
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);
            }
        }
Esempio n. 16
0
            public void ShouldConfigureFactory()
            {
                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=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringSimpleController\"/> " +
                                   "    <object id=\"DependencyController\" singleton=\"true\" type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SpringDependencyController\" > " +
                                   "      <constructor-arg> " +
                                   "        <object type=\"MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+StubDependency\" /> " +
                                   "      </constructor-arg> " +
                                   "    </object> " +
                                   "  </objects>";
                Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(objectXml));
                IResource resource = new InputStreamResource(stream, "In memory xml");
                var ctx = new GenericApplicationContext();
                var reader = new XmlObjectDefinitionReader(ctx);
                reader.LoadObjectDefinitions(resource);
                ctx.Refresh();
                SpringControllerFactory.Configure(ctx);
                IControllerFactory factory = new SpringControllerFactory();
                IController controller = factory.CreateController(null, "Simple");
                                                                  //Type.GetType("MvcContrib.UnitTests.ControllerFactories.SpringControllerFactoryTester+WhenAValidControllerTypeIsPassed+SimpleController"));

                Assert.That(controller, Is.Not.Null);
                Assert.That(controller, Is.AssignableFrom(typeof(WhenAValidControllerTypeIsPassed.SpringSimpleController)));
            }