View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index;
20  
21  import java.io.IOException;
22  import java.util.Collection;
23  
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 extends AbstractIndexCreatorHelper {
33      protected NexusIndexer nexusIndexer;
34  
35      protected Directory indexDir = new ByteBuffersDirectory();
36  
37      protected IndexingContext context;
38  
39      @Override
40      public void setUp() throws Exception {
41          //        indexDir = new SimpleFSDirectory(new File("/tmp/nexus-test"));
42          super.setUp();
43          // FileUtils.deleteDirectory( indexDir );
44          nexusIndexer = lookup(NexusIndexer.class);
45          prepareNexusIndexer(nexusIndexer);
46      }
47  
48      @Override
49      public void tearDown() throws Exception {
50          unprepareNexusIndexer(nexusIndexer);
51          super.tearDown();
52          // TODO: Brian reported, does not work on Windows because of left open files?
53          // FileUtils.deleteDirectory( indexDir );
54      }
55  
56      protected abstract void prepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception;
57  
58      protected void unprepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
59          nexusIndexer.removeIndexingContext(context, false);
60      }
61  
62      protected void assertGroup(int expected, String group, IndexingContext context) throws IOException {
63          // ArtifactInfo.UINFO - UN_TOKENIZED
64          // ArtifactInfo.GROUP_ID - TOKENIZED
65  
66          Term term = new Term(ArtifactInfo.GROUP_ID, group);
67          PrefixQuery pq = new PrefixQuery(term);
68  
69          // new WildcardQuery( //
70          // SpanTermQuery pq = new SpanTermQuery( term );
71          // PhraseQuery pq = new PhraseQuery();
72          // pq.add( new Term( ArtifactInfo.UINFO, group + "*" ) );
73  
74          FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(pq, context));
75          Collection<ArtifactInfo> artifacts = response.getResults();
76          assertEquals(artifacts.toString(), expected, artifacts.size());
77      }
78  }