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.File;
20  import java.io.StringWriter;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.codehaus.plexus.PlexusTestCase;
25  import org.codehaus.plexus.classworlds.ClassWorld;
26  import org.codehaus.plexus.classworlds.realm.ClassRealm;
27  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
28  import org.codehaus.plexus.component.repository.ComponentDescriptor;
29  import org.codehaus.plexus.component.repository.io.PlexusTools;
30  import org.codehaus.plexus.configuration.PlexusConfiguration;
31  import org.codehaus.plexus.metadata.merge.ComponentsXmlMerger;
32  import org.codehaus.plexus.metadata.merge.Merger;
33  import org.codehaus.plexus.metadata.merge.PlexusXmlMerger;
34  import org.jdom2.Document;
35  import org.jdom2.Element;
36  import org.jdom2.input.SAXBuilder;
37  
38  /**
39   * Test for the {@link DefaultComponentDescriptorWriter} class.
40   *
41   * @version $Rev$ $Date$
42   */
43  public class DefaultComponentDescriptorWriterTest
44      extends PlexusTestCase
45  {
46      private DefaultComponentDescriptorWriter descriptorWriter;
47  
48      // @Override
49      protected void setUp() throws Exception {
50          super.setUp();
51  
52          descriptorWriter = (DefaultComponentDescriptorWriter) lookup(ComponentDescriptorWriter.class);
53          assertNotNull(descriptorWriter);
54      }
55  
56      // @Override
57      protected void tearDown() throws Exception {
58          descriptorWriter = null;
59  
60          super.tearDown();
61      }
62  
63      public void testBasic() throws Exception {
64          ComponentSetDescriptor set = new ComponentSetDescriptor();
65  
66          ComponentDescriptor component = new ComponentDescriptor();
67          component.setImplementation("java.lang.String");
68          component.setRole("foo");
69          component.setRoleHint("bar");
70          component.setComponentFactory("baz");
71  
72          set.addComponentDescriptor(component);
73  
74          StringWriter writer = new StringWriter();
75          descriptorWriter.writeDescriptorSet(writer, set, false);
76          writer.flush();
77          writer.close();
78          
79          String xml = writer.toString();
80  
81          assertTrue(xml.length() != 0);
82  
83          PlexusConfiguration config = PlexusTools.buildConfiguration(xml);
84          assertNotNull(config);
85          
86          ClassWorld classWorld = new ClassWorld( "test", Thread.currentThread().getContextClassLoader() );
87          ClassRealm realm = classWorld.getRealm( "test" );
88          org.codehaus.plexus.component.repository.ComponentSetDescriptor set2 = PlexusTools.buildComponentSet(config, realm);
89          assertNotNull(set2);
90  
91          List components = set2.getComponents();
92          assertNotNull(components);
93          assertEquals(1, components.size());
94  
95          org.codehaus.plexus.component.repository.ComponentDescriptor component2 =
96                  (org.codehaus.plexus.component.repository.ComponentDescriptor) components.get(0);
97          assertNotNull(component2);
98          
99          assertEquals(component.getRole(), component2.getRole());
100         assertEquals(component.getRoleHint(), component2.getRoleHint());
101         assertEquals(component.getComponentFactory(), component2.getComponentFactory());
102 
103         //
104         // TODO: Verify requirements and configuration too... but I'm too lazy for that right now
105         //
106     }
107 
108     public void testComponentsOrder() throws Exception {
109         MetadataGenerator generator = (MetadataGenerator) lookup(MetadataGenerator.class);
110         assertNotNull(generator);
111 
112         MetadataGenerationRequest request = new MetadataGenerationRequest();
113         request.sourceDirectories = Collections.singletonList("src/main/java");
114         request.classesDirectory = new File("target/classes");
115         request.outputFile = new File("target/test-classes/components-sorted.xml");
116         request.sourceEncoding = "UTF-8";
117         request.useContextClassLoader = true;
118 
119         generator.generateDescriptor(request);
120 
121         assertTrue("Descriptor not generated", request.outputFile.exists());
122 
123         Document doc = new SAXBuilder().build(request.outputFile);
124 
125         // check if the components are sorted by role+impl
126         List<Element> components = doc.getRootElement().getChild("components").getChildren();
127         assertEquals("Number of components", 5, components.size());
128 
129         assertEquals("Component 1 role", ComponentDescriptorExtractor.class.getName(), components.get(0).getChild("role").getText());
130         assertEquals("Component 1 impl", ClassComponentDescriptorExtractor.class.getName(), components.get(0).getChild("implementation").getText());
131 
132         assertEquals("Component 2 role", ComponentDescriptorExtractor.class.getName(), components.get(1).getChild("role").getText());
133         assertEquals("Component 2 impl", SourceComponentDescriptorExtractor.class.getName(), components.get(1).getChild("implementation").getText());
134 
135         assertEquals("Component 3 role", MetadataGenerator.class.getName(), components.get(2).getChild("role").getText());
136         assertEquals("Component 3 impl", DefaultMetadataGenerator.class.getName(), components.get(2).getChild("implementation").getText());
137 
138         assertEquals("Component 4 role", Merger.class.getName(), components.get(3).getChild("role").getText());
139         assertEquals("Component 4 impl", ComponentsXmlMerger.class.getName(), components.get(3).getChild("implementation").getText());
140 
141         assertEquals("Component 5 role", Merger.class.getName(), components.get(4).getChild("role").getText());
142         assertEquals("Component 5 impl", PlexusXmlMerger.class.getName(), components.get(4).getChild("implementation").getText());
143     }
144 }