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 org.codehaus.plexus.PlexusTestCase;
22  import org.codehaus.plexus.component.repository.ComponentDescriptor;
23  import org.codehaus.plexus.component.repository.ComponentRequirement;
24  import org.codehaus.plexus.configuration.PlexusConfiguration;
25  
26  import com.thoughtworks.qdox.JavaProjectBuilder;
27  import com.thoughtworks.qdox.model.JavaClass;
28  import com.thoughtworks.qdox.model.JavaSource;
29  
30  /**
31   * Tests for the {@link QDoxComponentGleaner} class.
32   * 
33   * @version $Rev$ $Date$
34   */
35  public class QDoxComponentGleanerTest
36      extends PlexusTestCase
37  {
38      private QDoxComponentGleaner gleaner;
39  
40      private JavaProjectBuilder builder;
41  
42      // @Override
43      protected void setUp()
44          throws Exception
45      {
46          super.setUp();
47  
48          gleaner = new QDoxComponentGleaner();
49          builder = new JavaProjectBuilder();
50      }
51  
52      // @Override
53      protected void tearDown()
54          throws Exception
55      {
56          gleaner = null;
57          builder = null;
58  
59          super.tearDown();
60      }
61  
62      private JavaSource addSource( final String name )
63          throws IOException
64      {
65          File url = new File( getBasedir(), "src/test/java/" + getClass().getPackage().getName().replace( '.', '/' ) + "/" + name );
66          assertTrue( url.exists() );
67          return builder.addSource( url );
68      }
69      
70  
71      private JavaClass loadJavaClass( final String name )
72          throws IOException
73      {
74          JavaSource source = addSource( name );
75          assertNotNull( source );
76  
77          List<JavaClass> classes = source.getClasses();
78          assertNotNull( classes );
79          assertEquals( 1, classes.size() );
80  
81          assertNotNull( classes.get(0) );
82  
83          return classes.get(0);
84      }
85  
86      private ComponentDescriptor<?> glean( final String name, final String[] supporting )
87          throws Exception
88      {
89          if ( supporting != null )
90          {
91              for ( String aSupporting : supporting )
92              {
93                  addSource( aSupporting );
94              }
95          }
96  
97          return gleaner.glean( builder, loadJavaClass( name ) );
98      }
99  
100     private ComponentDescriptor<?> glean( final String name )
101         throws Exception
102     {
103         return glean( name, null );
104     }
105 
106     public void testNoAnnotationsClass()
107         throws Exception
108     {
109         ComponentDescriptor<?> component = glean( "NoAnnotationsClass.java" );
110         assertNull( component );
111     }
112 
113     public void testAbstractClass()
114         throws Exception
115     {
116         ComponentDescriptor<?> component = glean( "AbstractClass.java" );
117         assertNull( component );
118     }
119 
120     /*
121     public void testAbstractWithAnnoClass() throws Exception {
122         ComponentDescriptor component = glean("AbstractWithAnnoClass.java");
123         assertNull(component);
124     }
125     */
126 
127     public void testNoAnnotationsIntf()
128         throws Exception
129     {
130         ComponentDescriptor<?> component = glean( "NoAnnotationsIntf.java" );
131         assertNull( component );
132     }
133 
134     public void testMyComponent()
135         throws Exception
136     {
137         addSource( "ChildComponent.java" );
138         ComponentDescriptor<?> component = glean( "MyComponent.java" );
139         assertNotNull( component );
140 
141         assertEquals( MyComponent.class.getName(), component.getRole() );
142         assertEquals( "foo", component.getRoleHint() );
143 
144         List<ComponentRequirement> requirements = component.getRequirements();
145         assertNotNull( requirements );
146         assertEquals( 1, requirements.size() );
147 
148         ComponentRequirement requirement = requirements.get( 0 );
149         assertNotNull( requirement );
150         assertEquals( ChildComponent.class.getName(), requirement.getRole() );
151 
152         PlexusConfiguration config = component.getConfiguration();
153         assertNotNull( config );
154         assertEquals( 1, config.getChildCount() );
155 
156         PlexusConfiguration child = config.getChild( 0 );
157         assertNotNull( child );
158         assertEquals( "foo", child.getName() );
159         assertEquals( "bar", child.getValue() );
160     }
161 }