Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple unit test for basic components of new indexing infrastructure
Add base unit tests for indexing components which covers only most basic non-integration scenarios for simple cases only.
- Loading branch information
1 parent
1c9629a
commit 1ad1c64
Showing
14 changed files
with
470 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.neo4j.io; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
|
||
import org.neo4j.test.NestedThrowableMatcher; | ||
|
||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
|
||
@RunWith( MockitoJUnitRunner.class ) | ||
public class IOUtilsTest | ||
{ | ||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Mock | ||
private AutoCloseable faultyClosable; | ||
@Mock | ||
private AutoCloseable goodClosable1; | ||
@Mock | ||
private AutoCloseable goodClosable2; | ||
|
||
@Test | ||
public void closeAllSilently() throws Exception | ||
{ | ||
IOUtils.closeAllSilently( goodClosable1, faultyClosable, goodClosable2 ); | ||
|
||
verify( goodClosable1 ).close(); | ||
verify( goodClosable2 ).close(); | ||
verify( faultyClosable ).close(); | ||
} | ||
|
||
@Test | ||
public void closeAllAndRethrowException() throws Exception | ||
{ | ||
doThrow( new IOException( "Faulty closable" ) ).when( faultyClosable ).close(); | ||
|
||
expectedException.expect( IOException.class ); | ||
expectedException.expectMessage( "Exception closing multiple resources" ); | ||
expectedException.expect( new NestedThrowableMatcher( IOException.class ) ); | ||
|
||
IOUtils.closeAll( goodClosable1, faultyClosable, goodClosable2 ); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
community/io/src/test/java/org/neo4j/test/NestedThrowableMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.neo4j.test; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
public class NestedThrowableMatcher extends TypeSafeMatcher<Throwable> | ||
{ | ||
private final Class<? extends Throwable> expectedType; | ||
|
||
public NestedThrowableMatcher( Class<? extends Throwable> expectedType ) | ||
{ | ||
this.expectedType = expectedType; | ||
} | ||
|
||
@Override | ||
public void describeTo( Description description ) | ||
{ | ||
description.appendText( "expect " ) | ||
.appendValue( expectedType ) | ||
.appendText( " to be exception cause." ); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely( Throwable item ) | ||
{ | ||
Throwable currentThrowable = item; | ||
do | ||
{ | ||
if ( expectedType.isInstance( currentThrowable ) ) | ||
{ | ||
return true; | ||
} | ||
currentThrowable = currentThrowable.getCause(); | ||
} | ||
while ( currentThrowable != null ); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ene-index/src/test/java/org/neo4j/kernel/api/impl/index/reader/SimpleIndexReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package org.neo4j.kernel.api.impl.index.reader; | ||
|
||
import org.apache.lucene.search.BooleanQuery; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.MatchAllDocsQuery; | ||
import org.apache.lucene.search.NumericRangeQuery; | ||
import org.apache.lucene.search.PrefixQuery; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.neo4j.helpers.TaskCoordinator; | ||
import org.neo4j.kernel.api.impl.index.DocValuesCollector; | ||
import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher; | ||
import org.neo4j.kernel.api.impl.index.sampler.NonUniqueLuceneIndexSampler; | ||
import org.neo4j.kernel.api.impl.index.sampler.UniqueLuceneIndexSampler; | ||
import org.neo4j.kernel.api.index.IndexConfiguration; | ||
import org.neo4j.kernel.configuration.Config; | ||
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig; | ||
import org.neo4j.storageengine.api.schema.IndexReader; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SimpleIndexReaderTest | ||
{ | ||
private final PartitionSearcher partitionSearcher = mock( PartitionSearcher.class ); | ||
private final IndexSearcher indexSearcher = mock( IndexSearcher.class ); | ||
private final IndexSamplingConfig samplingConfig = new IndexSamplingConfig( new Config() ); | ||
private final TaskCoordinator taskCoordinator = new TaskCoordinator( 0, TimeUnit.MILLISECONDS ); | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
when( partitionSearcher.getIndexSearcher() ).thenReturn( indexSearcher ); | ||
} | ||
|
||
@Test | ||
public void releaseSearcherOnClose() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.close(); | ||
|
||
verify( partitionSearcher ).close(); | ||
} | ||
|
||
@Test | ||
public void seekQueryReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.seek( "test" ); | ||
|
||
verify( indexSearcher ).search( any( TermQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void scanQueryReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.scan(); | ||
|
||
verify( indexSearcher ).search( any( MatchAllDocsQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void stringRangeSeekQueryReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.rangeSeekByString( "a", false, "b", true ); | ||
|
||
verify( indexSearcher ).search( any( TermQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void prefixRangeSeekQueryReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.rangeSeekByPrefix( "bb" ); | ||
|
||
verify( indexSearcher ).search( any( PrefixQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void numberRangeSeekQueryReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.rangeSeekByNumberInclusive( 7, 8 ); | ||
|
||
verify( indexSearcher ).search( any( NumericRangeQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void countIndexedNodesReachSearcher() throws IOException | ||
{ | ||
IndexReader simpleIndexReader = getUniqueSimpleReader(); | ||
|
||
simpleIndexReader.countIndexedNodes( 2, "testValue" ); | ||
|
||
verify( indexSearcher ).search( any( BooleanQuery.class ), any( DocValuesCollector.class ) ); | ||
} | ||
|
||
@Test | ||
public void uniqueIndexSamplerForUniqueIndex() | ||
{ | ||
SimpleIndexReader uniqueSimpleReader = getUniqueSimpleReader(); | ||
Assert.assertThat( uniqueSimpleReader.createSampler(), instanceOf( UniqueLuceneIndexSampler.class ) ); | ||
} | ||
|
||
@Test | ||
public void nonUuniqueIndexSamplerForNonUniqueIndex() | ||
{ | ||
SimpleIndexReader uniqueSimpleReader = getNonUniqueSimpleReader(); | ||
Assert.assertThat( uniqueSimpleReader.createSampler(), instanceOf( NonUniqueLuceneIndexSampler.class) ); | ||
} | ||
|
||
private SimpleIndexReader getNonUniqueSimpleReader() | ||
{ | ||
return new SimpleIndexReader( partitionSearcher, IndexConfiguration.NON_UNIQUE, samplingConfig, taskCoordinator ); | ||
} | ||
|
||
private SimpleIndexReader getUniqueSimpleReader() | ||
{ | ||
return new SimpleIndexReader( partitionSearcher, IndexConfiguration.UNIQUE, samplingConfig, taskCoordinator ); | ||
} | ||
} |
Oops, something went wrong.