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

com.greenpepper.reflect.AbstractFixture Maven / Gradle / Ivy

The newest version!
package com.greenpepper.reflect;

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

import com.greenpepper.util.Introspector;

import static com.greenpepper.util.NameUtils.toJavaIdentifierForm;

/**
 * 

Abstract AbstractFixture class.

* * @author oaouattara * @version $Id: $Id */ public abstract class AbstractFixture implements Fixture { protected final Object target; /** *

Constructor for AbstractFixture.

* * @param target a {@link java.lang.Object} object. */ public AbstractFixture(Object target) { this.target = target; } /** *

Getter for the field target.

* * @return a {@link java.lang.Object} object. */ public Object getTarget() { return target; } /** {@inheritDoc} */ public boolean canSend(String message) { return getSendMessage(message) != null; } /** {@inheritDoc} */ public boolean canCheck(String message) { return getCheckMessage(message) != null; } /** {@inheritDoc} */ public Message check(String message) throws NoSuchMessageException { Message check = getCheckMessage(message); if (check == null) throw new NoSuchMessageException(message); return check; } /** {@inheritDoc} */ public Message send(String message) throws NoSuchMessageException { Message send = getSendMessage(message); if (send == null) throw new NoSuchMessageException(message); return send; } /** *

getCheckMessage.

* * @param name a {@link java.lang.String} object. * @return a {@link com.greenpepper.reflect.Message} object. */ protected abstract Message getCheckMessage(String name); /** *

getSendMessage.

* * @param name a {@link java.lang.String} object. * @return a {@link com.greenpepper.reflect.Message} object. */ protected abstract Message getSendMessage(String name); /** *

getSetter.

* * @param type a {@link java.lang.Class} object. * @param name a {@link java.lang.String} object. * @return a {@link java.lang.reflect.Method} object. */ protected Method getSetter(Class type, String name) { return introspector(type).getSetter( toJavaIdentifierForm(name)); } /** *

getField.

* * @param type a {@link java.lang.Class} object. * @param name a {@link java.lang.String} object. * @return a {@link java.lang.reflect.Field} object. */ protected Field getField(Class type, String name) { return introspector(type).getField( toJavaIdentifierForm(name)); } /** *

getMethods.

* * @param type a {@link java.lang.Class} object. * @param name a {@link java.lang.String} object. * @return a {@link java.util.List} object. */ protected List getMethods(Class type, String name) { return introspector(type).getMethods( toJavaIdentifierForm(name)); } /** *

getGetter.

* * @param type a {@link java.lang.Class} object. * @param name a {@link java.lang.String} object. * @return a {@link java.lang.reflect.Method} object. */ protected Method getGetter(Class type, String name) { return introspector(type).getGetter( toJavaIdentifierForm(name)); } /** *

introspector.

* * @param type a {@link java.lang.Class} object. * @return a {@link com.greenpepper.util.Introspector} object. */ protected Introspector introspector(Class type) { return Introspector.ignoringCase(type); } /** *

getSystemUnderTest.

* * @return a {@link java.lang.Object} object. */ protected Object getSystemUnderTest() { for (Method method : target.getClass().getMethods()) { if (method.isAnnotationPresent(SystemUnderTest.class)) { return invoke(method); } } Method m = getGetter( target.getClass(), "systemUnderTest" ); return m != null ? invoke( m ) : null; } /** *

invoke.

* * @param method a {@link java.lang.reflect.Method} object. * @return a {@link java.lang.Object} object. */ protected Object invoke(Method method) { try { return method.invoke(target); } catch (Exception e) { return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy