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.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 junit.framework.TestCase;
30  
31  import org.apache.directory.server.schema.bootstrap.ApacheSchema;
32  import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader;
33  import org.apache.directory.server.schema.bootstrap.CoreSchema;
34  import org.apache.directory.server.schema.bootstrap.Schema;
35  import org.apache.directory.server.schema.bootstrap.SystemSchema;
36  import org.apache.directory.server.schema.registries.DefaultOidRegistry;
37  import org.apache.directory.server.schema.registries.DefaultRegistries;
38  import org.apache.directory.shared.ldap.schema.AttributeType;
39  
40  
41  /**
42   * A unit test case for the BootstrapSchemaLoader class.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   * @version $Rev: 658889 $
46   */
47  public class BootstrapSchemaLoaderTest extends TestCase
48  {
49      BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
50      DefaultRegistries registries;
51  
52  
53      protected void setUp() throws Exception
54      {
55          super.setUp();
56          registries = new DefaultRegistries( "bootstrap", loader, 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          Set<Schema> schemas = new HashSet<Schema>();
70          schemas.add( new CoreSchema() );
71          schemas.add( new ApacheSchema() );
72          schemas.add( new SystemSchema() );
73  
74          loader.loadWithDependencies( schemas, registries );
75          AttributeType type;
76  
77          // from core.schema
78          type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
79          assertNotNull( type );
80  
81          // from apache.schema
82          type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
83          assertNotNull( type );
84  
85          // from system.schema
86          type = registries.getAttributeTypeRegistry().lookup( "distinguishedName" );
87          assertNotNull( type );
88      }
89  
90  
91      public void testSystemSchemaLoad() throws Exception
92      {
93          SystemSchema systemSchema = new SystemSchema();
94          loader.load( systemSchema, registries, false );
95  
96          AttributeType type;
97          type = registries.getAttributeTypeRegistry().lookup( "distinguishedName" );
98          assertNotNull( type );
99  
100         type = registries.getAttributeTypeRegistry().lookup( "objectClass" );
101         assertNotNull( type );
102 
103         type = registries.getAttributeTypeRegistry().lookup( "modifyTimestamp" );
104         assertNotNull( type );
105     }
106 
107 
108     public void testApacheSchemaLoad() throws Exception
109     {
110         testSystemSchemaLoad();
111         ApacheSchema apacheSchema = new ApacheSchema();
112         loader.load( apacheSchema, registries, false );
113 
114         AttributeType type;
115         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
116         assertNotNull( type );
117 
118         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
119         assertNotNull( type );
120 
121         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
122         assertNotNull( type );
123     }
124 
125 
126     public void testEveDepsSchemaLoad() throws Exception
127     {
128         Set<Schema> schemas = new HashSet<Schema>();
129         schemas.add( new ApacheSchema() );
130         schemas.add( new SystemSchema() );
131 
132         loader.loadWithDependencies( schemas, registries );
133         AttributeType type;
134         type = registries.getAttributeTypeRegistry().lookup( "apacheNdn" );
135         assertNotNull( type );
136 
137         type = registries.getAttributeTypeRegistry().lookup( "apacheAlias" );
138         assertNotNull( type );
139 
140         type = registries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
141         assertNotNull( type );
142     }
143 
144 
145     public void testCoreSchemaLoad() throws Exception
146     {
147         testSystemSchemaLoad();
148         CoreSchema coreSchema = new CoreSchema();
149         loader.load( coreSchema, registries, false );
150 
151         AttributeType type;
152         type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
153         assertNotNull( type );
154 
155         type = registries.getAttributeTypeRegistry().lookup( "countryName" );
156         assertNotNull( type );
157 
158         type = registries.getAttributeTypeRegistry().lookup( "serialNumber" );
159         assertNotNull( type );
160     }
161 
162 
163     public void testCoreDepsSchemaLoad() throws Exception
164     {
165         Set<Schema> schemas = new HashSet<Schema>();
166         schemas.add( new CoreSchema() );
167         schemas.add( new SystemSchema() );
168 
169         loader.loadWithDependencies( schemas, registries );
170         AttributeType type;
171         type = registries.getAttributeTypeRegistry().lookup( "knowledgeInformation" );
172         assertNotNull( type );
173 
174         type = registries.getAttributeTypeRegistry().lookup( "countryName" );
175         assertNotNull( type );
176 
177         type = registries.getAttributeTypeRegistry().lookup( "serialNumber" );
178         assertNotNull( type );
179     }
180 
181 
182     /**
183      * Attempts to resolve the dependent schema objects of all entities that
184      * refer to other objects within the registries.
185      *
186      * @throws NamingException if there are problems.
187      */
188     public void testReferentialIntegrity() throws Exception
189     {
190         if ( System.getProperties().containsKey( "ignore.ref.integ.test" ) )
191         {
192             System.err.println( "REFERENTIAL INTEGRITY TESTS BYPASSED!!!" );
193             return;
194         }
195 
196         testLoadAll();
197         List errors = registries.checkRefInteg();
198         assertNotNull( errors );
199 
200         StringBuffer buf = new StringBuffer();
201 
202         if ( !errors.isEmpty() )
203         {
204             buf.append( "expected empty erorrs but got " ).append( errors.size() ).append( " errors:\n" );
205             for ( int ii = 0; ii < errors.size(); ii++ )
206             {
207                 buf.append( '\t' ).append( errors.get( ii ).toString() ).append( '\n' );
208             }
209 
210             StringWriter out = new StringWriter();
211             Exception e = ( Exception ) errors.get( 0 );
212             e.printStackTrace( new PrintWriter( out ) );
213             buf.append( "\nfirst exception trace:\n" + out.getBuffer().toString() );
214         }
215 
216         assertTrue( buf.toString(), errors.isEmpty() );
217     }
218 }