View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.metadata;
18  
19  import java.io.StringWriter;
20  import java.util.List;
21  
22  import org.codehaus.plexus.PlexusTestCase;
23  import org.codehaus.plexus.classworlds.ClassWorld;
24  import org.codehaus.plexus.classworlds.realm.ClassRealm;
25  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
26  import org.codehaus.plexus.component.repository.ComponentDescriptor;
27  import org.codehaus.plexus.component.repository.io.PlexusTools;
28  import org.codehaus.plexus.configuration.PlexusConfiguration;
29  
30  /**
31   * Test for the {@link DefaultComponentDescriptorWriter} class.
32   *
33   * @version $Rev$ $Date$
34   */
35  public class DefaultComponentDescriptorWriterTest
36      extends PlexusTestCase
37  {
38      private DefaultComponentDescriptorWriter descriptorWriter;
39  
40      // @Override
41      protected void setUp() throws Exception {
42          super.setUp();
43  
44          descriptorWriter = (DefaultComponentDescriptorWriter) lookup(ComponentDescriptorWriter.class);
45          assertNotNull(descriptorWriter);
46      }
47  
48      // @Override
49      protected void tearDown() throws Exception {
50          descriptorWriter = null;
51  
52          super.tearDown();
53      }
54  
55      public void testBasic() throws Exception {
56          ComponentSetDescriptor set = new ComponentSetDescriptor();
57  
58          ComponentDescriptor component = new ComponentDescriptor();
59          component.setImplementation("java.lang.String");
60          component.setRole("foo");
61          component.setRoleHint("bar");
62          component.setComponentFactory("baz");
63  
64          set.addComponentDescriptor(component);
65  
66          StringWriter writer = new StringWriter();
67          descriptorWriter.writeDescriptorSet(writer, set, false);
68          writer.flush();
69          writer.close();
70          
71          String xml = writer.toString();
72  
73          assertTrue(xml.length() != 0);
74  
75          PlexusConfiguration config = PlexusTools.buildConfiguration(xml);
76          assertNotNull(config);
77          
78          ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() );
79          ClassRealm realm = classWorld.getRealm( "test" );
80          org.codehaus.plexus.component.repository.ComponentSetDescriptor set2 = PlexusTools.buildComponentSet(config, realm);
81          assertNotNull(set2);
82  
83          List components = set2.getComponents();
84          assertNotNull(components);
85          assertEquals(1, components.size());
86  
87          org.codehaus.plexus.component.repository.ComponentDescriptor component2 =
88                  (org.codehaus.plexus.component.repository.ComponentDescriptor) components.get(0);
89          assertNotNull(component2);
90          
91          assertEquals(component.getRole(), component2.getRole());
92          assertEquals(component.getRoleHint(), component2.getRoleHint());
93          assertEquals(component.getComponentFactory(), component2.getComponentFactory());
94  
95          //
96          // TODO: Verify requirements and configuration too... but I'm too lazy for that right now
97          //
98      }
99  }