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