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