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.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.apache.directory.server.core.integ.Level;
27  import org.apache.directory.server.core.integ.SetupMode;
28  import org.apache.directory.server.core.integ.annotations.ApplyLdifFiles;
29  import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
30  import org.apache.directory.server.core.integ.annotations.CleanupLevel;
31  import org.apache.directory.server.core.integ.annotations.Factory;
32  import org.apache.directory.server.core.integ.annotations.Mode;
33  import org.junit.runner.Description;
34  
35  
36  /**
37   * Inheritable settings of a test suite, test class, or test method.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  public class InheritableServerSettings
43  {
44      /** the default setup mode to use if inheritance leads to null value */
45      public static final SetupMode DEFAULT_MODE = SetupMode.ROLLBACK;
46      
47      /** the default factory to use if inheritance leads to a null value */
48      public static final LdapServerFactory DEFAULT_FACTORY = LdapServerFactory.DEFAULT;
49  
50      /** parent settings to inherit from */
51      private final InheritableServerSettings parent;
52      
53      /** JUnit test description containing all annotations queried */
54      private final Description description;
55      
56      /** default level at which a service is cleaned up */
57      private static final Level DEFAULT_CLEANUP_LEVEL = Level.SUITE;
58  
59  
60      /**
61       * Creates a new InheritableServerSettings instance for test suites description.
62       *
63       * @param description JUnit description for the suite
64       */
65      public InheritableServerSettings( Description description )
66      {
67          this.description = description;
68          this.parent = null;
69      }
70  
71  
72      /**
73       * Creates a new InheritableServerSettings instance based on a test object's
74       * description and it's parent's settings.
75       *
76       * @param description JUnit description for the test object
77       * @param parent the parent settings or null if the test entity is a suite
78       */
79      public InheritableServerSettings( Description description, InheritableServerSettings parent )
80      {
81          this.description = description;
82          this.parent = parent;
83  
84          if ( description.isSuite() && ! isSuiteLevel() )
85          {
86              throw new IllegalStateException( String.format( "The parent must be null for %s suite",
87                      description.getDisplayName() ) );
88          }
89      }
90  
91  
92      /**
93       * @return the description of the running test
94       */
95      public Description getDescription()
96      {
97          return description;
98      }
99  
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 }