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

org.mattressframework.test.model.SimpleResourceBeanUtil Maven / Gradle / Ivy

/*
 * Copyright 2007-2008, Josh Devins.
 *
 * 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.mattressframework.test.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.mattressframework.api.annotations.HeaderParam;
import org.mattressframework.api.annotations.HttpContext;
import org.mattressframework.api.annotations.HttpEntity;
import org.mattressframework.api.annotations.PathParam;
import org.mattressframework.api.annotations.QueryParam;

/**
 * A simple utility class for statically accessing methods on the test resource
 * beans, {@link ValidSimpleResourceBean} and {@link InvalidSimpleResourceBean}.
 * 
 * @author Josh Devins ([email protected])
 */
public final class SimpleResourceBeanUtil {

    private static Map invalidSimpleResourceBeanMethods;

    private static Map validSimpleResourceBeanMethods;

    private static Map, Annotation> annotations;

    static {
        validSimpleResourceBeanMethods =
                initializeMap(ValidSimpleResourceBean.class.getMethods());
        invalidSimpleResourceBeanMethods =
                initializeMap(InvalidSimpleResourceBean.class.getMethods());

        annotations = new HashMap, Annotation>();
        initializeAnnotation(PathParam.class,
                ValidSimpleResourceBean.TEST_PATH_VARIABLE_METHOD);
        initializeAnnotation(QueryParam.class,
                ValidSimpleResourceBean.TEST_QUERY_VARIABLE_METHOD);
        initializeAnnotation(HeaderParam.class,
                ValidSimpleResourceBean.TEST_HEADER_VARIABLE_METHOD);
        initializeAnnotation(HttpEntity.class,
                ValidSimpleResourceBean.TEST_ENTITY_VARIABLE_METHOD);
        initializeAnnotation(HttpContext.class,
                ValidSimpleResourceBean.TEST_CONTEXT_VARIABLE_METHOD);
    }

    public static  T getAnnotation(final Class clazz) {
        return clazz.cast(annotations.get(clazz));
    }

    public static Method getInvalidMethod(final String methodName) {
        return invalidSimpleResourceBeanMethods.get(methodName);
    }

    public static Method getValidMethod(final String methodName) {
        return validSimpleResourceBeanMethods.get(methodName);
    }

    private static void initializeAnnotation(
            final Class clazz, final String methodName) {
        annotations.put(
                clazz,
                validSimpleResourceBeanMethods.get(methodName).getParameterAnnotations()[0][0]);
    }

    private static Map initializeMap(final Method[] methods) {

        Map map = new HashMap(methods.length);

        for (Method method : methods) {
            map.put(method.getName(), method);
        }

        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy