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 *****************************************************************************/
008package org.picocontainer.lifecycle;
009
010import org.junit.Before;
011import org.junit.Test;
012import org.picocontainer.DefaultPicoContainer;
013import org.picocontainer.LifecycleStrategy;
014import org.picocontainer.MutablePicoContainer;
015import org.picocontainer.behaviors.Caching;
016import org.picocontainer.containers.EmptyPicoContainer;
017import org.picocontainer.monitors.NullComponentMonitor;
018
019import javax.annotation.PostConstruct;
020import javax.annotation.PreDestroy;
021
022import static org.junit.Assert.assertEquals;
023
024/**
025 *
026 * @author Mauro Talevi
027 */
028public class JavaEE5LifecycleStrategyTestCase {
029
030    MutablePicoContainer pico;
031
032    public static class ProPostAnnotationJava5Startable {
033
034        protected final 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    public static class ProPostAnnotationJava5Startable2 extends ProPostAnnotationJava5Startable {
053
054        public ProPostAnnotationJava5Startable2(StringBuilder sb) {
055            super(sb);
056        }
057
058        @PostConstruct
059        public void subPot() {
060            sb.append("subPost()");
061        }
062
063        @PreDestroy
064        public void subPre() {
065            sb.append("subPre()");
066        }
067
068    }
069
070    private LifecycleStrategy strategy;
071
072    @Before
073    public void setUp(){
074        strategy = new JavaEE5LifecycleStrategy(new NullComponentMonitor());
075        pico = new DefaultPicoContainer(new Caching(), strategy, new EmptyPicoContainer());
076        pico.addComponent(StringBuilder.class);
077        pico.addComponent(ProPostAnnotationJava5Startable.class);
078    }
079
080    @Test public void testStartable(){
081        pico.start();
082        assertEquals("post()", pico.getComponent(StringBuilder.class).toString());
083    }
084
085    @Test public void testStopHasNoMeaning(){
086        pico.start();
087        pico.stop();
088        assertEquals("post()", pico.getComponent(StringBuilder.class).toString());
089    }
090
091    @Test public void testDispose(){
092        pico.start();
093        pico.dispose();
094        assertEquals("post()pre()", pico.getComponent(StringBuilder.class).toString());
095    }
096
097    @Test public void testDisposeOfSubClass(){
098        pico.removeComponent(ProPostAnnotationJava5Startable.class);
099        pico.addComponent(ProPostAnnotationJava5Startable2.class);
100        pico.start();
101        pico.dispose();
102        assertEquals("post()subPost()subPre()pre()", pico.getComponent(StringBuilder.class).toString());
103    }
104
105    @Test public void testSerializable(){
106    }
107
108     public static class ProPostAnnotationJava5Startable3 extends ProPostAnnotationJava5Startable {
109
110       public ProPostAnnotationJava5Startable3(StringBuilder sb) {
111           super(sb);
112       }
113
114       @PostConstruct
115       @Override
116       public void post() {
117           sb.append("subPost3()");
118       }
119
120       @PreDestroy
121       public void subPre() {
122           sb.append("subPre3()");
123       }
124   }
125
126   @Test
127   public void testLifecycleOfSubclassWhichOverrides(){
128       pico.removeComponent(ProPostAnnotationJava5Startable.class);
129       pico.addComponent(ProPostAnnotationJava5Startable3.class);
130       pico.start();
131       pico.dispose();
132       assertEquals("subPost3()subPre3()pre()", pico.getComponent(StringBuilder.class).toString());
133   }
134
135}