ValidationErrorsTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Validation » 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 » Validation » ValidationErrorsTests.cs
#region License

/*
 * Copyright 2004 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;
using System.IO;
using System.Xml.Serialization;
using NUnit.Framework;

using Spring.Context.Support;

#endregion

namespace Spring.Validation{
  /// <summary>
  /// Unit tests for the ValidationErrors class.
  /// </summary>
  /// <author>Rick Evans</author>
    /// <author>Goran Milosavljevic</author>
  [TestFixture]
  public sealed class ValidationErrorsTests
  {
    private const string GoodErrorKey = "key";
    private ErrorMessage ErrorMessageTwo = new ErrorMessage("This Is Eva Green", null);
    private ErrorMessage ErrorMessageOne = new ErrorMessage("Kissing Leads To Brain Disease", null);

        [Test]
        public void ResolveErrorsWithoutMessageSource()
        {
            ValidationErrors errors = new ValidationErrors();
            errors.AddError(string.Empty, ErrorMessageOne);
            IList resolvedErrors = errors.GetResolvedErrors(string.Empty, null);
            Assert.AreEqual(ErrorMessageOne.Id, (string)resolvedErrors[0]);
        }
    [Test]
    public void ContainsNoErrorsDirectlyAfterInstantiation()
    {
      IValidationErrors errors = new ValidationErrors();
      Assert.IsTrue(errors.IsEmpty);
      Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
    }

    [Test]
    [ExpectedException(typeof (ArgumentNullException))]
    public void AddErrorWithNullMessage()
    {
      new ValidationErrors().AddError(GoodErrorKey, null);
    }

    [Test]
        [ExpectedException(typeof(ArgumentNullException))]
    public void AddErrorWithNullKey()
    {
      IValidationErrors errors = new ValidationErrors();
      errors.AddError(null, ErrorMessageOne);
    }

    [Test]
    public void AddErrorSunnyDay()
    {
      IValidationErrors errors = new ValidationErrors();
      errors.AddError(GoodErrorKey, ErrorMessageOne);
      Assert.IsFalse(errors.IsEmpty);
      Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
      Assert.AreEqual(1, errors.GetErrors(GoodErrorKey).Count);
    }

    [Test]
    public void AddTwoErrorsSameKey()
    {
      IValidationErrors errors = new ValidationErrors();
      errors.AddError(GoodErrorKey, ErrorMessageOne);
      errors.AddError(GoodErrorKey, ErrorMessageTwo);
      Assert.IsFalse(errors.IsEmpty);
      Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
      Assert.AreEqual(2, errors.GetErrors(GoodErrorKey).Count);
    }

        [Test]
        public void EmptyErrorsReturnEmptyCollections()
        {
            IValidationErrors errors = new ValidationErrors();

            IList typedErrors = errors.GetErrors("xyz");
            Assert.IsNotNull(typedErrors);
            Assert.AreEqual(0, typedErrors.Count);

            IList resolvedErrors = errors.GetResolvedErrors("xyz", new NullMessageSource());
            Assert.IsNotNull(resolvedErrors);
            Assert.AreEqual(0, resolvedErrors.Count);
        }

    [Test]
    public void MergeErrorsWithNull()
    {
      IValidationErrors errors = new ValidationErrors();
      errors.AddError(GoodErrorKey, ErrorMessageOne);
      errors.AddError(GoodErrorKey, ErrorMessageTwo);
      errors.MergeErrors(null);

      // must be unchanged with no Exception thrown...
      Assert.IsFalse(errors.IsEmpty);
      Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
      Assert.AreEqual(2, errors.GetErrors(GoodErrorKey).Count);
    }

    [Test]
    public void MergeErrors()
    {
      ValidationErrors otherErrors = new ValidationErrors();
      const string anotherKey = "anotherKey";
      otherErrors.AddError(anotherKey, ErrorMessageTwo);
      otherErrors.AddError(GoodErrorKey, ErrorMessageTwo);


      IValidationErrors errors = new ValidationErrors();
      errors.AddError(GoodErrorKey, ErrorMessageOne);
      errors.MergeErrors(otherErrors);

      Assert.IsFalse(errors.IsEmpty);
        IList mergedErrors = errors.GetErrors(GoodErrorKey);
        Assert.IsNotNull(mergedErrors);
      Assert.AreEqual(2, mergedErrors.Count);
            Assert.AreEqual(ErrorMessageOne, mergedErrors[0]);
            Assert.AreEqual(ErrorMessageTwo, mergedErrors[1]);

      IList otherErrorsForKey = errors.GetErrors(anotherKey);
      Assert.IsNotNull(otherErrorsForKey);
            Assert.AreEqual(1, otherErrorsForKey.Count);
            Assert.AreEqual(ErrorMessageTwo, otherErrorsForKey[0]);
        }

        [Test]
        public void SerializeErrors()
        {
            ValidationErrors errors = new ValidationErrors();
            ErrorMessageOne = new ErrorMessage("Kissing Leads To Brain Disease", new object[] {"Param11", 5, "Param13"});
            ErrorMessageTwo = new ErrorMessage("This Is Eva Green", new object[] { "Param21", 'g' , new object[] {"Goran", "Milosavljevic"} });
            ErrorMessage ErrorMessageThree = new ErrorMessage("Third error message", null);
            ErrorMessage ErrorMessageFour = new ErrorMessage("Fourth error message", new object[]{});
            errors.AddError("key1", ErrorMessageOne);
            errors.AddError("key1", ErrorMessageTwo);
            errors.AddError("key2", ErrorMessageThree);
            errors.AddError("key3", ErrorMessageFour);

            Stream streamBefore = new MemoryStream();
            Stream streamAfter = new MemoryStream();

            // serialize ValidationErrors
            XmlSerializer serializer = new XmlSerializer(typeof(ValidationErrors));
            serializer.Serialize(streamBefore, errors);
            streamBefore.Position = 0;

            // deserialize ValidationErrors
            ValidationErrors result = (ValidationErrors) serializer.Deserialize(streamBefore);
            
            // serialize ValidationErrors
            serializer.Serialize(streamAfter, result);
            
            // compare ValidationErrors instances
            byte[] byteBefore = new byte[streamBefore.Length];
            byte[] byteAfter = new byte[streamAfter.Length];
            
            Assert.AreEqual(byteAfter.Length, byteBefore.Length);
            
            streamBefore.Position = 0;
            streamAfter.Position = 0;
            streamBefore.Read(byteBefore, 0, (int) streamBefore.Length);
            streamAfter.Read(byteAfter, 0, (int) streamAfter.Length);
            
            Assert.AreEqual(byteBefore, byteAfter);
        }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.