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.Date;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  
32  import org.apache.lucene.index.Term;
33  import org.apache.lucene.search.FilteredQuery;
34  import org.apache.lucene.search.PrefixQuery;
35  import org.apache.lucene.search.Query;
36  import org.apache.lucene.search.QueryWrapperFilter;
37  import org.apache.lucene.search.TermQuery;
38  import org.apache.lucene.store.Directory;
39  import org.apache.lucene.store.FSDirectory;
40  import org.apache.maven.index.ArtifactInfo;
41  import org.apache.maven.index.FlatSearchRequest;
42  import org.apache.maven.index.FlatSearchResponse;
43  import org.apache.maven.index.NexusIndexer;
44  import org.apache.maven.index.context.IndexingContext;
45  import org.apache.maven.index.packer.DefaultIndexPacker;
46  import org.apache.maven.index.updater.DefaultIndexUpdater;
47  
48  public class DefaultIndexNexusIndexerTest
49      extends MinimalIndexNexusIndexerTest
50  {
51      @Override
52      protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
53          throws Exception
54      {
55          context =
56              nexusIndexer.addIndexingContext( "test-default", "test", repo, indexDir, null, null, DEFAULT_CREATORS );
57  
58          assertNull( context.getTimestamp() ); // unknown upon creation
59  
60          nexusIndexer.scan( context );
61  
62          assertNotNull( context.getTimestamp() );
63      }
64  
65      public void testPlugin()
66          throws Exception
67      {
68          // String term = "plugin";
69          // String term = "maven-core-it-plugin";
70          String term = "org.apache.maven.plugins";
71  
72          // Query bq = new TermQuery(new Term(ArtifactInfo.GROUP_ID, "org.apache.maven.plugins"));
73          // Query bq = new TermQuery(new Term(ArtifactInfo.ARTIFACT_ID, term));
74          Query bq = new PrefixQuery( new Term( ArtifactInfo.GROUP_ID, term ) );
75          // BooleanQuery bq = new BooleanQuery();
76          // bq.add(new PrefixQuery(new Term(ArtifactInfo.GROUP_ID, term + "*")), Occur.SHOULD);
77          // bq.add(new PrefixQuery(new Term(ArtifactInfo.ARTIFACT_ID, term + "*")), Occur.SHOULD);
78          TermQuery tq = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-plugin" ) );
79          Query query = new FilteredQuery( tq, new QueryWrapperFilter( bq ) );
80  
81          FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
82  
83          Collection<ArtifactInfo> r = response.getResults();
84  
85          assertEquals( r.toString(), 1, r.size() );
86  
87          ArtifactInfo ai = r.iterator().next();
88  
89          assertEquals( "org.apache.maven.plugins", ai.groupId );
90          assertEquals( "maven-core-it-plugin", ai.artifactId );
91          assertEquals( "core-it", ai.prefix );
92  
93          List<String> goals = ai.goals;
94          assertEquals( 14, goals.size() );
95          assertEquals( "catch", goals.get( 0 ) );
96          assertEquals( "fork", goals.get( 1 ) );
97          assertEquals( "fork-goal", goals.get( 2 ) );
98          assertEquals( "touch", goals.get( 3 ) );
99          assertEquals( "setter-touch", goals.get( 4 ) );
100         assertEquals( "generate-envar-properties", goals.get( 5 ) );
101         assertEquals( "generate-properties", goals.get( 6 ) );
102         assertEquals( "loadable", goals.get( 7 ) );
103         assertEquals( "light-touch", goals.get( 8 ) );
104         assertEquals( "package", goals.get( 9 ) );
105         assertEquals( "reachable", goals.get( 10 ) );
106         assertEquals( "runnable", goals.get( 11 ) );
107         assertEquals( "throw", goals.get( 12 ) );
108         assertEquals( "tricky-params", goals.get( 13 ) );
109     }
110 
111     public void testPluginPackaging()
112         throws Exception
113     {
114         Query query = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-plugin" ) );
115         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
116         // repo contains 3 artifacts with packaging "maven-plugin", but one of the is actually an archetype!
117         assertEquals( response.getResults().toString(), 2, response.getTotalHits() );
118     }
119 
120     public void testSearchArchetypes()
121         throws Exception
122     {
123         // TermQuery tq = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype"));
124         // BooleanQuery bq = new BooleanQuery();
125         // bq.add(new WildcardQuery(new Term(ArtifactInfo.GROUP_ID, term + "*")), Occur.SHOULD);
126         // bq.add(new WildcardQuery(new Term(ArtifactInfo.ARTIFACT_ID, term + "*")), Occur.SHOULD);
127         // FilteredQuery query = new FilteredQuery(tq, new QueryWrapperFilter(bq));
128 
129         Query q = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-archetype" ) );
130         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
131         Collection<ArtifactInfo> r = response.getResults();
132 
133         assertEquals( 4, r.size() );
134 
135         Iterator<ArtifactInfo> it = r.iterator();
136         {
137             ArtifactInfo ai = it.next();
138             assertEquals( "org.apache.directory.server", ai.groupId );
139             assertEquals( "apacheds-schema-archetype", ai.artifactId );
140             assertEquals( "1.0.2", ai.version );
141         }
142         {
143             ArtifactInfo ai = it.next();
144             assertEquals( "org.apache.servicemix.tooling", ai.groupId );
145             assertEquals( "servicemix-service-engine", ai.artifactId );
146             assertEquals( "3.1", ai.version );
147         }
148         {
149             ArtifactInfo ai = it.next();
150             assertEquals( "org.terracotta.maven.archetypes", ai.groupId );
151             assertEquals( "pojo-archetype", ai.artifactId );
152             assertEquals( "1.0.3", ai.version );
153         }
154         {
155             ArtifactInfo ai = it.next();
156             assertEquals( "proptest", ai.groupId );
157             assertEquals( "proptest-archetype", ai.artifactId );
158             assertEquals( "1.0", ai.version );
159         }
160     }
161 
162     public void testIndexTimestamp()
163         throws Exception
164     {
165         ByteArrayOutputStream os = new ByteArrayOutputStream();
166 
167         DefaultIndexPacker.packIndexArchive( context, os );
168 
169         Thread.sleep( 1000L );
170 
171         File newIndex = new File( getBasedir(), "target/test-new" );
172 
173         Directory newIndexDir = FSDirectory.open( newIndex );
174 
175         DefaultIndexUpdater.unpackIndexArchive( new ByteArrayInputStream( os.toByteArray() ), newIndexDir, context );
176 
177         IndexingContext newContext =
178             nexusIndexer.addIndexingContext( "test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS );
179 
180         assertEquals( context.getTimestamp().getTime(), newContext.getTimestamp().getTime() );
181 
182         assertEquals( context.getTimestamp(), newContext.getTimestamp() );
183 
184         // make sure context has the same artifacts
185 
186         Query query = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "qdox", SearchType.SCORED );
187 
188         FlatSearchRequest request = new FlatSearchRequest( query, newContext );
189         FlatSearchResponse response = nexusIndexer.searchFlat( request );
190         Collection<ArtifactInfo> r = response.getResults();
191 
192         assertEquals( 2, r.size() );
193 
194         List<ArtifactInfo> list = new ArrayList<ArtifactInfo>( r );
195 
196         assertEquals( 2, list.size() );
197 
198         ArtifactInfo ai = list.get( 0 );
199 
200         assertEquals( "1.6.1", ai.version );
201 
202         ai = list.get( 1 );
203 
204         assertEquals( "1.5", ai.version );
205 
206         assertEquals( "test", ai.repository );
207 
208         Date timestamp = newContext.getTimestamp();
209 
210         newContext.close( false );
211 
212         newIndexDir = FSDirectory.open( newIndex );
213 
214         DefaultIndexUpdater.unpackIndexArchive( new ByteArrayInputStream( os.toByteArray() ), newIndexDir, context );
215 
216         newContext =
217             nexusIndexer.addIndexingContext( "test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS );
218 
219         assertEquals( timestamp, newContext.getTimestamp() );
220 
221         newContext.close( true );
222 
223         assertFalse( new File( newIndex, "timestamp" ).exists() );
224     }
225 
226     public void testArchetype()
227         throws Exception
228     {
229         String term = "proptest";
230 
231         Query bq = new PrefixQuery( new Term( ArtifactInfo.GROUP_ID, term ) );
232         TermQuery tq = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-archetype" ) );
233         Query query = new FilteredQuery( tq, new QueryWrapperFilter( bq ) );
234 
235         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
236 
237         Collection<ArtifactInfo> r = response.getResults();
238 
239         assertEquals( r.toString(), 1, r.size() );
240     }
241 
242     public void testArchetypePackaging()
243         throws Exception
244     {
245         Query query = new TermQuery( new Term( ArtifactInfo.PACKAGING, "maven-archetype" ) );
246         FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
247         assertEquals( response.getResults().toString(), 4, response.getTotalHits() );
248     }
249 
250     public void testBrokenJar()
251         throws Exception
252     {
253         Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "brokenjar", SearchType.SCORED );
254 
255         FlatSearchRequest searchRequest = new FlatSearchRequest( q );
256 
257         FlatSearchResponse response = nexusIndexer.searchFlat( searchRequest );
258 
259         Set<ArtifactInfo> r = response.getResults();
260 
261         assertEquals( r.toString(), 1, r.size() );
262 
263         ArtifactInfo ai = r.iterator().next();
264 
265         assertEquals( "brokenjar", ai.groupId );
266         assertEquals( "brokenjar", ai.artifactId );
267         assertEquals( "1.0", ai.version );
268         assertEquals( null, ai.classNames );
269     }
270 
271     public void testMissingPom()
272         throws Exception
273     {
274         Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "missingpom", SearchType.SCORED );
275 
276         FlatSearchRequest searchRequest = new FlatSearchRequest( q );
277 
278         FlatSearchResponse response = nexusIndexer.searchFlat( searchRequest );
279 
280         Set<ArtifactInfo> r = response.getResults();
281 
282         assertEquals( r.toString(), 1, r.size() );
283 
284         ArtifactInfo ai = r.iterator().next();
285 
286         assertEquals( "missingpom", ai.groupId );
287         assertEquals( "missingpom", ai.artifactId );
288         assertEquals( "1.0", ai.version );
289         // See Nexus 2318. It should be null for a jar without classes
290         assertNull( ai.classNames );
291     }
292 
293 }