View Javadoc

1   package org.apache.maven.index.updater;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Eugene Kuleshov
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          // assertNull( context.getTimestamp() ); // unknown upon creation
60  
61          nexusIndexer.scan( context );
62  
63          Date timestamp = context.getTimestamp();
64  
65          assertNotNull( timestamp );
66  
67          // save and restore index to be used by common tests
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() ); // unknown upon creation
94  
95          // save and restore index to be used by common tests
96          // the point is that this is virgin context, and timestamp is null,
97          // and it should remain null
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 }