View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index.updater;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.lucene.search.IndexSearcher;
26  import org.apache.lucene.search.Query;
27  import org.apache.maven.index.AbstractIndexCreatorHelper;
28  import org.apache.maven.index.ArtifactContext;
29  import org.apache.maven.index.ArtifactInfo;
30  import org.apache.maven.index.IteratorSearchRequest;
31  import org.apache.maven.index.IteratorSearchResponse;
32  import org.apache.maven.index.MAVEN;
33  import org.apache.maven.index.NexusIndexer;
34  import org.apache.maven.index.SearchType;
35  import org.apache.maven.index.artifact.Gav;
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.codehaus.plexus.util.FileUtils;
40  
41  import static org.junit.Assert.assertEquals;
42  
43  public abstract class AbstractIndexUpdaterTest extends AbstractIndexCreatorHelper {
44      File testBasedir;
45  
46      File repoDir;
47  
48      File indexDir;
49  
50      String repositoryId = "test";
51  
52      String repositoryUrl = "http://repo1.maven.org/maven2/";
53  
54      NexusIndexer indexer;
55  
56      IndexUpdater updater;
57  
58      IndexPacker packer;
59  
60      IndexingContext context;
61  
62      @Override
63      public void setUp() throws Exception {
64          super.setUp();
65  
66          testBasedir = new File(getBasedir(), "/target/indexUpdater");
67          testBasedir.mkdirs();
68  
69          repoDir = new File(getBasedir(), "/target/indexUpdaterRepoDir");
70          repoDir.mkdirs();
71  
72          indexDir = super.getDirectory("indexerUpdater");
73          indexDir.mkdirs();
74  
75          indexer = lookup(NexusIndexer.class);
76  
77          updater = lookup(IndexUpdater.class);
78  
79          packer = lookup(IndexPacker.class);
80  
81          context = indexer.addIndexingContext(
82                  repositoryId, repositoryId, repoDir, indexDir, repositoryUrl, null, MIN_CREATORS);
83      }
84  
85      @Override
86      public void tearDown() throws Exception {
87          super.tearDown();
88  
89          // this one closes it too
90          indexer.removeIndexingContext(context, true);
91  
92          FileUtils.forceDelete(testBasedir);
93  
94          FileUtils.forceDelete(repoDir);
95  
96          FileUtils.forceDelete(indexDir);
97      }
98  
99      protected ArtifactContext createArtifactContext(
100             String repositoryId, String groupId, String artifactId, String version, String classifier) {
101         String path = createPath(groupId, artifactId, version, classifier);
102         File pomFile = new File(path + ".pom");
103         File artifact = new File(path + ".jar");
104         File metadata = null;
105         ArtifactInfo artifactInfo = new ArtifactInfo(repositoryId, groupId, artifactId, version, classifier, "jar");
106         Gav gav = new Gav(
107                 groupId,
108                 artifactId,
109                 version,
110                 classifier,
111                 "jar",
112                 null,
113                 null,
114                 artifact.getName(),
115                 false,
116                 null,
117                 false,
118                 null);
119         return new ArtifactContext(pomFile, artifact, metadata, artifactInfo, gav);
120     }
121 
122     protected String createPath(String groupId, String artifactId, String version, String classifier) {
123         return "/" + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version
124                 + (classifier == null ? "" : "-" + classifier);
125     }
126 
127     protected void packIndex(File targetDir, IndexingContext context) throws IllegalArgumentException, IOException {
128         final IndexSearcher indexSearcher = context.acquireIndexSearcher();
129         try {
130             IndexPackingRequest request = new IndexPackingRequest(context, indexSearcher.getIndexReader(), targetDir);
131             request.setUseTargetProperties(true);
132             packer.packIndex(request);
133         } finally {
134             context.releaseIndexSearcher(indexSearcher);
135         }
136     }
137 
138     protected void searchFor(String groupId, int expected, IndexingContext context) throws IOException, Exception {
139         Query q = indexer.constructQuery(MAVEN.GROUP_ID, groupId, SearchType.EXACT);
140 
141         IteratorSearchRequest req;
142 
143         if (context != null) {
144             req = new IteratorSearchRequest(q, context);
145         } else {
146             req = new IteratorSearchRequest(q);
147         }
148 
149         IteratorSearchResponse response = indexer.searchIterator(req);
150 
151         ArrayList<ArtifactInfo> ais = new ArrayList<>(response.getTotalHits());
152 
153         for (ArtifactInfo ai : response) {
154             ais.add(ai);
155         }
156 
157         assertEquals(ais.toString(), expected, ais.size());
158     }
159 }