/* * Copyright 2005 The Apache Software Foundation. * * 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. */ using System; using System.Collections; using System.Text; using Agility.Core; using Agility.Extras.Spring; using Nexus.Core.Helpers; using NUnit.Framework; using Spring.Context; namespace Nexus.Core { /// /// Provide base SetUp method and convenience methods /// for tests that use a IRequestCatalog. /// /// [TestFixture] public class CatalogBaseTest { /// /// Catalog instance that tests can use. /// protected IRequestCatalog catalog; /// /// Setup catalog between tests. /// /// [SetUp] public virtual void SetUp() { IApplicationContext factory = Objects.Factory(); catalog = factory.GetObject("Catalog") as IRequestCatalog; } /// /// Exercise Setup method. /// /// [Test] public void AssertSetUp() { Assert.IsTrue(catalog != null, "Expected non-null catalog."); } #region IRequestContext tests /// /// Determine if the context contains each key in keys. /// /// Context to process /// Keys to verify /// True if contact contains each key in keys protected bool ContainsKeys(IContext context, string[] keys) { bool found = true; foreach (string key in keys) { found = found && context.Contains(key); } return found; } /// /// Determine if the Criteria for context contains each key in keys. /// /// Context to process /// Keys to verify /// True if Criteria for contact contains each key in keys public bool ContainsCriteriaKeys(IRequestContext context, string[] keys) { if (!context.HasCriteria()) return false; IDictionary criteria = context.Criteria; bool found = true; foreach (string v in keys) { found = found && criteria.Contains(v); } return found; } protected void FaultText(Exception fault) { StringBuilder text = new StringBuilder("["); text.Append(fault.Message); text.Append("] "); text.Append(fault.Source); text.Append(fault.StackTrace); Assert.Fail(text.ToString()); } /// /// Convenience method to confirm that no Exception was caught. /// /// Context under test /// public void AssertNoFault(IRequestContext context) { if (context.HasFault) { FaultText(context.Fault); } } /// /// Convenience method to confirm that no Exception was caught. /// /// Helper under test /// public void AssertNoFault(IViewHelper helper ) { if (helper.HasFault) FaultText(helper.Fault) ; } /// /// Convenience method to confirm /// that there are no alerts or fault. /// /// Context under test /// public void AssertNominal(IRequestContext context) { AssertNoFault(context); bool hasAlerts = context.HasAlerts; if (hasAlerts) { // TODO: Use new TextOnly method here. StringBuilder outer = new StringBuilder(); IDictionary store = context.Alerts; ICollection keys = store.Keys; foreach (string key in keys) { StringBuilder inner = new StringBuilder(); inner.Append(key); inner.Append(": "); IList messages = store[key] as IList; foreach (string message in messages) { inner.Append(message); inner.Append(";"); } outer.Append(inner.ToString()); outer.Append("/n"); } Assert.Fail(outer.ToString()); } } /// /// Convenience method to confirm /// that there are no alerts or fault. /// /// Helper under test /// public void AssertNominal(IViewHelper helper) { AssertNoFault(helper); bool hasAlerts = helper.HasAlerts; if (hasAlerts) { Assert.Fail(helper.AlertsText); } } /// /// Confirm that the value is stored in the context under the key. /// /// The context to check /// The key /// The value protected void AssertKey(IDictionary context, string key, string value) { Assert.IsNotNull(value, "Value is null"); Assert.IsNotNull(key, "Key is null"); Assert.IsTrue(value.Equals(context[key]), "Key:Value mismatch: " + key + ":" + value); } /// /// Confirm that the given context contains the given keys. /// /// The context to check /// The keys to check protected void AssertKeys(IRequestContext context, string[] keys) { Assert.IsTrue(ContainsKeys(context, keys), "Missing keys."); } /// /// Confirm that the context contains the keys, /// that each key represents an non-null IList, /// and that each IList is not empty. /// /// The context to check /// The list keys protected void AssertListKeys(IRequestContext context, string[] keys) { AssertKeys(context, keys); foreach (string key in keys) { IList list = context[key] as IList; Assert.IsNotNull(list, "List is null: " + key); Assert.IsTrue(list.Count > 0, "List is empty"); } } /// /// Call AssertList(string,int) with no minimum. /// /// protected IRequestContext AssertList(string id) { return AssertList(id, 0); } /// /// Execute the Command for the given id, /// and confirm that the return state is Nominal, /// has an Outcome, /// that the Outcome is an non-null IList, /// and that the IList containes at list minCount items. /// /// The List Command to check /// The minimum number of items protected IRequestContext AssertList(string id, int minCount) { IRequestContext context = catalog.GetRequestContext(id); catalog.ExecuteRequest(context); AssertNominal(context); Assert.IsTrue(context.HasOutcome, "Expected outcome"); IList list = context.Outcome as IList; Assert.IsNotNull(list, "Expected outcome as IList"); Assert.IsTrue(list.Count >= minCount, "Expected list entries"); return context; } #endregion #region data access tests /// /// Virtual method for populating a context /// for use with other routine tests. /// /// protected virtual void Populate(IDictionary context) { // override to populate context throw new NotImplementedException("CatalogBaseTest.Populate must be overridden."); } /// /// Virtual method for populating a context /// for an insert test. /// /// protected virtual void PopulateInsert(IDictionary context) { Populate(context); } /// /// Insert and then delete a new record, /// calling the Populate method to fill the context with the appropriate values. /// /// The "save" command name /// The name of the primary key field /// The primary key value initially set by Populate /// The "delete" command name protected IRequestContext AssertInsertDelete(string insertId, string keyId, string keyValue, string deleteId) { IRequestContext context = catalog.GetRequestContext(insertId); PopulateInsert(context); context[keyId] = String.Empty; catalog.ExecuteRequest(context); AssertNominal(context); Assert.IsFalse(keyValue.Equals(context[keyId]), "Expected new primary key"); ICommand delete = catalog.GetCommand(deleteId); delete.Execute(context); AssertNominal(context); return context; } protected IRequestContext AssertEdit(string editId, string keyId, string keyValue, string[] keys) { IRequestContext context = catalog.GetRequestContext(editId); context[keyId] = keyValue; catalog.ExecuteRequest(context); AssertNominal(context); Assert.IsTrue(ContainsKeys(context, keys), "Missing fields"); return context; } /// /// Update the given record (usually to the same values). /// /// The "save" command /// The name of the primary key /// The value of the primary key protected IRequestContext AssertUpdate(string updateId, string keyId, string keyValue) { IRequestContext context = catalog.GetRequestContext(updateId); Populate(context); catalog.ExecuteRequest(context); AssertNominal(context); Assert.IsTrue(keyValue.Equals(context[keyId])); return context; } #endregion } }