KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > jndi > SchemaOpsTest


1 /**
2  * Created by IntelliJ IDEA.
3  * User: betch01
4  * Date: Dec 3, 2002
5  * Time: 12:09:51 PM
6  * To change this template use Options | File Templates.
7  */

8 package com.ca.commons.jndi;
9
10 import junit.framework.*;
11
12 import javax.naming.directory.*;
13 import javax.naming.NamingException JavaDoc;
14 import java.util.*;
15
16 public class SchemaOpsTest extends TestCase
17 {
18     private static boolean testWithDirectory = false;
19
20     private static SchemaOps virtualOps = null;
21     private static SchemaOps directorySchemaOps = null;
22
23     public void testGetOID()
24     {
25         assertEquals("0.9.2342.19200300.100.4.4", virtualOps.getOID(syntaxValue1));
26         assertEquals("2.5.4.0", virtualOps.getOID(attributeTypes[0]));
27         assertEquals("1.3.6.1.4.1.1466.115.121.1.31", virtualOps.getOID("( 1.3.6.1.4.1.1466.115.121.1.31 ) "));
28         assertEquals("1.3.6.1.4.1.1466.115.121.1.32", virtualOps.getOID("(1.3.6.1.4.1.1466.115.121.1.32 "));
29         assertEquals("1.3.6.1.4.1.1466.115.121.1.33", virtualOps.getOID("(1.3.6.1.4.1.1466.115.121.1.33)"));
30         assertEquals("1.3.6.1.4.1.1466.115.121.1.34", virtualOps.getOID("1.3.6.1.4.1.1466.115.121.1.34"));
31     }
32
33     private static String JavaDoc syntaxValue1 = "( 0.9.2342.19200300.100.4.4 NAME 'newPilotPerson' SUP ( person ) " +
34             "STRUCTURAL MAY ( uid $ mail $ drink $ roomNumber $ userClass $ homePhone $ homePostalAddress " +
35             " $ secretary $ personalTitle $ preferredDeliveryMethod $ businessCategory $ janetMailbox " +
36             " $ otherMailbox $ mobile $ pager $ organizationalStatus $ mailPreferenceOption $ personalSignature ) ) ";
37
38     private static String JavaDoc syntaxValue2 = "( 0.9.2342.19200300.100.4.4 NAME ( 'newPilotPerson' 'fred' 'neuerFleigerMensh' ) SUP ( person ) " +
39             "STRUCTURAL MAY ( uid $ mail $ drink $ roomNumber $ userClass $ homePhone $ homePostalAddress " +
40             " $ secretary $ personalTitle $ preferredDeliveryMethod $ businessCategory $ janetMailbox " +
41             " $ otherMailbox $ mobile $ pager $ organizationalStatus $ mailPreferenceOption $ personalSignature ) ) ";
42
43     /*
44      * Test case where there are no spaces between things...
45      */

46     private static String JavaDoc syntaxValue3 = "(9.9.9 NAME 'test' SUP (top) STRUCTURAL MAY " +
47             "(uid$mail$drink$roomNumber$userClass$homePhone$ " +
48             "homePostalAddress $ secretary $ personalTitle $ preferredDeliveryMethod $ " +
49             "businessCategory $ janetMailbox $ otherMailbox $ mobile $ pager $ " +
50             "organizationalStatus $ mailPreferenceOption $ personalSignature) )";
51
52     private static String JavaDoc[] syntaxValue3Mays =
53             {"uid", "mail", "drink", "roomNumber", "userClass", "homePhone",
54             "homePostalAddress", "secretary", "personalTitle", "preferredDeliveryMethod",
55             "businessCategory", "janetMailbox", "otherMailbox", "mobile", "pager",
56             "organizationalStatus", "mailPreferenceOption", "personalSignature"};
57
58
59     private static String JavaDoc[] attributeTypes = {
60             "( 2.5.4.0 NAME ( 'objectClass' 'oc' 'objectClass' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
61             "( 2.5.4.1 NAME ( 'aliasedObjectName' 'aliasedObjectName' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
62             "( 2.5.4.2 NAME 'knowledgeInformation' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
63             "( 2.5.4.3 NAME ( 'cn' 'commonName' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
64             "( 2.5.4.4 NAME ( 'sn' 'surname' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
65             "( 2.5.4.5 NAME 'serialNumber' SYNTAX 1.3.6.1.4.1.1466.115.121.1.44 )",
66             "( 2.5.4.6 NAME ( 'c' 'countryName' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
67             "( 2.5.4.7 NAME ( 'l' 'localityName' ) SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
68             "( 2.5.4.8 NAME ( 'st' 'stateOrProvinceName' ) SYNTAX",
69             "( 1.3.6.1.4.1.453.7.3.2.4 NAME 'mhsBadAddressSearchPoint' SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 )",
70             "( 1.3.6.1.4.1.453.7.3.2.5 NAME 'mhsBadAddressSearchAttributes' SYNTAX 1.3.6.1.4.1.1466.115.121.1.5 )",
71             "( 1.3.6.1.4.1.453.7.3.2.6 NAME 'mhsBodyPartConversionService' SYNTAX 1.3.6.1.4.1.1466.115.121.1.5 )",
72             "( 1.3.6.1.4.1.3327.6.1000.1.13 NAME 'cert_authCertSerialNumber' SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 )",
73             "( 1.3.6.1.4.1.3327.77.4.1.2 NAME 'uNSPSCTitle' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE )" };
74
75     // note sorted order, no redundancies
76
private static String JavaDoc[] sortedAttributeNames = {"aliasedObjectName", "c", "cert_authCertSerialNumber",
77                                                     "cn","commonName","countryName","knowledgeInformation",
78                                                     "l","localityName","mhsBadAddressSearchAttributes","mhsBadAddressSearchPoint",
79                                                     "mhsBodyPartConversionService","objectClass","oc",
80                                                     "serialNumber","sn","st",
81                                                     "stateOrProvinceName","surname", "uNSPSCTitle"};
82 /*
83     private static String[] sortedAttributeNames = {"aliasedObjectName", "aliasedObjectName", "c", "cert_authCertSerialNumber",
84                                                     "cn","commonName","countryName","knowledgeInformation",
85                                                     "l","localityName","mhsBadAddressSearchAttributes","mhsBadAddressSearchPoint",
86                                                     "mhsBodyPartConversionService","objectClass","objectClass","oc",
87                                                     "serialNumber","sn","st",
88                                                     "stateOrProvinceName","surname"};
89 */

90
91
92     private static String JavaDoc binaryStrings = "mhsBadAddressSearchAttributes mhsBodyPartConversionService cert_authCertSerialNumber ";
93
94     private static String JavaDoc[] ldapSyntaxes = {
95             "( 1.3.6.1.4.1.1466.115.121.1.4 DESC 'Audio' )",
96             "( 1.3.6.1.4.1.1466.115.121.1.5 DESC 'Binary' )",
97             "( 1.3.6.1.4.1.1466.115.121.1.7 DESC 'Boolean' )",
98             "( 1.3.6.1.4.1.1466.115.121.1.8 DESC 'Certificate' )",
99             "( 1.3.6.1.4.1.1466.115.121.1.9 DESC 'Certificate List' )" };
100
101     private static String JavaDoc[] nameForms = {
102             "( 1.3.6.1.4.1.3327.7.1 NAME 'country-top-NF' OC country MUST ( c ) )",
103             "( 1.3.6.1.4.1.3327.7.2 NAME 'o-top-NF' OC organization MUST ( o ) )",
104             "( 1.3.6.1.4.1.3327.7.3 NAME 'o-country-NF' OC organization MUST ( o ) MAY ( dnQualifier ) )" };
105
106     private static String JavaDoc[] objectClasses = {
107             "( 2.5.6.0 NAME 'top' ABSTRACT MUST ( objectClass ) )",
108             "( 2.5.6.1 NAME 'alias' SUP ( top ) STRUCTURAL MUST ( aliasedObjectName ) )",
109             "( 2.5.6.2 NAME 'country' SUP ( top ) STRUCTURAL MUST ( c ) MAY ( description $ searchGuide ) )",
110             "( 2.5.6.3 NAME 'locality' SUP ( top ) STRUCTURAL MAY ( description $ searchGuide $ l $ st $ street $ seeAlso ) )",
111             "( 1.1.1.1.1.1 NAME 'xxxPerson' DESC 'Person im EEA GDS-System' AUXILIARY MAY ( eeaBadgeNumber $ eeaPersonalHash ) X-NDS_NOT_CONTAINER '1' )"};
112
113
114
115     private static String JavaDoc[] objectClassesNames = {"top","alias","country","locality", "xxxPerson" }; // note unsorted order
116
private static String JavaDoc[] sortedObjectClassesNames = {"alias","country","locality", "top", "xxxPerson"}; // note sorted order
117

118     private static String JavaDoc[] topLevelNames;
119
120     private static BasicAttributes virtualSchema;
121
122     private static String JavaDoc[] syntaxNames2 = new String JavaDoc[]{"newPilotPerson", "fred", "neuerFleigerMensh"};
123
124     public SchemaOpsTest(String JavaDoc name)
125     {
126         super(name);
127     }
128
129     public static Test suite()
130     {
131         return new TestSuite(SchemaOpsTest.class);
132     }
133
134     public static void main(String JavaDoc[] args)
135     {
136         junit.textui.TestRunner.run(suite());
137     }
138
139     private BasicAttribute getAttribute(String JavaDoc[] values, String JavaDoc name)
140     {
141         BasicAttribute retAtt = new BasicAttribute(name);
142         for (int i=0; i<values.length; i++)
143             retAtt.add(values[i]);
144         return retAtt;
145     }
146
147     protected void setUp()
148     {
149         // virtualSchema is used to run basic tests in the absence of a live directory
150
System.out.println("running stand alone tests");
151         virtualSchema = new BasicAttributes();
152         topLevelNames = new String JavaDoc[] {"attributeTypes","ldapSyntaxes","nameForms","objectClasses"};
153         virtualSchema.put(getAttribute(attributeTypes, topLevelNames[0]));
154         virtualSchema.put(getAttribute(ldapSyntaxes, topLevelNames[1]));
155         virtualSchema.put(getAttribute(nameForms, topLevelNames[2]));
156         virtualSchema.put(getAttribute(objectClasses, topLevelNames[3]));
157         virtualOps = new SchemaOps(virtualSchema);
158
159     }
160
161     private boolean initDirectory()
162     {
163         if (testWithDirectory)
164         {
165             System.out.println("running directory link tests");
166             DirContext ctx = null;
167             try
168             {
169                 ConnectionData cdata = new ConnectionData();
170                 cdata.setURL("ldap://betch01:19389");
171                 ctx = BasicOps.openContext(cdata);
172
173                 //ctx = BasicOps.openContext("ldap://betch01:19389");
174
}
175             catch (NamingException JavaDoc e)
176             {
177                 fail("exception opening directory connection: " + e);
178             }
179
180             if (ctx == null)
181             {
182                 fail("Unable to open directory connection to test Schema Ops");
183             }
184
185             try
186             {
187                 directorySchemaOps = new SchemaOps(ctx);
188             }
189             catch (NamingException JavaDoc e)
190             {
191                 fail("unable to initialise directory connection " + e);
192             }
193
194             return true;
195         }
196         else
197             return false;
198     }
199
200     private boolean checkFixtures()
201     {
202
203         if (virtualOps == null)
204         {
205             System.out.println("skipping test - no schema ops object");
206             return false;
207         }
208
209         return true;
210     }
211
212     public void testStuff() throws NamingException JavaDoc
213     {
214         if (!checkFixtures())
215             return;
216
217         //virtualOps.debugPrint("");
218
}
219
220
221     public void testValueParser()
222         throws NamingException JavaDoc
223     {
224         BasicAttributes atts = virtualOps.getAttributesFromSchemaValue(syntaxValue1);
225         assertNotNull(atts.get("OID"));
226         assertNotNull(atts.get("NAME"));
227         assertNotNull(atts.get("SUP"));
228         assertNotNull(atts.get("STRUCTURAL"));
229         assertNotNull(atts.get("MAY"));
230
231
232         Attributes bloop = virtualOps.getAttributesFromSchemaValue(syntaxValue3);
233         debugPrintAttribute("WIERD BRACKET THING", bloop);
234
235         Enumeration mayValues = bloop.get("MAY").getAll();
236
237         for (int i=0; mayValues.hasMoreElements(); i++)
238             assertEquals(syntaxValue3Mays[i], mayValues.nextElement().toString());
239     }
240
241     public void testNameParser()
242         throws NamingException JavaDoc
243     {
244         assertEquals("newPilotPerson", virtualOps.getNames(syntaxValue1)[0]);
245
246         String JavaDoc[] names = virtualOps.getNames(syntaxValue2);
247         assertEquals(names.length, syntaxNames2.length);
248         for (int i = 0; i < syntaxNames2.length; i++)
249         {
250             assertEquals(names[i], syntaxNames2[i]);
251         }
252
253         //syntaxValue3Mays
254
assertEquals("newPilotPerson", virtualOps.getFirstName(syntaxValue1));
255
256         assertEquals("newPilotPerson", virtualOps.getFirstName(syntaxValue2));
257
258     }
259
260     public void testObjectClasses()
261         throws NamingException JavaDoc
262     {
263         ArrayList list = virtualOps.objectClasses();
264         assertNotNull(list);
265         assertTrue(list.size() == sortedObjectClassesNames.length);
266         for (int i=0; i<list.size(); i++)
267             assertTrue(" testing equality between ("+i+")" + list.get(i) + " and " + objectClassesNames[i],list.get(i).equals(sortedObjectClassesNames[i]));
268     }
269
270     public void testAttributeNames()
271         throws NamingException JavaDoc
272     {
273         ArrayList list = virtualOps.attributeNames();
274         assertNotNull("read attribute list", list);
275         assertTrue("read list size: " + list.size() + " should equal stored list: " + sortedAttributeNames.length , list.size() == sortedAttributeNames.length);
276         for (int i=0; i<list.size(); i++)
277             assertTrue(" testing equality between ("+i+")" + list.get(i) + " and " + sortedAttributeNames[i],list.get(i).equals(sortedAttributeNames[i]));
278     }
279
280
281     public void testGetNewBinaryAttributes()
282     {
283         String JavaDoc bloop = virtualOps.getNewBinaryAttributes();
284         assertEquals(bloop, binaryStrings);
285     }
286
287     public void testGetAttributeSyntax()
288     {
289         assertEquals("1.3.6.1.4.1.1466.115.121.1.38", virtualOps.getAttributeSyntax("objectClass"));
290         assertEquals("1.3.6.1.4.1.1466.115.121.1.38", virtualOps.getAttributeSyntax("oc"));
291         assertEquals("1.3.6.1.4.1.1466.115.121.1.12", virtualOps.getAttributeSyntax("aliasedObjectName"));
292         assertEquals("1.3.6.1.4.1.1466.115.121.1.15", virtualOps.getAttributeSyntax("knowledgeInformation"));
293     }
294
295     public void testDirGetAttributeSyntax()
296     {
297         if (initDirectory())
298         {
299             assertEquals("1.3.6.1.4.1.1466.115.121.1.38", directorySchemaOps.getAttributeSyntax("objectClass"));
300             assertEquals("1.3.6.1.4.1.1466.115.121.1.38", directorySchemaOps.getAttributeSyntax("oc"));
301             assertEquals("1.3.6.1.4.1.1466.115.121.1.12", directorySchemaOps.getAttributeSyntax("aliasedObjectName"));
302             assertEquals("1.3.6.1.4.1.1466.115.121.1.15", directorySchemaOps.getAttributeSyntax("knowledgeInformation"));
303         }
304     }
305
306     public void testDirSchemaLookup()
307     {
308         if (initDirectory())
309         {
310             assertEquals("c",directorySchemaOps.schemaLookup("schema=countryName,schema=attributeTypes", "NAME"));
311             assertEquals("Certificate",directorySchemaOps.schemaLookup("schema=Certificate,schema=ldapSyntaxes", "DESC"));
312         }
313     }
314
315
316     public void testSchemaLookup()
317     {
318         assertEquals("c",virtualOps.schemaLookup("schema=countryName,schema=attributeTypes", "NAME"));
319         assertEquals("Certificate",virtualOps.schemaLookup("schema=Certificate,schema=ldapSyntaxes", "DESC"));
320         assertEquals("objectClass", virtualOps.schemaLookup("AttributeDefinition/objectClass", "NAME"));
321         assertEquals("objectClass", virtualOps.schemaLookup("attributeTypes/objectClass", "NAME"));
322     }
323
324 //.schemaLookup("AttributeDefinition/objectClass", "NAME");
325

326     public void testSchemaList()
327         throws NamingException JavaDoc
328     {
329         ArrayList nextLevel = virtualOps.listEntryNames("schema=objectClasses");
330         for (int i=0; i<nextLevel.size(); i++)
331             assertEquals(objectClassesNames[i], nextLevel.get(i) );
332
333         nextLevel = virtualOps.listEntryNames("");
334         for (int i=0; i<topLevelNames.length; i++)
335             nextLevel.contains(topLevelNames[i]);
336
337         nextLevel = virtualOps.listEntryNames("cn=schema");
338         for (int i=0; i<topLevelNames.length; i++)
339             nextLevel.contains(topLevelNames[i]);
340     }
341
342     public void testGetNameOfObjectClassAttribute()
343     {
344         assertEquals("objectClass", virtualOps.getNameOfObjectClassAttribute());
345     }
346
347     public void testTranslateOID()
348     {
349         assertEquals("knowledgeInformation", virtualOps.translateOID("2.5.4.2"));
350         assertEquals("mhsBadAddressSearchPoint", virtualOps.translateOID("1.3.6.1.4.1.453.7.3.2.4"));
351         assertEquals("o-top-NF", virtualOps.translateOID("1.3.6.1.4.1.3327.7.2"));
352     }
353
354     public void testMultipleNamedAttributes()
355         throws NamingException JavaDoc
356     {
357         Attributes cn1 = virtualOps.getAttributesFromSchemaName("schema=commonName,schema=attributeTypes");
358         Attributes cn2 = virtualOps.getAttributesFromSchemaName("schema=cn,schema=attributeTypes");
359
360         assertNotNull(cn1);
361         assertNotNull(cn2);
362         assertEquals(cn1.get("OID"), cn2.get("OID"));
363
364     }
365
366     // test parsing of:
367
//"( 1.1.1.1.1.1 NAME 'xxxPerson' DESC 'Person im EEA GDS-System' AUXILIARY MAY ( eeaBadgeNumber $ eeaPersonalHash ) X-NDS_NOT_CONTAINER '1' )"};
368
//and
369
//"( 2.5.6.2 NAME 'country' SUP ( top ) STRUCTURAL MUST ( c ) MAY ( description $ searchGuide ) )",
370

371     public void testObjectClassAttributeParsing()
372         throws NamingException JavaDoc
373     {
374         Attributes oc = virtualOps.getAttributes("schema=xxxPerson,schema=objectClasses");
375
376         //debugPrintAttribute("XXX Person", oc);
377

378         assertEquals(oc.get("NAME").get(), "xxxPerson");
379         assertEquals(oc.get("DESC").get(), "Person im EEA GDS-System");
380
381         oc = virtualOps.getAttributes("schema=country,schema=objectClasses");
382         assertEquals(oc.get("NAME").get(), "country");
383         assertEquals(oc.get("OID").get(), "2.5.6.2");
384
385
386     }
387
388     public void testObjectClassAttributeParsingWithMixedCases()
389         throws NamingException JavaDoc
390     {
391         Attributes oc = virtualOps.getAttributes("schema=xxxperson,schema=objectClasses");
392
393         //debugPrintAttribute("XXX Person", oc);
394
assertNotNull("unable to read mixed case schema object", oc);
395         assertNotNull("unable to read name of mixed case schema object", oc.get("NAME"));
396
397         assertEquals(oc.get("NAME").get(), "xxxPerson");
398         assertEquals(oc.get("DESC").get(), "Person im EEA GDS-System");
399
400         oc = virtualOps.getAttributes("schema=country,schema=objectClasses");
401         assertEquals(oc.get("NAME").get(), "country");
402         assertEquals(oc.get("OID").get(), "2.5.6.2");
403
404
405     }
406
407     public void debugPrintAttribute(String JavaDoc msg, Attributes atts)
408         throws NamingException JavaDoc
409     {
410         System.out.println(msg);
411         Enumeration bloop = atts.getIDs();
412         while (bloop.hasMoreElements())
413         {
414             String JavaDoc id = bloop.nextElement().toString();
415             Enumeration vals = atts.get(id).getAll();
416             while (vals.hasMoreElements())
417                 System.out.println(" " + id + " : " + vals.nextElement().toString());
418         }
419
420     }
421
422     public void testGetTypeName()
423         throws NamingException JavaDoc
424     {
425         assertEquals("objectClasses", virtualOps.getTypeName("schema=objectClasses,cn=schema"));
426         assertEquals("attributeTypes", virtualOps.getTypeName("schema=cn,schema=attributeTypes,cn=schema"));
427         assertEquals("attributeTypes", virtualOps.getTypeName("schema=cn,schema=attributeTypes"));
428     }
429
430     public void testGetSpecificName()
431         throws NamingException JavaDoc
432     {
433         assertEquals("cn", virtualOps.getSpecificName("schema=cn,schema=attributeTypes,cn=schema"));
434         assertEquals("cn", virtualOps.getSpecificName("schema=cn,schema=attributeTypes"));
435         assertEquals("xxxPerson", virtualOps.getSpecificName("schema=xxxPerson,schema=objectClass"));
436     }
437     public void testMangleEntryName()
438     {
439         assertEquals("schema=objectClasses", virtualOps.mangleEntryName("schema=ClassDefinition,cn=schema"));
440         assertEquals("schema=ldapSyntaxes", virtualOps.mangleEntryName("schema=SyntaxDefinition"));
441         assertEquals("schema=cn,schema=attributeTypes", virtualOps.mangleEntryName("AttributeDefinition/cn;binary"));
442     } // ;binary,schema=AttributeDefinition>
443

444     public void testIsAttributeSingleValued()
445     {/* TE */
446         assertTrue("Testing if uNSPSCTitle is a SINGLE-VALUE attribute.", virtualOps.isAttributeSingleValued("uNSPSCTitle"));
447         assertTrue("Testing if countryName is a SINGLE-VALUE attribute.", (!virtualOps.isAttributeSingleValued("countryName")));
448     }
449 }
450
451
Popular Tags