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 org.apache.directory.server.core.integ.InheritableSettings;
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( TestServiceContext 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.setService( null );
70 context.setState( context.getNonExistentState() );
71 System.gc();
72 }
73
74
75 /**
76 * Action where an attempt is made to erase the contents of the
77 * working directory used by the service for various files including
78 * partition database files.
79 *
80 * @throws IOException on errors while deleting the working directory
81 */
82 public void cleanup() throws IOException
83 {
84 LOG.debug( "calling cleanup()" );
85 doDelete( context.getService().getWorkingDirectory() );
86 }
87
88
89 /**
90 * Action where an attempt is made to start up the service.
91 *
92 * @throws Exception on failures to start the core directory service
93 */
94 public void startup() throws Exception
95 {
96 LOG.debug( "calling startup()" );
97 context.getService().startup();
98 }
99
100
101 /**
102 * Action where an attempt is made to shutdown the service.
103 *
104 * @throws Exception on failures to stop the core directory service
105 */
106 public void shutdown() throws Exception
107 {
108 LOG.debug( "calling shutdown()" );
109 context.getService().shutdown();
110 }
111
112
113 /**
114 * Action where an attempt is made to revert the service to it's
115 * initial start up state by using a previous snapshot.
116 *
117 * @throws Exception on failures to revert the state of the core
118 * directory service
119 */
120 public void revert() throws Exception
121 {
122 LOG.debug( "calling revert()" );
123 context.getService().revert();
124 }
125
126
127 /**
128 * Action where an attempt is made to run a test against the service.
129 *
130 * All annotations should have already been processed for
131 * InheritableSettings yet they and others can be processed since we have
132 * access to the method annotations below
133 *
134 * @param testClass the class whose test method is to be run
135 * @param testMethod the test method which is to be run
136 * @param notifier a notifier to report failures to
137 * @param settings the inherited settings and annotations associated with
138 * the test method
139 */
140 public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableSettings settings )
141 {
142 LOG.debug( "calling test(): {}, mode {}", settings.getDescription().getDisplayName(), settings.getMode() );
143
144 if ( testMethod.isIgnored() )
145 {
146 // The test is ignored
147 return;
148 }
149
150 switch ( settings.getMode() )
151 {
152 case ROLLBACK:
153 try
154 {
155 context.getService().getChangeLog().tag();
156 }
157 catch ( Exception e )
158 {
159 // @TODO - we might want to check the revision of the service before
160 // we presume that it has been soiled. Some tests may simply perform
161 // some read operations or checks on the service and may not alter it
162 notifier.testAborted( settings.getDescription(), e );
163 return;
164 }
165
166 // Inject the LDIFs, if any
167 injectLdifs( context.getService(), settings );
168
169 TestServiceContext.invokeTest( testClass, testMethod, notifier, settings.getDescription() );
170
171 try
172 {
173 revert();
174 }
175 catch ( Exception e )
176 {
177 // @TODO - we might want to check the revision of the service before
178 // we presume that it has been soiled. Some tests may simply perform
179 // some read operations or checks on the service and may not alter it
180 notifier.testAborted( settings.getDescription(), e );
181 return;
182 }
183
184 return;
185
186 case RESTART :
187 // Inject the LDIFs, if any
188 injectLdifs( context.getService(), settings );
189
190
191 TestServiceContext.invokeTest( testClass, testMethod, notifier, settings.getDescription() );
192
193 try
194 {
195 shutdown();
196 }
197 catch ( Exception e )
198 {
199 // @TODO - we might want to check the revision of the service before
200 // we presume that it has been soiled. Some tests may simply perform
201 // some read operations or checks on the service and may not alter it
202 notifier.testAborted( settings.getDescription(), e );
203 return;
204 }
205
206 try
207 {
208 startup();
209 }
210 catch ( Exception e )
211 {
212 LOG.error( "Failed to create and start new server instance: " + e );
213 notifier.testAborted( settings.getDescription(), e );
214 return;
215 }
216
217 return;
218
219 default:
220 return;
221 }
222 }
223 }