1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.schema.bootstrap;
18  
19  
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import javax.naming.NamingException;
27  
28  import junit.framework.TestCase;
29  
30  import org.apache.ldap.common.schema.AttributeType;
31  
32  
33  /***
34   * A unit test case for the BootstrapSchemaLoader class.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 264732 $
38   */
39  public class BootstrapSchemaLoaderTest extends TestCase
40  {
41      BootstrapRegistries registries;
42  
43  
44      protected void setUp() throws Exception
45      {
46          super.setUp();
47          registries = new BootstrapRegistries();
48      }
49  
50  
51      protected void tearDown() throws Exception
52      {
53          super.tearDown();
54          registries = null;
55      }
56  
57  
58      public void testLoadAll() throws NamingException
59      {
60          BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
61          Set schemas = new HashSet();
62          schemas.add( new AutofsSchema() );
63          schemas.add( new CoreSchema() );
64          schemas.add( new CosineSchema() );
65          schemas.add( new CorbaSchema() );
66          schemas.add( new ApacheSchema() );
67          schemas.add( new InetorgpersonSchema() );
68          schemas.add( new JavaSchema() );
69          schemas.add( new Krb5kdcSchema() );
70          schemas.add( new NisSchema() );
71          schemas.add( new SystemSchema() );
72  
73          loader.load( schemas, registries );
74          AttributeType type;
75  
76          // from autofs.schema
77          type = registries.getAttributeTypeRegistry().lookup( "automountInformation" );
78          assertNotNull( type );
79  
80          // from core.schema
81          type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
82          assertNotNull( type );
83  
84          // from cosine.schema
85          type = registries.getAttributeTypeRegistry().lookup( "textEncodedORAddress" );
86          assertNotNull( type );
87  
88          // from corba.schema
89          type = registries.getAttributeTypeRegistry().lookup( "corbaRepositoryId" );
90          assertNotNull( type );
91  
92          // from eve.schema
93          type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
94          assertNotNull( type );
95  
96          // from inetorgperson.schema
97          type = registries.getAttributeTypeRegistry().lookup( "carLicense" );
98          assertNotNull( type );
99  
100         // from java.schema
101         type = registries.getAttributeTypeRegistry().lookup( "javaClassName" );
102         assertNotNull( type );
103 
104         // from krb5kdc.schema
105         type = registries.getAttributeTypeRegistry().lookup( "krb5PrincipalName" );
106         assertNotNull( type );
107 
108         // from nis.schema
109         type = registries.getAttributeTypeRegistry().lookup( "homeDirectory" );
110         assertNotNull( type );
111 
112         // from system.schema
113         type = registries.getAttributeTypeRegistry().lookup( "distinguishedName" );
114         assertNotNull( type );
115 
116     }
117 
118 
119     public void testSystemSchemaLoad() throws NamingException
120     {
121         SystemSchema systemSchema = new SystemSchema();
122         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
123         loader.load( systemSchema, registries );
124 
125         AttributeType type;
126         type = registries.getAttributeTypeRegistry().lookup( "distinguishedName" );
127         assertNotNull( type );
128 
129         type = registries.getAttributeTypeRegistry().lookup( "objectClass" );
130         assertNotNull( type );
131 
132         type = registries.getAttributeTypeRegistry().lookup( "modifyTimestamp" );
133         assertNotNull( type );
134     }
135 
136 
137     public void testApacheSchemaLoad() throws NamingException
138     {
139         testSystemSchemaLoad();
140 
141         ApacheSchema apacheSchema = new ApacheSchema();
142         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
143         loader.load( apacheSchema, registries );
144 
145         AttributeType type;
146         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
147         assertNotNull( type );
148 
149         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
150         assertNotNull( type );
151 
152         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
153         assertNotNull( type );
154     }
155 
156 
157     public void testEveDepsSchemaLoad() throws NamingException
158     {
159         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
160         
161         Set schemas = new HashSet();
162         schemas.add( new ApacheSchema() );
163         schemas.add( new SystemSchema() );
164         
165         loader.load( schemas, registries );
166         AttributeType type;
167         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
168         assertNotNull( type );
169 
170         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
171         assertNotNull( type );
172 
173         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
174         assertNotNull( type );
175     }
176 
177 
178     public void testCoreSchemaLoad() throws NamingException
179     {
180         testSystemSchemaLoad();
181 
182         CoreSchema coreSchema = new CoreSchema();
183         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
184         loader.load( coreSchema, registries );
185 
186         AttributeType type;
187         type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
188         assertNotNull( type );
189 
190         type = registries.getAttributeTypeRegistry().lookup( "countryName" );
191         assertNotNull( type );
192 
193         type = registries.getAttributeTypeRegistry().lookup( "serialNumber" );
194         assertNotNull( type );
195     }
196 
197 
198     public void testCoreDepsSchemaLoad() throws NamingException
199     {
200         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
201 
202         Set schemas = new HashSet();
203         schemas.add( new CoreSchema() );
204         schemas.add( new SystemSchema() );
205         
206         loader.load( schemas, registries );
207         AttributeType type;
208         type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
209         assertNotNull( type );
210 
211         type = registries.getAttributeTypeRegistry().lookup( "countryName" );
212         assertNotNull( type );
213 
214         type = registries.getAttributeTypeRegistry().lookup( "serialNumber" );
215         assertNotNull( type );
216     }
217 
218 
219     public void testJavaSchemaLoad() throws NamingException
220     {
221         testCoreSchemaLoad();
222 
223         JavaSchema javaSchema = new JavaSchema();
224         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
225         loader.load( javaSchema, registries );
226 
227         AttributeType type;
228         type = registries.getAttributeTypeRegistry().lookup( "javaFactory" );
229         assertNotNull( type );
230 
231         type = registries.getAttributeTypeRegistry().lookup( "javaSerializedData" );
232         assertNotNull( type );
233 
234         type = registries.getAttributeTypeRegistry().lookup( "javaClassNames" );
235         assertNotNull( type );
236     }
237 
238 
239     public void testJavaDepsSchemaLoad() throws NamingException
240     {
241         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
242 
243         Set schemas = new HashSet();
244         schemas.add( new CoreSchema() );
245         schemas.add( new JavaSchema() );
246         schemas.add( new SystemSchema() );
247         
248         loader.load( schemas, registries );
249         AttributeType type;
250         type = registries.getAttributeTypeRegistry().lookup( "javaFactory" );
251         assertNotNull( type );
252 
253         type = registries.getAttributeTypeRegistry().lookup( "javaSerializedData" );
254         assertNotNull( type );
255 
256         type = registries.getAttributeTypeRegistry().lookup( "javaClassNames" );
257         assertNotNull( type );
258     }
259 
260 
261     public void testEveAndJavaDepsSchemaLoad() throws NamingException
262     {
263         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
264 
265         Set schemas = new HashSet();
266         schemas.add( new ApacheSchema() );
267         schemas.add( new CoreSchema() );
268         schemas.add( new JavaSchema() );
269         schemas.add( new SystemSchema() );
270 
271         loader.load( schemas, registries );
272         AttributeType type;
273         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
274         assertNotNull( type );
275 
276         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
277         assertNotNull( type );
278 
279         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
280         assertNotNull( type );
281     }
282 
283 
284     /***
285      * Attempts to resolve the dependent schema objects of all entities that
286      * refer to other objects within the registries.
287      *
288      * @throws NamingException if there are problems.
289      */
290     public void testReferentialIntegrity() throws NamingException
291     {
292         if ( System.getProperties().containsKey( "ignore.ref.integ.test" ) )
293         {
294             System.err.println( "REFERENTIAL INTEGRITY TESTS BYPASSED!!!" );
295             return;
296         }
297 
298         testLoadAll();
299         List errors = registries.checkRefInteg();
300         assertNotNull( errors );
301 
302         StringBuffer buf = new StringBuffer();
303 
304         if ( ! errors.isEmpty() )
305         {
306             buf.append( "expected empty erorrs but got " )
307                     .append( errors.size() ).append( " errors:\n" );
308             for ( int ii = 0; ii < errors.size(); ii++ )
309             {
310                 buf.append( '\t' ).append( errors.get( ii ).toString() ).append( '\n' );
311             }
312 
313             StringWriter out = new StringWriter();
314             Exception e = ( Exception ) errors.get( 0 );
315             e.printStackTrace( new PrintWriter( out ) );
316             buf.append( "\nfirst exception trace:\n" + out.getBuffer().toString() );
317         }
318 
319         assertTrue( buf.toString(), errors.isEmpty() );
320     }
321 }
322