Dashboard > GreenPepper Open Documentation Refactoring > ... > 7. Advanced developer guide > Using custom types
  GreenPepper Open Documentation Refactoring Log In View a printable version of the current page.  
  Using custom types
Added by Laurent Cobos, last edited by Laurent Cobos on Aug 13, 2009  (view change)
Labels: 
(None)

Using custom types

All the examples in the documents are in strings, but fixtures want to process and return other data types.

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 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);
    }
}

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

In Java
public static void register( TypeConverter converter)
"In C#"
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 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.

Java

public static T parse(String val);

And then to revert back to a string,

public static String toString(T value)

C#

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

From example to fixture

1 First will verify if the type is can self convert (ie public static T parse(String) or public static T ValueOf(string))
2 If not, look for a registered TypeConverter that can handles the type.
3 An UnsupportedOperationException will be thrown

From fixture return value to String

1 First will verify if the type is can self revert (ie public static String toString(T) or public static T ToString(string))
2 If not, look for a registered TypeConverter that can handles the type.
3 Use the toString() or ToString() method on the data itself.

DEMONSTRATION LICENSE - This Confluence site is for demonstration purposes only. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.4.3 Build:#705 Mar 21, 2007) - Bug/feature request - Contact Administrators