Search in sources :

Example 1 with PersistentFieldChange

use of org.apache.tapestry5.services.PersistentFieldChange 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 PersistentFieldChange

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

the class PersistentFieldBundleImplTest method get_nested_component_value.

@Test
public void get_nested_component_value() {
    String value = "FIELD-VALUE";
    PersistentFieldChange change = new PersistentFieldChangeImpl("foo.bar", "field", value);
    Collection<PersistentFieldChange> changes = Arrays.asList(change);
    PersistentFieldBundle bundle = new PersistentFieldBundleImpl(changes);
    assertTrue(bundle.containsValue("foo.bar", "field"));
    assertSame(bundle.getValue("foo.bar", "field"), value);
    assertFalse(bundle.containsValue("foo.bar", "other"));
}
Also used : PersistentFieldBundle(org.apache.tapestry5.services.PersistentFieldBundle) PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Test(org.testng.annotations.Test)

Example 3 with PersistentFieldChange

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

the class PersistentFieldBundleImplTest method get_root_component_value.

@Test
public void get_root_component_value() {
    String value = "FIELD-VALUE";
    PersistentFieldChange change = new PersistentFieldChangeImpl("", "field", value);
    Collection<PersistentFieldChange> changes = Arrays.asList(change);
    PersistentFieldBundle bundle = new PersistentFieldBundleImpl(changes);
    assertTrue(bundle.containsValue("", "field"));
    assertTrue(bundle.containsValue(null, "field"));
    assertSame(bundle.getValue("", "field"), value);
    assertSame(bundle.getValue(null, "field"), value);
    assertFalse(bundle.containsValue("", "other"));
    assertFalse(bundle.containsValue(null, "other"));
}
Also used : PersistentFieldBundle(org.apache.tapestry5.services.PersistentFieldBundle) PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Test(org.testng.annotations.Test)

Example 4 with PersistentFieldChange

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

the class SessionPersistentFieldStrategyTest method gather_changes_with_active_session.

@Test
public void gather_changes_with_active_session() {
    Session session = mockSession();
    Request request = mockRequest();
    train_getSession(request, false, session);
    train_getAttributeNames(session, "state:foo.Bar:", "state:foo.Bar::root", "state:foo.Bar:nested:down");
    train_getAttribute(session, "state:foo.Bar::root", "ROOT");
    train_getAttribute(session, "state:foo.Bar:nested:down", "DOWN");
    replay();
    SessionPersistentFieldStrategy stategy = new SessionPersistentFieldStrategy(request);
    Collection<PersistentFieldChange> changes = stategy.gatherFieldChanges("foo.Bar");
    assertEquals(changes.size(), 2);
    Iterator<PersistentFieldChange> i = changes.iterator();
    PersistentFieldChange change1 = i.next();
    assertEquals(change1.getComponentId(), "");
    assertEquals(change1.getFieldName(), "root");
    assertEquals(change1.getValue(), "ROOT");
    PersistentFieldChange change2 = i.next();
    assertEquals(change2.getComponentId(), "nested");
    assertEquals(change2.getFieldName(), "down");
    assertEquals(change2.getValue(), "DOWN");
    verify();
}
Also used : Request(org.apache.tapestry5.http.services.Request) PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Session(org.apache.tapestry5.http.services.Session) Test(org.testng.annotations.Test)

Example 5 with PersistentFieldChange

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

the class AbstractSessionPersistentFieldStrategy method gatherFieldChanges.

public final Collection<PersistentFieldChange> gatherFieldChanges(String pageName) {
    Session session = request.getSession(false);
    if (session == null)
        return Collections.emptyList();
    List<PersistentFieldChange> result = newList();
    String fullPrefix = prefix + pageName + ":";
    for (String name : session.getAttributeNames(fullPrefix)) {
        Object persistedValue = session.getAttribute(name);
        Object applicationValue = persistedValue == null ? null : convertPersistedToApplicationValue(persistedValue);
        PersistentFieldChange change = buildChange(name, applicationValue);
        result.add(change);
        didReadChange(session, name);
    }
    return result;
}
Also used : PersistentFieldChange(org.apache.tapestry5.services.PersistentFieldChange) Session(org.apache.tapestry5.http.services.Session)

Aggregations

PersistentFieldChange (org.apache.tapestry5.services.PersistentFieldChange)9 Test (org.testng.annotations.Test)8 Request (org.apache.tapestry5.http.services.Request)5 Session (org.apache.tapestry5.http.services.Session)3 PersistentFieldBundle (org.apache.tapestry5.services.PersistentFieldBundle)3 Link (org.apache.tapestry5.http.Link)2 PersistentFieldStrategy (org.apache.tapestry5.services.PersistentFieldStrategy)2