1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.core.integ.state;
20
21
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.naming.NamingException;
28
29 import org.apache.directory.server.core.DirectoryService;
30 import org.apache.directory.server.core.entry.DefaultServerEntry;
31 import org.apache.directory.server.core.integ.InheritableSettings;
32 import org.apache.directory.shared.ldap.ldif.LdifEntry;
33 import org.apache.directory.shared.ldap.ldif.LdifReader;
34 import org.junit.internal.runners.TestClass;
35 import org.junit.internal.runners.TestMethod;
36 import org.junit.runner.notification.RunNotifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47
48 public abstract class AbstractState implements TestServiceState
49 {
50
51 private static final Logger LOG = LoggerFactory.getLogger( AbstractState.class );
52
53
54 protected final TestServiceContext context;
55
56
57 private static final String DESTROY_ERR = "Cannot destroy when service is in NonExistant state";
58 private static final String CLEANUP_ERROR = "Cannot cleanup when service is in NonExistant state";
59 private static final String STARTUP_ERR = "Cannot startup when service is in NonExistant state";
60 private static final String SHUTDOWN_ERR = "Cannot shutdown service in NonExistant state.";
61 private static final String REVERT_ERROR = "Cannot revert when service is in NonExistant state";
62
63
64
65
66
67
68
69 protected AbstractState( TestServiceContext context )
70 {
71 this.context = context;
72 }
73
74
75
76
77
78
79
80
81
82
83
84 public void create( InheritableSettings settings ) throws NamingException
85 {
86 }
87
88
89
90
91
92
93
94 public void destroy()
95 {
96 LOG.error( DESTROY_ERR );
97 throw new IllegalStateException( DESTROY_ERR );
98 }
99
100
101
102
103
104
105
106
107
108 public void cleanup() throws IOException
109 {
110 LOG.error( CLEANUP_ERROR );
111 throw new IllegalStateException( CLEANUP_ERROR );
112 }
113
114
115
116
117
118
119
120 public void startup() throws Exception
121 {
122 LOG.error( STARTUP_ERR );
123 throw new IllegalStateException( STARTUP_ERR );
124 }
125
126
127
128
129
130
131
132 public void shutdown() throws Exception
133 {
134 LOG.error( SHUTDOWN_ERR );
135 throw new IllegalStateException( SHUTDOWN_ERR );
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public void test( TestClass testClass, TestMethod testMethod, RunNotifier notifier, InheritableSettings settings )
153 {
154 }
155
156
157
158
159
160
161
162
163
164 public void revert() throws Exception
165 {
166 LOG.error( REVERT_ERROR );
167 throw new IllegalStateException( REVERT_ERROR );
168 }
169
170
171
172
173
174
175
176
177 protected void injectLdifs( DirectoryService service, InheritableSettings settings )
178 {
179 List<String> ldifs = new ArrayList<String>();
180
181 ldifs = settings.getLdifs( ldifs );
182
183 if ( ldifs.size() != 0 )
184 {
185 for ( String ldif:ldifs )
186 {
187 try
188 {
189 StringReader in = new StringReader( ldif );
190 LdifReader ldifReader = new LdifReader( in );
191 LdifEntry entry = ldifReader.next();
192
193 service.getAdminSession().add(
194 new DefaultServerEntry( service.getRegistries(), entry.getEntry() ) );
195 }
196 catch ( Exception e )
197 {
198 LOG.error( "Cannot inject the following entry : {}. Error : {}.", ldif, e.getMessage() );
199 }
200 }
201 }
202 }
203 }