org.failearly.dataset.internal.resource.DataSetResourceFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.failearly.dataset.internal.resource.DataSetResourceFactory.java

Source

/*
 * dataSet - Test Support For Datastores.
 *
 * Copyright (C) 2014-2014 Marko Umek (http://fail-early.com/contact)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package org.failearly.dataset.internal.resource;

import org.apache.commons.lang.StringUtils;
import org.failearly.dataset.DataSet;
import org.failearly.dataset.DataStoreSetup;
import org.failearly.dataset.config.DataSetProperties;
import org.failearly.dataset.datastore.DataSetResource;
import org.failearly.dataset.internal.generator.resolver.GeneratorCreator;

import java.lang.reflect.Method;
import java.util.List;

/**
 * DataSetResource provides factory methods. These creates {@link org.failearly.dataset.datastore.DataSetResource}
 * based on existence of the resource, and the type of the resource (with or without template suffix).
 */
public final class DataSetResourceFactory {
    private DataSetResourceFactory() {
    }

    /**
     * Creates a DataSetResource from {@link org.failearly.dataset.DataStoreSetup}.
     * @param resourceName a resource name
     * @param dataStoreSetup the dataStoreInitializer
     * @param generatorCreators for template resources the generatorCreators
     * @param testClass the test class (used for resolving relative path and resource loading).
     * @return an appropriate DataSetResource instance
     */
    public static DataSetResource createDataSetResourceFromDataStoreInitializer(String resourceName,
            DataStoreSetup dataStoreSetup, List<GeneratorCreator> generatorCreators, Class<?> testClass) {
        String trimmedResourceName = trim(resourceName);
        if (trimmedResourceName == null) {
            return new NullDataSetResource("<empty resource path>", dataStoreSetup);
        }
        return createDataSetResource(resolveResourceName(trimmedResourceName, absoluteResourcePath(testClass)),
                testClass, dataStoreSetup, generatorCreators);
    }

    /**
     * Creates a DataSetResource from test {@link java.lang.reflect.Method}.
     * @param resourceType the resource type
     * @param resourceName a resource name
     * @param dataSet the data set
     * @param generatorCreators for template resources the generatorCreators
     * @return an appropriate DataSetResource instance
     */
    public static DataSetResource createDataSetResourceFromMethod(ResourceType resourceType, String resourceName,
            DataSet dataSet, List<GeneratorCreator> generatorCreators, Method testMethod) {
        resourceName = trim(resourceName);
        if (resourceName == null) {
            return createDefaultDataSetResourceFromMethod(resourceType, dataSet, generatorCreators, testMethod);
        }
        return createDataSetResource(resolveResourceName(resourceName, absoluteResourcePath(testMethod)),
                testMethod.getDeclaringClass(), dataSet, generatorCreators);
    }

    /**
     * Creates a default DataSetResource from test {@link java.lang.Class}.
     * @param resourceType the resource type
     * @param dataSet the data set
     * @param generatorCreators for template resources the generatorCreators
     * @param testMethod the test method (used for resolving relative path and resource loading).
     * @return an appropriate DataSetResource instance
     */
    public static DataSetResource createDefaultDataSetResourceFromMethod(ResourceType resourceType, DataSet dataSet,
            List<GeneratorCreator> generatorCreators, Method testMethod) {
        return createDataSetResource(
                resolveResourceName(createDefaultResourceName(testMethod, dataSet, resourceType),
                        absoluteResourcePath(testMethod)),
                testMethod.getDeclaringClass(), dataSet, generatorCreators);
    }

    /**
     * Creates a DataSetResource from test {@link java.lang.Class}.
     * @param resourceType the resource type
     * @param resourceName a resource name
     * @param dataSet the data set
     * @param generatorCreators for template resources the generatorCreators
     * @param testClass the test class (used for resolving relative path and resource loading).
     * @return an appropriate DataSetResource instance
     */
    public static DataSetResource createDataSetResourceFromClass(ResourceType resourceType, String resourceName,
            DataSet dataSet, List<GeneratorCreator> generatorCreators, Class<?> testClass) {
        resourceName = trim(resourceName);
        if (resourceName == null) {
            return createDefaultDataSetResourceFromClass(resourceType, dataSet, generatorCreators, testClass);
        }
        return createDataSetResource(resolveResourceName(resourceName, absoluteResourcePath(testClass)), testClass,
                dataSet, generatorCreators);
    }

    /**
     * Creates a default DataSetResource from test {@link java.lang.Class}.
     * @param resourceType the resource type
     * @param dataSet the data set
     * @param generatorCreators for template resources the generatorCreators
     * @param testClass the test class (used for resolving relative path and resource loading).
     * @return an appropriate DataSetResource instance
     */
    public static DataSetResource createDefaultDataSetResourceFromClass(ResourceType resourceType, DataSet dataSet,
            List<GeneratorCreator> generatorCreators, Class<?> testClass) {
        return createDataSetResource(
                resolveResourceName(createDefaultResourceName(testClass, dataSet, resourceType),
                        absoluteResourcePath(testClass)),
                testClass, dataSet, generatorCreators);
    }

    private static DataSetResource createDataSetResource(String resourceName, Class<?> testClass, DataSet dataSet,
            List<GeneratorCreator> generatorCreators) {
        if (doesNotExistResource(testClass, resourceName)) {
            return new NullDataSetResource(resourceName, dataSet);
        }

        if (isTemplateResource(resourceName)) {
            return new TemplateDataSetResource(resourceName, dataSet, testClass, generatorCreators);
        }

        return new StandardDataSetResource(resourceName, dataSet, testClass);
    }

    private static DataSetResource createDataSetResource(String resourceName, Class<?> testClass,
            DataStoreSetup dataStoreSetup, List<GeneratorCreator> generatorCreators) {
        if (doesNotExistResource(testClass, resourceName)) {
            return new NullDataSetResource(resourceName, dataStoreSetup);
        }

        if (isTemplateResource(resourceName)) {
            return new TemplateDataSetResource(resourceName, dataStoreSetup, testClass, generatorCreators);
        }

        return new StandardDataSetResource(resourceName, dataStoreSetup, testClass);
    }

    private static boolean isTemplateResource(String resourceName) {
        return resourceName.endsWith(DataSetProperties.getTemplateSuffix());
    }

    private static boolean doesNotExistResource(Class<?> testClass, String resourceName) {
        return null == testClass.getResource(resourceName);
    }

    private static String createDefaultResourceName(Class<?> testClass, DataSet dataSet,
            ResourceType resourceType) {
        return testClass.getSimpleName() + resolveDataStoreSuffix(dataSet, resourceType);
    }

    private static String createDefaultResourceName(Method testMethod, DataSet dataSet, ResourceType resourceType) {
        return testMethod.getDeclaringClass().getSimpleName() + "-" + testMethod.getName()
                + resolveDataStoreSuffix(dataSet, resourceType);
    }

    private static String resolveDataStoreSuffix(DataSet dataSet, ResourceType resourceType) {
        String suffix = resourceType.resolveDataStoreSuffix(dataSet);
        if (!suffix.startsWith(".")) {
            suffix = "." + suffix;
        }
        return suffix;
    }

    private static String resolveResourceName(String resourceName, String resourcePath) {
        if (resourceName.startsWith("/"))
            return resourceName;
        return resourcePath + resourceName;
    }

    private static String absoluteResourcePath(Method testMethod) {
        return absoluteResourcePath(testMethod.getDeclaringClass());
    }

    private static String absoluteResourcePath(Class<?> clazz) {
        return "/" + packageAsName(clazz).replace(".", "/") + "/";
    }

    private static String packageAsName(Class<?> clazz) {
        return clazz.getPackage().getName();
    }

    private static String trim(String resourceName) {
        return StringUtils.trimToNull(resourceName);
    }

}