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

org.jspringbot.keyword.test.data.TestData Maven / Gradle / Ivy

There is a newer version: 1.9
Show newest version
/*
 * Copyright (c) 2012. JSpringBot. All Rights Reserved.
 *
 * See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The JSpringBot licenses this file to You 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 org.jspringbot.keyword.test.data;

import au.com.bytecode.opencsv.CSVWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Represents a single test data
 */
public class TestData implements Serializable {
    private List headers;

    private List> contents;

    private int headerColumnSize;

    public TestData(List headers) {
        this.headers = headers;

        headerColumnSize = 0;
        for(String header : headers) {
            if(StringUtils.isNotEmpty(header)) {
                headerColumnSize++;
            }
        }
    }

    public void addContent(List content) {
        if(CollectionUtils.isEmpty(content)) {
            return;
        }

        if(contents == null) {
            contents = new ArrayList>();
        }

        contents.add(content);
    }

    public int getHeaderColumnSize() {
        return headerColumnSize;
    }

    public boolean hasContent() {
        return CollectionUtils.isNotEmpty(contents);
    }

    public String getHeader(int index) {
        return headers.get(index);
    }

    public List buildContentIndices() {
        List indices = new ArrayList(contents.size());

        for(int i = 0; i < contents.size(); i++) {
            indices.add(i);
        }

        return indices;
    }

    public int getSize() {
        return CollectionUtils.size(contents);
    }

    public String getCSVContent() throws IOException {
        StringWriter str = new StringWriter();
        CSVWriter writer = new CSVWriter(str);

        try {
            for(List content : contents) {
                List line = content.subList(headerColumnSize - 1, content.size());
                writer.writeNext(line.toArray(new String[line.size()]));
            }
        } finally {
            IOUtils.closeQuietly(writer);
            IOUtils.closeQuietly(str);
        }

        return str.toString();
    }

    public String getContent(int contentIndex, int contentColumnIndex) {
        return contents.get(contentIndex).get(contentColumnIndex + (headerColumnSize - 1));
    }

    public List getContentAsList(int contentColumnIndexKey, String key, int contentColumnIndexValue) {
        List contentList = new ArrayList();

        if(CollectionUtils.isNotEmpty(contents)) {
            for(List content : contents) {
                if(content.get(contentColumnIndexKey + (headerColumnSize - 1)).equals(key)) {
                    contentList.add(content.get(contentColumnIndexValue + (headerColumnSize - 1)));
                }
            }
        }

        return contentList;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy