001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.util.Collection;
018: import java.util.Iterator;
019:
020: import org.apache.tapestry.internal.test.InternalBaseTestCase;
021: import org.apache.tapestry.services.PersistentFieldChange;
022: import org.apache.tapestry.services.Request;
023: import org.apache.tapestry.services.Session;
024: import org.testng.annotations.Test;
025:
026: public class SessionPersistentFieldStrategyTest extends
027: InternalBaseTestCase {
028: @Test
029: public void post_change_to_root_component() {
030: Session session = mockSession();
031: Request request = mockRequest();
032: Object value = new Object();
033:
034: train_getSession(request, true, session);
035:
036: session.setAttribute("state:foo.Bar::field", value);
037:
038: replay();
039:
040: SessionPersistentFieldStrategy strategy = new SessionPersistentFieldStrategy(
041: request);
042:
043: strategy.postChange("foo.Bar", null, "field", value);
044:
045: verify();
046: }
047:
048: @Test
049: public void post_change_to_nested_component() {
050: Session session = mockSession();
051: Request request = mockRequest();
052: Object value = new Object();
053:
054: train_getSession(request, true, session);
055:
056: session.setAttribute("state:foo.Bar:fee.fum:field", value);
057:
058: replay();
059:
060: SessionPersistentFieldStrategy strategy = new SessionPersistentFieldStrategy(
061: request);
062:
063: strategy.postChange("foo.Bar", "fee.fum", "field", value);
064:
065: verify();
066: }
067:
068: @Test
069: public void gather_changes_with_no_session() {
070: Request request = mockRequest();
071:
072: train_getSession(request, false, null);
073:
074: replay();
075:
076: SessionPersistentFieldStrategy strategy = new SessionPersistentFieldStrategy(
077: request);
078:
079: Collection<PersistentFieldChange> changes = strategy
080: .gatherFieldChanges("foo.Bar");
081:
082: assertTrue(changes.isEmpty());
083:
084: verify();
085: }
086:
087: @Test
088: public void gather_changes_with_active_session() {
089: Session session = mockSession();
090: Request request = mockRequest();
091:
092: train_getSession(request, false, session);
093: train_getAttributeNames(session, "state:foo.Bar:",
094: "state:foo.Bar::root", "state:foo.Bar:nested:down");
095:
096: train_getAttribute(session, "state:foo.Bar::root", "ROOT");
097: train_getAttribute(session, "state:foo.Bar:nested:down", "DOWN");
098:
099: replay();
100:
101: SessionPersistentFieldStrategy stategy = new SessionPersistentFieldStrategy(
102: request);
103:
104: Collection<PersistentFieldChange> changes = stategy
105: .gatherFieldChanges("foo.Bar");
106:
107: assertEquals(changes.size(), 2);
108:
109: Iterator<PersistentFieldChange> i = changes.iterator();
110:
111: PersistentFieldChange change1 = i.next();
112:
113: assertEquals(change1.getComponentId(), "");
114: assertEquals(change1.getFieldName(), "root");
115: assertEquals(change1.getValue(), "ROOT");
116:
117: PersistentFieldChange change2 = i.next();
118:
119: assertEquals(change2.getComponentId(), "nested");
120: assertEquals(change2.getFieldName(), "down");
121: assertEquals(change2.getValue(), "DOWN");
122:
123: verify();
124: }
125: }
|