View Javadoc

1   package org.apache.maven.index.creator;
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 org.apache.lucene.search.BooleanClause;
23  import org.apache.lucene.search.BooleanQuery;
24  import org.apache.maven.index.ArtifactContext;
25  import org.apache.maven.index.ArtifactInfo;
26  import org.apache.maven.index.FlatSearchRequest;
27  import org.apache.maven.index.FlatSearchResponse;
28  import org.apache.maven.index.NexusIndexer;
29  import org.apache.maven.index.OSGI;
30  import org.apache.maven.index.context.IndexCreator;
31  import org.apache.maven.index.context.IndexingContext;
32  import org.apache.maven.index.expr.StringSearchExpression;
33  import org.codehaus.plexus.PlexusTestCase;
34  import org.codehaus.plexus.util.FileUtils;
35  
36  import java.io.File;
37  import java.util.Arrays;
38  import java.util.List;
39  
40  /**
41   * @author Olivier Lamy
42   */
43  public class OsgiArtifactIndexCreatorTest
44      extends PlexusTestCase
45  {
46      protected IndexCreator indexCreator;
47  
48      private NexusIndexer nexusIndexer;
49  
50      static final String INDEX_ID = "osgi-test1";
51  
52      @Override
53      public void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          indexCreator = this.lookup( IndexCreator.class, OsgiArtifactIndexCreator.ID );
59  
60          nexusIndexer = this.lookup( NexusIndexer.class );
61      }
62  
63      public void testAssertIndexCreatorComponentExists()
64          throws Exception
65      {
66          assertNotNull( indexCreator );
67      }
68  
69      public void testPopulateArtifactInfo()
70          throws Exception
71      {
72          File artifact = new File( getBasedir(),
73                                    "src/test/repo-with-osgi/org/apache/karaf/features/org.apache.karaf.features.command/2.2.2/org.apache.karaf.features.command-2.2.2.jar" );
74  
75          File pom = new File( getBasedir(),
76                               "src/test/repo-with-osgi/org/apache/karaf/features/org.apache.karaf.features.command/2.2.2/org.apache.karaf.features.command-2.2.2.pom" );
77  
78          ArtifactInfo artifactInfo =
79              new ArtifactInfo( "test", "org.apache.karaf.features", "org.apache.karaf.features.command", "2.2.2", null );
80  
81          ArtifactContext artifactContext = new ArtifactContext( pom, artifact, null, artifactInfo, null );
82  
83          indexCreator.populateArtifactInfo( artifactContext );
84  
85          assertNotNull( "bundleSymbolicName", artifactContext.getArtifactInfo().bundleSymbolicName );
86  
87          assertNotNull( "bundleVersion", artifactContext.getArtifactInfo().bundleVersion );
88  
89          assertNotNull( "bundleExportPackage", artifactContext.getArtifactInfo().bundleExportPackage );
90  
91          assertEquals( "org.apache.karaf.features.command", artifactContext.getArtifactInfo().bundleSymbolicName );
92  
93          assertEquals( "2.2.2", artifactContext.getArtifactInfo().bundleVersion );
94  
95          assertEquals(
96              "org.apache.karaf.features.command.completers;uses:=\"org.apache.karaf.features,org.apache.karaf.shell.console,org.apache.karaf.shell.console.completer\";version=\"2.2.2\",org.apache.karaf.features.command;uses:=\"org.apache.felix.gogo.commands,org.apache.karaf.features,org.apache.karaf.shell.console,org.osgi.framework,org.apache.felix.service.command\";version=\"2.2.2\"",
97              artifactContext.getArtifactInfo().bundleExportPackage );
98  
99          ArtifactInfo ai = artifactContext.getArtifactInfo();
100 
101         assertEquals( "This bundle provides the Karaf shell commands to manipulate features.", ai.bundleDescription );
102         assertEquals( "Apache Karaf :: Features :: Command", ai.bundleName );
103         assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", ai.bundleLicense );
104         assertEquals( "http://www.apache.org/", ai.bundleDocUrl );
105 
106         assertEquals(
107             "javax.management,javax.management.loading,org.apache.felix.gogo.commands;version=\"[0.6,1)\",org.apache.felix.service.command;status=provisional;version=\"[0.6,1)\",org.apache.karaf.features;version=\"[2.2,3)\",org.apache.karaf.shell.console;version=\"[2.2,3)\",org.apache.karaf.shell.console.completer;version=\"[2.2,3)\",org.osgi.framework;version=\"[1.5,2)\",org.osgi.service.blueprint;version=\"[1.0.0,2.0.0)\"",
108             ai.bundleImportPackage );
109     }
110 
111 
112     private void indexOSGIRepo()
113         throws Exception
114     {
115 
116         File repo = new File( getBasedir(), "src/test/repo-with-osgi" );
117 
118         File repoIndexDir = new File( getBasedir(), "target/test/repo-with-osgi/.index/" );
119 
120         if ( repoIndexDir.exists() )
121         {
122             FileUtils.deleteDirectory( repoIndexDir );
123         }
124 
125         repoIndexDir.mkdirs();
126 
127         List<IndexCreator> indexCreators =
128             Arrays.<IndexCreator>asList( new MinimalArtifactInfoIndexCreator(), new JarFileContentsIndexCreator(),
129                                          new MavenPluginArtifactInfoIndexCreator(), new OsgiArtifactIndexCreator() );
130 
131         IndexingContext indexingContext =
132             nexusIndexer.addIndexingContext( INDEX_ID, INDEX_ID, repo, repoIndexDir, "http://www.apache.org",
133                                              "http://www.apache.org/.index", indexCreators );
134         indexingContext.setSearchable( true );
135         nexusIndexer.scan( indexingContext, false );
136 
137 
138     }
139 
140     public void testIndexOSGIRepoThenSearch()
141         throws Exception
142     {
143 
144         try
145         {
146             indexOSGIRepo();
147 
148             BooleanQuery q = new BooleanQuery();
149 
150             q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME,
151                                                 new StringSearchExpression( "org.apache.karaf.features.command" ) ),
152                    BooleanClause.Occur.MUST );
153 
154             FlatSearchRequest request = new FlatSearchRequest( q );
155             FlatSearchResponse response = nexusIndexer.searchFlat( request );
156 
157             // here only one results !
158             assertEquals( 1, response.getResults().size() );
159 
160             q = new BooleanQuery();
161 
162             q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME,
163                                                 new StringSearchExpression( "org.apache.karaf.features.core" ) ),
164                    BooleanClause.Occur.MUST );
165 
166             request = new FlatSearchRequest( q );
167             response = nexusIndexer.searchFlat( request );
168 
169             // here two results !
170             assertEquals( 2, response.getResults().size() );
171         }
172         finally
173         {
174             nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true );
175         }
176     }
177 
178     public void testIndexOSGIRepoThenSearchWithVersion()
179         throws Exception
180     {
181 
182         indexOSGIRepo();
183 
184         try
185         {
186 
187             BooleanQuery q = new BooleanQuery();
188 
189             q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME,
190                                                 new StringSearchExpression( "org.apache.karaf.features.core" ) ),
191                    BooleanClause.Occur.MUST );
192 
193             q.add( nexusIndexer.constructQuery( OSGI.VERSION, new StringSearchExpression( "2.2.1" ) ),
194                    BooleanClause.Occur.MUST );
195 
196             FlatSearchRequest request = new FlatSearchRequest( q );
197             FlatSearchResponse response = nexusIndexer.searchFlat( request );
198 
199             // here only one results as we use version
200             assertEquals( 1, response.getResults().size() );
201         }
202         finally
203         {
204             nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true );
205         }
206 
207     }
208 
209     public void testIndexOSGIRepoThenSearchWithExportPackage()
210         throws Exception
211     {
212 
213         indexOSGIRepo();
214 
215         try
216         {
217 
218             BooleanQuery q = new BooleanQuery();
219 
220             q.add( nexusIndexer.constructQuery( OSGI.EXPORT_PACKAGE, new StringSearchExpression(
221                 "org.apache.karaf.features.command.completers" ) ), BooleanClause.Occur.MUST );
222 
223             FlatSearchRequest request = new FlatSearchRequest( q );
224             FlatSearchResponse response = nexusIndexer.searchFlat( request );
225 
226             //System.out.println("results with export package query " + response.getResults() );
227             assertEquals( 1, response.getResults().size() );
228 
229             ArtifactInfo ai = response.getResults().iterator().next();
230 
231             assertEquals( "org.apache.karaf.features", ai.groupId );
232             assertEquals( "org.apache.karaf.features.command", ai.artifactId );
233             assertEquals( "2.2.2", ai.version );
234             assertEquals( "org.apache.karaf.features.command", ai.bundleSymbolicName );
235             assertEquals( "2.2.2", ai.bundleVersion );
236 
237             assertEquals( "This bundle provides the Karaf shell commands to manipulate features.",
238                           ai.bundleDescription );
239             assertEquals( "Apache Karaf :: Features :: Command", ai.bundleName );
240             assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", ai.bundleLicense );
241             assertEquals( "http://www.apache.org/", ai.bundleDocUrl );
242 
243             assertEquals(
244                 "javax.management,javax.management.loading,org.apache.felix.gogo.commands;version=\"[0.6,1)\",org.apache.felix.service.command;status=provisional;version=\"[0.6,1)\",org.apache.karaf.features;version=\"[2.2,3)\",org.apache.karaf.shell.console;version=\"[2.2,3)\",org.apache.karaf.shell.console.completer;version=\"[2.2,3)\",org.osgi.framework;version=\"[1.5,2)\",org.osgi.service.blueprint;version=\"[1.0.0,2.0.0)\"",
245                 ai.bundleImportPackage );
246 
247         }
248         finally
249         {
250             nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true );
251         }
252 
253     }
254 
255     public void testIndexOSGIRepoThenSearchWithExportService()
256         throws Exception
257     {
258 
259         indexOSGIRepo();
260 
261         try
262         {
263 
264             BooleanQuery q = new BooleanQuery();
265 
266             q.add( nexusIndexer.constructQuery( OSGI.EXPORT_SERVICE, new StringSearchExpression(
267                 "org.apache.felix.bundlerepository.RepositoryAdmin" ) ), BooleanClause.Occur.MUST );
268 
269             FlatSearchRequest request = new FlatSearchRequest( q );
270             FlatSearchResponse response = nexusIndexer.searchFlat( request );
271 
272             //System.out.println("results with export package query " + response.getResults() );
273             assertEquals( 1, response.getResults().size() );
274 
275             ArtifactInfo ai = response.getResults().iterator().next();
276             System.out.println( "ai " + ai );
277 
278             assertEquals( "org.apache.felix", ai.groupId );
279             assertEquals( "org.apache.felix.bundlerepository", ai.artifactId );
280             assertEquals( "1.6.6", ai.version );
281             assertEquals( "bundle", ai.packaging );
282             assertEquals( "org.apache.felix.bundlerepository", ai.bundleSymbolicName );
283             assertEquals( "1.6.6", ai.bundleVersion );
284 
285         }
286         finally
287         {
288             nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true );
289         }
290 
291     }
292 
293     // Export-Service: org.apache.felix.bundlerepository.RepositoryAdmin,org.osgi.service.obr.RepositoryAdmin
294 
295 }