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.structure;
016:
017: import static org.easymock.EasyMock.contains;
018: import static org.easymock.EasyMock.same;
019:
020: import java.util.Locale;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.tapestry.internal.test.InternalBaseTestCase;
024: import org.apache.tapestry.runtime.PageLifecycleListener;
025: import org.testng.annotations.Test;
026:
027: public class PageImplTest extends InternalBaseTestCase {
028: private final Locale _locale = Locale.ENGLISH;
029:
030: private static final String LOGICAL_PAGE_NAME = "MyPage";
031:
032: @Test
033: public void accessor_methods() {
034: ComponentPageElement root = mockComponentPageElement();
035:
036: replay();
037:
038: Page page = new PageImpl(LOGICAL_PAGE_NAME, _locale, null, null);
039:
040: assertNull(page.getRootElement());
041:
042: page.setRootElement(root);
043:
044: assertSame(page.getLocale(), _locale);
045: assertSame(page.getRootElement(), root);
046: assertSame(page.getLogicalName(), LOGICAL_PAGE_NAME);
047:
048: verify();
049: }
050:
051: @Test
052: public void detach_notification() {
053: PageLifecycleListener listener1 = newPageLifecycle();
054: PageLifecycleListener listener2 = newPageLifecycle();
055:
056: listener1.containingPageDidDetach();
057: listener2.containingPageDidDetach();
058:
059: replay();
060:
061: Page page = new PageImpl(null, _locale, null, null);
062:
063: page.addLifecycleListener(listener1);
064: page.addLifecycleListener(listener2);
065:
066: assertFalse(page.detached());
067:
068: verify();
069: }
070:
071: /** Also checks that listeners are invoked, even if the page is dirty. */
072: @Test
073: public void detach_dirty_if_dirty_count_non_zero() {
074: PageLifecycleListener listener = newPageLifecycle();
075:
076: listener.containingPageDidDetach();
077:
078: replay();
079:
080: Page page = new PageImpl(null, _locale, null, null);
081:
082: page.addLifecycleListener(listener);
083:
084: page.incrementDirtyCount();
085:
086: assertTrue(page.detached());
087:
088: verify();
089: }
090:
091: /**
092: * Also checks that all listeners are invoked, even if one of them throws an exception.
093: */
094: @Test
095: public void detach_dirty_if_listener_throws_exception() {
096: ComponentPageElement element = mockComponentPageElement();
097: Log log = mockLog();
098: PageLifecycleListener listener1 = newPageLifecycle();
099: PageLifecycleListener listener2 = newPageLifecycle();
100: RuntimeException t = new RuntimeException(
101: "Listener detach exception.");
102:
103: train_getLog(element, log);
104:
105: listener1.containingPageDidDetach();
106: setThrowable(t);
107:
108: log.error(contains("failed during page detach"), same(t));
109:
110: listener2.containingPageDidDetach();
111:
112: replay();
113:
114: Page page = new PageImpl(null, _locale, null, null);
115: page.setRootElement(element);
116:
117: page.addLifecycleListener(listener1);
118: page.addLifecycleListener(listener2);
119:
120: assertTrue(page.detached());
121:
122: verify();
123:
124: }
125:
126: protected final void train_getLog(ComponentPageElement element,
127: Log log) {
128: expect(element.getLog()).andReturn(log);
129: }
130:
131: @Test
132: public void attach_notification() {
133:
134: PageLifecycleListener listener1 = newPageLifecycle();
135: PageLifecycleListener listener2 = newPageLifecycle();
136:
137: listener1.containingPageDidAttach();
138: listener2.containingPageDidAttach();
139:
140: replay();
141:
142: Page page = new PageImpl(null, _locale, null, null);
143:
144: page.addLifecycleListener(listener1);
145: page.addLifecycleListener(listener2);
146:
147: page.attached();
148:
149: verify();
150: }
151:
152: private PageLifecycleListener newPageLifecycle() {
153: return newMock(PageLifecycleListener.class);
154: }
155:
156: @Test
157: public void load_notification() {
158: PageLifecycleListener listener1 = newPageLifecycle();
159: PageLifecycleListener listener2 = newPageLifecycle();
160:
161: listener1.containingPageDidLoad();
162: listener2.containingPageDidLoad();
163:
164: replay();
165:
166: Page page = new PageImpl(LOGICAL_PAGE_NAME, _locale, null, null);
167:
168: page.addLifecycleListener(listener1);
169: page.addLifecycleListener(listener2);
170:
171: page.loaded();
172:
173: verify();
174: }
175:
176: @Test
177: public void get_by_nested_id_for_blank_value_returns_root_component() {
178: ComponentPageElement root = mockComponentPageElement();
179:
180: replay();
181:
182: Page page = new PageImpl(LOGICAL_PAGE_NAME, _locale, null, null);
183:
184: page.setRootElement(root);
185:
186: assertSame(page.getComponentElementByNestedId(""), root);
187:
188: verify();
189: }
190: }
|