1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
39
40
41
42 public class StartedPristineState extends AbstractState
43 {
44 private static final Logger LOG = LoggerFactory.getLogger( StartedPristineState.class );
45
46
47
48
49
50
51
52
53 public StartedPristineState( TestServerContext context )
54 {
55 super( context );
56 }
57
58
59
60
61
62
63
64
65
66 public void cleanup() throws IOException
67 {
68 LOG.debug( "calling cleanup()" );
69 doDelete( context.getLdapServer().getDirectoryService().getWorkingDirectory() );
70 }
71
72
73
74
75
76
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
89
90
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
103
104
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
118
119
120
121
122
123
124
125
126
127
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
136 return;
137 }
138
139 switch ( settings.getMode() )
140 {
141 case PRISTINE:
142
143 try
144 {
145 injectLdifs( context.getLdapServer().getDirectoryService(), settings );
146 }
147 catch ( Exception e )
148 {
149
150
151
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
165
166
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
192 injectLdifs( context.getLdapServer().getDirectoryService(), settings );
193 }
194 catch ( Exception e )
195 {
196
197
198
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
213
214
215 notifier.testAborted( settings.getDescription(), e );
216 return;
217 }
218 return;
219
220 default:
221 return;
222 }
223 }
224 }