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.ioc.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018:
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.tapestry.ioc.Registry;
024: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
025: import org.apache.tapestry.ioc.services.PipelineBuilder;
026: import org.testng.annotations.AfterClass;
027: import org.testng.annotations.BeforeClass;
028: import org.testng.annotations.Test;
029:
030: /**
031: * Integration tests for the PipelineBuilder service.
032: */
033: public class PipelineBuilderImplTest extends IOCInternalTestCase {
034:
035: private PipelineBuilder _builder;
036:
037: private Registry _registry;
038:
039: @BeforeClass
040: public void setup_builder() {
041: _registry = buildRegistry();
042: _builder = _registry.getService("PipelineBuilder",
043: PipelineBuilder.class);
044: }
045:
046: @AfterClass
047: public void shutdown_builder() {
048: _registry.shutdown();
049:
050: _builder = null;
051: _registry = null;
052: }
053:
054: @Test
055: public void pipeline_with_filters() {
056: Log log = mockLog();
057:
058: replay();
059:
060: StandardFilter subtracter = new StandardFilter() {
061: public int run(int i, StandardService service) {
062: return service.run(i) - 2;
063: }
064: };
065:
066: StandardFilter multiplier = new StandardFilter() {
067: public int run(int i, StandardService service) {
068: return 2 * service.run(i);
069: }
070: };
071:
072: StandardFilter adder = new StandardFilter() {
073: public int run(int i, StandardService service) {
074: return service.run(i + 3);
075: }
076: };
077:
078: StandardService terminator = new StandardService() {
079: public int run(int i) {
080: return i;
081: }
082: };
083:
084: StandardService pipeline = _builder.build(log,
085: StandardService.class, StandardFilter.class, Arrays
086: .asList(subtracter, multiplier, adder),
087: terminator);
088:
089: // Should be order subtracter, multipler, adder
090: assertEquals(pipeline.run(5), 14);
091: assertEquals(pipeline.run(10), 24);
092:
093: verify();
094: }
095:
096: @Test
097: public void pipeline_without_filters_is_terminator() {
098: Log log = mockLog();
099: StandardService terminator = newMock(StandardService.class);
100:
101: replay();
102:
103: List<StandardFilter> filters = newList();
104:
105: StandardService pipeline = _builder.build(log,
106: StandardService.class, StandardFilter.class, filters,
107: terminator);
108:
109: assertSame(pipeline, terminator);
110:
111: verify();
112: }
113:
114: @Test
115: public void pipeline_with_default_terminator() {
116: Log log = mockLog();
117:
118: replay();
119:
120: List<StandardFilter> filters = newList();
121:
122: StandardService pipeline = _builder.build(log,
123: StandardService.class, StandardFilter.class, filters);
124:
125: assertEquals(pipeline.run(99), 0);
126:
127: verify();
128: }
129: }
|