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