001/*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved.            *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD      *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file.                                                    *
007 *                                                                           *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
009 *****************************************************************************/
010package org.picocontainer.testmodel;
011
012import static org.junit.Assert.assertNotNull;
013
014import org.picocontainer.Disposable;
015import org.picocontainer.PicoContainer;
016import org.picocontainer.Startable;
017
018
019public abstract class RecordingLifecycle implements Startable, Disposable {
020    private final StringBuffer recording;
021
022    protected RecordingLifecycle(StringBuffer recording) {
023        this.recording = recording;
024    }
025
026    public void start() {
027        recording.append("<").append(code());
028    }
029
030    public void stop() {
031        recording.append(code()).append(">");
032    }
033
034    public void dispose() {
035        recording.append("!").append(code());
036    }
037    
038    public String recording() {
039        return recording.toString();
040    }
041
042    private String code() {
043        String name = getClass().getName();
044        int idx = Math.max(name.lastIndexOf('$'), name.lastIndexOf('.'));
045        return name.substring(idx + 1);
046    }
047    
048    public interface Recorder extends  Startable, Disposable {
049        String recording();
050    }
051
052    public static class One extends RecordingLifecycle implements Recorder {
053        public One(StringBuffer sb) {
054            super(sb);
055        }
056    }
057
058    public static class Two extends RecordingLifecycle {
059        public Two(StringBuffer sb, One one) {
060            super(sb);
061            assertNotNull(one);
062        }
063    }
064
065    public static class Three extends RecordingLifecycle {
066        public Three(StringBuffer sb, One one, Two two) {
067            super(sb);
068            assertNotNull(one);
069            assertNotNull(two);
070        }
071    }
072
073    public static class Four extends RecordingLifecycle {
074        public Four(StringBuffer sb, Two two, Three three, One one) {
075            super(sb);
076            assertNotNull(one);
077            assertNotNull(two);
078            assertNotNull(three);
079        }
080    }
081
082    public static class FiveTriesToBeMalicious extends RecordingLifecycle {
083        public FiveTriesToBeMalicious(StringBuffer sb, PicoContainer pc) {
084            super(sb);
085            assertNotNull(pc);
086            sb.append("Whao! Should not get instantiated!!");
087        }
088    }
089    
090}