001: // Copyright 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 static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018:
019: import java.util.Collection;
020:
021: import org.apache.tapestry.Block;
022: import org.apache.tapestry.internal.structure.ComponentPageElement;
023: import org.apache.tapestry.internal.structure.Page;
024: import org.apache.tapestry.internal.test.InternalBaseTestCase;
025: import org.apache.tapestry.services.BeanBlockContribution;
026: import org.apache.tapestry.services.BeanBlockSource;
027: import org.testng.annotations.Test;
028:
029: public class BeanBlockSourceImplTest extends InternalBaseTestCase {
030: @Test
031: public void found_display_block() {
032: Block block = mockBlock();
033: RequestPageCache cache = mockRequestPageCache();
034: Page page = mockPage();
035: BeanBlockContribution contribution = new BeanBlockContribution(
036: "mydata", "MyPage", "mydisplay", false);
037: Collection<BeanBlockContribution> configuration = newList(contribution);
038:
039: train_get(cache, "MyPage", page);
040: train_getBlock(page, "mydisplay", block);
041:
042: replay();
043:
044: BeanBlockSource source = new BeanBlockSourceImpl(cache,
045: configuration);
046:
047: // Check case insensitivity while we are at it.
048: assertTrue(source.hasDisplayBlock("MyData"));
049: Block actual = source.getDisplayBlock("MyData");
050:
051: assertSame(actual, block);
052:
053: verify();
054: }
055:
056: @Test
057: public void display_block_not_found() {
058: RequestPageCache cache = mockRequestPageCache();
059: Collection<BeanBlockContribution> configuration = newList();
060:
061: replay();
062:
063: BeanBlockSource source = new BeanBlockSourceImpl(cache,
064: configuration);
065:
066: try {
067: assertFalse(source.hasDisplayBlock("MyData"));
068: source.getDisplayBlock("MyData");
069: unreachable();
070: } catch (RuntimeException ex) {
071: assertEquals(
072: ex.getMessage(),
073: "There is no defined way to display data of type \'MyData\'. Make a contribution to the BeanBlockSource service for this type.");
074: }
075:
076: verify();
077: }
078:
079: @Test
080: public void edit_block_not_found() {
081: RequestPageCache cache = mockRequestPageCache();
082: Collection<BeanBlockContribution> configuration = newList();
083:
084: replay();
085:
086: BeanBlockSource source = new BeanBlockSourceImpl(cache,
087: configuration);
088:
089: try {
090: source.getEditBlock("MyData");
091: unreachable();
092: } catch (RuntimeException ex) {
093: assertEquals(
094: ex.getMessage(),
095: "There is no defined way to edit data of type \'MyData\'. Make a contribution to the BeanBlockSource service for this type.");
096: }
097:
098: verify();
099: }
100:
101: @Test
102: public void found_edit_block() {
103: Block block = mockBlock();
104: RequestPageCache cache = mockRequestPageCache();
105: Page page = mockPage();
106: BeanBlockContribution contribution = new BeanBlockContribution(
107: "mydata", "MyPage", "mydisplay", true);
108: Collection<BeanBlockContribution> configuration = newList(contribution);
109:
110: train_get(cache, "MyPage", page);
111: train_getBlock(page, "mydisplay", block);
112:
113: replay();
114:
115: BeanBlockSource source = new BeanBlockSourceImpl(cache,
116: configuration);
117:
118: // Check case insensitivity while we are at it.
119: Block actual = source.getEditBlock("MyData");
120:
121: assertSame(actual, block);
122:
123: verify();
124: }
125:
126: protected final void train_getBlock(Page page, String blockId,
127: Block block) {
128: ComponentPageElement element = mockComponentPageElement();
129: train_getRootElement(page, element);
130:
131: expect(element.getBlock(blockId)).andReturn(block);
132: }
133: }
|