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 java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    
026    import org.apache.directory.server.core.integ.Level;
027    import org.apache.directory.server.core.integ.SetupMode;
028    import org.apache.directory.server.core.integ.annotations.ApplyLdifFiles;
029    import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
030    import org.apache.directory.server.core.integ.annotations.CleanupLevel;
031    import org.apache.directory.server.core.integ.annotations.Factory;
032    import org.apache.directory.server.core.integ.annotations.Mode;
033    import org.junit.runner.Description;
034    
035    
036    /**
037     * Inheritable settings of a test suite, test class, or test method.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev$, $Date$
041     */
042    public class InheritableServerSettings
043    {
044        /** the default setup mode to use if inheritance leads to null value */
045        public static final SetupMode DEFAULT_MODE = SetupMode.ROLLBACK;
046        
047        /** the default factory to use if inheritance leads to a null value */
048        public static final LdapServerFactory DEFAULT_FACTORY = LdapServerFactory.DEFAULT;
049    
050        /** parent settings to inherit from */
051        private final InheritableServerSettings parent;
052        
053        /** JUnit test description containing all annotations queried */
054        private final Description description;
055        
056        /** default level at which a service is cleaned up */
057        private static final Level DEFAULT_CLEANUP_LEVEL = Level.SUITE;
058    
059    
060        /**
061         * Creates a new InheritableServerSettings instance for test suites description.
062         *
063         * @param description JUnit description for the suite
064         */
065        public InheritableServerSettings( Description description )
066        {
067            this.description = description;
068            this.parent = null;
069        }
070    
071    
072        /**
073         * Creates a new InheritableServerSettings instance based on a test object's
074         * description and it's parent's settings.
075         *
076         * @param description JUnit description for the test object
077         * @param parent the parent settings or null if the test entity is a suite
078         */
079        public InheritableServerSettings( Description description, InheritableServerSettings parent )
080        {
081            this.description = description;
082            this.parent = parent;
083    
084            if ( description.isSuite() && ! isSuiteLevel() )
085            {
086                throw new IllegalStateException( String.format( "The parent must be null for %s suite",
087                        description.getDisplayName() ) );
088            }
089        }
090    
091    
092        /**
093         * @return the description of the running test
094         */
095        public Description getDescription()
096        {
097            return description;
098        }
099    
100    
101        /**
102         * @return the settings inherited from the parent
103         */
104        public InheritableServerSettings getParent()
105        {
106            return parent;
107        }
108    
109    
110        /**
111         * @return <code>true</code> if we are at the suite level
112         */
113        public boolean isSuiteLevel()
114        {
115            return parent == null;
116        }
117    
118    
119        /**
120         * @return <code>true</code> if we are at the class level
121         */
122        public boolean isClassLevel()
123        {
124            return ( parent != null ) && ( parent.getParent() == null );
125        }
126    
127    
128        /**
129         * @return <code>true</code> if we are at the method level
130         */
131        public boolean isMethodLevel()
132        {
133            return ( parent != null ) && ( parent.getParent() != null );
134        }
135    
136    
137        /**
138         * @return the test mode. Default to ROLLBACK
139         */
140        public SetupMode getMode()
141        {
142            SetupMode parentMode = DEFAULT_MODE;
143            
144            if ( parent != null )
145            {
146                parentMode = parent.getMode();
147            }
148    
149            // Get the @Mode annotation
150            Mode annotation = description.getAnnotation( Mode.class );
151            
152            if ( annotation == null )
153            {
154                return parentMode;
155            }
156            else
157            {
158                return annotation.value();
159            }
160        }
161    
162    
163        /**
164         * @return the DirectoryService factory 
165         * @throws IllegalAccessException if we can't access the factory
166         * @throws InstantiationException if the DirectoryService can't be instanciated
167         */
168        public LdapServerFactory getFactory() throws IllegalAccessException, InstantiationException
169        {
170            LdapServerFactory parentFactory = DEFAULT_FACTORY;
171            
172            if ( parent != null )
173            {
174                parentFactory = parent.getFactory();
175            }
176    
177            Factory annotation = description.getAnnotation( Factory.class );
178            
179            if ( annotation == null )
180            {
181                return parentFactory;
182            }
183            else
184            {
185                return ( LdapServerFactory ) annotation.value().newInstance();
186            }
187        }
188    
189    
190        /**
191         * Get a list of entries from a LDIF declared as an annotation
192         *
193         * @param ldifs the list of LDIFs we want to feed  
194         * @return a list of entries described using a LDIF format
195         */
196        public List<String> getLdifs( List<String> ldifs )
197        {
198            if ( ldifs == null )
199            {
200                ldifs = new ArrayList<String>();
201            }
202    
203            if ( parent != null )
204            {
205                parent.getLdifs( ldifs );
206            }
207    
208            ApplyLdifs annotation = description.getAnnotation( ApplyLdifs.class );
209            
210            if ( ( annotation != null ) && ( annotation.value() != null ) )
211            {
212                ldifs.addAll( Arrays.asList( annotation.value() ) );
213            }
214    
215            return ldifs;
216        }
217    
218    
219        /**
220         * Get a list of files containing entries described using the LDIF format.
221         *
222         * @param ldifFiles the list to feed
223         * @return a list of files containing some LDIF data
224         */
225        public List<String> getLdifFiles( List<String> ldifFiles )
226        {
227            if ( ldifFiles == null )
228            {
229                ldifFiles = new ArrayList<String>();
230            }
231    
232            if ( parent != null )
233            {
234                parent.getLdifFiles( ldifFiles );
235            }
236    
237            ApplyLdifFiles annotation = description.getAnnotation( ApplyLdifFiles.class );
238            
239            if ( ( annotation != null ) && ( annotation.value() != null ) )
240            {
241                ldifFiles.addAll( Arrays.asList( annotation.value() ) );
242            }
243    
244            return ldifFiles;
245        }
246    
247    
248        /**
249         * @return teh cleanup level. Defualt to SUITE
250         */
251        public Level getCleanupLevel()
252        {
253            Level parentCleanupLevel = DEFAULT_CLEANUP_LEVEL;
254            
255            if ( parent != null )
256            {
257                parentCleanupLevel = parent.getCleanupLevel();
258            }
259    
260            CleanupLevel annotation = description.getAnnotation( CleanupLevel.class );
261            
262            if ( annotation == null )
263            {
264                return parentCleanupLevel;
265            }
266            else
267            {
268                return annotation.value();
269            }
270        }
271    }