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

org.cinchapi.concourse.test.Variables Maven / Gradle / Ivy

Go to download

A framework for writing end-to-end integration tests using the Concourse client and server

The newest version!
/*
 * Copyright (c) 2015 Cinchapi, Inc.
 * 
 * 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 org.cinchapi.concourse.test;

import java.util.Map;

import javax.annotation.Nullable;

import com.google.common.collect.Maps;

/**
 * A static collection of variables. This class should be used to register test
 * variables so that they can be dumped in the event of a test failure.
 * 
 * @author jnelson
 */
public final class Variables {

    /**
     * Remove all the registered variables.
     */
    public static void clear() {
        vars.clear();
    }

    /**
     * Dump the variables to a formatted string.
     * 
     * @return a string with a dump of the variables.
     */
    public static String dump() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry entry : vars.entrySet()) {
            sb.append(entry.getKey() + " = " + entry.getValue() + " ("
                    + entry.getValue().getClass().getName() + ")");
            sb.append("\n");
        }
        return sb.toString();
    }

    /**
     * Return the value of {@code variable} if it is registered, {@code null}
     * otherwise. This method will throw a ClassCastException if it is not
     * possible to cast the value to T.
     * 
     * @param key
     * @return the value of {@code variable}
     */
    @SuppressWarnings("unchecked")
    @Nullable
    public static  T get(String variable) {
        if(vars.containsKey(variable)) {
            return (T) vars.get(variable);
        }
        return null;
    }

    /**
     * Register {@code variable} as {@code value}.
     * 
     * @param variable
     * @param value
     * @return {@code value}
     */
    public static  T register(String variable, T value) {
        vars.put(variable, value);
        return value;
    }

    /**
     * The variables that are currently stored.
     */
    private final static Map vars = Maps.newLinkedHashMap();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy