View Javadoc
1   package org.codehaus.plexus.component.discovery;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.PlexusTestCase;
20  import org.codehaus.plexus.classworlds.ClassWorld;
21  import org.codehaus.plexus.classworlds.realm.ClassRealm;
22  import org.codehaus.plexus.component.repository.ComponentDescriptor;
23  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
24  import org.codehaus.plexus.context.DefaultContext;
25  
26  import java.io.File;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  /**
32   * @author Jason van Zyl
33   *
34   * @version $Id$
35   */
36  public class ComponentDiscovererTest
37      extends PlexusTestCase
38  {
39      public void testDefaultComponentDiscoverer()
40          throws Exception
41      {
42          ComponentDiscoverer componentDiscoverer = new DefaultComponentDiscoverer();
43  
44          ClassWorld classWorld = new ClassWorld();
45  
46          ClassRealm core = classWorld.newRealm( "core" );
47  
48          File testClasses = new File( getBasedir(), "target/test-classes" );
49  
50          core.addURL( testClasses.toURL() );
51  
52          File classes = new File( getBasedir(), "target/classes" );
53  
54          core.addURL( classes.toURL() );
55  
56          List<ComponentSetDescriptor> componentSetDescriptors = componentDiscoverer.findComponents( new DefaultContext(), core );
57  
58          ComponentDescriptor<?> componentDescriptor = byImplementation(componentSetDescriptors).get( "org.codehaus.plexus.component.discovery.DefaultDiscoveredComponent" );
59  
60          assertNotNull("componentDescriptor is null", componentDescriptor );
61  
62          assertEquals( "org.codehaus.plexus.component.discovery.DiscoveredComponent", componentDescriptor.getRole() );
63  
64          assertEquals( "org.codehaus.plexus.component.discovery.DefaultDiscoveredComponent", componentDescriptor.getImplementation() );
65      }
66  
67      private static Map<String, ComponentDescriptor<?>> byImplementation(List<ComponentSetDescriptor> descriptorSets) {
68          TreeMap<String, ComponentDescriptor<?>> index = new TreeMap<String, ComponentDescriptor<?>>();
69          for ( ComponentSetDescriptor descriptorSet : descriptorSets )
70          {
71              for ( ComponentDescriptor<?> descriptor : descriptorSet.getComponents() )
72              {
73                  index.put(descriptor.getImplementation(), descriptor);
74              }
75          }
76          return index;
77      }
78  }