View Javadoc
1   package org.apache.onami.lifecycle.jsr250;
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 com.google.inject.AbstractModule;
23  import com.google.inject.ConfigurationException;
24  
25  import javax.annotation.PreDestroy;
26  import javax.inject.Inject;
27  import com.google.inject.Key;
28  import com.google.inject.TypeLiteral;
29  import org.apache.onami.lifecycle.core.StageHandler;
30  import org.apache.onami.lifecycle.core.Stager;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import static com.google.inject.Guice.createInjector;
35  import static org.junit.Assert.assertTrue;
36  import static org.junit.Assert.fail;
37  
38  public final class PreDestroyTestCase
39  {
40  
41      @Inject
42      private Stager<PreDestroy> stager;
43  
44      private boolean closeInvoked = false;
45  
46      public void setStager( Stager<PreDestroy> stager )
47      {
48          this.stager = stager;
49      }
50  
51      @PreDestroy
52      public void close()
53      {
54          closeInvoked = true;
55      }
56  
57      @Before
58      public void setUp()
59      {
60          createInjector( new PreDestroyModule() )
61              .getMembersInjector( PreDestroyTestCase.class )
62              .injectMembers( this );
63      }
64  
65      @Test
66      public void closeMethodInvoked()
67      {
68          stager.stage();
69          assertTrue( closeInvoked );
70      }
71  
72      @Test( expected = ConfigurationException.class )
73      public void preDestroyAnnotatedMethodRequiresNoArgs()
74      {
75          createInjector( new PreDestroyModule() ).getInstance( WrongPreDestroyMethod.class );
76      }
77  
78      @Test//( expected = ConfigurationException.class )
79      public void disposeAnnotatedMethodThrowsException()
80      {
81          createInjector( new PreDestroyModule(), new AbstractModule()
82          {
83  
84              @Override
85              protected void configure()
86              {
87                  bind( ThrowingExceptionDisposeMethod.class ).toInstance( new ThrowingExceptionDisposeMethod() );
88              }
89  
90          } ).getInstance( Key.get( new TypeLiteral<Stager<PreDestroy>>() {} ) ).stage( new StageHandler()
91          {
92  
93              @Override
94              public <I> void onSuccess( I injectee )
95              {
96                  fail();
97              }
98  
99              @Override
100             public <I, E extends Throwable> void onError( I injectee, E error )
101             {
102                 assertTrue( injectee instanceof ThrowingExceptionDisposeMethod );
103                 assertTrue( error instanceof IllegalStateException );
104             }
105 
106         } );
107     }
108 
109 }