cvs commit: spice/sandbox/metaclass/src/test/org/realityforge/metaclass/tools/tasks MockProject.java PluginSetTestCase.java

Peter Donald <[email protected]> Thu, 20 Nov 2003 00:42:50 -0600
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
pdonald     2003/11/20 00:42:50

  Added:       sandbox/metaclass/src/java/org/realityforge/metaclass/tools/tasks
                        FilterSet.java InterceptorSet.java PluginSet.java
               sandbox/metaclass/src/test/org/realityforge/metaclass/tools/tasks
                        MockProject.java PluginSetTestCase.java
  Log:
  Add in concept of FilterSet and InterceptorSet for Ant tasks
  
  Revision  Changes    Path
  1.1                  spice/sandbox/metaclass/src/java/org/realityforge/metaclass/tools/tasks/FilterSet.java
  
  Index: FilterSet.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.tasks;
  
  /**
   * An Ant type representing a set of Filter definitions.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/20 06:42:49 $
   */
  public class FilterSet
      extends PluginSet
  {
      /**
       * Add an Filter definition that will create Filter to process metadata.
       *
       * @param element the Filter definition
       */
      public void addFilter( final PluginElement element )
      {
          addPlugin( "Filter", element );
      }
  
      /**
       * Add a set of Filters.
       *
       * @param set the Filter set
       */
      public void addFilterSet( final FilterSet set )
      {
          addPluginSet( set );
      }
  }
  
  
  
  1.1                  spice/sandbox/metaclass/src/java/org/realityforge/metaclass/tools/tasks/InterceptorSet.java
  
  Index: InterceptorSet.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.tasks;
  
  /**
   * An Ant type representing a set of Interceptor definitions.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/20 06:42:49 $
   */
  public class InterceptorSet
      extends PluginSet
  {
      /**
       * Add an interceptor definition that will create interceptor to process metadata.
       *
       * @param element the interceptor definition
       */
      public void addInterceptor( final PluginElement element )
      {
          addPlugin( "Interceptor", element );
      }
  
      /**
       * Add a set of Interceptors.
       *
       * @param set the interceptor set
       */
      public void addInterceptorSet( final InterceptorSet set )
      {
          addPluginSet( set );
      }
  }
  
  
  
  1.1                  spice/sandbox/metaclass/src/java/org/realityforge/metaclass/tools/tasks/PluginSet.java
  
  Index: PluginSet.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.tasks;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.types.DataType;
  import org.apache.tools.ant.types.Reference;
  
  /**
   * An Ant type that represents a set of Plugins.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/20 06:42:49 $
   */
  abstract class PluginSet
      extends DataType
  {
      /**
       * Set of PluginElement objects.
       */
      private final ArrayList m_plugins = new ArrayList();
  
      /**
       * Set of PluginSet objects.
       */
      private final ArrayList m_sets = new ArrayList();
  
      /**
       * Add a plugin to set.
       *
       * @param type the type
       * @param element the interceptor definition
       */
      void addPlugin( final String type,
                      final PluginElement element )
      {
          if( null == element.getName() )
          {
              throw new BuildException( type + " must have a name" );
          }
          m_plugins.add( element );
      }
  
      /**
       * Add a plugin to set.
       *
       * @param set the set
       */
      void addPluginSet( final PluginSet set )
      {
          m_sets.add( set );
      }
  
      /**
       * Convert PluginSet to a collection of plugins.
       *
       * @return the collection of Plugins
       */
      Collection toPlugins()
      {
          final Collection result = new ArrayList();
          result.addAll( m_plugins );
          final Iterator iterator = m_sets.iterator();
          while( iterator.hasNext() )
          {
              final PluginSet set = (PluginSet)iterator.next();
              result.addAll( set.toPlugins() );
          }
          return result;
      }
  
      /**
       * Makes this instance in effect a reference to another PluginSet
       * instance.
       *
       * <p>You must not set another attribute or nest elements inside
       * this element if you make it a reference.</p>
       *
       * @param reference the reference to which this instance is associated
       * @exception BuildException if this instance already has been configured.
       */
      public void setRefid( final Reference reference )
          throws BuildException
      {
          if( !m_plugins.isEmpty() || !m_sets.isEmpty() )
          {
              throw tooManyAttributes();
          }
          // change this to get the objects from the other reference
          final Object object =
              reference.getReferencedObject( getProject() );
          final Class clazz = getClass();
          if( clazz.isInstance( object ) )
          {
              final PluginSet other = (PluginSet)object;
              m_plugins.addAll( other.m_plugins );
              m_sets.addAll( other.m_sets );
          }
          else
          {
              final String message = reference.getRefId() +
                  " doesn\'t refer to a " + clazz.getName();
              throw new BuildException( message );
          }
  
          super.setRefid( reference );
      }
  }
  
  
  
  1.1                  spice/sandbox/metaclass/src/test/org/realityforge/metaclass/tools/tasks/MockProject.java
  
  Index: MockProject.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.tasks;
  
  import org.apache.tools.ant.Project;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/20 06:42:49 $
   */
  class MockProject
      extends Project
  {
      private final Object m_reference;
  
      MockProject( final Object reference )
      {
          m_reference = reference;
      }
  
      public Object getReference( final String key )
      {
          return m_reference;
      }
  }
  
  
  
  1.1                  spice/sandbox/metaclass/src/test/org/realityforge/metaclass/tools/tasks/PluginSetTestCase.java
  
  Index: PluginSetTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.tasks;
  
  import junit.framework.TestCase;
  import java.util.Collection;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.types.Reference;
  
  /**
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/20 06:42:50 $
   */
  public class PluginSetTestCase
      extends TestCase
  {
      public void testAddPluginWithNullName()
          throws Exception
      {
          final PluginElement element = new PluginElement();
          final FilterSet set = new FilterSet();
          try
          {
              set.addFilter( element );
          }
          catch( BuildException e )
          {
              return;
          }
          fail( "Expected to fail due to badly specified element" );
      }
  
      public void testAddSinglePlugin()
          throws Exception
      {
          final PluginElement element = new PluginElement();
          element.setName( "foo" );
          final FilterSet set = new FilterSet();
          set.addFilter( element );
          final Collection collection = set.toPlugins();
          assertEquals( "collection.size()", 1, collection.size() );
          assertEquals( "collection(0)",
                        element,
                        collection.iterator().next() );
      }
  
      public void testAddSinglePluginSet()
          throws Exception
      {
          final PluginElement element = new PluginElement();
          element.setName( "foo" );
          final FilterSet set = new FilterSet();
          set.addFilter( element );
          final FilterSet parent = new FilterSet();
          parent.addFilterSet( set );
          final Collection collection = parent.toPlugins();
          assertEquals( "collection.size()", 1, collection.size() );
          assertEquals( "collection(0)",
                        element,
                        collection.iterator().next() );
      }
  
      public void testSetRefID()
          throws Exception
      {
          final PluginElement element = new PluginElement();
          element.setName( "foo" );
          final FilterSet set = new FilterSet();
          set.addFilter( element );
          final FilterSet parent = new FilterSet();
          final MockProject project = new MockProject( set );
          parent.setProject( project );
          parent.setRefid( new Reference( "bar" ) );
  
          final Collection collection = parent.toPlugins();
          assertEquals( "collection.size()", 1, collection.size() );
          assertEquals( "collection(0)",
                        element,
                        collection.iterator().next() );
      }
  
      public void testSetRefIDOnBadType()
          throws Exception
      {
          final FilterSet parent = new FilterSet();
          final MockProject project = new MockProject( new Object() );
          parent.setProject( project );
          try
          {
              parent.setRefid( new Reference( "bar" ) );
          }
          catch( final BuildException be )
          {
              return;
          }
          fail( "Expected build exception due to bad type" );
      }
  
      public void testSetRefIDWithPluginAdded()
          throws Exception
      {
  
          final InterceptorSet parent = new InterceptorSet();
          final PluginElement element = new PluginElement();
          element.setName( "foo" );
          parent.addInterceptor( element );
          parent.addInterceptorSet( new InterceptorSet() );
          final MockProject project = new MockProject( new Object() );
          parent.setProject( project );
          try
          {
              parent.setRefid( new Reference( "bar" ) );
          }
          catch( final BuildException be )
          {
              return;
          }
          fail( "Expected build exception due to too many attributes" );
      }
  }