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