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.core.integ;
020    
021    
022    import org.apache.commons.io.FileUtils;
023    import org.apache.directory.server.annotations.CreateLdapServer;
024    import org.apache.directory.server.core.DirectoryService;
025    import org.apache.directory.server.core.annotations.CreatePartition;
026    import org.apache.directory.server.core.factory.DSAnnotationProcessor;
027    import org.apache.directory.server.core.factory.DefaultDirectoryServiceFactory;
028    import org.apache.directory.server.core.factory.PartitionFactory;
029    import org.apache.directory.server.factory.ServerAnnotationProcessor;
030    import org.apache.directory.server.ldap.LdapServer;
031    import org.junit.runner.Description;
032    import org.junit.runner.Runner;
033    import org.junit.runner.notification.Failure;
034    import org.junit.runner.notification.RunNotifier;
035    import org.junit.runners.Suite;
036    import org.junit.runners.model.InitializationError;
037    import org.junit.runners.model.RunnerBuilder;
038    import org.slf4j.Logger;
039    import org.slf4j.LoggerFactory;
040    
041    
042    /**
043     * A class to read and store the Suite's annotations. It's called when we start
044     * running a Suite, and will call all the classes contained in the suite.
045     * 
046     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047     * @version $Rev$, $Date$
048     */
049    public class FrameworkSuite extends Suite
050    {
051        /** A logger for this class */
052        private static final Logger LOG = LoggerFactory.getLogger( FrameworkSuite.class );
053    
054        /** The suite DS, if any */
055        private DirectoryService directoryService;
056        
057        /** The LdapServerBuilder for this class, if any */
058        private CreateLdapServer ldapServerBuilder;
059    
060        /** The LdapServer for this class, if any */
061        private LdapServer ldapServer;
062    
063        /**
064         * Creates a new instance of FrameworkSuite.
065         */
066        public FrameworkSuite( Class<?> clazz, RunnerBuilder builder ) throws InitializationError
067        {
068            super( clazz, builder );
069        }
070        
071        
072        /**
073         * Start and initialize the DS
074         */
075        private void startDS( Description description ) throws Exception
076        {
077            // Initialize and start the DS before running any test, if we have a DS annotation
078            directoryService = DSAnnotationProcessor.getDirectoryService( description );
079            
080            // and inject LDIFs if needed
081            if ( directoryService != null )
082            {
083                DSAnnotationProcessor.applyLdifs( description, directoryService );
084            }
085        }
086        
087        
088        /**
089         * Stop and clean the DS
090         */
091        private void stopDS()
092        {
093            if ( directoryService != null )
094            {
095                try
096                {
097                    LOG.debug( "Shuting down DS for {}", directoryService.getInstanceId() );
098                    directoryService.shutdown();
099                    FileUtils.deleteDirectory( directoryService.getWorkingDirectory() );
100                }
101                catch ( Exception e )
102                {
103                    // Do nothing
104                }
105            }
106        }
107        
108        
109        private void addPartitions( Description description )
110        {
111            CreatePartition createPartition = description.getAnnotation( CreatePartition.class );
112            
113            if ( createPartition != null )
114            {
115                
116            }
117        }
118    
119        
120        private void startLdapServer( Description description ) throws Exception
121        {
122            ldapServer = ServerAnnotationProcessor.getLdapServer( description, directoryService, 1024 );
123        }
124        
125        
126        private void stopLdapServer()
127        {
128            if ( ( ldapServer != null ) && ( ldapServer.isStarted() ) )
129            {
130                ldapServer.stop();
131            }
132        }
133        
134        
135        /**
136         * {@inheritDoc}
137         */
138        @Override
139        public void run( final RunNotifier notifier )
140        {
141            try
142            {
143                // print out information which partition factory we use
144                PartitionFactory partitionFactory = DefaultDirectoryServiceFactory.DEFAULT.getPartitionFactory();
145                System.out.println( "Using partition factory " + partitionFactory.getClass().getSimpleName() );
146    
147                // Create and initialize the Suite DS
148                startDS( getDescription() );
149                
150                // Add the partitions to this DS
151                addPartitions( getDescription() );
152                
153                // create and initialize the suite LdapServer
154                startLdapServer( getDescription() );
155                
156                // Run the suite
157                super.run( notifier );
158                
159                // Stop the LdapServer
160                stopLdapServer();
161                
162                // last, stop the DS if we have one
163                stopDS();
164            }
165            catch ( Exception e )
166            {
167                notifier.fireTestFailure(new Failure(getDescription(), e));
168            }
169        }
170    
171        /**
172         * {@inheritDoc}
173         */
174        @Override
175        protected void runChild( Runner runner, RunNotifier notifier )
176        {
177            // Store the suite into the class we will run
178            if( runner instanceof FrameworkRunner )
179            {
180                ( ( FrameworkRunner ) runner ).setSuite( this );
181                
182                // Now, call the class containing the tests
183                super.runChild( runner, notifier );
184            }
185            else
186            {
187                // there is something called org.junit.internal.builders.IgnoredClassRunner
188                super.runChild( runner, notifier );
189            }
190        }
191    
192    
193        /**
194         * @return the DirectoryService instance
195         */
196        public DirectoryService getDirectoryService()
197        {
198            return directoryService;
199        }
200    
201    
202        /**
203         * @param directoryService the directoryService to set
204         */
205        public void setDirectoryService( DirectoryService directoryService )
206        {
207            this.directoryService = directoryService;
208        }
209    
210    
211        /**
212         * @return the suiteLdapServerBuilder
213         */
214        public CreateLdapServer getLdapServerBuilder()
215        {
216            return ldapServerBuilder;
217        }
218    
219    
220        /**
221         * @return the suiteLdapServer
222         */
223        public LdapServer getLdapServer()
224        {
225            return ldapServer;
226        }
227    }