KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > MetaTestCase


1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE.txt file.
7 */

8
9 package org.apache.avalon.meta;
10
11 import java.io.File JavaDoc;
12
13 import junit.framework.TestCase;
14 import org.apache.avalon.meta.info.Service;
15 import org.apache.avalon.meta.info.Type;
16 import org.apache.avalon.meta.info.ContextDescriptor;
17 import org.apache.avalon.meta.info.EntryDescriptor;
18 import org.apache.avalon.meta.info.CategoryDescriptor;
19 import org.apache.avalon.meta.info.DependencyDescriptor;
20 import org.apache.avalon.meta.info.ServiceDescriptor;
21 import org.apache.avalon.meta.info.builder.tags.TypeTag;
22 import org.apache.avalon.meta.info.builder.tags.ServiceTag;
23 import com.thoughtworks.qdox.model.JavaClass;
24 import com.thoughtworks.qdox.model.JavaSource;
25 import com.thoughtworks.qdox.JavaDocBuilder;
26
27 /**
28  * A testcase for the meta tools package. The testcase verifies the number
29  * and integrity of service and type defintions created as a result of a scan
30  * of the working test directory for source files containing the avalon.version
31  * javadoc tag.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  */

35 public class MetaTestCase extends TestCase
36 {
37     private static final String JavaDoc PRIMARY = "org.apache.avalon.playground.Primary";
38     private static final String JavaDoc SECONDARY = "org.apache.avalon.playground.Secondary";
39     private static final String JavaDoc PRIMARY_S = "org.apache.avalon.playground.PrimaryService";
40     private static final String JavaDoc SECONDARY_S = "org.apache.avalon.playground.SecondaryService";
41
42    /**
43     * The qdox builder that constructs the JavaClass instances
44     * that hold the javadoc comment structures.
45     */

46     private JavaDocBuilder m_qdox = null;
47
48     private Service m_primaryService;
49     private Service m_secondaryService;
50     private Type m_primary;
51     private Type m_secondary;
52
53     public MetaTestCase()
54     {
55         this( "MetaTestCase" );
56     }
57
58     public MetaTestCase( String JavaDoc name )
59     {
60         super( name );
61     }
62
63    /**
64     * Setup the qdox javadoc builder and the type and
65     * service builders, and scan the test directory for
66     * sources, resulting in the population of a type and
67     * service list that this test case will verify for
68     * integrity.
69     *
70     * @exception Exception if a setup error occurs
71     */

72     protected void setUp() throws Exception JavaDoc
73     {
74         m_qdox = new JavaDocBuilder();
75         buildMeta();
76     }
77
78    /**
79     * Scan the test directory for source files and build up the
80     * qdox JavaSource defintions from which JavaClass defintions
81     * are resolved. If the class contains a avalon.version tag
82     * then we build a Type or Service instance (class or interface
83     * respectively, and add them to the type and service lists.
84     *
85     * @exception Exception if a build error occurs
86     */

87     public void buildMeta() throws Exception JavaDoc
88     {
89         String JavaDoc base = System.getProperty( "basedir" );
90         m_qdox.addSourceTree( new File JavaDoc( base, "target/test-classes" ) );
91         JavaSource[] sources = m_qdox.getSources();
92         for( int i=0; i<sources.length; i++ )
93         {
94             JavaSource source = sources[i];
95             JavaClass[] classes = source.getClasses();
96             for( int j=0; j<classes.length; j++ )
97             {
98                 JavaClass c = classes[j];
99                 if( c.isInterface() )
100                 {
101                     Service service = new ServiceTag( c ).getService();
102                     if( service == null )
103                     {
104                         fail( "encounter null service: " + c );
105                     }
106                     if( service.getReference().getClassname().equals( PRIMARY_S ) )
107                     {
108                         m_primaryService = service;
109                     }
110                     else if( service.getReference().getClassname().equals( SECONDARY_S ) )
111                     {
112                         m_secondaryService = service;
113                     }
114                     else
115                     {
116                         fail( "Unexpected (invalid) service reference: " + service );
117                     }
118                 }
119                 else
120                 {
121                     Type type = new TypeTag( c ).getType();
122                     if( type == null )
123                     {
124                         fail( "encounter null type: " + c );
125                     }
126                     if( type.getInfo().getClassname().equals( PRIMARY ) )
127                     {
128                         m_primary = type;
129                     }
130                     else if( type.getInfo().getClassname().equals( SECONDARY ) )
131                     {
132                         m_secondary = type;
133                     }
134                     else
135                     {
136                         fail( "Unexpected (invalid) type reference: " + type );
137                     }
138                 }
139             }
140         }
141     }
142
143    /**
144     * Verify the the build process generated two Type defintions.
145     */

146     public void testTypeCreation()
147     {
148         assertTrue( "primary != null", m_primary != null );
149         assertTrue( "secondary != null", m_secondary != null );
150     }
151
152    /**
153     * Verify the the build process generated two Service defintions.
154     */

155     public void testServiceCreation()
156     {
157         assertTrue( "primary service != null", m_primaryService != null );
158         assertTrue( "secondary service != null", m_secondaryService != null );
159     }
160
161    /**
162     * Verify the integrity of the primary Service definitions for integrity
163     * relative to the javadoc tag statements.
164     *
165     * @exception Exception if a verification error occurs
166     */

167     public void testPrimaryService() throws Exception JavaDoc
168     {
169         Service service = m_primaryService;
170         assertTrue( "version", service.getReference().getVersion().toString().equals( "9.8.0" ) );
171         assertTrue( "classname", service.getClassname().equals( PRIMARY_S ) );
172         assertTrue( "attribute", service.getAttribute("status").equals( "test" ) );
173     }
174
175    /**
176     * Verify the integrity of the secondary Service definitions for integrity
177     * relative to the javadoc tag statements.
178     *
179     * @exception Exception if a verification error occurs
180     */

181     public void testSecondaryService() throws Exception JavaDoc
182     {
183         Service service = m_secondaryService;
184         assertTrue(
185           "version", service.getReference().getVersion().toString().equals( "0.1.0" ) );
186         assertTrue( "classname", service.getClassname().equals( SECONDARY_S ) );
187     }
188
189     public void testPrimaryType() throws Exception JavaDoc
190     {
191         Type type = m_primary;
192         assertTrue( "version", type.getInfo().getVersion().toString().equals( "1.3.0" ) );
193         assertTrue( "name", type.getInfo().getName().equals( "primary-component" ) );
194         assertTrue(
195           "lifestyle", type.getInfo().getLifestyle().equals( "singleton" ) );
196
197         ContextDescriptor context = m_primary.getContext();
198         EntryDescriptor entry = context.getEntry( "home" );
199         if( entry == null )
200         {
201             assertTrue( "no context entries", false );
202             throw new Exception JavaDoc( "missing context" );
203         }
204         else
205         {
206             assertTrue( entry.getKey().equals( "home" ) );
207             assertTrue( entry.getClassname().equals( "java.io.File" ) );
208         }
209     }
210
211     public void testSecondaryType() throws Exception JavaDoc
212     {
213         Type type = m_secondary;
214         assertTrue( "version", type.getInfo().getVersion().toString().equals( "2.4.0" ) );
215         assertTrue( "name", type.getInfo().getName().equals( "secondary-component" ) );
216         CategoryDescriptor[] loggers = type.getCategories();
217         if( loggers.length == 1 )
218         {
219             CategoryDescriptor logger = loggers[0];
220             if( !logger.getName().equals( "system" ) )
221             {
222                 assertTrue( "Logger name is not system", false );
223                 throw new Exception JavaDoc( "Logger name is not system" );
224             }
225         }
226         else
227         {
228             assertTrue( "Loggers length != 1", false );
229             throw new Exception JavaDoc( "Loggers length != 1" );
230         }
231         DependencyDescriptor[] dependencies = type.getDependencies();
232         if( dependencies.length == 1 )
233         {
234             DependencyDescriptor dep = dependencies[0];
235             if( !dep.getReference().getClassname().equals( PRIMARY_S ) )
236             {
237                 assertTrue( "dependency classname", false );
238                 throw new Exception JavaDoc( "Dependency name is incorrect" );
239             }
240             if( !dep.getReference().getVersion().toString().equals( "1.3.0" ) )
241             {
242                 assertTrue( "dependency version: " + dep.getReference().getVersion(), false );
243                 throw new Exception JavaDoc( "Dependency version is incorrect" );
244             }
245             if( !dep.getKey().equals( "primary" ) )
246             {
247                 assertTrue( "dependency role : " + dep.getKey(), false );
248                 throw new Exception JavaDoc( "Dependency role name is incortrect" );
249             }
250         }
251         else
252         {
253             throw new Exception JavaDoc( "Dependency length != 1" );
254         }
255
256         //
257
// test if the service descriptor is valid
258
//
259

260         ServiceDescriptor[] services = type.getServices();
261         if( services.length == 1 )
262         {
263             ServiceDescriptor dep = services[0];
264             if( !dep.getReference().getClassname().equals( SECONDARY_S ) )
265             {
266                 assertTrue( "service classname: " + dep.getReference().getClassname(), false );
267                 throw new Exception JavaDoc( "Service classname is incorrect" );
268             }
269             if( !dep.getReference().getVersion().toString().equals( "0.1.0" ) )
270             {
271                 assertTrue( "service version: " + dep.getReference().getVersion(), false );
272                 throw new Exception JavaDoc( "Service version is incorrect" );
273             }
274         }
275         else
276         {
277             throw new Exception JavaDoc( "Services length != 1" );
278         }
279     }
280 }
281
Popular Tags