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.File;
23  import java.nio.file.Files;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.lucene.index.Term;
31  import org.apache.lucene.search.IndexSearcher;
32  import org.apache.lucene.search.Query;
33  import org.apache.lucene.search.TermQuery;
34  import org.apache.lucene.store.ByteBuffersDirectory;
35  import org.apache.lucene.store.Directory;
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.apache.maven.index.search.grouping.GAGrouping;
40  import org.apache.maven.index.updater.DefaultIndexUpdater;
41  import org.apache.maven.index.updater.IndexUpdateRequest;
42  import org.apache.maven.index.updater.IndexUpdater;
43  import org.junit.Test;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertTrue;
48  
49  /** http://issues.sonatype.org/browse/NEXUS-13 */
50  public class Nexus13NexusIndexerTest
51      extends AbstractNexusIndexerTest
52  {
53      protected File repo = new File( getBasedir(), "src/test/nexus-13" );
54  
55      @Override
56      protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
57          throws Exception
58      {
59          context = nexusIndexer.addIndexingContext( "nexus-13", "nexus-13", repo, indexDir, null, null, FULL_CREATORS );
60          nexusIndexer.scan( context );
61      }
62  
63      @Test
64      public void testSearchGroupedClasses()
65          throws Exception
66      {
67          {
68              Query q = nexusIndexer.constructQuery( MAVEN.CLASSNAMES, "cisco", SearchType.SCORED );
69  
70              GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
71              GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
72              Map<String, ArtifactInfoGroup> r = response.getResults();
73              assertEquals( r.toString(), 4, r.size() );
74  
75              assertTrue( r.containsKey( "cisco.infra.dft : dma.plugin.utils" ) );
76              assertTrue( r.containsKey( "cisco.infra.dft : dma.pom.enforcer" ) );
77              assertTrue( r.containsKey( "cisco.infra.dft : maven-dma-mgmt-plugin" ) );
78              assertTrue( r.containsKey( "cisco.infra.dft : maven-dma-plugin" ) );
79          }
80  
81          {
82              Query q = nexusIndexer.constructQuery( MAVEN.CLASSNAMES, "dft.plugin.utils", SearchType.SCORED );
83              GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
84              GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
85              Map<String, ArtifactInfoGroup> r = response.getResults();
86              assertEquals( r.toString(), 1, r.size() );
87  
88              assertTrue( r.containsKey( "cisco.infra.dft : dma.plugin.utils" ) );
89              assertEquals( "cisco.infra.dft : dma.plugin.utils",
90                  r.get( "cisco.infra.dft : dma.plugin.utils" ).getGroupKey() );
91          }
92      }
93  
94      @Test
95      public void testSearchArchetypes()
96          throws Exception
97      {
98          Query q = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-archetype" ) );
99  
100         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
101         Collection<ArtifactInfo> r = response.getResults();
102         assertEquals( r.toString(), 1, r.size() );
103 
104         ArtifactInfo ai = r.iterator().next();
105         assertEquals( "cisco.infra.dft", ai.getGroupId() );
106         assertEquals( "archetype.sdf", ai.getArtifactId() );
107         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
108     }
109 
110     @Test
111     public void testIndexTimestamp()
112         throws Exception
113     {
114         final File targetDir = Files.createTempDirectory( "testIndexTimestamp" ).toFile();
115         targetDir.deleteOnExit();
116 
117         final IndexPacker indexPacker = lookup( IndexPacker.class );
118         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
119         try
120         {
121             final IndexPackingRequest request =
122                 new IndexPackingRequest( context, indexSearcher.getIndexReader(), targetDir );
123             indexPacker.packIndex( request );
124         }
125         finally
126         {
127             context.releaseIndexSearcher( indexSearcher );
128         }
129 
130         Thread.sleep( 1000L );
131 
132         Directory indexDir = new ByteBuffersDirectory();
133 
134         IndexingContext newContext =
135             nexusIndexer.addIndexingContext( "test-new", "nexus-13", null, indexDir, null, null, DEFAULT_CREATORS );
136 
137         final IndexUpdater indexUpdater = lookup( IndexUpdater.class );
138         final IndexUpdateRequest updateRequest = new IndexUpdateRequest( newContext, new DefaultIndexUpdater.FileFetcher( targetDir ) );
139         indexUpdater.fetchAndUpdateIndex( updateRequest );
140 
141         assertEquals( 0, newContext.getTimestamp().getTime() - context.getTimestamp().getTime() );
142 
143         assertEquals( context.getTimestamp(), newContext.getTimestamp() );
144 
145         // make sure context has the same artifacts
146 
147         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "cisco", SearchType.SCORED );
148 
149         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q, newContext ) );
150         Collection<ArtifactInfo> r = response.getResults();
151 
152         assertEquals( 10, r.size() );
153 
154         List<ArtifactInfo> list = new ArrayList<>( r );
155 
156         assertEquals( 10, list.size() );
157 
158         ArtifactInfo ai = list.get( 0 );
159 
160         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
161 
162         ai = list.get( 1 );
163 
164         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
165 
166         assertEquals( "nexus-13", ai.getRepository() );
167 
168         newContext.close( true );
169     }
170 
171     @Test
172     public void testRootGroups()
173         throws Exception
174     {
175         Set<String> rootGroups = context.getRootGroups();
176         assertEquals( rootGroups.toString(), 1, rootGroups.size() );
177 
178         assertGroup( 10, "cisco", context );
179     }
180 
181     @Test
182     public void testSearchFlat()
183         throws Exception
184     {
185         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "cisco.infra", SearchType.SCORED );
186 
187         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
188         Collection<ArtifactInfo> r = response.getResults();
189         assertEquals( r.toString(), 10, r.size() );
190 
191         List<ArtifactInfo> list = new ArrayList<>( r );
192 
193         assertEquals( 10, list.size() );
194 
195         ArtifactInfo ai = list.get( 0 );
196 
197         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
198 
199         ai = list.get( 1 );
200 
201         assertEquals( "nexus-13", ai.getRepository() );
202 
203     }
204 
205     @Test
206     public void testSearchGrouped()
207         throws Exception
208     {
209         // ----------------------------------------------------------------------------
210         //
211         // ----------------------------------------------------------------------------
212         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "cisco.infra", SearchType.SCORED );
213 
214         GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
215         GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
216         Map<String, ArtifactInfoGroup> r = response.getResults();
217         assertEquals( 8, r.size() );
218 
219         ArtifactInfoGroup ig = r.values().iterator().next();
220 
221         assertEquals( "cisco.infra.dft : archetype.sdf", ig.getGroupKey() );
222 
223         assertEquals( 1, ig.getArtifactInfos().size() );
224 
225         List<ArtifactInfo> list = new ArrayList<>( ig.getArtifactInfos() );
226 
227         assertEquals( 1, list.size() );
228 
229         ArtifactInfo ai = list.get( 0 );
230 
231         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
232     }
233 
234     @Test
235     public void testSearchGroupedProblematicNames()
236         throws Exception
237     {
238 
239         // ----------------------------------------------------------------------------
240         // Artifacts with "problematic" names
241         // ----------------------------------------------------------------------------
242 
243         Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "dma.integr*", SearchType.SCORED );
244 
245         GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
246         GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
247         Map<String, ArtifactInfoGroup> r = response.getResults();
248 
249         assertEquals( 1, r.size() );
250 
251         ArtifactInfoGroup ig = r.values().iterator().next();
252 
253         assertEquals( "cisco.infra.dft : dma.integration.tests", ig.getGroupKey() );
254 
255         assertEquals( 1, ig.getArtifactInfos().size() );
256     }
257 
258     @Test
259     public void testIdentify()
260         throws Exception
261     {
262         Collection<ArtifactInfo> ais = nexusIndexer.identify( MAVEN.SHA1, "c8a2ef9d92a4b857eae0f36c2e01481787c5cbf8" );
263 
264         assertEquals( 1, ais.size() );
265 
266         ArtifactInfo ai = ais.iterator().next();
267 
268         assertNotNull( ai );
269 
270         assertEquals( "cisco.infra.dft", ai.getGroupId() );
271 
272         assertEquals( "dma.plugin.utils", ai.getArtifactId() );
273 
274         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
275 
276         // Using a file
277 
278         File artifact =
279             new File( repo,
280                 "cisco/infra/dft/maven-dma-mgmt-plugin/1.0-SNAPSHOT/maven-dma-mgmt-plugin-1.0-20080409.022326-2.jar" );
281 
282         ais = nexusIndexer.identify( artifact );
283         
284         assertEquals( 1, ais.size() );
285 
286         ai = ais.iterator().next();
287 
288         assertNotNull( ai );
289 
290         assertEquals( "cisco.infra.dft", ai.getGroupId() );
291 
292         assertEquals( "maven-dma-mgmt-plugin", ai.getArtifactId() );
293 
294         assertEquals( "1.0-SNAPSHOT", ai.getVersion() );
295     }
296 }