View Javadoc

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