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.schema.bootstrap;
21  
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import javax.naming.NamingException;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
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.SystemSchema;
37  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
38  import org.apache.directory.server.schema.registries.DefaultRegistries;
39  import org.apache.directory.shared.ldap.schema.AttributeType;
40  
41  
42  /**
43   * A unit test case for the BootstrapSchemaLoader class.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   * @version $Rev: 437012 $
47   */
48  public class ExtraSchemaLoadTest extends TestCase
49  {
50      DefaultRegistries registries;
51  
52  
53      protected void setUp() throws Exception
54      {
55          super.setUp();
56          registries = new DefaultRegistries( "bootstrap", new BootstrapSchemaLoader(), new DefaultOidRegistry() );
57      }
58  
59  
60      protected void tearDown() throws Exception
61      {
62          super.tearDown();
63          registries = null;
64      }
65  
66  
67      public void testLoadAll() throws Exception
68      {
69          BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
70          Set<Schema> schemas = new HashSet<Schema>();
71          schemas.add( new CoreSchema() );
72          schemas.add( new ApacheSchema() );
73          schemas.add( new ApachednsSchema() );
74          schemas.add( new SystemSchema() );
75          schemas.add( new JavaSchema() );
76          schemas.add( new CorbaSchema() );
77          schemas.add( new CosineSchema() );
78          schemas.add( new DhcpSchema() );
79          schemas.add( new InetorgpersonSchema() );
80          schemas.add( new MozillaSchema() );
81          schemas.add( new CollectiveSchema() );
82          schemas.add( new AutofsSchema() );
83          schemas.add( new NisSchema() );
84          schemas.add( new SambaSchema() );
85          schemas.add( new Krb5kdcSchema() );
86  
87          loader.loadWithDependencies( schemas, registries );
88          AttributeType type;
89  
90          // from autofs.schema
91          type = registries.getAttributeTypeRegistry().lookup( "automountInformation" );
92          assertNotNull( type );
93  
94          // from core.schema
95          type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
96          assertNotNull( type );
97  
98          // from cosine.schema
99          type = registries.getAttributeTypeRegistry().lookup( "textEncodedORAddress" );
100         assertNotNull( type );
101 
102         // from corba.schema
103         type = registries.getAttributeTypeRegistry().lookup( "corbaRepositoryId" );
104         assertNotNull( type );
105 
106         // from eve.schema
107         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
108         assertNotNull( type );
109 
110         // from inetorgperson.schema
111         type = registries.getAttributeTypeRegistry().lookup( "carLicense" );
112         assertNotNull( type );
113 
114         // from java.schema
115         type = registries.getAttributeTypeRegistry().lookup( "javaClassName" );
116         assertNotNull( type );
117 
118         // from krb5kdc.schema
119         type = registries.getAttributeTypeRegistry().lookup( "krb5PrincipalName" );
120         assertNotNull( type );
121 
122         // from nis.schema
123         type = registries.getAttributeTypeRegistry().lookup( "homeDirectory" );
124         assertNotNull( type );
125 
126         // from system.schema
127         type = registries.getAttributeTypeRegistry().lookup( "distinguishedName" );
128         assertNotNull( type );
129 
130     }
131 
132 
133     public void testApacheSchemaLoad() throws Exception
134     {
135         ApacheSchema apacheSchema = new ApacheSchema();
136         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
137         loader.load( new SystemSchema(), registries, false );
138         loader.load( apacheSchema, registries, false );
139 
140         AttributeType type;
141         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
142         assertNotNull( type );
143 
144         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
145         assertNotNull( type );
146 
147         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
148         assertNotNull( type );
149     }
150 
151 
152     public void testDepsSchemaLoad() throws Exception
153     {
154         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
155 
156         Set<Schema> schemas = new HashSet<Schema>();
157         schemas.add( new ApacheSchema() );
158         schemas.add( new SystemSchema() );
159 
160         loader.loadWithDependencies( schemas, registries );
161         AttributeType type;
162         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
163         assertNotNull( type );
164 
165         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
166         assertNotNull( type );
167 
168         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
169         assertNotNull( type );
170     }
171 
172 
173     public void testCoreSchemaLoad() throws NamingException
174     {
175         CoreSchema coreSchema = new CoreSchema();
176         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
177         loader.load( new SystemSchema(), registries, false );
178         loader.load( coreSchema, registries, false );
179 
180         AttributeType type;
181         type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
182         assertNotNull( type );
183 
184         type = registries.getAttributeTypeRegistry().lookup( "countryName" );
185         assertNotNull( type );
186 
187         type = registries.getAttributeTypeRegistry().lookup( "serialNumber" );
188         assertNotNull( type );
189     }
190 
191 
192     public void testJavaSchemaLoad() throws Exception
193     {
194         testCoreSchemaLoad();
195 
196         JavaSchema javaSchema = new JavaSchema();
197         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
198         loader.load( javaSchema, registries, false );
199 
200         AttributeType type;
201         type = registries.getAttributeTypeRegistry().lookup( "javaFactory" );
202         assertNotNull( type );
203 
204         type = registries.getAttributeTypeRegistry().lookup( "javaSerializedData" );
205         assertNotNull( type );
206 
207         type = registries.getAttributeTypeRegistry().lookup( "javaClassNames" );
208         assertNotNull( type );
209     }
210 
211 
212     public void testJavaDepsSchemaLoad() throws Exception
213     {
214         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
215 
216         Set<Schema> schemas = new HashSet<Schema>();
217         schemas.add( new CoreSchema() );
218         schemas.add( new JavaSchema() );
219         schemas.add( new SystemSchema() );
220 
221         loader.loadWithDependencies( schemas, registries );
222         AttributeType type;
223         type = registries.getAttributeTypeRegistry().lookup( "javaFactory" );
224         assertNotNull( type );
225 
226         type = registries.getAttributeTypeRegistry().lookup( "javaSerializedData" );
227         assertNotNull( type );
228 
229         type = registries.getAttributeTypeRegistry().lookup( "javaClassNames" );
230         assertNotNull( type );
231     }
232 
233 
234     public void testApacheAndJavaDepsSchemaLoad() throws Exception
235     {
236         BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
237 
238         Set<Schema> schemas = new HashSet<Schema>();
239         schemas.add( new ApacheSchema() );
240         schemas.add( new CoreSchema() );
241         schemas.add( new JavaSchema() );
242         schemas.add( new SystemSchema() );
243 
244         loader.loadWithDependencies( schemas, registries );
245         AttributeType type;
246         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
247         assertNotNull( type );
248 
249         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
250         assertNotNull( type );
251 
252         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
253         assertNotNull( type );
254     }
255 
256 
257     /**
258      * Attempts to resolve the dependent schema objects of all entities that
259      * refer to other objects within the registries.
260      *
261      * @throws NamingException if there are problems.
262      */
263     public void testReferentialIntegrity() throws Exception
264     {
265         if ( System.getProperties().containsKey( "ignore.ref.integ.test" ) )
266         {
267             System.err.println( "REFERENTIAL INTEGRITY TESTS BYPASSED!!!" );
268             return;
269         }
270 
271         testLoadAll();
272         List errors = registries.checkRefInteg();
273         assertNotNull( errors );
274 
275         StringBuffer buf = new StringBuffer();
276 
277         if ( !errors.isEmpty() )
278         {
279             buf.append( "expected empty erorrs but got " ).append( errors.size() ).append( " errors:\n" );
280             for ( int ii = 0; ii < errors.size(); ii++ )
281             {
282                 buf.append( '\t' ).append( errors.get( ii ).toString() ).append( '\n' );
283             }
284 
285             StringWriter out = new StringWriter();
286             Exception e = ( Exception ) errors.get( 0 );
287             e.printStackTrace( new PrintWriter( out ) );
288             buf.append( "\nfirst exception trace:\n" + out.getBuffer().toString() );
289         }
290 
291         assertTrue( buf.toString(), errors.isEmpty() );
292     }
293 }