1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
41
42 public class PartitionTreeTest
43 {
44
45
46
47 @Test public void testNewPartitionTree() throws NamingException
48 {
49
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
73
74 @Test public void testNewPartitionTree2Nodes() throws NamingException
75 {
76
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
112
113 @Test public void testNewPartitionTree2OverlapingNodes() throws NamingException
114 {
115
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
142
143 @Test public void testNewPartitionTree2NodesWithSameRoot() throws NamingException
144 {
145
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 }