View Javadoc
1   package org.codehaus.plexus.component.repository;
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 junit.framework.TestCase;
20  import org.codehaus.plexus.component.repository.io.PlexusTools;
21  import org.codehaus.plexus.classworlds.ClassWorld;
22  import org.codehaus.plexus.classworlds.realm.ClassRealm;
23  
24  import java.util.List;
25  
26  /**
27   *
28   *
29   * @author Jason van Zyl
30   *
31   * @version $Id$
32   */
33  public class ComponentDescriptorTest
34      extends TestCase
35  {
36      public void testSimpleComponentResolution()
37          throws Exception
38      {
39          String cc1 =
40              "<component>" +
41              "  <implementation>java.lang.String</implementation>" +
42              "  <role>c1</role>" +
43              "  <role-hint>role-hint</role-hint>" +
44              "  <component-profile>component-profile</component-profile>" +
45              "  <requirements>" +
46              "    <requirement>" +
47              "      <role>c2</role>" +
48              "   </requirement>" +
49              "    <requirement>" +
50              "      <role>c3</role>" +
51              "   </requirement>" +
52              "  </requirements>" +
53              "</component>";
54  
55          ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() );
56          ClassRealm realm = classWorld.getRealm( "test" );
57  
58          ComponentDescriptor<?> c1 = PlexusTools.buildComponentDescriptor( cc1, realm );
59  
60          assertEquals( "c1", c1.getRole() );
61  
62          assertEquals( "role-hint", c1.getRoleHint() );
63  
64          assertEquals( "component-profile", c1.getComponentProfile() );
65  
66          List<ComponentRequirement> requirements = c1.getRequirements();
67  
68          assertEquals( 2, requirements.size() );
69  
70          boolean containsC2 = false;
71  
72          boolean containsC3 = false;
73  
74          for ( ComponentRequirement requirement : requirements )
75          {
76              if ( requirement.getRole().equals( "c2" ) )
77              {
78                  containsC2 = true;
79              }
80              else if ( requirement.getRole().equals( "c3" ) )
81              {
82                  containsC3 = true;
83              }
84  
85          }
86  
87          assertTrue( containsC2 );
88  
89          assertTrue( containsC3 );
90      }
91      
92      public void testShouldNotBeEqualWhenRolesAreSameButHintsAreDifferent()
93      {
94          ComponentDescriptor<Object> desc = new ComponentDescriptor<Object>();
95          desc.setRole("one");
96          desc.setRoleHint("one");
97          
98          ComponentDescriptor<Object> desc2 = new ComponentDescriptor<Object>();
99          desc2.setRole("one");
100         desc2.setRoleHint("two");
101         
102         assertFalse(desc.equals(desc2));
103     }
104 }