Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Examples/Mainframe In directory sc8-pr-cvs1:/tmp/cvs-serv29018/DotNetMock.Examples/Mainframe Added Files: CustomerNumberCollector.cs CustomerNumberCollectorTests.cs IMainframeConnection.cs MainframeConnection.cs MockMainframeConnection.cs Log Message: --- NEW FILE: CustomerNumberCollector.cs --- using System; namespace DotNetMock.Examples.Mainframe { public class CustomerNumberCollector { private IMainframeConnection _connection = null; private string _name = ""; private string _userName = ""; private string _password = ""; public CustomerNumberCollector(IMainframeConnection connection) { _connection = connection; } public void CollectCustomerInformation(string customerNumber) { _connection.Connect("A"); _connection.SetScreen("MainMenu"); _connection.SetScreen("CustomerInformation"); _connection.PutField(23, 50, customerNumber, customerNumber.Length); _connection.SendKey(MainframeConnection.Keys.Enter); string errorMessage = _connection.GetField(23, 2, 25); if (errorMessage.Equals("CUSTOMER NUMBER NOT FOUND")) { _connection.Disconnect(); throw new ApplicationException("Customer Number could not be found"); } this.Name = _connection.GetField(20, 4, 25); this.UserName = _connection.GetField(21, 4, 8); this.Password = _connection.GetField(22, 4, 8); _connection.Disconnect(); } public void UpdateInformation(string customerNumber, string name, string userName, string password) { _connection.Connect("A"); _connection.SetScreen("MainMenu"); _connection.SetScreen("CustomerInformation"); _connection.PutField(23, 50, customerNumber, customerNumber.Length); _connection.SendKey(MainframeConnection.Keys.Enter); string errorMessage = _connection.GetField(23, 2, 25); if (errorMessage.Equals("CUSTOMER NUMBER NOT FOUND")) { _connection.Disconnect(); throw new ApplicationException("Customer Number could not be found"); } _connection.PutField(20, 4, name, name.Length); _connection.PutField(21, 4, userName, userName.Length); _connection.PutField(22, 4, password, password.Length); _connection.SendKey(MainframeConnection.Keys.Enter); errorMessage = _connection.GetField(23, 2, 25); if (errorMessage.Equals("TRANSACTION FAILED")) { _connection.Disconnect(); throw new ApplicationException("Customer Information update failed."); } this.Name = _connection.GetField(20, 4, 25); this.UserName = _connection.GetField(21, 4, 8); this.Password = _connection.GetField(22, 4, 8); } public string Name { get { return _name.Trim(); } set { _name = value; } } public string UserName { get { return _userName.Trim(); } set { _userName = value; } } public string Password { get { return _password.Trim(); } set { _password = value; } } } } --- NEW FILE: CustomerNumberCollectorTests.cs --- using System; using NUnit.Framework; namespace DotNetMock.Examples.Mainframe { [TestFixture] public class SerialNumberCollectorTests { private MockMainframeConnection mockConnection = null; string[,] customerNumberScreen = null; string[,] mainMenu = null; CustomerNumberCollector customer = null; [SetUp] public void Init() { mockConnection = new MockMainframeConnection(); customerNumberScreen = new string[25,80]; mainMenu = new string[25,80]; customer = new CustomerNumberCollector(mockConnection); mockConnection.CreateScreen("MainMenu", mainMenu); mockConnection.CreateScreen("CustomerInformation", customerNumberScreen); } [TearDown] public void Destroy() { mockConnection = null; customerNumberScreen = null; } [Test] [ExpectedException(typeof(ApplicationException))] public void InValidCustomerNumber() { setGeneralExpectations(); mockConnection.SetExpectedKeyPress(MainframeConnection.Keys.Enter); mockConnection.SetField("CustomerInformation", 23, 2, "CUSTOMER NUMBER NOT FOUND"); customer.CollectCustomerInformation("9999999"); } [Test] public void ValidCustomerNumber() { setGeneralExpectations(); mockConnection.SetField("CustomerInformation", 20, 4, "Pete Rose"); mockConnection.SetField("CustomerInformation", 21, 4, "pete1234"); mockConnection.SetField("CustomerInformation", 22, 4, "password"); mockConnection.SetExpectedKeyPress(MainframeConnection.Keys.Enter); customer.CollectCustomerInformation("1234567"); Assertion.AssertEquals("Pete Rose", customer.Name); Assertion.AssertEquals("pete1234", customer.UserName); Assertion.AssertEquals("password", customer.Password); } [Test] public void ValidCustomerNumberUpdate() { setGeneralExpectations(); mockConnection.SetField("CustomerInformation", 23, 3, "TRANSACTION ACCEPTED"); mockConnection.SetField("CustomerInformation", 20, 4, "Pete Rose"); mockConnection.SetField("CustomerInformation", 21, 4, "pete1234"); mockConnection.SetField("CustomerInformation", 22, 4, "password"); mockConnection.SetExpectedKeyPress(MainframeConnection.Keys.Enter); customer.UpdateInformation("1234567", "Babe Ruth", "baberuth", "babepass"); Assertion.AssertEquals("Babe Ruth", customer.Name); Assertion.AssertEquals("baberuth", customer.UserName); Assertion.AssertEquals("babepass", customer.Password); } [Test] [ExpectedException(typeof(ApplicationException))] public void InvalidCustomerUpdate() { setGeneralExpectations(); mockConnection.SetExpectedKeyPress(MainframeConnection.Keys.Enter); mockConnection.SetField("CustomerInformation", 23, 2, "TRANSACTION FAILED"); customer.UpdateInformation("1234567", "Babe Ruth", "baberuth", "babepass"); } [Test] [ExpectedException(typeof(ApplicationException))] public void InValidCustomerNumberUpdateInformation() { setGeneralExpectations(); mockConnection.SetExpectedKeyPress(MainframeConnection.Keys.Enter); mockConnection.SetField("CustomerInformation", 23, 2, "CUSTOMER NUMBER NOT FOUND"); customer.UpdateInformation("9999999", "", "", ""); } private void setGeneralExpectations() { mockConnection.SetExpectedConnectCalls(1); mockConnection.SetExpectedDisconnectCalls(1); mockConnection.SetExpectedSendKeyCalls(1); } } } --- NEW FILE: IMainframeConnection.cs --- using System; namespace DotNetMock.Examples.Mainframe { /// <summary> /// Interface to connect and utilize a 3rd party mainframe interface. /// This example utilizes NetManage's Rumba api to do screen scraping. /// </summary> public interface IMainframeConnection { /// <summary> /// Connect to a open Rumba Session. /// </summary> /// <param name="sessionID">Session ID to connect to</param> void Connect(string sessionID); /// <summary> /// Sets the screen to a predefined mainframe screen. /// </summary> /// <param name="screenName">Screen to change to</param> void SetScreen(string screenName); /// <summary> /// Puts given text onto the mainframe screen. /// </summary> /// <param name="row">Row #</param> /// <param name="column">Starting Column #</param> /// <param name="message">Message to place</param> /// <param name="length">Length of Message</param> void PutField(int row, int column, string message, int length); /// <summary> /// Gets the text from the screen. /// </summary> /// <param name="row">Row #</param> /// <param name="column">Column #</param> /// <param name="length">Length of the message to get</param> /// <returns>Requested text from the screen</returns> string GetField(int row, int column, int length); /// <summary> /// Sends the key to the screen /// </summary> /// <param name="key">Key to send</param> void SendKey(MainframeConnection.Keys key); /// <summary> /// Waits for control to be returned to the screen /// </summary> void Wait(); /// <summary> /// Disconnects from the current Mainframe session /// </summary> void Disconnect(); } } --- NEW FILE: MainframeConnection.cs --- using System; namespace DotNetMock.Examples.Mainframe { public class MainframeConnection : IMainframeConnection { public MainframeConnection() {} #region IMainframeConnection Implementation public void Connect(string sessionID) { } public void SetScreen(string screenName) { } public void PutField(int row, int column, string message, int length) { } public string GetField(int row, int column, int length) { return ""; } public void SendKey(MainframeConnection.Keys key) { } public void Wait() { } public void Disconnect() { } #endregion public enum Keys { Enter, Clear, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Shift_F1, Shift_F2, Shift_F3, Shift_F4, Shift_F5, Shift_F6, Shift_F7, Shift_F8, Shift_F9, Shift_F10, Shift_F11, Shift_F12, } } } --- NEW FILE: MockMainframeConnection.cs --- using System; using System.Collections; using DotNetMock; namespace DotNetMock.Examples.Mainframe { public class MockMainframeConnection : IMainframeConnection { private ExpectationCounter _connectCalls = new ExpectationCounter("MockMainframeConnection.ConnectCalls"); private ExpectationCounter _disconnectCalls = new ExpectationCounter("MockMainframeConnection.DisconnectCalls"); private ExpectationCounter _sendKeyCalls = new ExpectationCounter("MockMainframeConnection.SendCalls"); private ExpectationValue _keyPress = new ExpectationValue("MockMainframeConnection.KeyPress"); private Hashtable _screens = null; private string[,] _currentScreen = null; public MockMainframeConnection() { _screens = new Hashtable(); } public void CreateScreen(string screenName, string[,] screen) { foreach (string key in _screens.Keys) { if (key.Equals(screenName)) { throw new ApplicationException("Cannot add duplicate screen: " + screenName); } } _screens.Add(screenName, screen); } public void SetField(string screenName, int row, int column, string message) { string[,] screen = (string[,])_screens[screenName]; if (screen == null) { throw new ApplicationException("No valid screen setup for that screen name: " + screenName); } char[ ]messageArray = message.ToCharArray(); int currentColumn = column - 1; int currentRow = row - 1; for (int i = 0; i < messageArray.Length; i++) { screen[currentRow, currentColumn] = messageArray[i].ToString(); currentColumn++; } } public void SetExpectedConnectCalls(int calls) { _connectCalls.Expected = calls; } public void SetExpectedDisconnectCalls(int calls) { _disconnectCalls.Expected = calls; } public void SetExpectedKeyPress(MainframeConnection.Keys key) { _keyPress.Expected = key; } public void SetExpectedSendKeyCalls(int calls) { _sendKeyCalls.Expected = calls; } #region IMainframeConnection Implementation public void Connect(string sessionID) { _connectCalls.Inc(); } public void SetScreen(string screenName) { bool found = false; foreach (string key in _screens.Keys) { if (key.Equals(screenName)) { found = true; } } if (!found) { throw new ApplicationException("A screen named " + screenName + " has not been setup"); } _currentScreen = (string[,])_screens[screenName]; } public void PutField(int row, int column, string message, int length) { char[ ]messageArray = message.ToCharArray(); int currentColumn = column - 1; int currentRow = row - 1; for (int i = 0; i < messageArray.Length; i++) { _currentScreen[currentRow, currentColumn] = messageArray[i].ToString(); currentColumn++; } } public string GetField(int row, int column, int length) { string output = ""; int currentRow = row - 1; int currentColumn = column - 1; int currentLength = length; int i = 0; try { for (i = 1; i <= currentLength; i++) { output += _currentScreen[currentRow,currentColumn]; currentColumn++; } } catch (System.Exception ex) { throw new System.Exception("currentRow: " + currentRow.ToString() + " Current Column: " + currentColumn.ToString() + " Current Length: " + currentLength.ToString() + " I: " + i.ToString()); } return output; } public void SendKey(MainframeConnection.Keys key) { _sendKeyCalls.Inc(); _keyPress.Actual = key; } public void Wait() { } public void Disconnect() { _disconnectCalls.Inc(); } #endregion } } |