Skip to content
Browse files

Added UT for NPE

git-svn-id: file:///opt/svn/repositories/sonatype.org/nexus/trunk/nexus-indexer@3546 2aa8b3fc-8ebb-4439-a84f-95066eaea8ab
  • Loading branch information...
1 parent f120d7e commit bede2e01bfc3228aeb63f6bfc0a99d10817c8ba6 cstamas committed Mar 3, 2009
Showing with 50 additions and 14 deletions.
  1. +50 −14 src/test/java/org/sonatype/nexus/index/updater/IndexDataTest.java
View
64 src/test/java/org/sonatype/nexus/index/updater/IndexDataTest.java
@@ -36,7 +36,7 @@ protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
throws Exception
{
indexDir = new RAMDirectory();
-
+
context = nexusIndexer.addIndexingContext(
"test-default",
"test",
@@ -51,10 +51,10 @@ protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
nexusIndexer.scan( context );
Date timestamp = context.getTimestamp();
-
+
assertNotNull( timestamp );
- // save and restore index to be used by common tests
+ // save and restore index to be used by common tests
ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -68,48 +68,84 @@ protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
Date newTimestamp = DefaultIndexUpdater.unpackIndexData( is, newDir, context );
assertEquals( timestamp, newTimestamp );
-
+
context.replace( newDir );
}
-
+
+ public void testEmptyContext()
+ throws Exception
+ {
+ indexDir = new RAMDirectory();
+
+ context = nexusIndexer.addIndexingContext(
+ "test-default",
+ "test",
+ repo,
+ indexDir,
+ null,
+ null,
+ NexusIndexer.DEFAULT_INDEX );
+
+ assertNull( context.getTimestamp() ); // unknown upon creation
+
+ // save and restore index to be used by common tests
+ // the point is that this is virgin context, and timestamp is null,
+ // and it should remain null
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ IndexDataWriter dw = new IndexDataWriter( bos );
+ dw.write( context, null );
+
+ ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() );
+
+ newDir = new RAMDirectory();
+
+ Date newTimestamp = DefaultIndexUpdater.unpackIndexData( is, newDir, context );
+
+ assertEquals( null, newTimestamp );
+
+ context.replace( newDir );
+ }
+
public void testData()
throws Exception
{
IndexReader r1 = context.getIndexReader();
-
+
Map<String, ArtifactInfo> r1map = readIndex( r1 );
-
+
IndexReader r2 = IndexReader.open( newDir );
Map<String, ArtifactInfo> r2map = readIndex( r2 );
-
+
for ( Entry<String, ArtifactInfo> e : r1map.entrySet() )
{
String key = e.getKey();
assertTrue( "Expected for find " + key, r2map.containsKey( key ) );
}
-
+
assertEquals( r1map.size(), r2map.size() );
}
private Map<String, ArtifactInfo> readIndex( IndexReader r1 )
throws CorruptIndexException,
IOException
{
- Map<String,ArtifactInfo> map = new HashMap<String, ArtifactInfo>();
+ Map<String, ArtifactInfo> map = new HashMap<String, ArtifactInfo>();
for ( int i = 0; i < r1.maxDoc(); i++ )
{
Document document = r1.document( i );
-
+
ArtifactInfo ai = IndexUtils.constructArtifactInfo( document, context );
-
- if( ai != null)
+
+ if ( ai != null )
{
map.put( ai.getUinfo(), ai );
}
}
-
+
return map;
}

0 comments on commit bede2e0

Please sign in to comment.
Something went wrong with that request. Please try again.