Search in sources :

Example 1 with Holder

use of org.apache.tapestry5.internal.util.Holder in project tapestry-5 by apache.

the class ClientPersistentFieldStorageImplTest method store_and_restore_a_change.

@SuppressWarnings("unchecked")
@Test
public void store_and_restore_a_change() {
    Request request = mockRequest(null);
    Link link = mockLink();
    String pageName = "Foo";
    String componentId = "bar.baz";
    String fieldName = "biff";
    Object value = 99;
    final Holder<String> holder = captureLinkModification(link);
    replay();
    ClientPersistentFieldStorage storage1 = new ClientPersistentFieldStorageImpl(request, clientDataEncoder, analyzer);
    storage1.postChange(pageName, componentId, fieldName, value);
    List<PersistentFieldChange> changes1 = newList(storage1.gatherFieldChanges(pageName));
    storage1.updateLink(link);
    verify();
    assertEquals(changes1.size(), 1);
    PersistentFieldChange change1 = changes1.get(0);
    assertEquals(change1.getComponentId(), componentId);
    assertEquals(change1.getFieldName(), fieldName);
    assertEquals(change1.getValue(), value);
    // Now more training ...
    train_getParameter(request, ClientPersistentFieldStorageImpl.PARAMETER_NAME, holder.get());
    replay();
    ClientPersistentFieldStorage storage2 = new ClientPersistentFieldStorageImpl(request, clientDataEncoder, analyzer);
    List<PersistentFieldChange> changes2 = newList(storage2.gatherFieldChanges(pageName));
    verify();
    assertEquals(changes2.size(), 1);
    PersistentFieldChange change2 = changes2.get(0);
    assertEquals(change2.getComponentId(), componentId);
    assertEquals(change2.getFieldName(), fieldName);
    assertEquals(change2.getValue(), value);
    assertNotSame(change1, change2);
}
Also used : Request(org.apache.tapestry5.http.services.Request) PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Link(org.apache.tapestry5.http.Link) Test(org.testng.annotations.Test)

Example 2 with Holder

use of org.apache.tapestry5.internal.util.Holder in project tapestry-5 by apache.

the class SelectTest method submitted_option_matches_against_value_encoded_option_model_value.

/**
 * This a test for TAP5-2184
 */
@Test
public void submitted_option_matches_against_value_encoded_option_model_value() throws ValidationException {
    ValueEncoder<Integer> encoder = getService(ValueEncoderSource.class).getValueEncoder(Integer.class);
    ValidationTracker tracker = mockValidationTracker();
    Request request = mockRequest();
    Messages messages = mockMessages();
    FieldValidationSupport fvs = mockFieldValidationSupport();
    TypeCoercer typeCoercer = mockTypeCoercer();
    InternalComponentResources resources = mockInternalComponentResources();
    Binding selectModelBinding = mockBinding();
    expect(request.getParameter("xyz")).andReturn("5");
    expect(messages.contains(EasyMock.anyObject(String.class))).andReturn(false).anyTimes();
    expect(resources.getBinding("model")).andReturn(selectModelBinding);
    final Holder<SelectModel> modelHolder = Holder.create();
    expect(typeCoercer.coerce(EasyMock.or(EasyMock.isA(SelectModel.class), EasyMock.isNull()), EasyMock.eq(SelectModel.class))).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    expect(selectModelBinding.get()).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    Select select = new Select();
    tracker.recordInput(select, "5");
    fvs.validate(5, resources, null);
    replay();
    // TAP5-2184 is triggered by the automatic String->SelectModel coercion, because the OptionModel
    // values are Strings even if the desired property type is not (Integer, here). Select has a little
    // hack to run the model values through the ValueEncoder for comparison.
    modelHolder.put(getService(TypeCoercer.class).coerce("1,5,10,20", SelectModel.class));
    set(select, "encoder", encoder);
    set(select, "model", modelHolder.get());
    set(select, "request", request);
    set(select, "secure", SecureOption.ALWAYS);
    // Disable BeanValidationContextSupport
    set(select, "beanValidationDisabled", true);
    set(select, "tracker", tracker);
    set(select, "fieldValidationSupport", fvs);
    set(select, "typeCoercer", typeCoercer);
    set(select, "resources", resources);
    select.processSubmission("xyz");
    verify();
    assertEquals(get(select, "value"), 5);
}
Also used : Messages(org.apache.tapestry5.commons.Messages) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Request(org.apache.tapestry5.http.services.Request) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel) Test(org.testng.annotations.Test)

Example 3 with Holder

use of org.apache.tapestry5.internal.util.Holder in project tapestry-5 by apache.

the class PropertyConduitSourceImplTest method not_operator.

@Test
public void not_operator() {
    PropertyConduit conduit = source.create(IntegerHolder.class, "! value");
    IntegerHolder holder = new IntegerHolder();
    assertEquals(conduit.get(holder), Boolean.TRUE);
    holder.setValue(99);
    assertEquals(conduit.get(holder), Boolean.FALSE);
}
Also used : IntegerHolder(org.apache.tapestry5.integration.app1.data.IntegerHolder) PropertyConduit(org.apache.tapestry5.beanmodel.PropertyConduit) InternalPropertyConduit(org.apache.tapestry5.beanmodel.internal.InternalPropertyConduit) PropBindingFactoryTest(org.apache.tapestry5.internal.bindings.PropBindingFactoryTest) Test(org.testng.annotations.Test)

Example 4 with Holder

use of org.apache.tapestry5.internal.util.Holder in project tapestry-5 by apache.

the class PropertyConduitSourceImplTest method navigate_through_public_field.

@Test
public void navigate_through_public_field() {
    PublicFieldBean bean = new PublicFieldBean();
    PublicFieldBeanHolder holder = new PublicFieldBeanHolder(bean);
    bean.stringField = "x";
    PropertyConduit pc = source.create(PublicFieldBeanHolder.class, "bean.stringField");
    assertEquals(pc.get(holder), "x");
    pc.set(holder, "y");
    assertEquals(bean.stringField, "y");
}
Also used : PropertyConduit(org.apache.tapestry5.beanmodel.PropertyConduit) InternalPropertyConduit(org.apache.tapestry5.beanmodel.internal.InternalPropertyConduit) PropBindingFactoryTest(org.apache.tapestry5.internal.bindings.PropBindingFactoryTest) Test(org.testng.annotations.Test)

Example 5 with Holder

use of org.apache.tapestry5.internal.util.Holder in project tapestry-5 by apache.

the class PropertyConduitSourceImplTest method generic_parameterized_base_with_properties.

@Test
public void generic_parameterized_base_with_properties() {
    final String first = "John";
    final String last = "Doe";
    final SimpleBean simple = new SimpleBean();
    simple.setAge(2);
    simple.setFirstName(first);
    simple.setLastName(last);
    final RealizedParameters bean = new RealizedParameters();
    final Holder<SimpleBean> holder = new Holder<SimpleBean>();
    holder.put(simple);
    bean.setType1Property(holder);
    bean.setType2Property(1234L);
    bean.type1Field = holder;
    bean.type2Field = 5678L;
    bean.type2ArrayField = new Long[] { 123L, 456L };
    PropertyConduit conduit = source.create(RealizedParameters.class, "type1property.get().firstName");
    assertSame(conduit.get(bean), first);
    conduit.set(bean, "Change");
    assertSame(conduit.get(bean), "Change");
    conduit.set(bean, first);
    conduit = source.create(RealizedParameters.class, "type1field.get().firstName");
    assertSame(conduit.get(bean), first);
    conduit = source.create(RealizedParameters.class, "type2field");
    assertEquals(conduit.get(bean), bean.type2Field);
    conduit = source.create(RealizedParameters.class, "type2property");
    assertEquals(conduit.get(bean), bean.getType2Property());
    conduit = source.create(RealizedParameters.class, "type2ArrayField");
    assertEquals(conduit.get(bean), bean.type2ArrayField);
}
Also used : Holder(org.apache.tapestry5.internal.util.Holder) IntegerHolder(org.apache.tapestry5.integration.app1.data.IntegerHolder) PropertyConduit(org.apache.tapestry5.beanmodel.PropertyConduit) InternalPropertyConduit(org.apache.tapestry5.beanmodel.internal.InternalPropertyConduit) PropBindingFactoryTest(org.apache.tapestry5.internal.bindings.PropBindingFactoryTest) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)7 Request (org.apache.tapestry5.http.services.Request)4 PropertyConduit (org.apache.tapestry5.beanmodel.PropertyConduit)3 InternalPropertyConduit (org.apache.tapestry5.beanmodel.internal.InternalPropertyConduit)3 PropBindingFactoryTest (org.apache.tapestry5.internal.bindings.PropBindingFactoryTest)3 Messages (org.apache.tapestry5.commons.Messages)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 Link (org.apache.tapestry5.http.Link)2 IntegerHolder (org.apache.tapestry5.integration.app1.data.IntegerHolder)2 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 Holder (org.apache.tapestry5.internal.util.Holder)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 PersistentFieldChange (org.apache.tapestry5.services.PersistentFieldChange)2 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)2 EnumSelectModel (org.apache.tapestry5.util.EnumSelectModel)2 List (java.util.List)1 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)1 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)1