1 package org.apache.maven.index.updater;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.index.CorruptIndexException;
32 import org.apache.lucene.index.IndexReader;
33 import org.apache.lucene.store.Directory;
34 import org.apache.lucene.store.RAMDirectory;
35 import org.apache.maven.index.AbstractRepoNexusIndexerTest;
36 import org.apache.maven.index.ArtifactInfo;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.context.IndexUtils;
39 import org.apache.maven.index.updater.DefaultIndexUpdater;
40 import org.apache.maven.index.updater.IndexDataWriter;
41
42
43
44
45 public class IndexDataTest
46 extends AbstractRepoNexusIndexerTest
47 {
48 private Directory newDir;
49
50 @Override
51 protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
52 throws Exception
53 {
54 indexDir = new RAMDirectory();
55
56 context =
57 nexusIndexer.addIndexingContext( "test-default", "test", repo, indexDir, null, null, DEFAULT_CREATORS );
58
59
60
61 nexusIndexer.scan( context );
62
63 Date timestamp = context.getTimestamp();
64
65 assertNotNull( timestamp );
66
67
68
69 ByteArrayOutputStream bos = new ByteArrayOutputStream();
70
71 IndexDataWriter dw = new IndexDataWriter( bos );
72 dw.write( context, null );
73
74 ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() );
75
76 newDir = new RAMDirectory();
77
78 Date newTimestamp = DefaultIndexUpdater.unpackIndexData( is, newDir, context );
79
80 assertEquals( timestamp, newTimestamp );
81
82 context.replace( newDir );
83 }
84
85 public void testEmptyContext()
86 throws Exception
87 {
88 indexDir = new RAMDirectory();
89
90 context =
91 nexusIndexer.addIndexingContext( "test-default", "test", repo, indexDir, null, null, DEFAULT_CREATORS );
92
93 assertNull( context.getTimestamp() );
94
95
96
97
98
99 ByteArrayOutputStream bos = new ByteArrayOutputStream();
100
101 IndexDataWriter dw = new IndexDataWriter( bos );
102 dw.write( context, null );
103
104 ByteArrayInputStream is = new ByteArrayInputStream( bos.toByteArray() );
105
106 newDir = new RAMDirectory();
107
108 Date newTimestamp = DefaultIndexUpdater.unpackIndexData( is, newDir, context );
109
110 assertEquals( null, newTimestamp );
111
112 context.replace( newDir );
113 }
114
115 public void testData()
116 throws Exception
117 {
118 IndexReader r1 = context.getIndexReader();
119
120 Map<String, ArtifactInfo> r1map = readIndex( r1 );
121
122 IndexReader r2 = IndexReader.open( newDir );
123
124 Map<String, ArtifactInfo> r2map = readIndex( r2 );
125
126 for ( Entry<String, ArtifactInfo> e : r1map.entrySet() )
127 {
128 String key = e.getKey();
129 assertTrue( "Expected for find " + key, r2map.containsKey( key ) );
130 }
131
132 assertEquals( r1map.size(), r2map.size() );
133 }
134
135 private Map<String, ArtifactInfo> readIndex( IndexReader r1 )
136 throws CorruptIndexException, IOException
137 {
138 Map<String, ArtifactInfo> map = new HashMap<String, ArtifactInfo>();
139
140 for ( int i = 0; i < r1.maxDoc(); i++ )
141 {
142 Document document = r1.document( i );
143
144 ArtifactInfo ai = IndexUtils.constructArtifactInfo( document, context );
145
146 if ( ai != null )
147 {
148 map.put( ai.getUinfo(), ai );
149 }
150 }
151
152 return map;
153 }
154
155 }