001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core;
021    
022    import java.util.Iterator;
023    import java.util.NoSuchElementException;
024    
025    import javax.naming.NamingException;
026    
027    import org.apache.directory.shared.ldap.NotImplementedException;
028    import org.apache.directory.shared.ldap.cursor.ClosureMonitor;
029    import org.apache.directory.shared.ldap.cursor.Cursor;
030    import org.apache.directory.shared.ldap.cursor.CursorIterator;
031    import org.apache.directory.shared.ldap.entry.DefaultServerEntry;
032    import org.apache.directory.shared.ldap.entry.ServerEntry;
033    import org.apache.directory.shared.ldap.schema.SchemaManager;
034    
035    public class MockCursor implements Cursor<ServerEntry>
036    {
037        final int count;
038        int ii;
039        SchemaManager schemaManager;
040    
041    
042        public MockCursor(int count)
043        {
044            this.count = count;
045        }
046    
047    
048        public boolean available() 
049        {
050            return ii < count;
051        }
052    
053    
054        public void close() throws NamingException
055        {
056            ii = count;
057        }
058    
059    
060        public boolean hasMoreElements()
061        {
062            return ii < count;
063        }
064    
065    
066        public Object nextElement()
067        {
068            if ( ii >= count )
069            {
070                throw new NoSuchElementException();
071            }
072    
073            ii++;
074            
075            return new Object();
076        }
077    
078    
079        public void after( ServerEntry element ) throws Exception
080        {
081        }
082    
083    
084        public void afterLast() throws Exception
085        {
086        }
087    
088    
089        public void before( ServerEntry element ) throws Exception
090        {
091            throw new NotImplementedException();
092        }
093    
094    
095        public void beforeFirst() throws Exception
096        {
097            ii = -1;
098        }
099    
100    
101        public boolean first() throws Exception
102        {
103            ii = 0;
104            return ii < count;
105        }
106    
107    
108        public ServerEntry get() throws Exception
109        {
110            return new DefaultServerEntry( schemaManager );
111        }
112    
113    
114        public boolean isClosed() throws Exception
115        {
116            return false;
117        }
118    
119    
120        public boolean isElementReused()
121        {
122            return false;
123        }
124    
125    
126        public boolean last() throws Exception
127        {
128            ii = count;
129            return true;
130        }
131    
132    
133        public boolean next() 
134        {
135            if ( ii >= count )
136            {
137                return false;
138            }
139    
140            ii++;
141            
142            return true;
143        }
144    
145    
146        public boolean previous() throws Exception
147        {
148            if ( ii < 0 )
149            {
150                return false;
151            }
152            
153            ii--;
154            return true;
155        }
156    
157    
158        public Iterator<ServerEntry> iterator()
159        {
160            return new CursorIterator<ServerEntry>( this );
161        }
162    
163    
164        public void close( Exception reason ) throws Exception
165        {
166        }
167    
168    
169        public void setClosureMonitor( ClosureMonitor monitor )
170        {
171        }
172    
173    
174        public void setSchemaManager( SchemaManager schemaManager )
175        {
176            this.schemaManager = schemaManager;
177        }
178    }