avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/info/src/test/org/apache/avalon/framework/tools/infobuilder/test/data component1-info.xml
Date Fri, 04 Oct 2002 00:43:25 GMT
donaldp     2002/10/03 17:43:25

  Added:       info/src/test/org/apache/avalon/framework/tools/infobuilder/test
                        InfoBuilderTestCase.java
               info/src/test/org/apache/avalon/framework/tools/infobuilder/test/data
                        component1-info.xml
  Log:
  STart to write basic unit tests for loading infos.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/info/src/test/org/apache/avalon/framework/tools/infobuilder/test/InfoBuilderTestCase.java
  
  Index: InfoBuilderTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.framework.tools.infobuilder.test;
  
  import junit.framework.TestCase;
  import org.apache.avalon.framework.info.ComponentInfo;
  import org.apache.avalon.framework.info.ServiceInfo;
  import org.apache.avalon.framework.info.DependencyDescriptor;
  import org.apache.avalon.framework.info.Attribute;
  import org.apache.avalon.framework.info.LoggerDescriptor;
  import org.apache.avalon.framework.info.ComponentDescriptor;
  import org.apache.avalon.framework.info.EntryDescriptor;
  import org.apache.avalon.framework.info.ContextDescriptor;
  import org.apache.avalon.framework.info.ServiceDescriptor;
  import org.apache.avalon.framework.logger.ConsoleLogger;
  import org.apache.avalon.framework.tools.infobuilder.InfoBuilder;
  import java.util.Properties;
  
  /**
   * Abstract class which TestCases can extend.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/04 00:43:25 $
   */
  public class InfoBuilderTestCase
      extends TestCase
  {
      private static final Attribute[] EMPTY_ATTRIBUTES = new Attribute[ 0 ];
  
      private static final String BASE_PACKAGE =
          "org.apache.avalon.framework.tools.infobuilder.test.data.";
  
      private static final String COMPONENT1 = BASE_PACKAGE + "component1";
      private static final String COMPONENT2 = BASE_PACKAGE + "component2";
      private static final String COMPONENT3 = BASE_PACKAGE + "component3";
  
      public InfoBuilderTestCase( String name )
      {
          super( name );
      }
  
      public void testLoadXMLComponent1()
          throws Exception
      {
          final ComponentInfo actual = loadComponentInfo( COMPONENT1 );
          final ComponentInfo expected = constructDummyInfo();
  
          assertEqualInfos( COMPONENT1 + " should be equal to constructed actual",
                            expected,
                            actual );
      }
  
      private void assertEqualInfos( final String message,
                                     final ComponentInfo expected,
                                     final ComponentInfo actual )
      {
          final ComponentDescriptor expectedComponent = expected.getComponentDescriptor();
          final ComponentDescriptor actualComponent = actual.getComponentDescriptor();
          assertEqualComponents( message, expectedComponent, actualComponent );
  
          final LoggerDescriptor[] expectedLoggers = expected.getLoggers();
          final LoggerDescriptor[] actualLoggers = actual.getLoggers();
          assertEqualLoggers( message, expectedLoggers, actualLoggers );
  
          final ContextDescriptor expectedContext = expected.getContextDescriptor();
          final ContextDescriptor actualContext = actual.getContextDescriptor();
          assertEqualContext( message, expectedContext, actualContext );
  
          final ServiceDescriptor[] expectedServices = expected.getServices();
          final ServiceDescriptor[] actualServices = actual.getServices();
          assertEqualServices( message, expectedServices, actualServices );
  
          final DependencyDescriptor[] expectedDeps = expected.getDependencies();
          final DependencyDescriptor[] actualDeps = actual.getDependencies();
          assertEqualDeps( message, expectedDeps, actualDeps );
      }
  
      private void assertEqualDeps( final String message,
                                    final DependencyDescriptor[] expected,
                                    final DependencyDescriptor[] actual )
      {
          assertEquals( message + ": Dependencys.length", expected.length, actual.length );
          for( int i = 0; i < expected.length; i++ )
          {
              assertEquals( message + ": Dependencys[ " + i + "].service",
                            expected[ i ].getService(),
                            actual[ i ].getService() );
              assertEquals( message + ": Dependencys[ " + i + "].key",
                            expected[ i ].getKey(),
                            actual[ i ].getKey() );
              assertEqualAttributes( message + ": Dependencys[ " + i + "].attributes",
                                     expected[ i ].getAttributes(),
                                     actual[ i ].getAttributes() );
          }
      }
  
      private void assertEqualServices( final String message,
                                        final ServiceDescriptor[] expected,
                                        final ServiceDescriptor[] actual )
      {
          assertEquals( message + ": Services.length", expected.length, actual.length );
          for( int i = 0; i < expected.length; i++ )
          {
              assertEquals( message + ": Services[ " + i + "].type",
                            expected[ i ].getImplementationKey(),
                            actual[ i ].getImplementationKey() );
              assertEqualAttributes( message + ": Services[ " + i + "].attributes",
                                     expected[ i ].getAttributes(),
                                     actual[ i ].getAttributes() );
          }
      }
  
      private void assertEqualLoggers( final String message,
                                       final LoggerDescriptor[] expected,
                                       final LoggerDescriptor[] actual )
      {
          assertEquals( message + ": Loggers.length", expected.length, actual.length );
          for( int i = 0; i < expected.length; i++ )
          {
              assertEquals( message + ": Loggers[ " + i + "].name",
                            expected[ i ].getName(), actual[ i ].getName() );
              assertEqualAttributes( message + ": Loggers[ " + i + "].attributes",
                                     expected[ i ].getAttributes(), actual[ i ].getAttributes()
);
          }
      }
  
      private void assertEqualContext( final String message,
                                       final ContextDescriptor expected,
                                       final ContextDescriptor actual )
      {
          assertEquals( message + ": Context.type", expected.getType(), actual.getType() );
          assertEqualEntrys( message + ": Context.entrys", expected.getEntrys(), expected.getEntrys()
);
          assertEqualAttributes( message + ": Context.attribute",
                                 expected.getAttributes(),
                                 actual.getAttributes() );
      }
  
      private void assertEqualEntrys( final String message,
                                      final EntryDescriptor[] expected,
                                      final EntryDescriptor[] actual )
      {
          assertEquals( message + " Length", expected.length, actual.length );
          for( int i = 0; i < expected.length; i++ )
          {
              assertEquals( message + " [" + i + "].key",
                            expected[ i ].getKey(), actual[ i ].getKey() );
              assertEquals( message + " [" + i + "].type",
                            expected[ i ].getType(), actual[ i ].getType() );
              assertEqualAttributes( message + " [" + i + "].attribute",
                                     expected[ i ].getAttributes(),
                                     actual[ i ].getAttributes() );
          }
      }
  
      private void assertEqualComponents( final String message,
                                          final ComponentDescriptor expected,
                                          final ComponentDescriptor actual )
      {
          assertEquals( message + ": Component.name", expected.getName(), actual.getName()
);
          assertEquals( message + ": Component.type", expected.getImplementationKey(),
                        actual.getImplementationKey() );
          assertEqualAttributes( message + ": Component.attribute",
                                 expected.getAttributes(),
                                 actual.getAttributes() );
      }
  
      private void assertEqualAttributes( final String message,
                                          final Attribute[] expected,
                                          final Attribute[] actual )
      {
          assertEquals( message + " Length", expected.length, actual.length );
          for( int i = 0; i < expected.length; i++ )
          {
              assertEquals( message + " [" + i + "].name",
                            expected[ i ].getName(), actual[ i ].getName() );
              assertEqualParameters( message + " [" + i + "].parameters",
                                     expected[ i ], actual[ i ] );
          }
      }
  
      private void assertEqualParameters( final String message,
                                          final Attribute expected,
                                          final Attribute actual )
      {
          final String[] expectedNames = expected.getParameterNames();
          final String[] actualNames = actual.getParameterNames();
          assertEquals( message + " Length", expectedNames.length, actualNames.length );
  
          for( int i = 0; i < expectedNames.length; i++ )
          {
              final String name = expectedNames[ i ];
              assertEquals( message + " value",
                            expected.getParameter( name ),
                            actual.getParameter( name ) );
          }
      }
  
      private ComponentInfo constructDummyInfo()
      {
          final ComponentDescriptor component =
              new ComponentDescriptor( "component1",
                                       "org.realityforge.Component1",
                                       EMPTY_ATTRIBUTES );
  
          final LoggerDescriptor logger1 = new LoggerDescriptor( "", EMPTY_ATTRIBUTES );
          final LoggerDescriptor logger2 = new LoggerDescriptor( "audit", EMPTY_ATTRIBUTES
);
          final LoggerDescriptor[] loggers = new LoggerDescriptor[]{logger1, logger2};
  
          final EntryDescriptor entry1 = new EntryDescriptor( "mbean",
                                                              "javax.jmx.MBeanServer",
                                                              false,
                                                              EMPTY_ATTRIBUTES );
  
          final EntryDescriptor[] entrys = new EntryDescriptor[]{entry1};
          final ContextDescriptor context =
              new ContextDescriptor( "org.apache.avalon.phoenix.BlockContext",
                                     entrys,
                                     EMPTY_ATTRIBUTES );
  
          final Properties parameters = new Properties();
          parameters.setProperty( "display-name", "Special Service" );
          parameters.setProperty( "description-key", "service1.desc" );
          final Attribute attribute = new Attribute( "doc", parameters );
  
          final Attribute[] attributes = new Attribute[]{attribute};
          final ServiceDescriptor service1 = new ServiceDescriptor( "org.realityforge.Service1",
attributes );
          final ServiceDescriptor[] services = new ServiceDescriptor[]{service1};
          final DependencyDescriptor dependency1 =
              new DependencyDescriptor( "org.realityforge.Service2",
                                        "org.realityforge.Service2",
                                        true,
                                        EMPTY_ATTRIBUTES );
          final DependencyDescriptor dependency2 =
              new DependencyDescriptor( "foo",
                                        "org.realityforge.Service3",
                                        false,
                                        EMPTY_ATTRIBUTES );
          final DependencyDescriptor[] deps =
              new DependencyDescriptor[]{dependency1, dependency2};
  
          return new ComponentInfo( component, loggers, context, services, deps );
      }
  
      protected ComponentInfo loadComponentInfo( final String classname )
          throws Exception
      {
          final ClassLoader classLoader = getClass().getClassLoader();
          return createInfoBuilder().buildComponentInfo( classname, classLoader );
      }
  
      protected ServiceInfo loadServiceInfo( final String classname )
          throws Exception
      {
          final ClassLoader classLoader = getClass().getClassLoader();
          return createInfoBuilder().buildServiceInfo( classname, classLoader );
      }
  
      private InfoBuilder createInfoBuilder()
      {
          final InfoBuilder builder = new InfoBuilder();
          builder.enableLogging( new ConsoleLogger() );
          return builder;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/info/src/test/org/apache/avalon/framework/tools/infobuilder/test/data/component1-info.xml
  
  Index: component1-info.xml
  ===================================================================
  <?xml version="1.0" encoding="utf-8" ?>
  <!DOCTYPE component-info
        PUBLIC "-//AVALON/Component Info DTD Version 1.0//EN"
               "http://jakarta.apache.org/avalon/dtds/info/componentinfo_1_0.dtd" >
  
  <component-info>
      <component name="component1" type="org.realityforge.Component1">
      </component>
  
      <loggers>
          <logger/>
          <logger name="audit"/>
      </loggers>
  
      <context type="org.apache.avalon.phoenix.BlockContext">
          <entry key="mbean" type="javax.jmx.MBeanServer"/>
      </context>
  
      <services>
          <service type="org.realityforge.Service1">
              <attribute name="doc">
                  <param name="display-name" value="Special Service"/>
                  <param name="description-key" value="service1.desc"/>
              </attribute>
          </service>
      </services>
  
      <dependencies>
          <dependency type="org.realityforge.Service2" optional="true" />
          <dependency key="foo" type="org.realityforge.Service3"/>
      </dependencies>
  
  </component-info>
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message