Search in sources :

Example 1 with Environment

use of org.apache.tapestry5.services.Environment in project tapestry-5 by apache.

the class MessagesAnnotationConstraintGeneratorTest method pushAndTrainEnvironmentalObjects.

private void pushAndTrainEnvironmentalObjects(Environment e, boolean hasProperty, String propertyValue) {
    Messages messages = mockMessages();
    train_contains(messages, "testProperty-validate", hasProperty);
    if (hasProperty) {
        train_get(messages, "testProperty-validate", propertyValue);
    }
    EnvironmentMessages em = new EnvironmentMessages(messages, "testProperty");
    e.push(EnvironmentMessages.class, em);
    replay();
}
Also used : Messages(org.apache.tapestry5.commons.Messages)

Example 2 with Environment

use of org.apache.tapestry5.services.Environment in project tapestry-5 by apache.

the class BeanEditorTest method refresh_bean_validation_context.

@Test
public void refresh_bean_validation_context() {
    ComponentResources resources = mockComponentResources();
    BeanModelSource source = mockBeanModelSource();
    BeanModel model = mockBeanModel();
    Environment env = mockEnvironment();
    RegistrationData data = new RegistrationData();
    Messages messages = mockMessages();
    PropertyOverrides overrides = mockPropertyOverrides();
    BeanValidationContext beanValidationContext = newMock(BeanValidationContext.class);
    train_getBoundType(resources, "object", RegistrationData.class);
    train_createEditModel(source, RegistrationData.class, messages, model);
    train_getOverrideMessages(overrides, messages);
    expect(model.newInstance()).andReturn(data);
    expect(env.push(eq(BeanValidationContext.class), isA(BeanValidationContext.class))).andReturn(beanValidationContext);
    expect(model.getBeanType()).andReturn(RegistrationData.class);
    BeanEditContext ctxt = new BeanEditContext() {

        public Class<?> getBeanClass() {
            return RegistrationData.class;
        }

        public <T extends Annotation> T getAnnotation(Class<T> type) {
            return null;
        }
    };
    expect(env.push(eq(BeanEditContext.class), contextEq())).andReturn(ctxt);
    replay();
    BeanEditor component = new BeanEditor();
    component.inject(resources, overrides, source, env);
    component.doPrepare();
    verify();
}
Also used : RegistrationData(org.apache.tapestry5.integration.app1.data.RegistrationData) BeanModelSource(org.apache.tapestry5.beanmodel.services.BeanModelSource) Messages(org.apache.tapestry5.commons.Messages) BeanModel(org.apache.tapestry5.beanmodel.BeanModel) BeanValidationContext(org.apache.tapestry5.internal.BeanValidationContext) Environment(org.apache.tapestry5.services.Environment) PropertyOverrides(org.apache.tapestry5.PropertyOverrides) Annotation(java.lang.annotation.Annotation) ComponentResources(org.apache.tapestry5.ComponentResources) BeanEditContext(org.apache.tapestry5.services.BeanEditContext) Test(org.testng.annotations.Test)

Example 3 with Environment

use of org.apache.tapestry5.services.Environment in project tapestry-5 by apache.

the class MessagesAnnotationConstraintGeneratorTest method no_environment.

@Test
public void no_environment() {
    Environment e = getService(Environment.class);
    MessagesConstraintGenerator gen = new MessagesConstraintGenerator(e);
    assertNull(gen.buildConstraints(null, null));
}
Also used : Environment(org.apache.tapestry5.services.Environment) Test(org.testng.annotations.Test)

Example 4 with Environment

use of org.apache.tapestry5.services.Environment in project tapestry-5 by apache.

the class Form method beginRender.

void beginRender(MarkupWriter writer) {
    Link link = resources.createFormEventLink(EventConstants.ACTION, context);
    String actionURL = secure && secureEnabled ? link.toAbsoluteURI(true) : link.toURI();
    actionSink = new ComponentActionSink(logger, clientDataEncoder);
    clientId = javascriptSupport.allocateClientId(resources);
    // Pre-register some names, to prevent client-side collisions with function names
    // attached to the JS Form object.
    IdAllocator allocator = new IdAllocator();
    preallocateNames(allocator);
    formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
    environment.push(FormSupport.class, formSupport);
    environment.push(ValidationTracker.class, tracker);
    if (autofocus) {
        ValidationDecorator autofocusDecorator = new AutofocusValidationDecorator(environment.peek(ValidationDecorator.class), tracker, javascriptSupport);
        environment.push(ValidationDecorator.class, autofocusDecorator);
    }
    // Now that the environment is setup, inform the component or other
    // listeners that the form
    // is about to render.
    resources.triggerEvent(EventConstants.PREPARE_FOR_RENDER, context, null);
    resources.triggerEvent(EventConstants.PREPARE, context, null);
    // Push BeanValidationContext only after the container had a chance to prepare
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(validate));
    // Save the form element for later, in case we want to write an encoding
    // type attribute.
    form = writer.element("form", "id", clientId, "method", "post", "action", actionURL, "data-update-zone", zone, DATA_ATTRIBUTE, DATA_ATTRIBUTE_VALUE);
    if (clientValidation != ClientValidation.NONE) {
        writer.attributes("data-validate", "submit");
    }
    if (async) {
        javascriptSupport.require("t5/core/zone");
        writer.attributes("data-async-trigger", true);
    }
    resources.renderInformalParameters(writer);
    div = writer.element("div");
    for (String parameterName : link.getParameterNames()) {
        String[] values = link.getParameterValues(parameterName);
        for (String value : values) {
            // but the input value shouldn't be encoded.
            try {
                value = URLDecoder.decode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                logger.error("Enable to decode parameter value for parameter {} in form {}", parameterName, form.getName(), e);
            }
            writer.element("input", "type", "hidden", "name", parameterName, "value", value);
            writer.end();
        }
    }
    // div
    writer.end();
    environment.peek(Heartbeat.class).begin();
}
Also used : ComponentActionSink(org.apache.tapestry5.corelib.internal.ComponentActionSink) IdAllocator(org.apache.tapestry5.ioc.util.IdAllocator) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Heartbeat(org.apache.tapestry5.services.Heartbeat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AutofocusValidationDecorator(org.apache.tapestry5.internal.util.AutofocusValidationDecorator) Link(org.apache.tapestry5.http.Link) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

Example 5 with Environment

use of org.apache.tapestry5.services.Environment in project tapestry-5 by apache.

the class EnvironmentImplTest method peek_when_empty_returns_null.

@Test
public void peek_when_empty_returns_null() {
    Environment e = new EnvironmentImpl();
    assertNull(e.peek(Runnable.class));
    assertNull(e.peek(Map.class));
}
Also used : Environment(org.apache.tapestry5.services.Environment) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

Environment (org.apache.tapestry5.services.Environment)17 Test (org.testng.annotations.Test)17 ComponentResources (org.apache.tapestry5.ComponentResources)6 Messages (org.apache.tapestry5.commons.Messages)6 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)5 BeanModelSource (org.apache.tapestry5.beanmodel.services.BeanModelSource)5 BeanModel (org.apache.tapestry5.beanmodel.BeanModel)4 Location (org.apache.tapestry5.commons.Location)4 RegistrationData (org.apache.tapestry5.integration.app1.data.RegistrationData)3 BeanEditContext (org.apache.tapestry5.services.BeanEditContext)3 Annotation (java.lang.annotation.Annotation)2 FieldValidator (org.apache.tapestry5.FieldValidator)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)2 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)2 BeanValidationContextImpl (org.apache.tapestry5.internal.BeanValidationContextImpl)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Locale (java.util.Locale)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1