1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.entry;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import javax.naming.NamingException;
31
32 import org.apache.directory.server.schema.bootstrap.ApacheSchema;
33 import org.apache.directory.server.schema.bootstrap.ApachemetaSchema;
34 import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
35 import org.apache.directory.server.schema.bootstrap.CoreSchema;
36 import org.apache.directory.server.schema.bootstrap.CosineSchema;
37 import org.apache.directory.server.schema.bootstrap.InetorgpersonSchema;
38 import org.apache.directory.server.schema.bootstrap.Schema;
39 import org.apache.directory.server.schema.bootstrap.SystemSchema;
40 import org.apache.directory.server.schema.registries.DefaultOidRegistry;
41 import org.apache.directory.server.schema.registries.DefaultRegistries;
42 import org.apache.directory.server.schema.registries.OidRegistry;
43 import org.apache.directory.server.schema.registries.Registries;
44 import org.apache.directory.shared.ldap.entry.EntryAttribute;
45 import org.apache.directory.shared.ldap.entry.Modification;
46 import org.apache.directory.shared.ldap.entry.ModificationOperation;
47 import org.apache.directory.shared.ldap.entry.client.ClientAttribute;
48 import org.apache.directory.shared.ldap.entry.client.ClientModification;
49 import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
50 import org.apache.directory.shared.ldap.schema.AttributeType;
51
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertFalse;
54 import static org.junit.Assert.assertTrue;
55 import static org.junit.Assert.assertNotSame;
56 import static org.junit.Assert.fail;
57
58 import org.junit.BeforeClass;
59 import org.junit.Test;
60
61
62
63
64
65
66
67
68 public class ServerModificationTest
69 {
70 private static BootstrapSchemaLoader loader;
71 private static Registries registries;
72 private static OidRegistry oidRegistry;
73 private static AttributeType atCN;
74
75
76 private static AttributeType atC;
77
78
79
80
81
82 private ByteArrayOutputStream serializeValue( ServerModification value ) throws IOException
83 {
84 ObjectOutputStream oOut = null;
85 ByteArrayOutputStream out = new ByteArrayOutputStream();
86
87 try
88 {
89 oOut = new ObjectOutputStream( out );
90 value.serialize( oOut );
91 oOut.flush();
92 }
93 catch ( IOException ioe )
94 {
95 throw ioe;
96 }
97 finally
98 {
99 try
100 {
101 if ( oOut != null )
102 {
103 oOut.flush();
104 oOut.close();
105 }
106 }
107 catch ( IOException ioe )
108 {
109 throw ioe;
110 }
111 }
112
113 return out;
114 }
115
116
117
118
119
120 private ServerModification deserializeValue( ByteArrayOutputStream out ) throws IOException, ClassNotFoundException, NamingException
121 {
122 ObjectInputStream oIn = null;
123 ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
124
125 try
126 {
127 oIn = new ObjectInputStream( in );
128
129 ServerModification value = new ServerModification();
130 value.deserialize( oIn, registries.getAttributeTypeRegistry() );
131
132 return value;
133 }
134 catch ( IOException ioe )
135 {
136 throw ioe;
137 }
138 finally
139 {
140 try
141 {
142 if ( oIn != null )
143 {
144 oIn.close();
145 }
146 }
147 catch ( IOException ioe )
148 {
149 throw ioe;
150 }
151 }
152 }
153
154
155
156
157
158 @BeforeClass
159 public static void setup() throws Exception
160 {
161 loader = new BootstrapSchemaLoader();
162 oidRegistry = new DefaultOidRegistry();
163 registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
164
165
166 Set<Schema> bootstrapSchemas = new HashSet<Schema>();
167 bootstrapSchemas.add( new ApachemetaSchema() );
168 bootstrapSchemas.add( new ApacheSchema() );
169 bootstrapSchemas.add( new CoreSchema() );
170 bootstrapSchemas.add( new SystemSchema() );
171 bootstrapSchemas.add( new InetorgpersonSchema() );
172 bootstrapSchemas.add( new CosineSchema() );
173 loader.loadWithDependencies( bootstrapSchemas, registries );
174
175 atCN = registries.getAttributeTypeRegistry().lookup( "cn" );
176 atC = registries.getAttributeTypeRegistry().lookup( "c" );
177 }
178
179
180 @Test public void testCreateServerModification()
181 {
182 ServerAttribute attribute = new DefaultServerAttribute( atCN );
183 attribute.add( "test1", "test2" );
184
185 Modification mod = new ServerModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
186 Modification clone = mod.clone();
187
188 attribute.remove( "test2" );
189
190 ServerAttribute clonedAttribute = (ServerAttribute)clone.getAttribute();
191
192 assertEquals( 1, mod.getAttribute().size() );
193 assertTrue( mod.getAttribute().contains( "test1" ) );
194
195 assertEquals( 2, clonedAttribute.size() );
196 assertTrue( clone.getAttribute().contains( "test1" ) );
197 assertTrue( clone.getAttribute().contains( "test2" ) );
198 }
199
200
201
202
203
204
205 @Test
206 public void testCopyServerModification()
207 {
208 ServerAttribute attribute = new DefaultServerAttribute( atC );
209 attribute.add( "test1", "test2" );
210 Modification serverModification = new ServerModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
211
212 Modification copy = new ServerModification( registries, serverModification );
213
214 assertTrue( copy instanceof ServerModification );
215 assertEquals( copy, serverModification );
216
217 serverModification.setOperation( ModificationOperation.REMOVE_ATTRIBUTE );
218 assertEquals( ModificationOperation.ADD_ATTRIBUTE, copy.getOperation() );
219
220 ServerAttribute attribute2 = new DefaultServerAttribute( atCN, "t" );
221 serverModification.setAttribute( attribute2 );
222 assertNotSame( attribute2, copy.getAttribute() );
223 }
224
225
226
227
228
229
230 @Test
231 public void testCopyClientModification()
232 {
233 ClientAttribute attribute = new DefaultClientAttribute( atC.getName() );
234 attribute.add( "test1", "test2" );
235 Modification clientModification = new ClientModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
236
237 Modification copy = new ServerModification( registries, clientModification );
238
239 assertTrue( copy instanceof ServerModification );
240 assertFalse( copy instanceof ClientModification );
241 assertFalse( copy.equals( clientModification ) );
242 assertTrue( copy.getAttribute() instanceof ServerAttribute );
243 assertEquals( atC, ((ServerAttribute)copy.getAttribute()).getAttributeType() );
244 assertEquals( ModificationOperation.ADD_ATTRIBUTE, copy.getOperation() );
245 assertTrue( copy.getAttribute().contains( "test1", "test2" ) );
246
247 clientModification.setOperation( ModificationOperation.REMOVE_ATTRIBUTE );
248 assertEquals( ModificationOperation.ADD_ATTRIBUTE, copy.getOperation() );
249
250 ClientAttribute attribute2 = new DefaultClientAttribute( "cn", "t" );
251 clientModification.setAttribute( attribute2 );
252 assertNotSame( attribute2, copy.getAttribute() );
253 }
254
255
256 @Test
257 public void testSerializationModificationADD() throws ClassNotFoundException, IOException, NamingException
258 {
259 EntryAttribute attribute = new DefaultServerAttribute( atCN );
260 attribute.add( "test1", "test2" );
261
262 ServerModification mod = new ServerModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
263
264 Modification modSer = deserializeValue( serializeValue( mod ) );
265
266 assertEquals( mod, modSer );
267 }
268
269
270 @Test
271 public void testSerializationModificationREPLACE() throws ClassNotFoundException, IOException, NamingException
272 {
273 EntryAttribute attribute = new DefaultServerAttribute( atCN );
274 attribute.add( "test1", "test2" );
275
276 ServerModification mod = new ServerModification( ModificationOperation.REPLACE_ATTRIBUTE, attribute );
277
278 Modification modSer = deserializeValue( serializeValue( mod ) );
279
280 assertEquals( mod, modSer );
281 }
282
283
284 @Test
285 public void testSerializationModificationREMOVE() throws ClassNotFoundException, IOException, NamingException
286 {
287 EntryAttribute attribute = new DefaultServerAttribute( atCN );
288 attribute.add( "test1", "test2" );
289
290 ServerModification mod = new ServerModification( ModificationOperation.REMOVE_ATTRIBUTE, attribute );
291
292 Modification modSer = deserializeValue( serializeValue( mod ) );
293
294 assertEquals( mod, modSer );
295 }
296
297
298 @Test
299 public void testSerializationModificationNoAttribute() throws ClassNotFoundException, IOException, NamingException
300 {
301 ServerModification mod = new ServerModification();
302
303 mod.setOperation( ModificationOperation.ADD_ATTRIBUTE );
304
305 try
306 {
307 deserializeValue( serializeValue( mod ) );
308 fail();
309 }
310 catch ( IOException ioe )
311 {
312 assertTrue( true );
313 }
314 }
315 }