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.configuration;
21  
22  
23  import junit.framework.Assert;
24  import org.apache.directory.server.core.DirectoryService;
25  import org.apache.directory.server.core.entry.DefaultServerEntry;
26  import org.apache.directory.server.core.entry.ServerEntry;
27  import org.apache.directory.server.core.integ.CiRunner;
28  import org.apache.directory.server.core.interceptor.context.AddOperationContext;
29  import org.apache.directory.server.core.jndi.CoreContextFactory;
30  import org.apache.directory.server.core.partition.Partition;
31  import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
32  import org.apache.directory.shared.ldap.name.LdapDN;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  
36  import javax.naming.Context;
37  import javax.naming.InitialContext;
38  import javax.naming.NameNotFoundException;
39  import java.util.Hashtable;
40  
41  
42  /**
43   * Tests dynamic partition addition and removal.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   * @version $Rev: 689396 $
47   */
48  @RunWith ( CiRunner.class )
49  public class PartitionConfigurationIT
50  {
51      public static DirectoryService service;
52  
53  
54      @Test
55      public void testAddAndRemove() throws Exception
56      {
57          Partition partition = new JdbmPartition();
58          partition.setId( "removable" );
59          partition.setSuffix( "ou=removable" );
60          
61          // Test AddContextPartition
62          service.addPartition( partition );
63          
64          LdapDN suffixDn = new LdapDN( "ou=removable" );
65          suffixDn.normalize( service.getRegistries().getAttributeTypeRegistry().getNormalizerMapping() );
66          ServerEntry ctxEntry = new DefaultServerEntry( service.getRegistries(), suffixDn );
67          ctxEntry.put( "objectClass", "top" );
68          ctxEntry.get( "objectClass" ).add( "organizationalUnit" );
69          ctxEntry.put( "ou", "removable" );
70          partition.add( new AddOperationContext( service.getAdminSession(), ctxEntry ) );
71          
72          Hashtable<String,Object> env = new Hashtable<String,Object>();
73          env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
74          env.put( DirectoryService.JNDI_KEY, service );
75          env.put( Context.SECURITY_CREDENTIALS, "secret" );
76          env.put( Context.SECURITY_AUTHENTICATION, "simple" );
77          env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
78          Context ctx = new InitialContext( env );
79          Assert.assertNotNull( ctx.lookup( "ou=removable" ) );
80  
81          // Test removeContextPartition
82          service.removePartition( partition );
83          ctx = new InitialContext( env );
84          try
85          {
86              ctx.lookup( "ou=removable" );
87              Assert.fail( "NameNotFoundException should be thrown." );
88          }
89          catch ( NameNotFoundException e )
90          {
91              // Partition is removed.
92          }
93      }
94  }