View Javadoc
1   package org.apache.maven.index;
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.IOException;
23  import java.util.Collection;
24  import org.apache.lucene.index.Term;
25  import org.apache.lucene.search.PrefixQuery;
26  import org.apache.lucene.store.ByteBuffersDirectory;
27  import org.apache.lucene.store.Directory;
28  import org.apache.maven.index.context.IndexingContext;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  public abstract class AbstractNexusIndexerTest
33      extends AbstractIndexCreatorHelper
34  {
35      protected NexusIndexer nexusIndexer;
36  
37      protected Directory indexDir = new ByteBuffersDirectory();
38  
39      protected IndexingContext context;
40  
41      @Override
42      public void setUp()
43          throws Exception
44      {
45  //        indexDir = new SimpleFSDirectory(new File("/tmp/nexus-test"));
46          super.setUp();
47          // FileUtils.deleteDirectory( indexDir );
48          nexusIndexer = lookup( NexusIndexer.class );
49          prepareNexusIndexer( nexusIndexer );
50      }
51  
52      @Override
53      public void tearDown()
54          throws Exception
55      {
56          unprepareNexusIndexer( nexusIndexer );
57          super.tearDown();
58          // TODO: Brian reported, does not work on Windows because of left open files?
59          // FileUtils.deleteDirectory( indexDir );
60      }
61  
62      protected abstract void prepareNexusIndexer( NexusIndexer nexusIndexer )
63          throws Exception;
64  
65      protected void unprepareNexusIndexer( NexusIndexer nexusIndexer )
66          throws Exception
67      {
68          nexusIndexer.removeIndexingContext( context, false );
69      }
70  
71      protected void assertGroup( int expected, String group, IndexingContext context )
72          throws IOException
73      {
74          // ArtifactInfo.UINFO - UN_TOKENIZED
75          // ArtifactInfo.GROUP_ID - TOKENIZED
76  
77          Term term = new Term( ArtifactInfo.GROUP_ID, group );
78          PrefixQuery pq = new PrefixQuery( term );
79  
80          // new WildcardQuery( //
81          // SpanTermQuery pq = new SpanTermQuery( term );
82          // PhraseQuery pq = new PhraseQuery();
83          // pq.add( new Term( ArtifactInfo.UINFO, group + "*" ) );
84  
85          FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( pq, context ) );
86          Collection<ArtifactInfo> artifacts = response.getResults();
87          assertEquals( artifacts.toString(), expected, artifacts.size() );
88      }
89  
90  }