View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.core.integ;
20  
21  
22  import java.lang.reflect.Method;
23  
24  import static org.apache.directory.server.core.integ.state.TestServiceContext.cleanup;
25  import static org.apache.directory.server.core.integ.state.TestServiceContext.destroy;
26  import static org.apache.directory.server.core.integ.state.TestServiceContext.shutdown;
27  import static org.apache.directory.server.core.integ.state.TestServiceContext.test;
28  import org.junit.internal.runners.InitializationError;
29  import org.junit.internal.runners.JUnit4ClassRunner;
30  import org.junit.runner.Description;
31  import org.junit.runner.notification.Failure;
32  import org.junit.runner.notification.RunNotifier;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * A test runner for ApacheDS Core integration tests.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev$, $Date$
42   */
43  public class CiRunner extends JUnit4ClassRunner
44  {
45      private static final Logger LOG = LoggerFactory.getLogger( CiRunner.class );
46      private CiSuite suite;
47      private InheritableSettings settings;
48  
49  
50      public CiRunner( Class<?> clazz ) throws InitializationError
51      {
52          super( clazz );
53      }
54  
55  
56      protected InheritableSettings getSettings()
57      {
58          if ( settings != null )
59          {
60              return settings;
61          }
62  
63          if ( suite == null )
64          {
65              settings = new InheritableSettings( getDescription(), null );
66          }
67  
68          return settings;
69      }
70  
71  
72      @Override
73      public void run( final RunNotifier notifier )
74      {
75          super.run( notifier );
76          Level cleanupLevel = getSettings().getCleanupLevel();
77          
78          if ( cleanupLevel == Level.CLASS )
79          {
80              try
81              {
82                  shutdown();
83                  cleanup();
84                  destroy();
85              }
86              catch ( Exception e )
87              {
88                  LOG.error( "Encountered exception while trying to cleanup after test class: "
89                          + this.getDescription().getDisplayName(), e );
90                  notifier.fireTestFailure( new Failure( getDescription(), e ) );
91              }
92          }
93      }
94  
95  
96      @Override
97      protected void invokeTestMethod( Method method, final RunNotifier notifier )
98      {
99          LOG.debug( "About to invoke test method {}", method.getName() );
100         Description description = methodDescription( method );
101         test( getTestClass(), wrapMethod( method ), notifier, new InheritableSettings( description, getSettings() ) );
102 
103         Level cleanupLevel = getSettings().getCleanupLevel();
104         
105         if ( cleanupLevel == Level.METHOD )
106         {
107             try
108             {
109                 shutdown();
110                 cleanup();
111                 destroy();
112             }
113             catch ( Exception e )
114             {
115                 LOG.error( "Encountered exception while trying to cleanup after test class: "
116                         + this.getDescription().getDisplayName(), e );
117                 notifier.fireTestFailure( new Failure( getDescription(), e ) );
118             }
119         }
120     }
121 
122 
123     public void setSuite( CiSuite suite )
124     {
125         this.suite = suite;
126         this.settings = new InheritableSettings( getDescription(), suite.getSettings() );
127     }
128 
129 
130     public CiSuite getSuite()
131     {
132         return suite;
133     }
134 }