Author: kayyagari Date: Mon May 5 14:17:51 2008 New Revision: 653606 URL: http://svn.apache.org/viewvc?rev=653606&view=rev Log: fixed an NPE in CursorBuilder.buildAndCursor() AndCursorTest - added test cases for AndCursor Added: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/AndCursorTest.java Modified: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/CursorBuilder.java Modified: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/CursorBuilder.java URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/CursorBuilder.java?rev=653606&r1=653605&r2=653606&view=diff ============================================================================== --- directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/CursorBuilder.java (original) +++ directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/main/java/org/apache/directory/server/xdbm/search/impl/CursorBuilder.java Mon May 5 14:17:51 2008 @@ -159,7 +159,12 @@ for ( int ii = 0; ii < children.size(); ii++ ) { ExprNode child = children.get( ii ); - value = ( Long ) child.get( "count" ); + Object count = child.get( "count" ); + if( count == null ) + { + continue; + } + value = ( Long ) count; minValue = Math.min( minValue, value ); if ( minValue == value ) Added: directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/AndCursorTest.java URL: http://svn.apache.org/viewvc/directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/AndCursorTest.java?rev=653606&view=auto ============================================================================== --- directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/AndCursorTest.java (added) +++ directory/sandbox/akarasulu/bigbang/apacheds/xdbm-search/src/test/java/org/apache/directory/server/xdbm/search/impl/AndCursorTest.java Mon May 5 14:17:51 2008 @@ -0,0 +1,279 @@ +/* + * 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.*; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.naming.directory.Attributes; + +import org.apache.commons.io.FileUtils; +import org.apache.directory.server.core.cursor.Cursor; +import org.apache.directory.server.core.cursor.InvalidCursorPositionException; +import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex; +import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmStore; +import org.apache.directory.server.schema.SerializableComparator; +import org.apache.directory.server.schema.bootstrap.ApacheSchema; +import org.apache.directory.server.schema.bootstrap.ApachemetaSchema; +import org.apache.directory.server.schema.bootstrap.BootstrapSchemaLoader; +import org.apache.directory.server.schema.bootstrap.CollectiveSchema; +import org.apache.directory.server.schema.bootstrap.CoreSchema; +import org.apache.directory.server.schema.bootstrap.Schema; +import org.apache.directory.server.schema.bootstrap.SystemSchema; +import org.apache.directory.server.schema.registries.AttributeTypeRegistry; +import org.apache.directory.server.schema.registries.DefaultOidRegistry; +import org.apache.directory.server.schema.registries.DefaultRegistries; +import org.apache.directory.server.schema.registries.OidRegistry; +import org.apache.directory.server.schema.registries.Registries; +import org.apache.directory.server.xdbm.ForwardIndexEntry; +import org.apache.directory.server.xdbm.IndexEntry; +import org.apache.directory.server.xdbm.Store; +import org.apache.directory.server.xdbm.tools.StoreUtils; +import org.apache.directory.shared.ldap.constants.SchemaConstants; +import org.apache.directory.shared.ldap.filter.AndNode; +import org.apache.directory.shared.ldap.filter.ExprNode; +import org.apache.directory.shared.ldap.filter.FilterParser; +import org.apache.directory.shared.ldap.filter.PresenceNode; +import org.apache.directory.shared.ldap.filter.SubstringNode; +import org.junit.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * Test class for AndCursor. + * + * @author Apache Directory Project + * @version $Rev$, $Date$ + */ +public class AndCursorTest +{ + private static final Logger LOG = LoggerFactory.getLogger( AndCursorTest.class.getSimpleName() ); + + File wkdir; + Store store; + Registries registries = null; + AttributeTypeRegistry attributeRegistry; + EvaluatorBuilder evaluatorBuilder; + CursorBuilder cursorBuilder; + + public AndCursorTest() throws Exception + { + // setup the standard registries + BootstrapSchemaLoader loader = new BootstrapSchemaLoader(); + OidRegistry oidRegistry = new DefaultOidRegistry(); + registries = new DefaultRegistries( "bootstrap", loader, oidRegistry ); + SerializableComparator.setRegistry( registries.getComparatorRegistry() ); + + // load essential bootstrap schemas + Set bootstrapSchemas = new HashSet(); + bootstrapSchemas.add( new ApachemetaSchema() ); + bootstrapSchemas.add( new ApacheSchema() ); + bootstrapSchemas.add( new CoreSchema() ); + bootstrapSchemas.add( new SystemSchema() ); + bootstrapSchemas.add( new CollectiveSchema() ); + loader.loadWithDependencies( bootstrapSchemas, registries ); + attributeRegistry = registries.getAttributeTypeRegistry(); + } + + + @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 JdbmStore(); + store.setName( "example" ); + store.setCacheSize( 10 ); + store.setWorkingDirectory( wkdir ); + store.setSyncOnWrite( false ); + + store.addIndex( new JdbmIndex( SchemaConstants.OU_AT_OID ) ); + store.addIndex( new JdbmIndex( SchemaConstants.CN_AT_OID ) ); + StoreUtils.loadExampleData( store, registries ); + + evaluatorBuilder = new EvaluatorBuilder( store, registries ); + cursorBuilder = new CursorBuilder( store, evaluatorBuilder ); + + 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 testAndCursorWithCursorBuilder() throws Exception + { + String filter = "(&(cn=J*)(sn=*))"; + + ExprNode exprNode = FilterParser.parse( filter ); + + Cursor> cursor = cursorBuilder.build( exprNode ); + + cursor.beforeFirst(); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 8, ( long ) cursor.get().getId() ); + assertEquals( "jack daniels", cursor.get().getValue() ); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 6, ( long ) cursor.get().getId() ); + assertEquals( "jim bean", cursor.get().getValue() ); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 5, ( long ) cursor.get().getId() ); + assertEquals( "johnny walker", cursor.get().getValue() ); + + assertFalse( cursor.next() ); + assertFalse( cursor.available() ); + + cursor.close(); + assertTrue( cursor.isClosed() ); + } + + + @Test + public void testAndCursorWithManualFilter() throws Exception + { + AndNode andNode = new AndNode(); + + List> evaluators = new ArrayList>(); + Evaluator eval; + + ExprNode exprNode = new SubstringNode( "cn", "J*", null ); + eval = new SubstringEvaluator( ( SubstringNode ) exprNode, store, registries ); + Cursor> wrapped = new SubstringCursor( store, ( SubstringEvaluator ) eval ); + + /* adding this results in NPE adding Presence evaluator not + Substring evaluator but adding Substring cursor as wrapped cursor */ + // evaluators.add( eval ); + + andNode.addNode( exprNode ); + + exprNode = new PresenceNode( "sn" ); + eval = new PresenceEvaluator( ( PresenceNode ) exprNode, store, registries ); + evaluators.add( eval ); + + andNode.addNode( exprNode ); + + Cursor> cursor = ( Cursor> ) new AndCursor( wrapped, evaluators ); //cursorBuilder.build( andNode ); + + cursor.beforeFirst(); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 8, ( long ) cursor.get().getId() ); + assertEquals( "jack daniels", cursor.get().getValue() ); + + cursor.first(); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 6, ( long ) cursor.get().getId() ); + assertEquals( "jim bean", cursor.get().getValue() ); + + assertTrue( cursor.next() ); + assertTrue( cursor.available() ); + assertEquals( 5, ( long ) cursor.get().getId() ); + assertEquals( "johnny walker", cursor.get().getValue() ); + + assertFalse( cursor.next() ); + assertFalse( cursor.available() ); + + cursor.afterLast(); + + assertTrue( cursor.previous() ); + assertTrue( cursor.available() ); + assertEquals( 5, ( long ) cursor.get().getId() ); + assertEquals( "johnny walker", cursor.get().getValue() ); + + cursor.last(); + + assertTrue( cursor.previous() ); + assertTrue( cursor.available() ); + assertEquals( 6, ( long ) cursor.get().getId() ); + assertEquals( "jim bean", cursor.get().getValue() ); + + assertTrue( cursor.previous() ); + assertTrue( cursor.available() ); + assertEquals( 8, ( long ) cursor.get().getId() ); + assertEquals( "jack daniels", cursor.get().getValue() ); + + assertFalse( cursor.previous() ); + assertFalse( cursor.available() ); + + assertTrue( cursor.isElementReused() ); + + try + { + cursor.get(); + fail( "should fail with InvalidCursorPositionException" ); + } + catch( InvalidCursorPositionException ice ) { } + + try + { + cursor.after( new ForwardIndexEntry() ); + fail( "should fail with UnsupportedOperationException " ); + } + catch( UnsupportedOperationException uoe ) {} + + try + { + cursor.before( new ForwardIndexEntry() ); + fail( "should fail with UnsupportedOperationException " ); + } + catch( UnsupportedOperationException uoe ) {} + + } + + +}