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  
21  package org.apache.directory.server.core.partition.tree;
22  
23  import javax.naming.NamingException;
24  
25  import org.apache.directory.server.core.partition.Partition;
26  import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
27  import org.apache.directory.shared.ldap.name.LdapDN;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertTrue;
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.fail;
34  
35  
36  /**
37   * Test the partition tree manipulations.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev$, $Date$
41   */
42  public class PartitionTreeTest
43  {
44      /**
45       * Test the addition of a single partition
46       */
47      @Test public void testNewPartitionTree() throws NamingException
48      {
49          /** A structure to hold all the partitions */
50          BranchNode partitionLookupTree = new BranchNode();
51          
52          LdapDN suffix = new LdapDN( "dc=example, dc=com" );
53          Partition partition = new JdbmPartition();
54          partition.setSuffix( suffix.getUpName() );
55          
56          Node node = partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix, 0, partition );
57          
58          assertNotNull( node );
59          assertTrue( node instanceof BranchNode );
60          assertTrue( ((BranchNode)node).contains( "dc=com" ) );
61          
62          Node child = ((BranchNode)node).getChild( "dc=com" );
63          assertTrue( child instanceof BranchNode );
64          assertTrue( ((BranchNode)child).contains( "dc=example" ) );
65  
66          child = ((BranchNode)child).getChild( "dc=example" );
67          assertEquals( "dc=example, dc=com", ((LeafNode)child).getPartition().getSuffix() );
68      }
69  
70  
71      /**
72       * Test the addition of a two disjointed partition
73       */
74      @Test public void testNewPartitionTree2Nodes() throws NamingException
75      {
76          /** A structure to hold all the partitions */
77          BranchNode partitionLookupTree = new BranchNode();
78          
79          LdapDN suffix1 = new LdapDN( "dc=example, dc=com" );
80          Partition partition1 = new JdbmPartition();
81          partition1.setSuffix( suffix1.getUpName() );
82          
83          Node node = partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix1, 0, partition1 );
84          
85          LdapDN suffix2 = new LdapDN( "ou=system" );
86          Partition partition2 = new JdbmPartition();
87          partition2.setSuffix( suffix2.getUpName() );
88          
89          node = partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix2, 0, partition2 );
90  
91          assertNotNull( node );
92          assertTrue( node instanceof BranchNode );
93          assertTrue( ((BranchNode)node).contains( "ou=system" ) );
94          assertTrue( ((BranchNode)node).contains( "dc=com" ) );
95          
96          Node child = ((BranchNode)node).getChild( "ou=system" );
97          assertTrue( child instanceof LeafNode );
98          assertEquals( "ou=system", ((LeafNode)child).getPartition().getSuffix() );
99  
100         child = ((BranchNode)node).getChild( "dc=com" );
101         assertTrue( child instanceof BranchNode );
102         assertTrue( ((BranchNode)child).contains( "dc=example" ) );
103         
104         child = ((BranchNode)child).getChild( "dc=example" );
105         assertTrue( child instanceof LeafNode );
106         assertEquals( "dc=example, dc=com", ((LeafNode)child).getPartition().getSuffix() );
107     }
108 
109 
110     /**
111      * Test the addition of a two overlapping partitions
112      */
113     @Test public void testNewPartitionTree2OverlapingNodes() throws NamingException
114     {
115         /** A structure to hold all the partitions */
116         BranchNode partitionLookupTree = new BranchNode();
117         
118         LdapDN suffix1 = new LdapDN( "dc=com" );
119         Partition partition1 = new JdbmPartition();
120         partition1.setSuffix( suffix1.getUpName() );
121         
122         partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix1, 0, partition1 );
123         
124         LdapDN suffix2 = new LdapDN( "dc=example, dc=com" );
125         Partition partition2 = new JdbmPartition();
126         partition2.setSuffix( suffix2.getUpName() );
127         
128         try
129         {
130             partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix2, 0, partition2 );
131             fail();
132         }
133         catch ( NamingException ne )
134         {
135             assertTrue( true );
136         }
137     }
138 
139 
140     /**
141      * Test the addition of a two partitions with the same root
142      */
143     @Test public void testNewPartitionTree2NodesWithSameRoot() throws NamingException
144     {
145         /** A structure to hold all the partitions */
146         BranchNode partitionLookupTree = new BranchNode();
147         
148         LdapDN suffix1 = new LdapDN( "dc=example1, dc=com" );
149         Partition partition1 = new JdbmPartition();
150         partition1.setSuffix( suffix1.getUpName() );
151         
152         partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix1, 0, partition1 );
153         
154         LdapDN suffix2 = new LdapDN( "dc=example2, dc=com" );
155         Partition partition2 = new JdbmPartition();
156         partition2.setSuffix( suffix2.getUpName() );
157         
158         Node node = partitionLookupTree.recursivelyAddPartition( partitionLookupTree, suffix2, 0, partition2 );
159 
160         assertNotNull( node );
161         
162         assertTrue( node instanceof BranchNode );
163         assertTrue( ((BranchNode)node).contains( "dc=com" ) );
164         
165         Node child = ((BranchNode)node).getChild( "dc=com" );
166         assertTrue( child instanceof BranchNode );
167 
168         child = ((BranchNode)node).getChild( "dc=com" );
169         assertTrue( child instanceof BranchNode );
170         assertTrue( ((BranchNode)child).contains( "dc=example1" ) );
171         assertTrue( ((BranchNode)child).contains( "dc=example2" ) );
172         
173         Node child1 = ((BranchNode)child).getChild( "dc=example1" );
174         assertTrue( child1 instanceof LeafNode );
175         assertEquals( "dc=example1, dc=com", ((LeafNode)child1).getPartition().getSuffix() );
176 
177         Node child2 = ((BranchNode)child).getChild( "dc=example1" );
178         assertTrue( child2 instanceof LeafNode );
179         assertEquals( "dc=example1, dc=com", ((LeafNode)child2).getPartition().getSuffix() );
180     }
181 }