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   */
20  package org.apache.directory.server.core.schema;
21  
22  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
28  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
29  import org.apache.directory.server.schema.bootstrap.CoreSchema;
30  import org.apache.directory.server.schema.bootstrap.SystemSchema;
31  import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
32  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
33  import org.apache.directory.server.schema.registries.DefaultRegistries;
34  import org.apache.directory.shared.ldap.schema.AttributeType;
35  
36  import junit.framework.TestCase;
37  
38  
39  /**
40   * Tests methods in SchemaInterceptor.
41   */
42  public class SchemaServiceTest extends TestCase
43  {
44      DefaultRegistries registries;
45  
46  
47      public void setUp() throws Exception
48      {
49          BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
50          registries = new DefaultRegistries( "bootstrap", loader, new DefaultOidRegistry() );
51          loader.load( new SystemSchema(), registries, false );
52          loader.load( new ApacheSchema(), registries, false );
53          loader.load( new CoreSchema(), registries, false );
54      }
55  
56      
57      public void testDescendants() throws Exception
58      {
59          AttributeTypeRegistry attrRegistry = registries.getAttributeTypeRegistry();
60          Iterator list = attrRegistry.descendants( "name" );
61          Set<String> nameAttrs = new HashSet<String>();
62          while ( list.hasNext() )
63          {
64              AttributeType type = ( AttributeType ) list.next();
65              nameAttrs.add( type.getName() );
66          }
67          assertEquals( "size of attributes extending name", 13, nameAttrs.size() );
68          assertTrue( nameAttrs.contains( "dmdName" ) );
69          assertTrue( nameAttrs.contains( "o" ) );
70          assertTrue( nameAttrs.contains( "c" ) );
71          assertTrue( nameAttrs.contains( "initials" ) );
72          assertTrue( nameAttrs.contains( "ou" ) );
73          assertTrue( nameAttrs.contains( "sn" ) );
74          assertTrue( nameAttrs.contains( "title" ) );
75          assertTrue( nameAttrs.contains( "l" ) );
76          assertTrue( nameAttrs.contains( "apachePresence" ) );
77          assertTrue( nameAttrs.contains( "cn" ) );
78          assertTrue( nameAttrs.contains( "st" ) );
79          assertTrue( nameAttrs.contains( "givenName" ) );
80      }
81      
82  /*
83      public void testAlterObjectClassesBogusAttr() throws NamingException
84      {
85          Attribute attr = new AttributeImpl( "blah", "blah" );
86  
87          try
88          {
89              SchemaInterceptor.alterObjectClasses( attr, registries.getObjectClassRegistry() );
90              fail( "should not get here" );
91          }
92          catch ( LdapNamingException e )
93          {
94              assertEquals( ResultCodeEnum.OPERATIONS_ERROR, e.getResultCode() );
95          }
96  
97          attr = new AttributeImpl( "objectClass" );
98          SchemaInterceptor.alterObjectClasses( attr );
99          assertEquals( 0, attr.size() );
100     }
101 
102 
103     public void testAlterObjectClassesNoAttrValue() throws NamingException
104     {
105         Attribute attr = new AttributeImpl( "objectClass" );
106         SchemaInterceptor.alterObjectClasses( attr );
107         assertEquals( 0, attr.size() );
108     }
109 
110 
111     public void testAlterObjectClassesTopAttrValue() throws NamingException
112     {
113         Attribute attr = new AttributeImpl( "objectClass", "top" );
114         SchemaInterceptor.alterObjectClasses( attr, registries.getObjectClassRegistry() );
115         assertEquals( 0, attr.size() );
116     }
117 
118 
119     public void testAlterObjectClassesInetOrgPersonAttrValue() throws NamingException
120     {
121         Attribute attr = new AttributeImpl( "objectClass", "organizationalPerson" );
122         SchemaInterceptor.alterObjectClasses( attr, registries.getObjectClassRegistry() );
123         assertEquals( 2, attr.size() );
124         assertTrue( attr.contains( "person" ) );
125         assertTrue( attr.contains( "organizationalPerson" ) );
126     }
127 
128 
129     public void testAlterObjectClassesOverlapping() throws NamingException
130     {
131         Attribute attr = new AttributeImpl( "objectClass", "organizationalPerson" );
132         attr.add( "residentialPerson" );
133         SchemaInterceptor.alterObjectClasses( attr, registries.getObjectClassRegistry() );
134         assertEquals( 3, attr.size() );
135         assertTrue( attr.contains( "person" ) );
136         assertTrue( attr.contains( "organizationalPerson" ) );
137         assertTrue( attr.contains( "residentialPerson" ) );
138     }
139 
140 
141     public void testAlterObjectClassesOverlappingAndDsa() throws NamingException
142     {
143         Attribute attr = new AttributeImpl( "objectClass", "organizationalPerson" );
144         attr.add( "residentialPerson" );
145         attr.add( "dSA" );
146         SchemaInterceptor.alterObjectClasses( attr, registries.getObjectClassRegistry() );
147         assertEquals( 5, attr.size() );
148         assertTrue( attr.contains( "person" ) );
149         assertTrue( attr.contains( "organizationalPerson" ) );
150         assertTrue( attr.contains( "residentialPerson" ) );
151         assertTrue( attr.contains( "dSA" ) );
152         assertTrue( attr.contains( "applicationEntity" ) );
153     }
154     */
155 }