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;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.tapestry.ioc.ObjectCreator;
019: import org.apache.tapestry.ioc.def.ServiceDef;
020: import org.testng.annotations.Test;
021:
022: public class RecursiveServiceCreationCheckWrapperTest extends
023: IOCInternalTestCase {
024:
025: private static final String SOURCE_DESCRIPTION = "{SOURCE DESCRIPTION}";
026:
027: @Test
028: public void ensure_only_called_once() throws Exception {
029: Log log = mockLog();
030: ObjectCreatorSource source = mockObjectCreatorSource();
031: ObjectCreator delegate = mockObjectCreator();
032: Object service = new Object();
033:
034: ServiceDef def = new ServiceDefImpl(Runnable.class, "Bar",
035: "singleton", false, source);
036:
037: train_createObject(delegate, service);
038:
039: train_getDescription(source, SOURCE_DESCRIPTION);
040:
041: replay();
042:
043: ObjectCreator wrapper = new RecursiveServiceCreationCheckWrapper(
044: def, delegate, log);
045:
046: assertSame(wrapper.createObject(), service);
047:
048: try {
049: wrapper.createObject();
050: unreachable();
051: } catch (IllegalStateException ex) {
052: assertMessageContains(
053: ex,
054: "Construction of service 'Bar' has failed due to recursion: the service depends on itself in some way.",
055: SOURCE_DESCRIPTION,
056: "for references to another service that is itself dependent on service 'Bar'.");
057: }
058:
059: verify();
060: }
061:
062: @Test
063: public void reporting_of_construction_failure() throws Exception {
064: RuntimeException failure = new RuntimeException("Just cranky.");
065: Log log = mockLog();
066: ObjectCreatorSource source = mockObjectCreatorSource();
067: ObjectCreator delegate = mockObjectCreator();
068: Object service = new Object();
069:
070: ServiceDef def = new ServiceDefImpl(Runnable.class, "Bar",
071: "singleton", false, source);
072:
073: expect(delegate.createObject()).andThrow(failure);
074:
075: log.error("Construction of service Bar failed: Just cranky.",
076: failure);
077:
078: replay();
079:
080: ObjectCreator wrapper = new RecursiveServiceCreationCheckWrapper(
081: def, delegate, log);
082:
083: try {
084: wrapper.createObject();
085: unreachable();
086: } catch (RuntimeException ex) {
087: assertSame(ex, failure);
088: }
089:
090: verify();
091:
092: // Now test that the locked flag is not set and that the object may still be created.
093:
094: train_createObject(delegate, service);
095:
096: replay();
097:
098: assertSame(service, wrapper.createObject());
099:
100: verify();
101:
102: }
103: }
|