View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package org.codehaus.plexus.metadata.gleaner;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.util.List;
20  
21  import com.thoughtworks.qdox.JavaProjectBuilder;
22  import com.thoughtworks.qdox.model.JavaClass;
23  import com.thoughtworks.qdox.model.JavaSource;
24  import org.codehaus.plexus.PlexusTestCase;
25  import org.codehaus.plexus.component.repository.ComponentDescriptor;
26  import org.codehaus.plexus.component.repository.ComponentRequirement;
27  import org.codehaus.plexus.configuration.PlexusConfiguration;
28  
29  /**
30   * Tests for the {@link QDoxComponentGleaner} class.
31   *
32   * @version $Rev$ $Date$
33   */
34  public class QDoxComponentGleanerTest extends PlexusTestCase {
35      private QDoxComponentGleaner gleaner;
36  
37      private JavaProjectBuilder builder;
38  
39      // @Override
40      protected void setUp() throws Exception {
41          super.setUp();
42  
43          gleaner = new QDoxComponentGleaner();
44          builder = new JavaProjectBuilder();
45      }
46  
47      // @Override
48      protected void tearDown() throws Exception {
49          gleaner = null;
50          builder = null;
51  
52          super.tearDown();
53      }
54  
55      private JavaSource addSource(final String name) throws IOException {
56          File url = new File(
57                  getBasedir(),
58                  "src/test/java/" + getClass().getPackage().getName().replace('.', '/') + "/" + name);
59          assertTrue(url.exists());
60          return builder.addSource(url);
61      }
62  
63      private JavaClass loadJavaClass(final String name) throws IOException {
64          JavaSource source = addSource(name);
65          assertNotNull(source);
66  
67          List<JavaClass> classes = source.getClasses();
68          assertNotNull(classes);
69          assertEquals(1, classes.size());
70  
71          assertNotNull(classes.get(0));
72  
73          return classes.get(0);
74      }
75  
76      private ComponentDescriptor<?> glean(final String name, final String[] supporting) throws Exception {
77          if (supporting != null) {
78              for (String aSupporting : supporting) {
79                  addSource(aSupporting);
80              }
81          }
82  
83          return gleaner.glean(builder, loadJavaClass(name));
84      }
85  
86      private ComponentDescriptor<?> glean(final String name) throws Exception {
87          return glean(name, null);
88      }
89  
90      public void testNoAnnotationsClass() throws Exception {
91          ComponentDescriptor<?> component = glean("NoAnnotationsClass.java");
92          assertNull(component);
93      }
94  
95      public void testAbstractClass() throws Exception {
96          ComponentDescriptor<?> component = glean("AbstractClass.java");
97          assertNull(component);
98      }
99  
100     /*
101     public void testAbstractWithAnnoClass() throws Exception {
102         ComponentDescriptor component = glean("AbstractWithAnnoClass.java");
103         assertNull(component);
104     }
105     */
106 
107     public void testNoAnnotationsIntf() throws Exception {
108         ComponentDescriptor<?> component = glean("NoAnnotationsIntf.java");
109         assertNull(component);
110     }
111 
112     public void testMyComponent() throws Exception {
113         addSource("ChildComponent.java");
114         ComponentDescriptor<?> component = glean("MyComponent.java");
115         assertNotNull(component);
116 
117         assertEquals(MyComponent.class.getName(), component.getRole());
118         assertEquals("foo", component.getRoleHint());
119 
120         List<ComponentRequirement> requirements = component.getRequirements();
121         assertNotNull(requirements);
122         assertEquals(1, requirements.size());
123 
124         ComponentRequirement requirement = requirements.get(0);
125         assertNotNull(requirement);
126         assertEquals(ChildComponent.class.getName(), requirement.getRole());
127 
128         PlexusConfiguration config = component.getConfiguration();
129         assertNotNull(config);
130         assertEquals(1, config.getChildCount());
131 
132         PlexusConfiguration child = config.getChild(0);
133         assertNotNull(child);
134         assertEquals("foo", child.getName());
135         assertEquals("bar", child.getValue());
136     }
137 }