VariableAccessorTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Objects » Factory » Config » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Objects » Factory » Config » VariableAccessorTests.cs
#region License

/*
 * Copyright  2002-2007 the original author or authors.
 *
 * 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.
 */

#endregion

#region Imports

using System;
using System.Collections.Specialized;
using NUnit.Framework;

#endregion

namespace Spring.Objects.Factory.Config{
    /// <summary>
    /// Tests <see cref="VariableAccessor"/> functionality.
    /// </summary>
    /// <author>Erich Eichinger</author>
    [TestFixture]
    public class VariableAccessorTests
    {
        private static readonly Guid TESTGUID = Guid.NewGuid();
        private static readonly Guid TESTGUID_DEFAULT = Guid.NewGuid();

        private static readonly DateTime TESTDATETIME = new DateTime(2007, 07, 06, 11, 12, 13);
        private static readonly DateTime TESTDATETIME_DEFAULT = TESTDATETIME.AddDays(-1);
        
        private readonly IVariableSource _testVariableSource = new DictionaryVariableSource(null, true)
            .Add("ValidString", "String")
            .Add("EmptyString", "")
            .Add("ValidChar", "c")
            .Add("InvalidChar", "12")
            .Add("ValidBoolean", "true")
            .Add("InvalidBoolean", "")
            .Add("ValidByte", "1")
            .Add("InvalidByte", "")
            .Add("ValidInt16", "1")
            .Add("InvalidInt16", "")
            .Add("ValidInt32", "1")
            .Add("InvalidInt32", "")
            .Add("ValidInt64", "1")
            .Add("InvalidInt64", "")
            .Add("ValidFloat", "1")
            .Add("InvalidFloat", "")
            .Add("ValidDouble", "1")
            .Add("InvalidDouble", "")
            .Add("ValidDecimal", "1")
            .Add("InvalidDecimal", "")
            .Add("ValidGuid", TESTGUID.ToString())
            .Add("InvalidGuid", "")
            .Add("ValidDateTime", TESTDATETIME.ToString())
            .Add("InvalidDateTime", "blabla")
            .Add("ValidDateTimeUtcRoundtripFormatted", TESTDATETIME.ToUniversalTime().ToString("u"))
            ;

        [Test]
        public void AcceptsNullVariableSource()
        {
            VariableAccessor va = new VariableAccessor(null);
            Assert.AreEqual("default", va.GetString("somekey", "default"));
        }

        [Test]
        public void GetString()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual("String", va.GetString("ValidString", "DefaultString"));
            Assert.AreEqual("DefaultString", va.GetString("NonExistingString", "DefaultString"));
        }

        [Test]
        public void GetStringIgnoresEmptyString()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual("DefaultString", va.GetString("EmptyString", "DefaultString"));
        }

        [Test]
        public void GetChar()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual('c', va.GetChar("ValidChar", 'a'));
            Assert.AreEqual('a', va.GetChar("InvalidChar", 'a', false));
            try
            {
                va.GetChar("InvalidChar", 'a', true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetBoolean()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual(true, va.GetBoolean("ValidBoolean", false));
            Assert.AreEqual(true, va.GetBoolean("InvalidBoolean", true, false));
            try
            {
                va.GetBoolean("InvalidBoolean", true, true);
                Assert.Fail();
            }catch {}
        }

        [Test]
        public void GetByte()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((byte)1, va.GetByte("ValidByte", 2));
            Assert.AreEqual((byte)2, va.GetByte("InvalidByte", 2, false));
            try
            {
                va.GetByte("InvalidByte", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetInt16()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((short)1, va.GetInt16("ValidInt16", 2));
            Assert.AreEqual((short)2, va.GetInt16("InvalidInt16", 2, false));
            try
            {
                va.GetInt16("InvalidInt16", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetInt32()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((int)1, va.GetInt32("ValidInt32", 2));
            Assert.AreEqual((int)2, va.GetInt32("InvalidInt32", 2, false));
            try
            {
                va.GetInt32("InvalidInt32", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetInt64()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((long)1, va.GetInt64("ValidInt64", 2));
            Assert.AreEqual((long)2, va.GetInt64("InvalidInt64", 2, false));
            try
            {
                va.GetInt64("InvalidInt64", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetFloat()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((float)1, va.GetFloat("ValidFloat", 2.0f));
            Assert.AreEqual((float)2, va.GetFloat("InvalidFloat", 2.0f, false));
            try
            {
                va.GetFloat("InvalidFloat", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetDouble()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((double)1, va.GetDouble("ValidDouble", 2.0));
            Assert.AreEqual((double)2, va.GetDouble("InvalidDouble", 2.0, false));
            try
            {
                va.GetDouble("InvalidDouble", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetDecimal()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual((decimal)1, va.GetDecimal("ValidDecimal", 2.0m));
            Assert.AreEqual((decimal)2, va.GetDecimal("InvalidDecimal", 2.0m, false));
            try
            {
                va.GetDecimal("InvalidDecimal", 2, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetGuid()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual(TESTGUID, va.GetGuid("ValidGuid", TESTGUID_DEFAULT));
            Assert.AreEqual(TESTGUID_DEFAULT, va.GetGuid("InvalidGuid", TESTGUID_DEFAULT, false));
            try
            {
                va.GetGuid("InvalidGuid", TESTGUID_DEFAULT, true);
                Assert.Fail();
            }
            catch { }
        }

        [Test]
        public void GetDateTime()
        {
            VariableAccessor va = new VariableAccessor(_testVariableSource);
            Assert.AreEqual(TESTDATETIME, va.GetDateTime("ValidDateTime", null, TESTDATETIME_DEFAULT));
            Assert.AreEqual(TESTDATETIME.ToUniversalTime(), va.GetDateTime("ValidDateTimeUtcRoundtripFormatted", "u", TESTDATETIME_DEFAULT));
            Assert.AreEqual(TESTDATETIME_DEFAULT, va.GetDateTime("InvalidDateTime", null, TESTDATETIME_DEFAULT, false));
            try
            {
                va.GetDateTime("InvalidDateTime", null, TESTDATETIME_DEFAULT, true);
                Assert.Fail();
            }
            catch { }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.