Search in sources :

Example 6 with Environment

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

the class EnvironmentImplTest method peek_required_without_value_is_error.

@Test
public void peek_required_without_value_is_error() {
    Environment e = new EnvironmentImpl();
    Location l = mockLocation();
    Component c = mockComponent();
    replay();
    e.push(Location.class, l);
    e.push(Component.class, c);
    try {
        e.peekRequired(List.class);
        unreachable();
    } catch (UnknownValueException ex) {
        assertEquals(ex.getMessage(), "No object of type java.util.List is available from the Environment.");
    }
    verify();
}
Also used : UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) Environment(org.apache.tapestry5.services.Environment) Component(org.apache.tapestry5.runtime.Component) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 7 with Environment

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

the class EnvironmentImplTest method peek_required_does_not_list_previouly_available.

@Test
public void peek_required_does_not_list_previouly_available() {
    Environment e = new EnvironmentImpl();
    Location l = mockLocation();
    replay();
    e.push(Location.class, l);
    e.pop(Location.class);
    try {
        e.peekRequired(Location.class);
        unreachable();
    } catch (UnknownValueException ex) {
        assertEquals(ex.getMessage(), "No object of type org.apache.tapestry5.commons.Location is available from the Environment.");
        assertFalse(ex.getAvailableValues().getValues().contains("org.apache.tapestry5.ioc.Location"));
    }
    verify();
}
Also used : UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) Environment(org.apache.tapestry5.services.Environment) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 8 with Environment

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

the class EnvironmentImplTest method push_and_pop.

@Test
public void push_and_pop() {
    Environment e = new EnvironmentImpl();
    Runnable r1 = mockRunnable();
    Runnable r2 = mockRunnable();
    replay();
    assertNull(e.push(Runnable.class, r1));
    assertSame(r1, e.peek(Runnable.class));
    assertSame(r1, e.push(Runnable.class, r2));
    assertSame(r2, e.peek(Runnable.class));
    assertSame(r2, e.pop(Runnable.class));
    assertSame(r1, e.pop(Runnable.class));
    verify();
}
Also used : Environment(org.apache.tapestry5.services.Environment) Test(org.testng.annotations.Test)

Example 9 with Environment

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

the class EnvironmentImplTest method peek_required_when_available.

@Test
public void peek_required_when_available() {
    Environment e = new EnvironmentImpl();
    Location l = mockLocation();
    replay();
    e.push(Location.class, l);
    assertSame(l, e.peekRequired(Location.class));
    verify();
}
Also used : Environment(org.apache.tapestry5.services.Environment) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 10 with Environment

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

the class BeanEditor method doPrepare.

/**
 * Used to initialize the model if necessary, to instantiate the object being edited if necessary, and to push the
 * BeanEditContext into the environment.
 */
void doPrepare() {
    if (model == null) {
        Class type = resources.getBoundType("object");
        model = modelSource.createEditModel(type, overrides.getOverrideMessages());
        BeanModelUtils.modify(model, add, include, exclude, reorder);
    }
    if (object == null) {
        try {
            object = model.newInstance();
        } catch (Exception ex) {
            String message = String.format("Exception instantiating instance of %s (for component '%s'): %s", PlasticUtils.toTypeName(model.getBeanType()), resources.getCompleteId(), ex);
            throw new TapestryException(message, resources.getLocation(), ex);
        }
    }
    BeanEditContext context = new BeanEditContextImpl(model.getBeanType());
    cachedObject = object;
    environment.push(BeanEditContext.class, context);
    // TAP5-2101: Always provide a new BeanValidationContext
    environment.push(BeanValidationContext.class, new BeanValidationContextImpl(object));
}
Also used : BeanEditContextImpl(org.apache.tapestry5.internal.BeanEditContextImpl) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) BeanEditContext(org.apache.tapestry5.services.BeanEditContext) BeanValidationContextImpl(org.apache.tapestry5.internal.BeanValidationContextImpl)

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