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.state;
20  
21  
22  import java.io.IOException;
23  
24  import org.apache.directory.server.integ.InheritableServerSettings;
25  import static org.apache.directory.server.core.integ.IntegrationUtils.doDelete;
26  import org.junit.internal.runners.TestClass;
27  import org.junit.internal.runners.TestMethod;
28  import org.junit.runner.notification.RunNotifier;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  
34  /**
35   * The state of a running test service which has been used for running
36   * integration tests and has been reverted to contain the same content as it
37   * did when created and started.  It is not really pristine however for all
38   * practical purposes of integration testing it appears to be the same as
39   * when first started.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev$, $Date$
43   */
44  public class StartedNormalState extends AbstractState
45  {
46      private static final Logger LOG = LoggerFactory.getLogger( StartedNormalState.class );
47  
48  
49      /**
50       * 
51       * Creates a new instance of StartedNormalState.
52       *
53       * @param context the test's context
54       */
55      public StartedNormalState( TestServerContext context )
56      {
57          super( context );
58      }
59  
60  
61      /**
62       * Action where an attempt is made to destroy the service. This
63       * entails nulling out reference to it and triggering garbage
64       * collection.
65       */
66      public void destroy()
67      {
68          LOG.debug( "calling destroy()" );
69          context.getLdapServer().setDirectoryService( null );
70          context.setLdapServer( null );
71          context.setState( context.getNonExistentState() );
72          System.gc();
73      }
74  
75  
76      /**
77       * Action where an attempt is made to erase the contents of the
78       * working directory used by the service for various files including
79       * partition database files.
80       *
81       * @throws IOException on errors while deleting the working directory
82       */
83      public void cleanup() throws IOException
84      {
85          LOG.debug( "calling cleanup()" );
86          doDelete( context.getLdapServer().getDirectoryService().getWorkingDirectory() );
87      }
88  
89  
90      /**
91       * Action where an attempt is made to start up the service.
92       *
93       * @throws Exception on failures to start the core directory service
94       */
95      public void startup() throws Exception
96      {
97          LOG.debug( "calling start()" );
98          context.getLdapServer().getDirectoryService().startup();
99          context.getLdapServer().start();
100     }
101 
102 
103     /**
104      * Action where an attempt is made to shutdown the service.
105      *
106      * @throws Exception on failures to stop the core directory service
107      */
108     public void shutdown() throws Exception
109     {
110         LOG.debug( "calling shutdown()" );
111         context.getLdapServer().stop();
112         context.getLdapServer().getDirectoryService().shutdown();
113     }
114 
115 
116     /**
117      * Action where an attempt is made to revert the service to it's
118      * initial start up state by using a previous snapshot.
119      *
120      * @throws Exception on failures to revert the state of the core
121      * directory service
122      */
123     public void revert() throws Exception
124     {
125         LOG.debug( "calling revert()" );
126         context.getLdapServer().getDirectoryService().revert();
127     }
128 
129 
130     /**
131      * Action where an attempt is made to run a test against the service.
132      *
133      * All annotations should have already been processed for
134      * InheritableServerSettings yet they and others can be processed since we have
135      * access to the method annotations below
136      *
137      * @param testClass the class whose test method is to be run
138      * @param testMethod the test method which is to be run
139      * @param notifier a notifier to report failures to
140      * @param settings the inherited settings and annotations associated with
141      * the test method
142      */
143     public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableServerSettings settings )
144     {
145         LOG.debug( "calling test(): {}, mode {}", settings.getDescription().getDisplayName(), settings.getMode() );
146 
147         if ( testMethod.isIgnored() )
148         {
149             // The test is ignored
150             return;
151         }
152 
153         switch ( settings.getMode() )
154         {
155             case ROLLBACK:
156                 try
157                 {
158                     context.getLdapServer().getDirectoryService().getChangeLog().tag();
159 
160                     // Inject the LDIFs, if any 
161                     injectLdifs( context.getLdapServer().getDirectoryService(), settings );
162                 }
163                 catch ( Exception e )
164                 {
165                     // @TODO - we might want to check the revision of the service before
166                     // we presume that it has been soiled.  Some tests may simply perform
167                     // some read operations or checks on the service and may not alter it
168                     notifier.testAborted( settings.getDescription(), e );
169                     return;
170                 }
171 
172                 TestServerContext.invokeTest( testClass, testMethod, notifier, settings.getDescription() );
173                 
174                 try
175                 {
176                     revert();
177                 }
178                 catch ( Exception e )
179                 {
180                     // @TODO - we might want to check the revision of the service before
181                     // we presume that it has been soiled.  Some tests may simply perform
182                     // some read operations or checks on the service and may not alter it
183                     notifier.testAborted( settings.getDescription(), e );
184                     return;
185                 }
186                 
187                 return;
188                 
189             case RESTART :
190                 // Inject the LDIFs, if any 
191                 try
192                 {
193                     injectLdifs( context.getLdapServer().getDirectoryService(), settings );
194                 }
195                 catch ( Exception e )
196                 {
197                     // @TODO - we might want to check the revision of the service before
198                     // we presume that it has been soiled.  Some tests may simply perform
199                     // some read operations or checks on the service and may not alter it
200                     notifier.testAborted( settings.getDescription(), e );
201                     return;
202                 }
203 
204                 TestServerContext.invokeTest( testClass, testMethod, notifier, settings.getDescription() );
205 
206                 try
207                 {
208                     shutdown();
209                 }
210                 catch ( Exception e )
211                 {
212                     // @TODO - we might want to check the revision of the service before
213                     // we presume that it has been soiled.  Some tests may simply perform
214                     // some read operations or checks on the service and may not alter it
215                     notifier.testAborted( settings.getDescription(), e );
216                     return;
217                 }
218                 
219                 try
220                 {
221                     startup();
222                 }
223                 catch ( Exception e )
224                 {
225                     LOG.error( "Failed to create and start new server instance: " + e );
226                     notifier.testAborted( settings.getDescription(), e );
227                     return;
228                 }
229                 
230                 return;
231                 
232             default:
233                 return;
234         }
235     }
236 }