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