cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r441970 - in /cocoon/trunk: blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/ core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/ core/cocoon-core/src/main/java/org/apache/cocoon/core/o...
Date Sun, 10 Sep 2006 17:03:05 GMT
Author: cziegeler
Date: Sun Sep 10 10:03:04 2006
New Revision: 441970

URL: http://svn.apache.org/viewvc?view=rev&rev=441970
Log:
Start getting unit tests working again after recent refactorings

Removed:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonEnvironment.java
Modified:
    cocoon/trunk/blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/JCRSourceTestCase.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonElementParser.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java

Modified: cocoon/trunk/blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/JCRSourceTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/JCRSourceTestCase.java?view=diff&rev=441970&r1=441969&r2=441970
==============================================================================
--- cocoon/trunk/blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/JCRSourceTestCase.java
(original)
+++ cocoon/trunk/blocks/cocoon-jcr/cocoon-jcr-impl/src/test/java/org/apache/cocoon/jcr/source/JCRSourceTestCase.java
Sun Sep 10 10:03:04 2006
@@ -16,7 +16,6 @@
 package org.apache.cocoon.jcr.source;
 
 import org.apache.avalon.framework.CascadingRuntimeException;
-import org.apache.avalon.framework.context.DefaultContext;
 import org.apache.avalon.framework.service.ServiceSelector;
 
 import org.apache.cocoon.core.container.ContainerTestCase;
@@ -34,6 +33,7 @@
 import java.io.OutputStream;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Map;
 
 /**
  * @version $Id$
@@ -44,7 +44,7 @@
 
     private File tempDir;
 
-    protected void addContext(DefaultContext context) {
+    protected void addContext(Map context) {
         super.addContext(context);
         // Create a temp file
         try {

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonElementParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonElementParser.java?view=diff&rev=441970&r1=441969&r2=441970
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonElementParser.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/container/spring/avalon/AvalonElementParser.java
Sun Sep 10 10:03:04 2006
@@ -39,6 +39,7 @@
 import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.config.RuntimeBeanReference;
+import org.springframework.beans.factory.support.BeanDefinitionReader;
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.beans.factory.xml.ParserContext;
@@ -60,52 +61,74 @@
      * @see org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org.w3c.dom.Element,
org.springframework.beans.factory.xml.ParserContext)
      */
     public BeanDefinition parse(Element element, ParserContext parserContext) {
+        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
+        // read avalon style configuration
+        // the schema ensures that location is never null
+        final String location = element.getAttribute("location");
+        try {
+            final ConfigurationInfo info = this.readConfiguration(location, resourceLoader);
+    
+            this.createComponents(element,
+                                  info,
+                                  parserContext.getRegistry(),
+                                  parserContext.getDelegate().getReaderContext().getReader(),
+                                  resourceLoader);
+        } catch (Exception e) {
+            throw new BeanDefinitionStoreException("Unable to read Avalon configuration from
'" + location + "'.",e);
+        }
+        return null;
+    }
+
+    /**
+     * 
+     * @param element        Can be null
+     * @param registry
+     * @param reader         Can be null
+     * @param resourceLoader
+     */
+    public void createComponents(Element                element,
+                                 ConfigurationInfo      info,
+                                 BeanDefinitionRegistry registry,
+                                 BeanDefinitionReader   reader,
+                                 ResourceLoader         resourceLoader)
+    throws Exception {
         // add context
-        this.addContext(element, parserContext.getRegistry());
+        this.addContext(element, registry);
 
         // add service manager
         this.addComponent(AvalonServiceManager.class,
                 ProcessingUtil.SERVICE_MANAGER_ROLE,
                 null,
                 false,
-                parserContext.getRegistry());
+                registry);
 
-        // read avalon style configuration
-        // the schema ensures that location is never null
-        final String location = element.getAttribute("location");
-        final ResourceLoader resourceLoader = parserContext.getReaderContext().getReader().getResourceLoader();
-        try {
-            final ConfigurationInfo info = this.readConfiguration(location, resourceLoader);
-            // add logger
-            final String loggingConfiguration = element.getAttribute("loggingConfiguration");
-            this.addLogger(loggingConfiguration, parserContext.getRegistry(), info.getRootLogger());
-
-            // handle includes of spring configurations
-            final Iterator includeIter = info.getImports().iterator();
-            while ( includeIter.hasNext() ) {
-                final String uri = (String)includeIter.next();
-                parserContext.getDelegate().getReaderContext().getReader().loadBeanDefinitions(resourceLoader.getResource(uri));
+        // add logger
+        final String loggingConfiguration = (element == null ? null : element.getAttribute("loggingConfiguration"));
+        this.addLogger(loggingConfiguration, registry, info.getRootLogger());
+
+        // handle includes of spring configurations
+        final Iterator includeIter = info.getImports().iterator();
+        while ( includeIter.hasNext() ) {
+            if ( reader == null ) {
+                throw new Exception("Import of spring configuration files not supported.
(Reader is null)");
             }
+            final String uri = (String)includeIter.next();
+            reader.loadBeanDefinitions(resourceLoader.getResource(uri));
+        }
 
-            // then create components
-            this.createConfig(info, parserContext.getRegistry());
-
-            // register component infos for child factories
-            this.registerComponentInfo(info, parserContext.getRegistry());
-
-            // and finally add avalon bean post processor
-            final RootBeanDefinition beanDef = this.createBeanDefinition(AvalonBeanPostProcessor.class,
null, true);
-            beanDef.getPropertyValues().addPropertyValue("logger", new RuntimeBeanReference(ProcessingUtil.LOGGER_ROLE));
-            beanDef.getPropertyValues().addPropertyValue("context", new RuntimeBeanReference(ProcessingUtil.CONTEXT_ROLE));
-            beanDef.getPropertyValues().addPropertyValue("configurationInfo", new RuntimeBeanReference(ConfigurationInfo.class.getName()));
+        // then create components
+        this.createConfig(info, registry);
 
-            this.register(beanDef, AvalonBeanPostProcessor.class.getName(), parserContext.getRegistry());
+        // register component infos for child factories
+        this.registerComponentInfo(info, registry);
 
-        } catch (Exception e) {
-            throw new BeanDefinitionStoreException("Unable to read Avalon configuration from
'" + location + "'.",e);
-        }
+        // and finally add avalon bean post processor
+        final RootBeanDefinition beanDef = this.createBeanDefinition(AvalonBeanPostProcessor.class,
null, true);
+        beanDef.getPropertyValues().addPropertyValue("logger", new RuntimeBeanReference(ProcessingUtil.LOGGER_ROLE));
+        beanDef.getPropertyValues().addPropertyValue("context", new RuntimeBeanReference(ProcessingUtil.CONTEXT_ROLE));
+        beanDef.getPropertyValues().addPropertyValue("configurationInfo", new RuntimeBeanReference(ConfigurationInfo.class.getName()));
 
-        return null;
+        this.register(beanDef, AvalonBeanPostProcessor.class.getName(), registry);
     }
 
     protected ConfigurationInfo readConfiguration(String location, ResourceLoader resourceLoader)

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java?view=diff&rev=441970&r1=441969&r2=441970
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
Sun Sep 10 10:03:04 2006
@@ -30,7 +30,6 @@
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.cocoon.ProcessingUtil;
 import org.apache.cocoon.configuration.Settings;
-import org.apache.cocoon.core.container.spring.avalon.AvalonEnvironment;
 import org.apache.cocoon.environment.Context;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -112,7 +111,7 @@
         // create a minimal OSGi servlet context
         Context osgiServletContext = new OSGiServletContext(this.logger, componentContext);
    
 
-        AvalonEnvironment avalonEnvironment = this.createAvalonEnvironment(osgiServletContext);
+        //AvalonEnvironment avalonEnvironment = this.createAvalonEnvironment(osgiServletContext);
 		
 		// get the configuration file property
 		String configFile= (String) this.componentContext.getProperties().get(CONFIG_FILE);
@@ -137,7 +136,6 @@
      * @return the Avalon environment
      * @throws ServletException
      * @throws MalformedURLException
-     */
     private AvalonEnvironment createAvalonEnvironment(Context osgiServletContext) throws
ServletException, MalformedURLException {
         URL manifestUrl = this.componentContext.getBundleContext().getBundle().getEntry(MANIFEST_FILE);
 		String contextPath = manifestUrl.toString();
@@ -156,6 +154,7 @@
 		avalonEnvironment.settings = this.settings;
         return avalonEnvironment;
     }
+     */
     
     protected void deactivate(ComponentContext componentContext) {
         this.beanFactory.destroySingletons();

Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java?view=diff&rev=441970&r1=441969&r2=441970
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
Sun Sep 10 10:03:04 2006
@@ -18,6 +18,9 @@
 
 import java.io.InputStream;
 import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
@@ -35,11 +38,22 @@
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.Constants;
+import org.apache.cocoon.ProcessingUtil;
+import org.apache.cocoon.configuration.Settings;
 import org.apache.cocoon.configuration.impl.MutableSettings;
-import org.apache.cocoon.core.container.spring.avalon.AvalonEnvironment;
+import org.apache.cocoon.core.container.spring.avalon.AvalonElementParser;
 import org.apache.cocoon.core.container.spring.avalon.ConfigurationInfo;
+import org.apache.cocoon.core.container.spring.avalon.ConfigurationReader;
 import org.apache.cocoon.environment.mock.MockContext;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.config.BeanDefinitionHolder;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.core.io.DefaultResourceLoader;
+import org.w3c.dom.Element;
 
 /**
  * JUnit TestCase for Cocoon Components.
@@ -129,6 +143,8 @@
     /** The service manager to use */
     private ServiceManager manager;
 
+    private Map contextProperties;
+
     /** The context */
     private Context context;
 
@@ -207,9 +223,9 @@
         }
 
         // setup context
-        this.context = this.setupContext( conf.getChild( "context" ) );
+        this.contextProperties = this.setupContext( conf.getChild( "context" ) );
 
-        this.setupBeanFactories( conf.getChild( "components" ),  conf.getChild( "roles" )
);
+        this.setupBeanFactory( conf.getChild( "components" ),  conf.getChild( "roles" ) );
     }
 
     /**
@@ -240,9 +256,9 @@
      * A method addContext(DefaultContext context) is called here to enable subclasses
      * to put additional objects into the context programmatically.
      */
-    final private Context setupContext( final Configuration conf )
+    final private Map setupContext( final Configuration conf )
     throws Exception {
-        final DefaultContext context = new DefaultContext();
+        final Map defaultContext = new HashMap();
         final Configuration[] confs = conf.getChildren( "entry" );
         for( int i = 0; i < confs.length; i++ ) {
             final String key = confs[ i ].getAttribute( "name" );
@@ -250,28 +266,27 @@
             if( value == null ) {
                 String clazz = confs[ i ].getAttribute( "class" );
                 Object obj = getClass().getClassLoader().loadClass( clazz ).newInstance();
-                context.put( key, obj );
+                defaultContext.put( key, obj );
                 if( getLogger().isInfoEnabled() ) {
                     getLogger().info( "ContainerTestCase: added an instance of class " +
clazz + " to context entry " + key );
                 }
             } else {
-                context.put( key, value );
+                defaultContext.put( key, value );
                 if( getLogger().isInfoEnabled() ) {
                     getLogger().info( "ContainerTestCase: added value \"" + value + "\" to
context entry " + key );
                 }
             }
         }
-        context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new MockContext());
-        this.addContext( context );
-        context.makeReadOnly();
-        return context ;
+        defaultContext.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new MockContext());
+        this.addContext( defaultContext );
+        return defaultContext ;
     }
 
     /**
      * This method may be overwritten by subclasses to put additional objects
      * into the context programmatically.
      */
-    protected void addContext( DefaultContext context ) {
+    protected void addContext( Map defaultContext ) {
         // nothing to add here
     }
 
@@ -284,21 +299,30 @@
         // subclasses can add components here
     }
 
-    final private void setupBeanFactories( final Configuration confCM,
-                                           final Configuration confRM)
+    protected void addSettings(BeanDefinitionRegistry registry) {
+        RootBeanDefinition def = new RootBeanDefinition();
+        def.setBeanClass(MutableSettings.class);
+        def.setSingleton(true);
+        def.setLazyInit(false);
+        def.getConstructorArgumentValues().addIndexedArgumentValue(0, "test");
+        BeanDefinitionHolder holder = new BeanDefinitionHolder(def, Settings.ROLE);
+        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
+    }
+
+    final private void setupBeanFactory( final Configuration confCM,
+                                         final Configuration confRM)
     throws Exception {
-        final AvalonEnvironment avalonEnv = new AvalonEnvironment();
-        avalonEnv.logger = this.logger;
-        avalonEnv.context = this.context;
-        avalonEnv.settings = new MutableSettings("test");
         // read roles and components
-        // FIXME
-        ConfigurationInfo rolesInfo = null;//ConfigReader.readConfiguration(confRM, confCM,
null, avalonEnv, null);
+        ConfigurationInfo rolesInfo = ConfigurationReader.readConfiguration(confRM, confCM,
null, null);
         this.addComponents( rolesInfo );
-        // FIXME
-        this.beanFactory = null; //BeanFactoryUtil.createBeanFactory(avalonEnv, rolesInfo,
null, null);
-
+        this.beanFactory = new DefaultListableBeanFactory();
+        this.addSettings((DefaultListableBeanFactory)this.beanFactory);
+        
+        AvalonInstantiator aep = new AvalonInstantiator(this.contextProperties);
+        aep.createComponents(null, rolesInfo, (DefaultListableBeanFactory)this.beanFactory,
 null, new DefaultResourceLoader());
+        ((DefaultListableBeanFactory)this.beanFactory).preInstantiateSingletons();
         this.manager = (ServiceManager)this.beanFactory.getBean(ServiceManager.class.getName());
+        this.context = (Context)this.beanFactory.getBean(ProcessingUtil.CONTEXT_ROLE);
     }
 
     protected final Object lookup( final String key )
@@ -310,22 +334,23 @@
         manager.release( object );
     }
 
-    private Object getComponent(String classname,
-                                Configuration conf,
-                                Parameters p)
+    private Object getComponent(String        classname,
+                                Configuration config,
+                                Parameters    params)
     throws Exception {
         final Object instance = Class.forName(classname).newInstance();
         ContainerUtil.enableLogging(instance, getLogger());
         ContainerUtil.contextualize(instance, this.context);
         ContainerUtil.service(instance, getManager());
         if ( instance instanceof Configurable ) {
+            Configuration c = config;
             // default configuration to invoke method!
-            if ( conf == null ) {
-                conf = new DefaultConfiguration("", "-");
+            if ( c == null ) {
+                c = new DefaultConfiguration("", "-");
             }
-            ContainerUtil.configure(instance, conf);
-        }
-        if ( instance instanceof Parameterizable ) {
+            ContainerUtil.configure(instance, c);
+        } else if ( instance instanceof Parameterizable ) {
+            Parameters p = params;
             // default configuration to invoke method!
             if ( p == null ) {
                 p = new Parameters();
@@ -351,5 +376,69 @@
     protected Object getComponent(String classname)
     throws Exception {
         return this.getComponent(classname, null, null);
+    }
+
+    protected static class AvalonInstantiator extends AvalonElementParser {
+
+        protected final Map properties;
+
+        public AvalonInstantiator(Map props) {
+            this.properties = props;
+        }
+
+        protected void addContext(Element element, BeanDefinitionRegistry registry) {
+            RootBeanDefinition def = this.createBeanDefinition(ContextFactoryBean.class,
"init", false);
+            def.getPropertyValues().addPropertyValue("properties", this.properties);
+            this.register(def, ProcessingUtil.CONTEXT_ROLE, registry);
+        }
+
+        /**
+         * @see org.apache.cocoon.core.container.spring.avalon.AvalonElementParser#addLogger(java.lang.String,
org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.String)
+         */
+        protected void addLogger(String configuration, BeanDefinitionRegistry registry, String
loggerCategory) {
+            this.addComponent(ConsoleLogger.class, ProcessingUtil.LOGGER_ROLE, null, false,
registry);
+        }
+    }
+
+    protected static class ContextFactoryBean implements FactoryBean {
+        protected Map properties;
+
+        protected final DefaultContext avalonContext = new DefaultContext();
+
+        public void setProperties(Map properties) {
+            this.properties = properties;
+        }
+
+        public void init() {
+            if ( this.properties != null ) {
+                final Iterator i = this.properties.entrySet().iterator();
+                while ( i.hasNext() ) {
+                    final Map.Entry current = (Map.Entry)i.next();
+                    this.avalonContext.put(current.getKey(), current.getValue());
+                }
+            }
+            this.avalonContext.makeReadOnly();
+        }
+
+        /**
+         * @see org.springframework.beans.factory.FactoryBean#getObject()
+         */
+        public Object getObject() throws Exception {
+            return this.avalonContext;
+        }
+
+        /**
+         * @see org.springframework.beans.factory.FactoryBean#getObjectType()
+         */
+        public Class getObjectType() {
+            return Context.class;
+        }
+
+        /**
+         * @see org.springframework.beans.factory.FactoryBean#isSingleton()
+         */
+        public boolean isSingleton() {
+            return true;
+        }
     }
 }



Mime
View raw message