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.configuration;
18  
19  
20  import java.util.Hashtable;
21  
22  import javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.naming.NameNotFoundException;
25  import javax.naming.directory.Attributes;
26  import javax.naming.directory.BasicAttributes;
27  
28  import junit.framework.Assert;
29  
30  import org.apache.ldap.server.AbstractAdminTestCase;
31  import org.apache.ldap.server.jndi.CoreContextFactory;
32  import org.apache.ldap.server.partition.impl.btree.jdbm.JdbmContextPartition;
33  
34  
35  /***
36   * Tests {@link AddContextPartitionConfiguration} and
37   * {@link RemoveContextPartitionConfiguration} works correctly.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 264732 $
41   */
42  public class ContextPartitionConfigurationTest extends AbstractAdminTestCase
43  {
44      public ContextPartitionConfigurationTest()
45      {
46      }
47  
48      public void testAddAndRemove() throws Exception
49      {
50          MutableContextPartitionConfiguration partitionCfg =
51              new MutableContextPartitionConfiguration();
52          partitionCfg.setName( "removable" );
53          partitionCfg.setSuffix( "ou=removable" );
54          Attributes ctxEntry = new BasicAttributes( true );
55          ctxEntry.put( "objectClass", "top" );
56          ctxEntry.put( "ou", "removable" );
57          partitionCfg.setContextEntry( ctxEntry );
58          partitionCfg.setContextPartition( new JdbmContextPartition() );
59          
60          // Test AddContextPartition
61          AddContextPartitionConfiguration addCfg =
62              new AddContextPartitionConfiguration( partitionCfg );
63          
64          Hashtable env = new Hashtable();
65          env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName() );
66          env.putAll( addCfg.toJndiEnvironment() );
67          
68          Context ctx = new InitialContext( env );
69          Assert.assertNotNull( ctx.lookup( "ou=removable" ) );
70          
71          // Test removeContextPartition
72          RemoveContextPartitionConfiguration removeCfg =
73              new RemoveContextPartitionConfiguration( "ou=removable" );
74          env.putAll( removeCfg.toJndiEnvironment() );
75          
76          ctx = new InitialContext( env );
77          try
78          {
79              ctx.lookup( "ou=removable" );
80              Assert.fail( "NameNotFoundException should be thrown." );
81          }
82          catch( NameNotFoundException e )
83          {
84              // Partition is removed.
85          }
86      }
87  }