takimafr / spring-dbunit

DBUnit support for Spring
Apache License 2.0
40 stars 19 forks source link

Add support for ObjectDataSetDecorator #41

Open bidorffOL opened 9 years ago

bidorffOL commented 9 years ago

Hi,

Looking at DBUnit's documentation, I saw that ReplacementDataSet not only supported String values but also Objects, is any way to see an ObjectDataSetDecorator supported by spring-dbunit in the future ?

From what I saw, its implementation would be quite simple:

public interface ObjectDataSetDecorator {

    Object getObjectToReplace();

    Object getObjectReplacement();
}

As for DbUnitDatabasePopulator.decorateDataSetIfNeeded, it would have to be refactored:


    private IDataSet decorateDataSetIfNeeded(IDataSet dataSet,
            Class<? extends DataSetDecorator>[] decorators,
            Class<? extends ObjectDataSetDecorator>[] objectDecorators) {

        IDataSet resultDataSet = null;

        if (decorators != null && decorators.length > 0) {
            if (resultDataSet == null)
                resultDataSet = new ReplacementDataSet(dataSet);

            for (Class<? extends DataSetDecorator> decoratorClass : decorators) {
                try {
                    DataSetDecorator decorator = decoratorClass.newInstance();
                    resultDataSet.addReplacementSubstring(
                            decorator.getStringToReplace(),
                            decorator.getStringReplacement());
                } catch (InstantiationException e) {
                    LOGGER.error("Could not instantiate DataSetDecorator {}"
                            + decoratorClass, e);
                } catch (IllegalAccessException e) {
                    LOGGER.error("Could not instantiate DataSetDecorator {}"
                            + decoratorClass, e);
                }
            }
        }

        if (objectDecorators != null && objectDecorators.length > 0) {
            if (resultDataSet == null)
                resultDataSet = new ReplacementDataSet(dataSet);

            for (Class<? extends ObjectDataSetDecorator> decoratorClass : objectDecorators) {
                try {
                    ObjectDataSetDecorator decorator = decoratorClass.newInstance();
                    resultDataSet.addReplacementObject(
                            decorator.getObjectToReplace(),
                            decorator.getObjectReplacement());
                } catch (InstantiationException e) {
                    LOGGER.error("Could not instantiate ObjectDataSetDecorator {}"
                            + decoratorClass, e);
                } catch (IllegalAccessException e) {
                    LOGGER.error("Could not instantiate ObjectDataSetDecorator {}"
                            + decoratorClass, e);
                }
            }
        }

        if (resultDataSet == null)
            resultDataSet = dataSet,

        return resultDataSet;
    }

Unfortunalely, I have no experience whatsoever using Git and I fail to send you a pull-request :(

Thank you for considering this feature.

rsertelon commented 9 years ago

Hi, as mentioned in #40 I don't have much time to work on this project right now.

As there are now 2 issues waiting, I will try to find some time. I don't know if it will be before 2015, I'll keep you updated.

bidorffOL commented 9 years ago

Thanks !