001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.directory.server.integ;
020    
021    
022    import static org.apache.directory.server.integ.state.TestServerContext.cleanup;
023    import static org.apache.directory.server.integ.state.TestServerContext.destroy;
024    import static org.apache.directory.server.integ.state.TestServerContext.shutdown;
025    import static org.apache.directory.server.integ.state.TestServerContext.test;
026    
027    import org.apache.directory.server.core.integ.Level;
028    import org.junit.Ignore;
029    import org.junit.runner.Description;
030    import org.junit.runner.notification.Failure;
031    import org.junit.runner.notification.RunNotifier;
032    import org.junit.runners.BlockJUnit4ClassRunner;
033    import org.junit.runners.model.FrameworkMethod;
034    import org.junit.runners.model.InitializationError;
035    import org.junit.runners.model.Statement;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    
040    /**
041     * A test runner for ApacheDS Core integration tests.
042     *
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev$, $Date$
045     */
046    public class SiRunner extends BlockJUnit4ClassRunner
047    {
048        private static final Logger LOG = LoggerFactory.getLogger( SiRunner.class );
049        private SiSuite suite;
050        private InheritableServerSettings settings;
051    
052    
053        public SiRunner( Class<?> clazz ) throws InitializationError
054        {
055            super( clazz );
056        }
057    
058    
059        protected InheritableServerSettings getSettings()
060        {
061            if ( settings != null )
062            {
063                return settings;
064            }
065    
066            if ( suite == null )
067            {
068                settings = new InheritableServerSettings( getDescription(), null );
069            }
070    
071            return settings;
072        }
073    
074    
075        @Override
076        public void run( final RunNotifier notifier )
077        {
078            super.run( notifier );
079            Level cleanupLevel = getSettings().getCleanupLevel();
080    
081            if ( cleanupLevel == Level.CLASS )
082            {
083                try
084                {
085                    shutdown();
086                    cleanup();
087                    destroy();
088                }
089                catch ( Exception e )
090                {
091                    LOG.error( "Encountered exception while trying to cleanup after test class: "
092                        + this.getDescription().getDisplayName(), e );
093                    notifier.fireTestFailure( new Failure( getDescription(), e ) );
094                }
095            }
096        }
097    
098    
099        @Override
100        protected void runChild( FrameworkMethod method, RunNotifier notifier )
101        {
102            LOG.debug( "About to invoke test method {}", method.getName() );
103    
104            Description description = describeChild( method );
105            if ( method.getAnnotation( Ignore.class ) != null )
106            {
107                notifier.fireTestIgnored( description );
108                return;
109            }
110    
111            Statement statement = methodBlock( method );
112            test( getTestClass(), statement, notifier, new InheritableServerSettings( description, getSettings() ) );
113    
114            Level cleanupLevel = getSettings().getCleanupLevel();
115    
116            if ( cleanupLevel == Level.METHOD )
117            {
118                try
119                {
120                    shutdown();
121                    cleanup();
122                    destroy();
123                }
124                catch ( Exception e )
125                {
126                    LOG.error( "Encountered exception while trying to cleanup after test class: "
127                        + this.getDescription().getDisplayName(), e );
128                    notifier.fireTestFailure( new Failure( getDescription(), e ) );
129                }
130            }
131        }
132    
133    
134        public void setSuite( SiSuite suite )
135        {
136            this.suite = suite;
137            this.settings = new InheritableServerSettings( getDescription(), suite.getSettings() );
138        }
139    
140    
141        public SiSuite getSuite()
142        {
143            return suite;
144        }
145    }