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.core.integ.state;
20  
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.server.core.DirectoryService;
30  import org.apache.directory.server.core.entry.DefaultServerEntry;
31  import org.apache.directory.server.core.integ.InheritableSettings;
32  import org.apache.directory.shared.ldap.ldif.LdifEntry;
33  import org.apache.directory.shared.ldap.ldif.LdifReader;
34  import org.junit.internal.runners.TestClass;
35  import org.junit.internal.runners.TestMethod;
36  import org.junit.runner.notification.RunNotifier;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * The abstract state of a test service, containing the default state 
43   * transitions
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   * @version $Rev$, $Date$
47   */
48  public abstract class AbstractState implements TestServiceState
49  {
50      /** The class logger */
51      private static final Logger LOG = LoggerFactory.getLogger( AbstractState.class );
52  
53      /** The context for this test */
54      protected final TestServiceContext context;
55  
56      /** Error message when we can't destroy the service */
57      private static final String DESTROY_ERR = "Cannot destroy when service is in NonExistant state";
58      private static final String CLEANUP_ERROR = "Cannot cleanup when service is in NonExistant state";
59      private static final String STARTUP_ERR = "Cannot startup when service is in NonExistant state";
60      private static final String SHUTDOWN_ERR = "Cannot shutdown service in NonExistant state.";
61      private static final String REVERT_ERROR = "Cannot revert when service is in NonExistant state";
62  
63      /**
64       * 
65       * Creates a new instance of AbstractState.
66       *
67       * @param context The associated context
68       */
69      protected AbstractState( TestServiceContext context )
70      {
71          this.context = context;
72      }
73  
74  
75      /**
76       * Action where an attempt is made to create the service.  Service
77       * creation in this system is the combined instantiation and
78       * configuration which takes place when the factory is used to get
79       * a new instance of the service.
80       *
81       * @param settings The inherited settings
82       * @throws NamingException if we can't create the service
83       */
84      public void create( InheritableSettings settings ) throws NamingException
85      {
86      }
87  
88  
89      /**
90       * Action where an attempt is made to destroy the service. This
91       * entails nulling out reference to it and triggering garbage
92       * collection.
93       */
94      public void destroy()
95      {
96          LOG.error( DESTROY_ERR );
97          throw new IllegalStateException( DESTROY_ERR );
98      }
99  
100 
101     /**
102      * Action where an attempt is made to erase the contents of the
103      * working directory used by the service for various files including
104      * partition database files.
105      *
106      * @throws IOException on errors while deleting the working directory
107      */
108     public void cleanup() throws  IOException
109     {
110         LOG.error( CLEANUP_ERROR );
111         throw new IllegalStateException( CLEANUP_ERROR );
112     }
113 
114 
115     /**
116      * Action where an attempt is made to start up the service.
117      *
118      * @throws Exception on failures to start the core directory service
119      */
120     public void startup() throws Exception
121     {
122         LOG.error( STARTUP_ERR );
123         throw new IllegalStateException( STARTUP_ERR );
124     }
125 
126 
127     /**
128      * Action where an attempt is made to shutdown the service.
129      *
130      * @throws Exception on failures to stop the core directory service
131      */
132     public void shutdown() throws Exception
133     {
134         LOG.error( SHUTDOWN_ERR );
135         throw new IllegalStateException( SHUTDOWN_ERR );
136     }
137 
138 
139     /**
140      * Action where an attempt is made to run a test against the service.
141      *
142      * All annotations should have already been processed for
143      * InheritableSettings yet they and others can be processed since we have
144      * access to the method annotations below
145      *
146      * @param testClass the class whose test method is to be run
147      * @param testMethod the test method which is to be run
148      * @param notifier a notifier to report failures to
149      * @param settings the inherited settings and annotations associated with
150      * the test method
151      */
152     public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableSettings settings )
153     {
154     }
155 
156 
157     /**
158      * Action where an attempt is made to revert the service to it's
159      * initial start up state by using a previous snapshot.
160      *
161      * @throws Exception on failures to revert the state of the core
162      * directory service
163      */
164     public void revert() throws Exception
165     {
166         LOG.error( REVERT_ERROR );
167         throw new IllegalStateException( REVERT_ERROR );
168     }
169 
170     
171     /**
172      * Inject the Ldifs if any
173      *
174      * @param service the instantiated directory service
175      * @param settings the settings containing the ldif
176      */
177     protected void injectLdifs( DirectoryService service, InheritableSettings settings )
178     {
179         List<String> ldifs = new ArrayList<String>();
180 
181         ldifs =  settings.getLdifs( ldifs );
182         
183         if ( ldifs.size() != 0 )
184         {
185             for ( String ldif:ldifs )
186             {
187                 try
188                 {
189                     StringReader in = new StringReader( ldif );
190                     LdifReader ldifReader = new LdifReader( in );
191                     LdifEntry entry = ldifReader.next();
192                     
193                     service.getAdminSession().add( 
194                         new DefaultServerEntry( service.getRegistries(), entry.getEntry() ) );
195                 }
196                 catch ( Exception e )
197                 {
198                     LOG.error( "Cannot inject the following entry : {}. Error : {}.", ldif, e.getMessage() );
199                 }
200             }
201         }
202     }
203 }