2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.jetbrains
.idea
.maven
.indices
;
18 import org
.apache
.lucene
.document
.Document
;
19 import org
.apache
.lucene
.index
.IndexReader
;
20 import org
.apache
.lucene
.index
.Term
;
21 import org
.apache
.lucene
.search
.*;
22 import org
.apache
.maven
.artifact
.manager
.WagonManager
;
23 import org
.apache
.maven
.wagon
.events
.TransferEvent
;
24 import org
.apache
.maven
.wagon
.events
.TransferListener
;
25 import org
.jetbrains
.idea
.maven
.embedder
.MavenEmbedderFactory
;
26 import org
.jetbrains
.idea
.maven
.embedder
.MavenEmbedderWrapper
;
27 import org
.sonatype
.nexus
.index
.*;
28 import org
.sonatype
.nexus
.index
.context
.IndexingContext
;
29 import org
.sonatype
.nexus
.index
.updater
.IndexUpdateRequest
;
30 import org
.sonatype
.nexus
.index
.updater
.IndexUpdater
;
33 import java
.io
.IOException
;
34 import java
.util
.ArrayList
;
35 import java
.util
.Collection
;
36 import java
.util
.List
;
38 public class NexusIndexerTest
extends MavenIndicesTestCase
{
39 private MavenCustomRepositoryHelper myRepositoryHelper
;
40 private MavenEmbedderWrapper myEmbedder
;
41 private NexusIndexer myIndexer
;
42 private IndexUpdater myUpdater
;
43 private File myIndexDir
;
46 public void setUp() throws Exception
{
48 myRepositoryHelper
= new MavenCustomRepositoryHelper(myDir
, "local1_index", "local1", "remote");
50 myEmbedder
= MavenEmbedderFactory
.createEmbedder(getMavenGeneralSettings());
52 myIndexer
= myEmbedder
.getComponent(NexusIndexer
.class);
53 myUpdater
= myEmbedder
.getComponent(IndexUpdater
.class);
55 assertNotNull(myIndexer
);
56 assertNotNull(myUpdater
);
58 myIndexDir
= new File(myDir
, "index");
59 assertNotNull(myIndexDir
);
63 protected void tearDown() throws Exception
{
64 for (IndexingContext c
: myIndexer
.getIndexingContexts().values()) {
65 myIndexer
.removeIndexingContext(c
, false);
71 public void testSeraching() throws Exception
{
72 addContext("local1", new File(myRepositoryHelper
.getTestDataPath("local1_index")), null, null);
76 public void testUpdatingLocal() throws Exception
{
77 IndexingContext c
= addContext("local1", myIndexDir
, new File(myRepositoryHelper
.getTestDataPath("local1")), null);
78 myIndexer
.scan(c
, new NullScanningListener());
83 public void testDownloading() throws Exception
{
85 String url
= "file:///" + myRepositoryHelper
.getTestDataPath("remote");
86 IndexingContext c
= addContext(id
, myIndexDir
, null, url
);
88 IndexUpdateRequest request
= new IndexUpdateRequest(c
);
89 request
.setResourceFetcher(new MavenIndexFetcher(id
, url
, myEmbedder
.getComponent(WagonManager
.class), new NullTransferListener()));
90 myUpdater
.fetchAndUpdateIndex(request
);
95 public void testAddingArtifacts() throws Exception
{
96 IndexingContext c
= addContext("virtual", myIndexDir
, null, null);
100 ArtifactInfo ai
= new ArtifactInfo(c
.getRepositoryId(), "group", "id", "version", null);
101 ArtifactContext a
= new ArtifactContext(new File(myProjectPom
.getPath()), null, null, ai
, null);
103 myIndexer
.addArtifactToIndex(a
, c
);
105 Query q
= new TermQuery(new Term(ArtifactInfo
.GROUP_ID
, "group"));
106 Collection
<ArtifactInfo
> result
= myIndexer
.searchFlat(ArtifactInfo
.VERSION_COMPARATOR
, q
);
108 assertEquals(1, result
.size());
110 ArtifactInfo found
= new ArrayList
<ArtifactInfo
>(result
).get(0);
111 assertEquals("group", found
.groupId
);
112 assertEquals("id", found
.artifactId
);
113 assertEquals("version", found
.version
);
115 IndexReader r
= c
.getIndexReader();
116 for (int i
= 0; i
< r
.numDocs(); i
++) {
117 Document d
= r
.document(i
);
121 public void testIteratingAddedArtifacts() throws Exception
{
122 if (ignore()) return;
124 IndexingContext c
= addContext("virtual", myIndexDir
, null, null);
126 addArtifact(c
, "group1", "id1", "version1", "x:/path1");
127 addArtifact(c
, "group2", "id2", "version2", "x:/path2");
128 addArtifact(c
, "group3", "id3", "version3", "x:/path3");
130 IndexReader r
= c
.getIndexReader();
131 assertEquals(5, r
.numDocs());
132 List
<String
> result
= new ArrayList
<String
>();
133 for (int i
= 0; i
< r
.numDocs(); i
++) {
134 // third document becomes deleted somehow...
135 Document d
= r
.document(i
);
136 String uinfo
= d
.get(ArtifactInfo
.UINFO
);
139 System
.out
.println(result
);
142 public void testSearchingWithLucene() throws Exception
{
143 IndexSearcher searcher
= new IndexSearcher(myRepositoryHelper
.getTestDataPath("local1_index"));
144 Hits result
= searcher
.search(new TermQuery(new Term(ArtifactInfo
.GROUP_ID
, "junit")));
146 assertEquals(3, result
.length());
151 public void testAddingTwoContextsWithSameId() throws Exception
{
152 IndexingContext i1
= addContext("id", new File(myIndexDir
, "one"), null, null);
153 IndexingContext i2
= addContext("id", new File(myIndexDir
, "two"), null, null);
155 myIndexer
.removeIndexingContext(i1
, false);
156 myIndexer
.removeIndexingContext(i2
, false);
158 addContext("id", new File(myIndexDir
, "one"), null, null);
159 addContext("id", new File(myIndexDir
, "two"), null, null);
162 private void addArtifact(IndexingContext c
, String groupId
, String artifactId
, String version
, String path
) throws IOException
{
163 ArtifactInfo ai
= new ArtifactInfo(c
.getRepositoryId(), groupId
, artifactId
, version
, null);
165 ai
.lastModified
= -1;
166 ai
.sourcesExists
= ArtifactAvailablility
.fromString(Integer
.toString(0));
167 ai
.javadocExists
= ArtifactAvailablility
.fromString(Integer
.toString(0));
169 ArtifactContext a
= new ArtifactContext(new File(path
), null, null, ai
, null);
170 myIndexer
.addArtifactToIndex(a
, c
);
173 private IndexingContext
addContext(String id
, File indexDir
, File repoDir
, String repoUrl
) throws Exception
{
174 return myIndexer
.addIndexingContext(
180 null, // repo update url
181 NexusIndexer
.FULL_INDEX
);
184 private void assertSearchWorks() throws IOException
{
185 WildcardQuery q
= new WildcardQuery(new Term(ArtifactInfo
.ARTIFACT_ID
, "junit*"));
186 Collection
<ArtifactInfo
> result
= myIndexer
.searchFlat(ArtifactInfo
.VERSION_COMPARATOR
, q
);
187 assertEquals(3, result
.size());
190 private static class NullScanningListener
implements ArtifactScanningListener
{
191 public void scanningStarted(IndexingContext indexingContext
) {
194 public void scanningFinished(IndexingContext indexingContext
, ScanningResult scanningResult
) {
197 public void artifactError(ArtifactContext artifactContext
, Exception e
) {
200 public void artifactDiscovered(ArtifactContext artifactContext
) {
204 private static class NullTransferListener
implements TransferListener
{
205 public void transferInitiated(TransferEvent event
) {
208 public void transferStarted(TransferEvent event
) {
211 public void transferProgress(TransferEvent event
, byte[] bytes
, int i
) {
214 public void transferCompleted(TransferEvent event
) {
217 public void transferError(TransferEvent event
) {
220 public void debug(String s
) {