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