View Javadoc
1   package org.apache.onami.lifecycle.core;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static com.google.inject.matcher.Matchers.any;
23  import static java.util.Arrays.asList;
24  
25  import com.google.inject.AbstractModule;
26  import com.google.inject.Guice;
27  import javax.inject.Inject;
28  import com.google.inject.Injector;
29  import com.google.inject.Module;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import java.lang.annotation.Annotation;
34  import java.util.List;
35  
36  public class MultiLifeCycleTestCase
37  {
38      @Test
39      public void testOrdering()
40      {
41          Module lifeCycleModule = new TestLifeCycleModule(
42              asList( TestAnnotationA.class, TestAnnotationB.class, TestAnnotationC.class ) );
43          MultiLifeCycleObject obj = Guice.createInjector( lifeCycleModule ).getInstance( MultiLifeCycleObject.class );
44          Assert.assertEquals( "aaabbbc", obj.toString() );
45      }
46  
47      public static class Foo
48      {
49          @Inject
50          public Foo( Stager<TestAnnotationA> stager )
51          {
52              System.out.println( stager.getStage() );
53          }
54      }
55  
56      @Test
57      public void testStaging()
58      {
59          Module moduleA =
60              new TestLifeCycleStageModule( new DefaultStager<TestAnnotationA>( TestAnnotationA.class ) );
61          Module moduleB =
62              new TestLifeCycleStageModule( new DefaultStager<TestAnnotationB>( TestAnnotationB.class ) );
63          Module moduleC =
64              new TestLifeCycleStageModule( new DefaultStager<TestAnnotationC>( TestAnnotationC.class ) );
65  
66          Injector injector = Guice.createInjector( moduleA, moduleB, moduleC );
67          MultiLifeCycleObject obj = injector.getInstance( MultiLifeCycleObject.class );
68  
69          Assert.assertEquals( obj.toString(), "" );
70  
71          injector.getInstance( LifeCycleStageModule.key( TestAnnotationA.class ) ).stage();
72          Assert.assertEquals( "aaa", obj.toString() );
73          injector.getInstance( LifeCycleStageModule.key( TestAnnotationB.class ) ).stage();
74          Assert.assertEquals( "aaabbb", obj.toString() );
75          injector.getInstance( LifeCycleStageModule.key( TestAnnotationC.class ) ).stage();
76          Assert.assertEquals( "aaabbbc", obj.toString() );
77  
78          injector.getInstance( Foo.class );
79      }
80  
81      @Test
82      public void testStagingOrdering()
83      {
84          Module moduleA =
85              new TestLifeCycleStageModule( new DefaultStager<TestAnnotationA>( TestAnnotationA.class, DefaultStager.Order.FIRST_IN_FIRST_OUT ) );
86          Module moduleB =
87              new TestLifeCycleStageModule( new DefaultStager<TestAnnotationB>( TestAnnotationB.class, DefaultStager.Order.FIRST_IN_LAST_OUT ) );
88  
89          final StringBuilder str = new StringBuilder();
90          Module m = new AbstractModule()
91          {
92              @Override
93              protected void configure()
94              {
95                  binder().bind( StringBuilder.class ).toInstance( str );
96              }
97          };
98  
99          Injector injector = Guice.createInjector( moduleA, moduleB, m );
100         injector.getInstance( StageObject1.class );
101         injector.getInstance( StageObject2.class );
102 
103         injector.getInstance( LifeCycleStageModule.key( TestAnnotationA.class ) ).stage();
104         Assert.assertEquals( "1a2a", str.toString() );
105         str.setLength( 0 );
106 
107         injector.getInstance( LifeCycleStageModule.key( TestAnnotationB.class ) ).stage();
108         Assert.assertEquals( "2b1b", str.toString() );
109     }
110 
111     private static class TestLifeCycleModule extends LifeCycleModule
112     {
113 
114         private final List<? extends Class<? extends Annotation>> annotations;
115 
116         public TestLifeCycleModule( List<? extends Class<? extends Annotation>> annotations )
117         {
118             this.annotations = annotations;
119         }
120 
121         @Override
122         protected void configure()
123         {
124             bindLifeCycle( annotations, any() );
125         }
126     }
127 
128     private static class TestLifeCycleStageModule extends LifeCycleStageModule
129     {
130 
131         private final Stager<?> stager;
132 
133         public TestLifeCycleStageModule( Stager<?> stager )
134         {
135             this.stager = stager;
136         }
137 
138         @Override
139         protected void configureBindings()
140         {
141             bindStager( stager );
142         }
143     }
144 }