    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.txt file.                                                     *
    007     *****************************************************************************/
    008    package org.picocontainer.lifecycle;
    009
    010    import static org.junit.Assert.assertEquals;
    011
    012    import javax.annotation.PostConstruct;
    013    import javax.annotation.PreDestroy;
    014
    015    import org.junit.Before;
    016    import org.junit.Test;
    017    import org.picocontainer.DefaultPicoContainer;
    018    import org.picocontainer.LifecycleStrategy;
    019    import org.picocontainer.MutablePicoContainer;
    020    import org.picocontainer.behaviors.Caching;
    021    import org.picocontainer.containers.EmptyPicoContainer;
    022    import org.picocontainer.monitors.NullComponentMonitor;
    023
    024    /**
    025     *
    026     * @author Mauro Talevi
    027     */
    028    public class JavaEE5LifecycleStrategyTestCase {
    029
    030        MutablePicoContainer pico;
    031
    032        public static class ProPostAnnotationJava5Startable {
    033
    034            StringBuilder sb;
    035
    036            public ProPostAnnotationJava5Startable(StringBuilder sb) {
    037                this.sb = sb;
    038            }
    039
    040            @PostConstruct
    041            public void post() {
    042                sb.append("post()");
    043            }
    044
    045            @PreDestroy
    046            public void pre() {
    047                sb.append("pre()");
    048            }
    049
    050        }
    051
    052
    053        private LifecycleStrategy strategy;
    054
    055        @Before
    056        public void setUp(){
    057            strategy = new JavaEE5LifecycleStrategy(new NullComponentMonitor());
    058            pico = new DefaultPicoContainer(new Caching(), strategy, new EmptyPicoContainer());
    059            pico.addComponent(StringBuilder.class);
    060            pico.addComponent(ProPostAnnotationJava5Startable.class);
    061        }
    062
    063        @Test public void testStartable(){
    064            pico.start();
    065            assertEquals("post()", pico.getComponent(StringBuilder.class).toString());
    066        }
    067
    068        @Test public void testStopHasNoMeaning(){
    069            pico.start();
    070            pico.stop();
    071            assertEquals("post()", pico.getComponent(StringBuilder.class).toString());
    072        }
    073
    074        @Test public void testDispose(){
    075            pico.start();
    076            pico.dispose();
    077            assertEquals("post()pre()", pico.getComponent(StringBuilder.class).toString());
    078        }
    079
    080        @Test public void testSerializable(){
    081        }
    082
    083    }