All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crosstreelabs.testing.dbunit.EnhancedXmlDataSet Maven / Gradle / Ivy

/*
 * Copyright 2014 Crosstree Labs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.crosstreelabs.testing.dbunit;

import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.xml.XmlDataSet;

/**
 * The EnhancedXmlDataSet offers some additional functionality over the vanilla
 * dataset, including the ability to add binary data.
 */
public abstract class EnhancedXmlDataSet extends XmlDataSet {
    
    public EnhancedXmlDataSet(final Reader reader) throws DataSetException {
        super(reader);
    }

    public EnhancedXmlDataSet(final InputStream in) throws DataSetException {
        super(in);
    }

    @Override
    public void row(Object[] values) throws DataSetException {
        try {
            handleSpecialCases(values);
        } catch (UnsupportedEncodingException ex) {
            throw new DataSetException(ex);
        }
        super.row(values);
    }
    
    public abstract Map getConverters();
    
    
    private void handleSpecialCases(final Object[] values) throws UnsupportedEncodingException {
        for (int i = 0; i < values.length; i++) {
            if (values[i] == null || !values[i].toString().contains(":")
                    || getConverters() == null || getConverters().isEmpty()) {
                continue;
            }
            
            String str = values[i].toString();
            int pos = str.indexOf(":");
            if (pos == -1) {
                continue;
            }
            
            String function = str.substring(0, pos);
            if (getConverters().containsKey(function)) {
                String remainder = str.substring(pos+1);
                values[i] = getConverters().get(function).convert(remainder);
            }
        }
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy