1 8 9 package org.apache.avalon.meta; 10 11 import java.io.File ; 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 35 public class MetaTestCase extends TestCase 36 { 37 private static final String PRIMARY = "org.apache.avalon.playground.Primary"; 38 private static final String SECONDARY = "org.apache.avalon.playground.Secondary"; 39 private static final String PRIMARY_S = "org.apache.avalon.playground.PrimaryService"; 40 private static final String SECONDARY_S = "org.apache.avalon.playground.SecondaryService"; 41 42 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 name ) 59 { 60 super( name ); 61 } 62 63 72 protected void setUp() throws Exception 73 { 74 m_qdox = new JavaDocBuilder(); 75 buildMeta(); 76 } 77 78 87 public void buildMeta() throws Exception 88 { 89 String base = System.getProperty( "basedir" ); 90 m_qdox.addSourceTree( new File ( 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 146 public void testTypeCreation() 147 { 148 assertTrue( "primary != null", m_primary != null ); 149 assertTrue( "secondary != null", m_secondary != null ); 150 } 151 152 155 public void testServiceCreation() 156 { 157 assertTrue( "primary service != null", m_primaryService != null ); 158 assertTrue( "secondary service != null", m_secondaryService != null ); 159 } 160 161 167 public void testPrimaryService() throws Exception 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 181 public void testSecondaryService() throws Exception 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 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 ( "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 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 ( "Logger name is not system" ); 224 } 225 } 226 else 227 { 228 assertTrue( "Loggers length != 1", false ); 229 throw new Exception ( "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 ( "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 ( "Dependency version is incorrect" ); 244 } 245 if( !dep.getKey().equals( "primary" ) ) 246 { 247 assertTrue( "dependency role : " + dep.getKey(), false ); 248 throw new Exception ( "Dependency role name is incortrect" ); 249 } 250 } 251 else 252 { 253 throw new Exception ( "Dependency length != 1" ); 254 } 255 256 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 ( "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 ( "Service version is incorrect" ); 273 } 274 } 275 else 276 { 277 throw new Exception ( "Services length != 1" ); 278 } 279 } 280 } 281 | Popular Tags |