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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Set;
24  
25  import org.apache.lucene.index.Term;
26  import org.apache.lucene.search.WildcardQuery;
27  import org.apache.maven.index.artifact.Gav;
28  import org.apache.maven.index.artifact.M2GavCalculator;
29  import org.apache.maven.index.context.IndexingContext;
30  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.junit.Test;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  public class Nexus3293TimestampSnapshotTest extends AbstractIndexCreatorHelper {
37      private IndexingContext context;
38  
39      private NexusIndexer prepare() throws Exception, IOException, UnsupportedExistingLuceneIndexException {
40          NexusIndexer indexer = lookup(NexusIndexer.class);
41  
42          File indexDir = new File(getBasedir(), "target/index/test-" + System.currentTimeMillis());
43          FileUtils.deleteDirectory(indexDir);
44  
45          File repo = new File(getBasedir(), "src/test/nexus-3293");
46          repo.mkdirs();
47  
48          context = indexer.addIndexingContext("test", "test", repo, indexDir, null, null, DEFAULT_CREATORS);
49  
50          // IndexReader indexReader = context.getIndexSearcher().getIndexReader();
51          // int numDocs = indexReader.numDocs();
52          // for ( int i = 0; i < numDocs; i++ )
53          // {
54          // Document doc = indexReader.document( i );
55          // System.err.println( i + " : " + doc.get( ArtifactInfo.UINFO));
56          //
57          // }
58          return indexer;
59      }
60  
61      @Test
62      public void test_nexus_3293_releaseJar() throws Exception {
63          NexusIndexer indexer = prepare();
64  
65          File artifact = new File(getBasedir(), "src/test/nexus-3293/aopalliance/aopalliance/1.0/aopalliance-1.0jar");
66  
67          File pom = new File(getBasedir(), "src/test/nexus-3293/aopalliance/aopalliance/1.0/aopalliance-1.0.pom");
68  
69          ArtifactInfo artifactInfo = new ArtifactInfo("test", "aopalliance", "aopalliance", "1.0-SNAPSHOT", null, "jar");
70  
71          M2GavCalculator gavCalc = new M2GavCalculator();
72  
73          Gav jarGav = gavCalc.pathToGav("aopalliance/aopalliance/1.0/aopalliance-1.0.jar");
74          Gav pomGav = gavCalc.pathToGav("aopalliance/aopalliance/1.0/aopalliance-1.0.pom");
75  
76          ArtifactContext artifactContext = new ArtifactContext(pom, artifact, null, artifactInfo, jarGav);
77  
78          indexer.addArtifactToIndex(artifactContext, context);
79  
80          validateIndexContents(indexer);
81  
82          artifactContext = new ArtifactContext(pom, artifact, null, artifactInfo, jarGav);
83  
84          indexer.addArtifactToIndex(artifactContext, context);
85  
86          validateIndexContents(indexer);
87      }
88  
89      @Test
90      public void test_nexus_3293_indexTimestampedSnapshotJar() throws Exception {
91          NexusIndexer indexer = prepare();
92  
93          File artifact = new File(
94                  getBasedir(),
95                  "src/test/nexus-3293/aopalliance/aopalliance/1.0-SNAPSHOT/aopalliance-1.0-20100517.210215-13.jar");
96  
97          File pom = new File(
98                  getBasedir(),
99                  "src/test/nexus-3293/aopalliance/aopalliance/1.0-SNAPSHOT/aopalliance-1.0-20100517.210215-13.pom");
100 
101         ArtifactContextProducer artifactContextProducer = lookup(ArtifactContextProducer.class);
102 
103         ArtifactContext artifactContext = artifactContextProducer.getArtifactContext(context, artifact);
104 
105         indexer.addArtifactToIndex(artifactContext, context);
106 
107         validateIndexContents(indexer);
108 
109         artifactContext = artifactContextProducer.getArtifactContext(context, pom);
110 
111         indexer.addArtifactToIndex(artifactContext, context);
112 
113         validateIndexContents(indexer);
114     }
115 
116     private void validateIndexContents(NexusIndexer indexer) throws Exception {
117         WildcardQuery q = new WildcardQuery(new Term(ArtifactInfo.UINFO, "*aopalliance*"));
118 
119         FlatSearchRequest request = new FlatSearchRequest(q);
120         request.getContexts().add(context);
121 
122         FlatSearchResponse response = indexer.searchFlat(request);
123         Set<ArtifactInfo> r = response.getResults();
124         assertEquals(r.toString(), 1, r.size());
125 
126         ArtifactInfo ai = r.iterator().next();
127 
128         assertEquals("jar", ai.getPackaging());
129         assertEquals("jar", ai.getFileExtension());
130     }
131 }