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.Locale;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.tapestry.internal.structure.Page;
021: import org.apache.tapestry.internal.test.InternalBaseTestCase;
022: import org.apache.tapestry.ioc.services.ThreadLocale;
023: import org.apache.tapestry.services.ComponentClassResolver;
024: import org.easymock.EasyMock;
025: import org.testng.annotations.Test;
026:
027: public class PagePoolImplTest extends InternalBaseTestCase {
028: private static final String INPUT_PAGE_NAME = "mypage";
029:
030: private static final String LOGICAL_PAGE_NAME = "MyPage";
031:
032: private static final String PAGE_NAME = "com.foo.pages.MyPage";
033:
034: // This will change once we start supporting application localization.
035:
036: private final Locale _locale = Locale.getDefault();
037:
038: @Test
039: public void checkout_when_page_list_is_null() {
040: PageLoader loader = mockPageLoader();
041: Page page = mockPage();
042: ThreadLocale tl = mockThreadLocale();
043: ComponentClassResolver resolver = mockComponentClassResolver();
044: Log log = mockLog();
045:
046: train_canonicalizePageName(resolver, INPUT_PAGE_NAME,
047: LOGICAL_PAGE_NAME);
048:
049: train_getLocale(tl, _locale);
050:
051: train_loadPage(loader, LOGICAL_PAGE_NAME, _locale, page);
052:
053: replay();
054:
055: PagePool pool = new PagePoolImpl(log, loader, tl, resolver);
056:
057: assertSame(page, pool.checkout(INPUT_PAGE_NAME));
058:
059: verify();
060: }
061:
062: @Test
063: public void checkout_when_page_list_is_empty() {
064: Page page1 = mockPage();
065: Page page2 = mockPage();
066: PageLoader loader = mockPageLoader();
067: Log log = mockLog();
068: ThreadLocale tl = mockThreadLocale();
069: ComponentClassResolver resolver = mockComponentClassResolver();
070:
071: train_canonicalizePageName(resolver, INPUT_PAGE_NAME,
072: LOGICAL_PAGE_NAME);
073:
074: train_getLocale(tl, _locale);
075:
076: train_loadPage(loader, LOGICAL_PAGE_NAME, _locale, page1);
077:
078: replay();
079:
080: PagePool pool = new PagePoolImpl(log, loader, tl, resolver);
081:
082: assertSame(pool.checkout(INPUT_PAGE_NAME), page1);
083:
084: verify();
085:
086: train_detached(page1, false);
087: train_getLogicalName(page1, LOGICAL_PAGE_NAME);
088: train_getLocale(page1, _locale);
089:
090: replay();
091:
092: pool.release(page1);
093:
094: verify();
095:
096: train_canonicalizePageName(resolver, INPUT_PAGE_NAME,
097: LOGICAL_PAGE_NAME);
098: train_getLocale(tl, _locale);
099:
100: train_canonicalizePageName(resolver, INPUT_PAGE_NAME,
101: LOGICAL_PAGE_NAME);
102: train_getLocale(tl, _locale);
103:
104: train_loadPage(loader, LOGICAL_PAGE_NAME, _locale, page2);
105:
106: replay();
107:
108: assertSame(pool.checkout(INPUT_PAGE_NAME), page1);
109: assertSame(pool.checkout(INPUT_PAGE_NAME), page2);
110:
111: verify();
112:
113: }
114:
115: @Test
116: public void dirty_pages_are_not_pooled() {
117: PageLoader loader = mockPageLoader();
118: Page page = mockPage();
119: Log log = mockLog();
120:
121: train_detached(page, true);
122:
123: log.error(EasyMock.contains("is dirty, and will be discarded"));
124:
125: // The fact that we don't ask
126: // the page for its name is our clue that it is not being cached.
127:
128: replay();
129:
130: PagePool pool = new PagePoolImpl(log, loader, null, null);
131:
132: pool.release(page);
133:
134: verify();
135: }
136: }
|