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.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  
26  import org.apache.lucene.search.IndexSearcher;
27  import org.apache.lucene.search.Query;
28  import org.apache.maven.index.AbstractIndexCreatorHelper;
29  import org.apache.maven.index.ArtifactContext;
30  import org.apache.maven.index.ArtifactInfo;
31  import org.apache.maven.index.IteratorSearchRequest;
32  import org.apache.maven.index.IteratorSearchResponse;
33  import org.apache.maven.index.MAVEN;
34  import org.apache.maven.index.NexusIndexer;
35  import org.apache.maven.index.SearchType;
36  import org.apache.maven.index.artifact.Gav;
37  import org.apache.maven.index.context.IndexingContext;
38  import org.apache.maven.index.packer.IndexPacker;
39  import org.apache.maven.index.packer.IndexPackingRequest;
40  import org.codehaus.plexus.util.FileUtils;
41  
42  import static org.junit.Assert.assertEquals;
43  
44  public abstract class AbstractIndexUpdaterTest
45      extends AbstractIndexCreatorHelper
46  {
47      File testBasedir;
48  
49      File repoDir;
50  
51      File indexDir;
52  
53      String repositoryId = "test";
54  
55      String repositoryUrl = "http://repo1.maven.org/maven2/";
56  
57      NexusIndexer indexer;
58  
59      IndexUpdater updater;
60  
61      IndexPacker packer;
62  
63      IndexingContext context;
64  
65      @Override
66      public void setUp()
67          throws Exception
68      {
69          super.setUp();
70  
71          testBasedir = new File( getBasedir(), "/target/indexUpdater" );
72          testBasedir.mkdirs();
73  
74          repoDir = new File( getBasedir(), "/target/indexUpdaterRepoDir" );
75          repoDir.mkdirs();
76  
77          indexDir = super.getDirectory( "indexerUpdater" );
78          indexDir.mkdirs();
79  
80          indexer = lookup( NexusIndexer.class );
81  
82          updater = lookup( IndexUpdater.class );
83  
84          packer = lookup( IndexPacker.class );
85  
86          context =
87              indexer.addIndexingContext( repositoryId, repositoryId, repoDir, indexDir, repositoryUrl, null,
88                  MIN_CREATORS );
89      }
90  
91      @Override
92      public void tearDown()
93          throws Exception
94      {
95          super.tearDown();
96  
97          // this one closes it too
98          indexer.removeIndexingContext( context, true );
99  
100         FileUtils.forceDelete( testBasedir );
101 
102         FileUtils.forceDelete( repoDir );
103 
104         FileUtils.forceDelete( indexDir );
105     }
106 
107     protected ArtifactContext createArtifactContext( String repositoryId, String groupId, String artifactId,
108                                                      String version, String classifier )
109    {
110         String path = createPath( groupId, artifactId, version, classifier );
111         File pomFile = new File( path + ".pom" );
112         File artifact = new File( path + ".jar" );
113         File metadata = null;
114         ArtifactInfo artifactInfo = new ArtifactInfo( repositoryId, groupId, artifactId, version, classifier, "jar");
115         Gav gav =
116             new Gav( groupId, artifactId, version, classifier, "jar", null, null, artifact.getName(), false,
117                 null, false, null );
118         return new ArtifactContext( pomFile, artifact, metadata, artifactInfo, gav );
119     }
120 
121     protected String createPath( String groupId, String artifactId, String version, String classifier )
122     {
123         return "/" + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version
124             + ( classifier == null ? "" : "-" + classifier );
125     }
126 
127     protected void packIndex( File targetDir, IndexingContext context )
128         throws IllegalArgumentException, IOException
129     {
130         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
131         try
132         {
133             IndexPackingRequest request = new IndexPackingRequest( context, indexSearcher.getIndexReader(), targetDir );
134             request.setUseTargetProperties( true );
135             packer.packIndex( request );
136         } finally
137         {
138             context.releaseIndexSearcher( indexSearcher );
139         }
140     }
141 
142     protected void searchFor( String groupId, int expected, IndexingContext context )
143         throws IOException, Exception
144     {
145         Query q = indexer.constructQuery( MAVEN.GROUP_ID, groupId, SearchType.EXACT );
146 
147         IteratorSearchRequest req;
148 
149         if ( context != null )
150         {
151             req = new IteratorSearchRequest( q, context );
152         }
153         else
154         {
155             req = new IteratorSearchRequest( q );
156         }
157 
158         IteratorSearchResponse response = indexer.searchIterator( req );
159 
160         ArrayList<ArtifactInfo> ais = new ArrayList<>( response.getTotalHits() );
161 
162         for ( ArtifactInfo ai : response )
163         {
164             ais.add( ai );
165         }
166 
167         assertEquals( ais.toString(), expected, ais.size() );
168     }
169 
170 }