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.partition.impl.btree.jdbm;
21  
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.junit.Before;
26  import org.junit.After;
27  import org.junit.Test;
28  import static org.junit.Assert.*;
29  import org.apache.directory.server.schema.bootstrap.*;
30  import org.apache.directory.server.schema.registries.*;
31  import org.apache.directory.server.schema.SerializableComparator;
32  
33  import java.io.File;
34  import java.util.Set;
35  import java.util.HashSet;
36  
37  import jdbm.RecordManager;
38  import jdbm.recman.BaseRecordManager;
39  
40  
41  /**
42   * Test cases for JdbmMasterTable.
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   * @version $$Rev$$
46   */
47  public class JdbmMasterTableTest
48  {
49      private static final Logger LOG = LoggerFactory.getLogger( JdbmMasterTableTest.class.getSimpleName() );
50      private static final String TEST_OUTPUT_PATH = "test.output.path";
51  
52      transient JdbmMasterTable<Integer> table;
53      transient File dbFile;
54      transient RecordManager recman;
55      transient Registries registries = null;
56      transient AttributeTypeRegistry attributeRegistry;
57  
58  
59      public JdbmMasterTableTest() throws Exception
60      {
61          // setup the standard registries
62          BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
63          OidRegistry oidRegistry = new DefaultOidRegistry();
64          registries = new DefaultRegistries( "bootstrap", loader, oidRegistry );
65          SerializableComparator.setRegistry( registries.getComparatorRegistry() );
66  
67          // load essential bootstrap schemas
68          Set<Schema> bootstrapSchemas = new HashSet<Schema>();
69          bootstrapSchemas.add( new ApachemetaSchema() );
70          bootstrapSchemas.add( new ApacheSchema() );
71          bootstrapSchemas.add( new CoreSchema() );
72          bootstrapSchemas.add( new SystemSchema() );
73          loader.loadWithDependencies( bootstrapSchemas, registries );
74          attributeRegistry = registries.getAttributeTypeRegistry();
75      }
76  
77  
78      @Before
79      public void createTable() throws Exception
80      {
81          destryTable();
82          File tmpDir = null;
83          if ( System.getProperty( TEST_OUTPUT_PATH, null ) != null )
84          {
85              tmpDir = new File( System.getProperty( TEST_OUTPUT_PATH ) );
86          }
87  
88          dbFile = File.createTempFile( getClass().getSimpleName(), "db", tmpDir );
89          recman = new BaseRecordManager( dbFile.getAbsolutePath() );
90  
91          table = new JdbmMasterTable<Integer>( recman, registries );
92          LOG.debug( "Created new table and populated it with data" );
93  
94          JdbmMasterTable<Integer> t2 = new JdbmMasterTable<Integer>( recman, registries );
95          t2.close();
96      }
97  
98  
99      @After
100     public void destryTable() throws Exception
101     {
102         if ( table != null )
103         {
104             table.close();
105         }
106 
107         table = null;
108 
109         if ( recman != null )
110         {
111             recman.close();
112         }
113 
114         recman = null;
115 
116         if ( dbFile != null )
117         {
118             String fileToDelete = dbFile.getAbsolutePath();
119             new File( fileToDelete + ".db" ).delete();
120             new File( fileToDelete + ".lg" ).delete();
121 
122             dbFile.delete();
123         }
124 
125         dbFile = null;
126     }
127 
128 
129     @Test
130     public void testAll() throws Exception
131     {
132         assertNull( table.get( 0L ) );
133         assertEquals( 0, table.count() );
134 
135         assertEquals( 0, ( long ) table.getCurrentId() );
136         assertEquals( 1, ( long ) table.getNextId() );
137         assertEquals( 1, ( long ) table.getCurrentId() );
138         assertEquals( 0, table.count() );
139 
140         assertEquals( 1, ( long ) table.getCurrentId() );
141         assertEquals( 2, ( long ) table.getNextId() );
142         assertEquals( 2, ( long ) table.getCurrentId() );
143 
144         assertNull( table.getProperty( "foo" ) );
145         table.setProperty( "foo", "bar" );
146         assertEquals( "bar", table.getProperty( "foo" ) );
147     }
148 }