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.integ;
20  
21  
22  import java.lang.reflect.Method;
23  
24  import static org.apache.directory.server.integ.state.TestServerContext.cleanup;
25  import static org.apache.directory.server.integ.state.TestServerContext.destroy;
26  import static org.apache.directory.server.integ.state.TestServerContext.shutdown;
27  import static org.apache.directory.server.integ.state.TestServerContext.test;
28  
29  import org.apache.directory.server.core.integ.Level;
30  import org.junit.internal.runners.InitializationError;
31  import org.junit.internal.runners.JUnit4ClassRunner;
32  import org.junit.runner.Description;
33  import org.junit.runner.notification.Failure;
34  import org.junit.runner.notification.RunNotifier;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  
39  /**
40   * A test runner for ApacheDS Core integration tests.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev$, $Date$
44   */
45  public class SiRunner extends JUnit4ClassRunner
46  {
47      private static final Logger LOG = LoggerFactory.getLogger( SiRunner.class );
48      private SiSuite suite;
49      private InheritableServerSettings settings;
50  
51  
52      public SiRunner( Class<?> clazz ) throws InitializationError
53      {
54          super( clazz );
55      }
56  
57  
58      protected InheritableServerSettings getSettings()
59      {
60          if ( settings != null )
61          {
62              return settings;
63          }
64  
65          if ( suite == null )
66          {
67              settings = new InheritableServerSettings( getDescription(), null );
68          }
69  
70          return settings;
71      }
72  
73  
74      @Override
75      public void run( final RunNotifier notifier )
76      {
77          super.run( notifier );
78          Level cleanupLevel = getSettings().getCleanupLevel();
79          
80          if ( cleanupLevel == Level.CLASS )
81          {
82              try
83              {
84                  shutdown();
85                  cleanup();
86                  destroy();
87              }
88              catch ( Exception e )
89              {
90                  LOG.error( "Encountered exception while trying to cleanup after test class: "
91                          + this.getDescription().getDisplayName(), e );
92                  notifier.fireTestFailure( new Failure( getDescription(), e ) );
93              }
94          }
95      }
96  
97  
98      @Override
99      protected void invokeTestMethod( Method method, final RunNotifier notifier )
100     {
101         LOG.debug( "About to invoke test method {}", method.getName() );
102         Description description = methodDescription( method );
103         test( getTestClass(), wrapMethod( method ), notifier, new InheritableServerSettings( description, getSettings() ) );
104 
105         Level cleanupLevel = getSettings().getCleanupLevel();
106         
107         if ( cleanupLevel == Level.METHOD )
108         {
109             try
110             {
111                 shutdown();
112                 cleanup();
113                 destroy();
114             }
115             catch ( Exception e )
116             {
117                 LOG.error( "Encountered exception while trying to cleanup after test class: "
118                         + this.getDescription().getDisplayName(), e );
119                 notifier.fireTestFailure( new Failure( getDescription(), e ) );
120             }
121         }
122     }
123 
124 
125     public void setSuite( SiSuite suite )
126     {
127         this.suite = suite;
128         this.settings = new InheritableServerSettings( getDescription(), suite.getSettings() );
129     }
130 
131 
132     public SiSuite getSuite()
133     {
134         return suite;
135     }
136 }