First basic test application

After explaining the basics, it’s time to demonstrate the usage of the AppBuilder. Let’s implement a simple application. A class called “Transform0ab3Tests” should trigger the transformation of a string. In the first step, the string will be uppered and in a second step reversed. The following chart demonstrates this flow:

Flow1.png

Translated to the flow language, we get the following definition:

FlowDefinition: MainFlow {

	Step: Transform0ab3Tests
	Step: ToUpper
	Step: ToReverse
	
	Wire: Transform0ab3Tests.Value -> ToUpper.Value
	Wire: ToUpper.Uppered -> ToReverse.Value
	Wire: ToReverse.Reversed -> Transform0ab3Tests.Result

	Wire: ToUpper.ExceptionPin -> Transform0ab3Tests.ExceptionPin
	Wire: ToReverse.ExceptionPin -> Transform0ab3Tests.ExceptionPin
}
The source code of the two steps „ToUpper“ and „ToReverse” is very simple:

using System;

namespace AB3.AppBuilderTests.TestClasses
{
    public class ToUpper {
        public void Value(string value) {
            try {
                this.Uppered(value.ToUpper());
            }
            catch (Exception ex) {
                this.ExceptionPin(ex);
            }
        }
 
        public event Action<string> Uppered;
        public event Action<Exception> ExceptionPin;
     }
}

using System;
using System.Linq;
 
namespace AB3.AppBuilderTests.TestClasses
{
    public class ToReverse {
        public void Value(string value) {
            try {
                this.Reversed(new string(value.ToCharArray().Reverse().ToArray()));
            }
            catch (Exception ex) {
                this.ExceptionPin(ex);
            }
        }
 
        public event Action<string> Reversed;
        public event Action<Exception> ExceptionPin;
    }
}
The third component is our test class which demonstrated the usage of the AppBuilder class:

[TestMethod]
public void TestFlowDefinition0_Method1()	{

	AppBuilder tAppBuilder = new AppBuilder();
	tAppBuilder.LoadFlowsFromFile(@"{Your path!}\FD.Transform0.ab3");

	tAppBuilder.AddPreparedStepInstance("MainFlow","Transform0Tests"this);
	tAppBuilder.BuildApp(nullnull);
 
	Value("This is my test string!");
	Assert.AreEqual(@"!GNIRTS TSET YM SI SIHT"this._Result);
	}
As you see, the first thing that you have to do is to create an instance of the AppBuilder itself.
After that, you load your flow definition. In this case, simply by loading from a file using “LoadFlowsFromFile” and the full name of the file.

The final step is normally to build the application by “BuildApp”. This creates the instances of the steps, connects the events and so on. The AppBuilder is designed to create the instances of the steps. In this case, however, we want to use the test class itself as a step and this instance already exists. Therefore, we have to tell the AppBuilder that the existing instance of Transform0Tests should be used. This is done by “AddPreparedStepInstance” which tells in which Flow namespace (“MainfFlow”) and with which name (“Transform0Tests”) the instance should be used.
After that, the application can be is built.
Finally, we need the pins of “Transform0Tests” to send and receive the information (already defined in the flow definition):

public event Action<string> Value;
 
private string _Result = null;
private Exception _Exception = null;
 
public void Result(string result){
	_Result = result;
}
 
public void ExceptionPin(Exception exception) {
	_Exception = exception;
}

Last edited Apr 19, 2013 at 4:27 PM by InneHo, version 3

Comments

No comments yet.