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  
24  import javax.naming.NamingException;
25  
26  import org.apache.directory.server.core.integ.DirectoryServiceFactory;
27  import org.apache.directory.server.core.integ.InheritableSettings;
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( TestServiceContext 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( InheritableSettings settings ) throws NamingException
68      {
69          LOG.debug( "calling create()" );
70  
71          try
72          {
73              DirectoryServiceFactory factory = settings.getFactory();
74              context.setService( 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.getService().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.getService().startup();
114     }
115 
116 
117     /**
118      * This method is a bit different.  Consider this method to hold the logic
119      * which is needed to shift the context state from the present state to a
120      * started state so we can call test on the current state of the context.
121      *
122      * Basically if the service is not needed or the test is ignored, then we
123      * just invoke the test: if ignored the test is not dealt with by the
124      * MethodRoadie run method.
125      *
126      * In tests not ignored requiring setup modes RESTART and CUMULATIVE we
127      * simply create the service and start it up without a cleanup.  In the
128      * PRISTINE and ROLLBACK modes we do the same but cleanup() before a
129      * restart.
130      *
131      * @see TestServiceState#test(TestClass, TestMethod, RunNotifier, InheritableSettings) 
132      */
133     public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableSettings settings )
134     {
135         LOG.debug( "calling test(): {}, mode {}", settings.getDescription().getDisplayName(), settings.getMode() );
136 
137         if ( testMethod.isIgnored() )
138         {
139             // The test is ignored
140             return;
141         }
142 
143         switch ( settings.getMode() )
144         {
145             case CUMULATIVE:
146             case RESTART:
147                 try
148                 {
149                     create( settings );
150                 }
151                 catch ( NamingException ne )
152                 {
153                     LOG.error( "Failed to create and start new server instance: " + ne );
154                     notifier.testAborted( settings.getDescription(), ne );
155                     return;
156                 }
157 
158                 try
159                 {
160                     startup();
161                 }
162                 catch ( Exception e )
163                 {
164                     LOG.error( "Failed to create and start new server instance: " + e );
165                     notifier.testAborted( settings.getDescription(), e );
166                     return;
167                 }
168 
169                 
170                 context.setState( context.getStartedNormalState() );
171                 context.getState().test( testClass, testMethod, notifier, settings );
172                 return;
173 
174 
175             case PRISTINE:
176             case ROLLBACK:
177                 try
178                 {
179                     create( settings );
180                 }
181                 catch ( NamingException ne )
182                 {
183                     LOG.error( "Failed to create and start new server instance: " + ne );
184                     notifier.testAborted( settings.getDescription(), ne );
185                     return;
186                 }
187 
188                 try
189                 {
190                     cleanup();
191                 }
192                 catch ( IOException ioe )
193                 {
194                     LOG.error( "Failed to create and start new server instance: " + ioe );
195                     notifier.testAborted( settings.getDescription(), ioe );
196                     return;
197                 }
198 
199                 try
200                 {
201                     startup();
202                 }
203                 catch ( Exception e )
204                 {
205                     LOG.error( "Failed to create and start new server instance: " + e );
206                     notifier.testAborted( settings.getDescription(), e );
207                     return;
208                 }
209 
210                 context.setState( context.getStartedPristineState() );
211                 context.getState().test( testClass, testMethod, notifier, settings );
212                 return;
213 
214             default:
215                 return;
216         }
217     }
218 }