Модуль проверки аннотаций модульного тестирования Struts2. - PullRequest
3 голосов
/ 19 февраля 2011

Привет У меня есть следующий класс модели страницы.

public class CarModel extends PageModel {
    private static final long serialVersionUID = 4766706536002862335L;

    private String            name;
    private String            description;


    @RequiredStringValidator(type = ValidatorType.FIELD, key = "errors.required.name", trim = true)
    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @RequiredStringValidator(type = ValidatorType.FIELD, key = "errors.required.description", trim = true)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }
}

У меня есть класс действий.

@Component(value = "CarAction")
@Namespace(value = "/car")
@Action(value = "/car")
@ExceptionMapping(exception = "com.cars.AutomobileException", result = ".page.car")
@Result(name = "success", type = "tiles", location = ".page.car")
public class CarAction extends BaseAction implements ParameterAware {
   private CarModel pageModel;

   @Action(value = "/add", results = @Result(name = "addResult", params = { "actionName",
            "addResult" }, type = "chain"))
    @InputConfig(methodName = "execute")
    @Validations(visitorFields = { @VisitorFieldValidator(shortCircuit = true, message = "Their are errors to be corrected", fieldName = "pageModel") })
    public String add() {
        this.validate();
        return SUCCESS;
    }
}

вот мое базовое действие.

@InterceptorRefs({
        @InterceptorRef(value = "createSession"),
        @InterceptorRef(value = "tokenSession", params = { "includeMethods", "add,addResult" }),
        @InterceptorRef(value = "exception"),
        @InterceptorRef(value = "alias"),
        @InterceptorRef(value = "i18n"),
        @InterceptorRef(value = "chain"),
        @InterceptorRef(value = "timer"),
        @InterceptorRef(value = "logger"),
        @InterceptorRef(value = "checkbox"),
        @InterceptorRef(value = "staticParams"),
        @InterceptorRef(value = "actionMappingParams"),
        @InterceptorRef(value = "params", params = { "excludeParams", "dojo\\..*,^struts\\..*" }),
        @InterceptorRef(value = "conversionError"),
        @InterceptorRef(value = "validation", params = { "excludeMethods", "cancel,execute,reset,next,previous,updateItemPerPage,goToPage,browse", "validateAnnotatedMethodOnly", "true" }),
        @InterceptorRef(value = "annotationWorkflow", params = { "excludeMethods", "input,back,cancel,browse" }),
        @InterceptorRef(value = "execAndWait") })
@ExceptionMappings({
        @ExceptionMapping(exception = "org.springframework.dao.DataAccessException", result = "..page.data.error"),
        @ExceptionMapping(result = ".page.servlet.error", exception = "javax.servlet.ServletException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.NullPointerException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.outOfMemory"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.ArrayIndexOutOfBoundsException"),
        @ExceptionMapping(result = ".page.runtime.error", exception = "java.lang.ArithmeticException") })
@Results({
        @Result(name = "invalid.token", location = ".page.runtime.error", type = "tiles"),
        @Result(name = "wait", location = ".page.wait", type = "tiles") })
@ParentPackage("tiles-default")
public abstract class BaseAction extends ActionSupport {
    private static final long serialVersionUID = -5898754579799304669L;

}

в моем тесте junit

this.request.setParameter("name", "FD");
final ActionProxy proxy = this.getActionProxy("/car/car");
Assert.assertNotNull(proxy);

final CarAction carAction = CarAction.class.cast(proxy.getAction());

final CarModel pageModel = new CarModel(5);

carAction.setPageModel(pageModel);
Assert.assertEquals(carAction.execute(), Action.SUCCESS);

-- this is where it fails. 
final Map<String, List<String>> fieldErrors = carAction.getFieldErrors();
Assert.assertFalse(fieldErrors.isEmpty());

Кто-нибудь знает, как получить проверку аннотации для запуска в тесте junit?

Спасибо

...