Why ?
The system under development is a bridge between your Fixtures and the system your testing.
If you want to change the way GreenPepper is finding/instanciating your fixtures, or if you need to hook the document execution then you can define a Custom System Under Development.
Using custom types
All the examples in the documents are in strings, but fixtures want to process and return other data types.
GreenPepper provides a mechanism to help conversion from String to other data types and back again.
so an example that look likes
rule for | SomeFixture | with parameter | 100$ |
---|---|---|---|
color | someBool | answer | |
255,55,10 | yes | 14% | |
235,5,0 | no | 21% |
Will match a class with the following method
public class SomeFixture { public SomeFixture(Ammount ammount) {...} public setColor(RGB color) ... public someBool(boolean yesNo).. public Ratio answer()... }
Lets look at how GreenPepper will match these.
Converters
Converters are class implementing the interface com.greenpepper.converter.TypeConverter in java or GreenPepper.Converters.ITypeConverter in C#
public interface TypeConverter { boolean canConvertTo( Class type ); Object parse( String value, Class type ); String toString( Object value ); }
namespace GreenPepper.Converters { public interface ITypeConverter { bool CanConvertTo(Type type); object ValueOf(string value, Type type); string ToString(object value); } }
GreenPepper provides out of the box type converters for the following types
- Integer
- Long
- Float
- Double
- Date
- Boolean
- Array
- String
or their simpler form int, long ...
(order is important see adding a new type converter below)
The ArrayConverter calls recursively the other converters depending on the component type the array holds.
Adding a new type converter
The com.greenpepper.GreenPepper class provides a method to add your own type converter
public static void register( TypeConverter converter)
public static void Register( ITypeConverter converter)
The better place to register your custom type is in a custom system under developement :
public static class CustomSystemUnderDevelopment extends DefaultSystemUnderDevelopment { public CustomSystemUnderDevelopment( String... params ) { GreenPepper.register(new MyCustomTypeConverter()); } }
The converters are always check in an LIFO manners. If two converters can process a data type the last one that has been register will be used. That way, you can provides your own converters in place of the standard GreenPepper converters.
Self conversion
Instead of registering a TypeConverter, you can uses self converting types.
Self converting type implies that you add a static parse method to your class.
public static T parse(String val); // And then to revert back to a string, public static String toString(T value)
public static T ValueOf(string text) // And then to revert back to a string, public static String ToString(T value)
Your class does not have to provides both of them.
Rules of conversion
Before version 4.0.1, the order was the oposite.
From example to fixture
- First GreenPepper will look for a registered TypeConverter that can handles the type.
- Second GreenPepper will verify if the type is can self convert (ie public static T parse(String) or public static T ValueOf(string))
- An UnsupportedOperationException will be thrown
From fixture return value to String
- First GreenPepper will look for a registered TypeConverter that can handles the type.
- Second GreenPepper will verify if the type is can self revert (ie public static String toString(T) or public static T ToString(string))
- Use the toString() or ToString() method on the data itself.
Changing how GreenPepper is finding your Fixtures
This could be useful when you are using for example an IOC, like Spring (see Spring Example) or just when you want to add locations (packages in java, namespaces in .Net) for GreenPepper can resolve your fixtures.
To change the system under development :
- With GreenPepper Server : Server system under development configuration
- With GreenPepper Open : Command line system under development configuration
Only for specifying location to resolve fixtures (packages in java, namespaces in .Net)
By this custom system under development you tell GreenPepper to look in "com.mycompany.fixtures" and "com.mycompany.specials.fixtures" to resolve fixtures in specfications that your are running.
Changing Fixture instanciation mechanism (Spring example)
Hooking document execution
Add Comment