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 javax.naming.NamingException;
25  
26  import org.apache.directory.server.integ.LdapServerFactory;
27  import org.apache.directory.server.integ.InheritableServerSettings;
28  import static org.apache.directory.server.core.integ.IntegrationUtils.doDelete;
29  import org.junit.internal.runners.TestClass;
30  import org.junit.internal.runners.TestMethod;
31  import org.junit.runner.notification.RunNotifier;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * The state of a test service when it has not yet been created.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  public class NonExistentState extends AbstractState
43  {
44      private static final Logger LOG = LoggerFactory.getLogger( NonExistentState.class );
45  
46  
47      /**
48       * Creates a new instance of NonExistentState.
49       *
50       * @param context the test context
51       */
52      public NonExistentState( TestServerContext context )
53      {
54          super( context );
55      }
56  
57  
58      /**
59       * Action where an attempt is made to create the service.  Service
60       * creation in this system is the combined instantiation and
61       * configuration which takes place when the factory is used to get
62       * a new instance of the service.
63       *
64       * @param settings The inherited settings
65       * @throws NamingException if we can't create the service
66       */
67      public void create( InheritableServerSettings settings ) throws NamingException
68      {
69          LOG.debug( "calling create()" );
70  
71          try
72          {
73              LdapServerFactory factory = settings.getFactory();
74              context.setLdapServer( factory.newInstance() );
75          }
76          catch ( InstantiationException ie )
77          {
78              throw new NamingException( ie.getMessage() );
79          }
80          catch ( IllegalAccessException iae )
81          {
82              throw new NamingException( iae.getMessage() );
83          }
84          catch ( Exception e )
85          {
86              throw new NamingException( e.getMessage() );
87          }
88      }
89  
90  
91      /**
92       * Action where an attempt is made to erase the contents of the
93       * working directory used by the service for various files including
94       * partition database files.
95       *
96       * @throws IOException on errors while deleting the working directory
97       */
98      public void cleanup() throws IOException
99      {
100         LOG.debug( "calling cleanup()" );
101         doDelete( context.getLdapServer().getDirectoryService().getWorkingDirectory() );
102     }
103 
104 
105     /**
106      * Action where an attempt is made to start up the service.
107      *
108      * @throws Exception on failures to start the core directory service
109      */
110     public void startup() throws Exception
111     {
112         LOG.debug( "calling startup()" );
113         context.getLdapServer().getDirectoryService().startup();
114         context.getLdapServer().start();
115     }
116 
117 
118     /**
119      * This method is a bit different.  Consider this method to hold the logic
120      * which is needed to shift the context state from the present state to a
121      * started state so we can call test on the current state of the context.
122      *
123      * Basically if the service is not needed or the test is ignored, then we
124      * just invoke the test: if ignored the test is not dealt with by the
125      * MethodRoadie run method.
126      *
127      * In tests not ignored requiring setup modes RESTART and CUMULATIVE we
128      * simply create the service and start it up without a cleanup.  In the
129      * PRISTINE and ROLLBACK modes we do the same but cleanup() before a
130      * restart.
131      *
132      * @see TestServerState#test(TestClass, TestMethod, RunNotifier, InheritableServerSettings) 
133      */
134     public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableServerSettings settings )
135     {
136         LOG.debug( "calling test(): {}, mode {}", settings.getDescription().getDisplayName(), settings.getMode() );
137 
138         if ( testMethod.isIgnored() )
139         {
140             // The test is ignored
141             return;
142         }
143 
144         switch ( settings.getMode() )
145         {
146             case CUMULATIVE:
147             case RESTART:
148                 try
149                 {
150                     create( settings );
151                 }
152                 catch ( NamingException ne )
153                 {
154                     LOG.error( "Failed to create and start new server instance: " + ne );
155                     notifier.testAborted( settings.getDescription(), ne );
156                     return;
157                 }
158 
159                 try
160                 {
161                     startup();
162                 }
163                 catch ( Exception e )
164                 {
165                     LOG.error( "Failed to create and start new server instance: " + e );
166                     notifier.testAborted( settings.getDescription(), e );
167                     return;
168                 }
169 
170                 
171                 context.setState( context.getStartedNormalState() );
172                 context.getState().test( testClass, testMethod, notifier, settings );
173                 return;
174 
175 
176             case PRISTINE:
177             case ROLLBACK:
178                 try
179                 {
180                     create( settings );
181                 }
182                 catch ( NamingException ne )
183                 {
184                     LOG.error( "Failed to create and start new server instance: " + ne );
185                     notifier.testAborted( settings.getDescription(), ne );
186                     return;
187                 }
188 
189                 try
190                 {
191                     cleanup();
192                 }
193                 catch ( IOException ioe )
194                 {
195                     LOG.error( "Failed to create and start new server instance: " + ioe );
196                     notifier.testAborted( settings.getDescription(), ioe );
197                     return;
198                 }
199 
200                 try
201                 {
202                     startup();
203                 }
204                 catch ( Exception e )
205                 {
206                     LOG.error( "Failed to create and start new server instance: " + e );
207                     notifier.testAborted( settings.getDescription(), e );
208                     return;
209                 }
210 
211                 context.setState( context.getStartedPristineState() );
212                 context.getState().test( testClass, testMethod, notifier, settings );
213                 return;
214 
215             default:
216                 return;
217         }
218     }
219 }