blob: f5d7006f6c9d97c478529d443adc85ea5ea3405d [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.directory.server.xdbm.search.impl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.directory.server.xdbm.IndexCursor;
import org.apache.directory.server.xdbm.Store;
import org.apache.directory.server.xdbm.StoreUtils;
import org.apache.directory.server.xdbm.impl.avl.AvlIndex;
import org.apache.directory.server.xdbm.impl.avl.AvlStore;
import org.apache.directory.server.xdbm.search.Optimizer;
import org.apache.directory.shared.ldap.constants.SchemaConstants;
import org.apache.directory.shared.ldap.entry.Entry;
import org.apache.directory.shared.ldap.filter.ExprNode;
import org.apache.directory.shared.ldap.filter.FilterParser;
import org.apache.directory.shared.ldap.name.NameComponentNormalizer;
import org.apache.directory.shared.ldap.schema.SchemaManager;
import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
import org.apache.directory.shared.ldap.schema.loader.ldif.LdifSchemaLoader;
import org.apache.directory.shared.ldap.schema.manager.impl.DefaultSchemaManager;
import org.apache.directory.shared.ldap.schema.normalizers.ConcreteNameComponentNormalizer;
import org.apache.directory.shared.ldap.schema.syntaxCheckers.UuidSyntaxChecker;
import org.apache.directory.shared.ldap.util.LdapExceptionUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Tests the cursor functionality with deeply nested filters.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public class NestedFilterTest
{
private static final Logger LOG = LoggerFactory.getLogger( NestedFilterTest.class.getSimpleName() );
File wkdir;
Store<Entry, Long> store;
static SchemaManager schemaManager = null;
EvaluatorBuilder evaluatorBuilder;
CursorBuilder cursorBuilder;
Optimizer optimizer;
static FilterNormalizingVisitor visitor;
@BeforeClass
static public void setup() throws Exception
{
// setup the standard registries
String workingDirectory = System.getProperty( "workingDirectory" );
if ( workingDirectory == null )
{
String path = NestedFilterTest.class.getResource( "" ).getPath();
int targetPos = path.indexOf( "target" );
workingDirectory = path.substring( 0, targetPos + 6 );
}
File schemaRepository = new File( workingDirectory, "schema" );
SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor( new File( workingDirectory ) );
extractor.extractOrCopy( true );
LdifSchemaLoader loader = new LdifSchemaLoader( schemaRepository );
schemaManager = new DefaultSchemaManager( loader );
boolean loaded = schemaManager.loadAllEnabled();
if ( !loaded )
{
fail( "Schema load failed : " + LdapExceptionUtils.printErrors( schemaManager.getErrors() ) );
}
loaded = schemaManager.loadWithDeps( loader.getSchema( "collective" ) );
if ( !loaded )
{
fail( "Schema load failed : " + LdapExceptionUtils.printErrors( schemaManager.getErrors() ) );
}
NameComponentNormalizer ncn = new ConcreteNameComponentNormalizer( schemaManager );
visitor = new FilterNormalizingVisitor( ncn, schemaManager );
}
@Before
public void createStore() throws Exception
{
destryStore();
// setup the working directory for the store
wkdir = File.createTempFile( getClass().getSimpleName(), "db" );
wkdir.delete();
wkdir = new File( wkdir.getParentFile(), getClass().getSimpleName() );
wkdir.mkdirs();
// initialize the store
store = new AvlStore<Entry>();
store.setId( "example" );
store.setCacheSize( 10 );
store.setPartitionDir( wkdir );
store.setSyncOnWrite( false );
store.addIndex( new AvlIndex( SchemaConstants.OU_AT_OID ) );
store.addIndex( new AvlIndex( SchemaConstants.CN_AT_OID ) );
StoreUtils.loadExampleData( store, schemaManager );
evaluatorBuilder = new EvaluatorBuilder( store, schemaManager );
cursorBuilder = new CursorBuilder( store, evaluatorBuilder );
optimizer = new DefaultOptimizer( store );
LOG.debug( "Created new store" );
}
@After
public void destryStore() throws Exception
{
if ( store != null )
{
store.destroy();
}
store = null;
if ( wkdir != null )
{
FileUtils.deleteDirectory( wkdir );
}
wkdir = null;
}
@Test
public void testNestedAndnOr() throws Exception
{
String filter = "(|(&(cn=J*)(sn=w*))(ou=apache))";
ExprNode exprNode = FilterParser.parse( filter );
exprNode.accept( visitor );
optimizer.annotate( exprNode );
IndexCursor<?, Entry, Long> cursor = cursorBuilder.build( exprNode );
assertTrue( cursor.next() );
assertTrue( cursor.available() );
assertEquals( 5, ( long ) cursor.get().getId() );
assertEquals( "walker", cursor.get().getValue() );
assertTrue( cursor.next() );
assertTrue( cursor.available() );
assertEquals( 7, ( long ) cursor.get().getId() );
assertEquals( "apache", cursor.get().getValue() );
assertTrue( cursor.next() );
assertTrue( cursor.available() );
assertEquals( 9, ( long ) cursor.get().getId() );
assertEquals( "apache", cursor.get().getValue() );
assertFalse( cursor.next() );
}
@Test
public void testNestedAndnNot() throws Exception
{
String filter = "(&(&(cn=Jo*)(sn=w*))(!(ou=apache)))";
ExprNode exprNode = FilterParser.parse( filter );
optimizer.annotate( exprNode );
IndexCursor<?, Entry, Long> cursor = cursorBuilder.build( exprNode );
assertTrue( cursor.next() );
assertTrue( cursor.available() );
assertEquals( 5, ( long ) cursor.get().getId() );
assertEquals( "walker", cursor.get().getValue() );
assertFalse( cursor.next() );
}
@Test
public void testNestedNotnOrnAnd() throws Exception
{
String filter = "(&(|(postalCode=5)(postalCode=6))(!(ou=sales)))";
UuidSyntaxChecker uuidSynChecker = new UuidSyntaxChecker();
ExprNode exprNode = FilterParser.parse( filter );
optimizer.annotate( exprNode );
IndexCursor<?, Entry, Long> cursor = cursorBuilder.build( exprNode );
Set<Long> set = new HashSet<Long>();
while ( cursor.next() )
{
assertTrue( cursor.available() );
set.add( cursor.get().getId() );
assertTrue( uuidSynChecker.isValidSyntax( cursor.get().getValue() ) );
}
assertEquals( 2, set.size() );
assertTrue( set.contains( 7L ) );
assertTrue( set.contains( 8L ) );
assertFalse( cursor.next() );
}
@Test
public void testNestedOrnNot() throws Exception
{
String filter = "(!(|(|(cn=Jo*)(sn=w*))(!(ou=apache))))";
ExprNode exprNode = FilterParser.parse( filter );
optimizer.annotate( exprNode );
IndexCursor<?, Entry, Long> cursor = cursorBuilder.build( exprNode );
}
}