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