1 package org.apache.maven.archiva.consumers.lucene;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
29 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
30 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
31 import org.apache.maven.archiva.indexer.search.SearchResults;
32 import org.apache.maven.archiva.model.ArchivaArtifact;
33 import org.apache.maven.archiva.model.ArchivaArtifactModel;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 public class IndexJavaPublicMethodsConsumerTest
42 extends PlexusInSpringTestCase
44 DatabaseUnprocessedArtifactConsumer indexMethodsConsumer;
46 IndexJavaPublicMethodsCrossRepositorySearch searcher;
48 private RepositoryContentIndexFactory indexFactory;
54 indexMethodsConsumer =
55 (DatabaseUnprocessedArtifactConsumer) lookup( DatabaseUnprocessedArtifactConsumer.class,
56 "index-public-methods" );
58 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
59 config.setId( "test-repo" );
60 config.setLayout( "default" );
61 config.setLocation( getBasedir() + "/target/test-classes/test-repo" );
62 config.setName( "Test Repository" );
64 addRepoToConfiguration( "index-public-methods", config );
66 indexFactory = (RepositoryContentIndexFactory) lookup (RepositoryContentIndexFactory.class, "lucene" );
67 searcher = new IndexJavaPublicMethodsCrossRepositorySearch( config, indexFactory );
70 private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration )
73 ArchivaConfiguration archivaConfiguration =
74 (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint );
75 Configuration configuration = archivaConfiguration.getConfiguration();
76 configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) );
77 configuration.addManagedRepository( repoConfiguration );
80 public void testJarPublicMethods()
83 ArchivaArtifact artifact =
84 createArtifact( "org.apache.archiva", "archiva-index-methods-jar-test", "1.0", "jar" );
85 indexMethodsConsumer.processArchivaArtifact( artifact );
87 List<String> selectedRepos = new ArrayList<String>();
88 selectedRepos.add( "test-repo" );
90 // search for class names
91 SearchResults results = searcher.searchForBytecode( "", selectedRepos, "FirstPackageApp", new SearchResultLimits( 0 ) );
92 assertEquals( 1, results.getTotalHits() );
94 results = searcher.searchForBytecode( "", selectedRepos, "SecondPackageApp", new SearchResultLimits( 0 ) );
95 assertEquals( 1, results.getTotalHits() );
97 // search for public methods
98 results = searcher.searchForBytecode( "", selectedRepos, "appMethodOne", new SearchResultLimits( 0 ) );
99 assertEquals( 1, results.getTotalHits() );
101 // should return only the overridding public method in SecondPackageApp
102 results = searcher.searchForBytecode( "", selectedRepos, "protectedMethod", new SearchResultLimits( 0 ) );
103 assertEquals( 1, results.getTotalHits() );
105 // should not return any private methods
106 results = searcher.searchForBytecode( "", selectedRepos, "privMethod", new SearchResultLimits( 0 ) );
107 assertEquals( 0, results.getTotalHits() );
109 // test for public variables?
112 private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type )
114 ArchivaArtifactModel model = new ArchivaArtifactModel();
115 model.setGroupId( groupId );
116 model.setArtifactId( artifactId );
117 model.setVersion( version );
118 model.setType( type );
119 model.setRepositoryId( "test-repo" );
121 return new ArchivaArtifact( model );