001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.directory.server.integ.state;
020
021
022 import static org.apache.directory.server.core.integ.IntegrationUtils.doDelete;
023
024 import java.io.IOException;
025
026 import javax.naming.NamingException;
027
028 import org.apache.directory.server.integ.InheritableServerSettings;
029 import org.apache.directory.server.integ.LdapServerFactory;
030 import org.junit.runner.notification.RunNotifier;
031 import org.junit.runners.model.Statement;
032 import org.junit.runners.model.TestClass;
033 import org.slf4j.Logger;
034 import org.slf4j.LoggerFactory;
035
036
037 /**
038 * The state of a test service when it has not yet been created.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 * @version $Rev$, $Date$
042 */
043 public class NonExistentState extends AbstractState
044 {
045 private static final Logger LOG = LoggerFactory.getLogger( NonExistentState.class );
046
047
048 /**
049 * Creates a new instance of NonExistentState.
050 *
051 * @param context the test context
052 */
053 public NonExistentState( TestServerContext context )
054 {
055 super( context );
056 }
057
058
059 /**
060 * Action where an attempt is made to create the service. Service
061 * creation in this system is the combined instantiation and
062 * configuration which takes place when the factory is used to get
063 * a new instance of the service.
064 *
065 * @param settings The inherited settings
066 * @throws NamingException if we can't create the service
067 */
068 public void create( InheritableServerSettings settings ) throws NamingException
069 {
070 LOG.debug( "calling create()" );
071
072 try
073 {
074 LdapServerFactory factory = settings.getFactory();
075 context.setLdapServer( factory.newInstance() );
076 }
077 catch ( InstantiationException ie )
078 {
079 throw new NamingException( ie.getMessage() );
080 }
081 catch ( IllegalAccessException iae )
082 {
083 throw new NamingException( iae.getMessage() );
084 }
085 catch ( Exception e )
086 {
087 throw new NamingException( e.getMessage() );
088 }
089 }
090
091
092 /**
093 * Action where an attempt is made to erase the contents of the
094 * working directory used by the service for various files including
095 * partition database files.
096 *
097 * @throws IOException on errors while deleting the working directory
098 */
099 public void cleanup() throws IOException
100 {
101 LOG.debug( "calling cleanup()" );
102 doDelete( context.getLdapServer().getDirectoryService().getWorkingDirectory() );
103 }
104
105
106 /**
107 * Action where an attempt is made to start up the service.
108 *
109 * @throws Exception on failures to start the core directory service
110 */
111 public void startup() throws Exception
112 {
113 LOG.debug( "calling startup()" );
114 context.getLdapServer().getDirectoryService().startup();
115 context.getLdapServer().start();
116 }
117
118
119 /**
120 * This method is a bit different. Consider this method to hold the logic
121 * which is needed to shift the context state from the present state to a
122 * started state so we can call test on the current state of the context.
123 *
124 * Basically if the service is not needed or the test is ignored, then we
125 * just invoke the test: if ignored the test is not dealt with by the
126 * MethodRoadie run method.
127 *
128 * In tests not ignored requiring setup modes RESTART and CUMULATIVE we
129 * simply create the service and start it up without a cleanup. In the
130 * PRISTINE and ROLLBACK modes we do the same but cleanup() before a
131 * restart.
132 *
133 * @see TestServerState#test(TestClass, TestMethod, RunNotifier, InheritableServerSettings)
134 */
135 public void test( TestClass testClass, Statement statement, RunNotifier notifier, InheritableServerSettings settings )
136 {
137 LOG.debug( "calling test(): {}, mode {}", settings.getDescription().getDisplayName(), settings.getMode() );
138
139 switch ( settings.getMode() )
140 {
141 case CUMULATIVE:
142 case RESTART:
143 try
144 {
145 create( settings );
146 }
147 catch ( NamingException ne )
148 {
149 LOG.error( "Failed to create and start new server instance: " + ne );
150 testAborted( notifier, settings.getDescription(), ne );
151 return;
152 }
153
154 try
155 {
156 startup();
157 }
158 catch ( Exception e )
159 {
160 LOG.error( "Failed to create and start new server instance: " + e );
161 testAborted( notifier, settings.getDescription(), e );
162 return;
163 }
164
165
166 context.setState( context.getStartedNormalState() );
167 context.getState().test( testClass, statement, notifier, settings );
168 return;
169
170
171 case PRISTINE:
172 case ROLLBACK:
173 try
174 {
175 create( settings );
176 }
177 catch ( NamingException ne )
178 {
179 LOG.error( "Failed to create and start new server instance: " + ne );
180 testAborted( notifier, settings.getDescription(), ne );
181 return;
182 }
183
184 try
185 {
186 cleanup();
187 }
188 catch ( IOException ioe )
189 {
190 LOG.error( "Failed to create and start new server instance: " + ioe );
191 testAborted( notifier, settings.getDescription(), ioe );
192 return;
193 }
194
195 try
196 {
197 startup();
198 }
199 catch ( Exception e )
200 {
201 LOG.error( "Failed to create and start new server instance: " + e );
202 testAborted( notifier, settings.getDescription(), e );
203 return;
204 }
205
206 context.setState( context.getStartedPristineState() );
207 context.getState().test( testClass, statement, notifier, settings );
208 return;
209
210 default:
211 return;
212 }
213 }
214 }