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