You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/12/22 21:46:27 UTC
svn commit: r489752 - in
/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon:
AbstractTestCase.java SitemapComponentTestCase.java SitemapTestCase.java
core/container/ContainerTestCase.java
environment/mock/MockRequestAttributes.java
Author: cziegeler
Date: Fri Dec 22 12:46:26 2006
New Revision: 489752
URL: http://svn.apache.org/viewvc?view=rev&rev=489752
Log:
Start cleaning up test cases
Added:
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java (with props)
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java (with props)
Modified:
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapComponentTestCase.java
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapTestCase.java
cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/core/container/ContainerTestCase.java
Added: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java?view=auto&rev=489752
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java (added)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java Fri Dec 22 12:46:26 2006
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.cocoon.configuration.Settings;
+import org.apache.cocoon.configuration.impl.MutableSettings;
+import org.apache.cocoon.core.container.spring.ServletContextFactoryBean;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.mock.MockContext;
+import org.apache.cocoon.environment.mock.MockRequest;
+import org.apache.cocoon.environment.mock.MockRequestAttributes;
+import org.apache.cocoon.environment.mock.MockResponse;
+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.DefaultListableBeanFactory;
+import org.springframework.beans.factory.support.RootBeanDefinition;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.request.RequestContextHolder;
+
+/**
+ * This class sets up all necessary environment information to implement
+ * own test cases.
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public abstract class AbstractTestCase extends TestCase {
+
+ private MockRequest request;
+ private MockResponse response;
+ private MockContext context;
+ private Map objectmodel;
+
+ private MockRequestAttributes requestAttributes;
+
+ /** The bean factory. */
+ private DefaultListableBeanFactory beanFactory;
+
+ public final MockRequest getRequest() {
+ return this.request;
+ }
+
+ public final MockResponse getResponse() {
+ return this.response;
+ }
+
+ public final MockContext getContext() {
+ return this.context;
+ }
+
+ public final Map getObjectModel() {
+ return this.objectmodel;
+ }
+
+ /** Return the bean factory. */
+ protected ConfigurableListableBeanFactory getBeanFactory() {
+ return this.beanFactory;
+ }
+
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ // setup object model
+ this.setUpObjectModel();
+
+ // create bean factory
+ this.createBeanFactory();
+ // initialize bean factory
+ this.initBeanFactory();
+
+ // setup request attributes
+ this.requestAttributes = new MockRequestAttributes(this.getRequest());
+ RequestContextHolder.setRequestAttributes(this.requestAttributes);
+
+ // setting up an webapplicationcontext is neccesarry to make spring believe
+ // it runs in a servlet container. we initialize it with our current
+ // bean factory to get consistent bean resolution behaviour
+ this.setUpRootApplicationContext();
+ }
+
+ /**
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ if ( this.requestAttributes != null ) {
+ this.requestAttributes.requestCompleted();
+ this.requestAttributes = null;
+ }
+ RequestContextHolder.resetRequestAttributes();
+
+ if( this.beanFactory != null ) {
+ this.beanFactory.destroySingletons();
+ this.beanFactory = null;
+ }
+ super.tearDown();
+ }
+
+ /**
+ * Set up the object model.
+ */
+ protected void setUpObjectModel() {
+ this.request = this.createRequest();
+ this.response = this.createResponse();
+ this.context = this.createContext();
+ this.objectmodel = this.createObjectModel();
+
+ this.objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, this.getRequest());
+ this.objectmodel.put(ObjectModelHelper.RESPONSE_OBJECT, this.getResponse());
+ this.objectmodel.put(ObjectModelHelper.CONTEXT_OBJECT, this.getContext());
+ }
+
+ protected void setUpRootApplicationContext() {
+ // set up servlet context access first
+ final ServletContextFactoryBean scfb = new ServletContextFactoryBean();
+ scfb.setServletContext(this.getContext());
+
+ WebApplicationContext staticWebApplicationContext = new MockWebApplicationContext(this.getBeanFactory(), this.getContext());
+ this.getContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, staticWebApplicationContext);
+ }
+
+ protected void createBeanFactory()
+ throws Exception {
+ this.beanFactory = new DefaultListableBeanFactory();
+ this.addSettings();
+ }
+
+ protected void initBeanFactory() {
+ this.beanFactory.preInstantiateSingletons();
+ }
+
+ protected void addSettings() {
+ 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, this.beanFactory);
+ }
+
+ protected MockRequest createRequest() {
+ return new MockRequest();
+ }
+
+ protected MockResponse createResponse() {
+ return new MockResponse();
+ }
+
+ protected Map createObjectModel() {
+ return new HashMap();
+ }
+
+ protected MockContext createContext() {
+ return new MockContext();
+ }
+}
Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/AbstractTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapComponentTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapComponentTestCase.java?view=diff&rev=489752&r1=489751&r2=489752
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapComponentTestCase.java (original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapComponentTestCase.java Fri Dec 22 12:46:26 2006
@@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -40,18 +39,11 @@
import org.apache.cocoon.components.flow.FlowHelper;
import org.apache.cocoon.components.flow.Interpreter;
import org.apache.cocoon.components.source.SourceResolverAdapter;
-import org.apache.cocoon.core.container.spring.Container;
-import org.apache.cocoon.core.container.spring.ServletContextFactoryBean;
import org.apache.cocoon.core.container.spring.avalon.ComponentInfo;
import org.apache.cocoon.core.container.spring.avalon.ConfigurationInfo;
-import org.apache.cocoon.environment.ObjectModelHelper;
-import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.internal.EnvironmentHelper;
-import org.apache.cocoon.environment.mock.MockContext;
import org.apache.cocoon.environment.mock.MockEnvironment;
import org.apache.cocoon.environment.mock.MockRedirector;
-import org.apache.cocoon.environment.mock.MockRequest;
-import org.apache.cocoon.environment.mock.MockResponse;
import org.apache.cocoon.generation.Generator;
import org.apache.cocoon.matching.Matcher;
import org.apache.cocoon.serialization.Serializer;
@@ -64,7 +56,6 @@
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.xml.sax.SAXParser;
import org.custommonkey.xmlunit.Diff;
-import org.springframework.web.context.WebApplicationContext;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -78,37 +69,17 @@
public final static Parameters EMPTY_PARAMS = Parameters.EMPTY_PARAMETERS;
- private MockRequest request = new MockRequest();
- private MockResponse response = new MockResponse();
- private MockContext context = new MockContext();
private MockRedirector redirector = new MockRedirector();
- private Map objectmodel = new HashMap();
-
- public final MockRequest getRequest() {
- return request;
- }
-
- public final MockResponse getResponse() {
- return response;
- }
-
- public final MockContext getContext() {
- return context;
- }
public final MockRedirector getRedirector() {
return redirector;
}
- public final Map getObjectModel() {
- return objectmodel;
- }
-
protected void addContext(DefaultContext context) {
- context.put(ContextHelper.CONTEXT_REQUEST_OBJECT, request);
- context.put(ContextHelper.CONTEXT_RESPONSE_OBJECT, response);
- context.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectmodel);
- context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, getContext());
+ context.put(ContextHelper.CONTEXT_REQUEST_OBJECT, this.getRequest());
+ context.put(ContextHelper.CONTEXT_RESPONSE_OBJECT, this.getResponse());
+ context.put(ContextHelper.CONTEXT_OBJECT_MODEL, this.getObjectModel());
+ context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, this.getContext());
}
/**
@@ -116,30 +87,7 @@
*/
public void setUp() throws Exception {
super.setUp();
- objectmodel.clear();
-
- request.reset();
- MockContext cont = this.getContext();
-
- //setting up an webapplicationcontext is neccesarry to make spring believe
- //it runs in a servlet container. we initialize it with our current
- //bean factory to get consistent bean resolution behaviour
- WebApplicationContext staticWebApplicationContext = new MockWebApplicationContext(this.getBeanFactory(), cont);
- cont.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, staticWebApplicationContext);
- // now setup container stuff
- ServletContextFactoryBean scfb = new ServletContextFactoryBean();
- scfb.setServletContext(cont);
- final Container container = Container.getCurrentContainer();
- request.setAttribute(Container.CONTAINER_REQUEST_ATTRIBUTE, container, Request.REQUEST_SCOPE);
- objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request);
-
- response.reset();
- objectmodel.put(ObjectModelHelper.RESPONSE_OBJECT, response);
-
- context.reset();
- objectmodel.put(ObjectModelHelper.CONTEXT_OBJECT, context);
-
- redirector.reset();
+ this.redirector.reset();
}
/**
@@ -206,7 +154,7 @@
matcher = (Matcher) selector.select(type);
assertNotNull("Test lookup of matcher", matcher);
- result = matcher.match(pattern, objectmodel, parameters);
+ result = matcher.match(pattern, this.getObjectModel(), parameters);
} catch (ServiceException ce) {
getLogger().error("Could not retrieve matcher", ce);
@@ -248,7 +196,7 @@
assertNotNull("Test lookup of selector", sel);
- result = sel.select(expression, objectmodel, parameters);
+ result = sel.select(expression, this.getObjectModel(), parameters);
} catch (ServiceException ce) {
getLogger().error("Could not retrieve selector", ce);
@@ -292,7 +240,7 @@
assertNotNull("Test lookup of action", action);
result = action.act(redirector, new SourceResolverAdapter(resolver, this.getManager()),
- objectmodel, source, parameters);
+ this.getObjectModel(), source, parameters);
} catch (ServiceException ce) {
getLogger().error("Could not retrieve action", ce);
@@ -340,7 +288,7 @@
assertNotNull("Test lookup of generator", generator);
generator.setup(new SourceResolverAdapter(resolver, getManager()),
- objectmodel, source, parameters);
+ this.getObjectModel(), source, parameters);
DOMBuilder builder = new DOMBuilder();
generator.setConsumer(new WhitespaceFilter(builder));
@@ -379,7 +327,7 @@
// enter & leave environment, as a manager is looked up using
// the processing context stack
MockEnvironment env = new MockEnvironment();
- env.setObjectModel(this.objectmodel);
+ env.setObjectModel(this.getObjectModel());
Processor processor = new MockProcessor(this.getBeanFactory());
EnvironmentHelper.enterProcessor(processor, env);
@@ -411,7 +359,7 @@
assertNotNull("Test lookup of transformer", transformer);
transformer.setup(new SourceResolverAdapter(resolver, getManager()),
- objectmodel, source, parameters);
+ this.getObjectModel(), source, parameters);
DOMBuilder builder = new DOMBuilder();
transformer.setConsumer(new WhitespaceFilter(builder));
Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapTestCase.java?view=diff&rev=489752&r1=489751&r2=489752
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapTestCase.java (original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/SitemapTestCase.java Fri Dec 22 12:46:26 2006
@@ -19,42 +19,25 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Vector;
-import junit.framework.TestCase;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.cocoon.Processor;
-import org.apache.cocoon.configuration.PropertyProvider;
-import org.apache.cocoon.core.TestPropertyProvider;
import org.apache.cocoon.environment.Environment;
-import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.internal.EnvironmentHelper;
-import org.apache.cocoon.environment.mock.MockContext;
import org.apache.cocoon.environment.mock.MockEnvironment;
-import org.apache.cocoon.environment.mock.MockRequest;
-import org.apache.cocoon.environment.mock.MockResponse;
-import org.springframework.beans.factory.config.ConfigurableBeanFactory;
/**
* TODO - We need to find a way to setup the bean factory!
* @version $Id$
*
*/
-public class SitemapTestCase extends TestCase {
-
- private MockRequest request = new MockRequest();
- private MockResponse response = new MockResponse();
- private MockContext environmentContext = new MockContext();
- private Map objectmodel = new HashMap();
+public class SitemapTestCase extends AbstractTestCase {
private Logger logger;
private String classDir;
- private ConfigurableBeanFactory container;
private ServiceManager serviceManager;
private Processor rootProcessor;
@@ -64,33 +47,16 @@
String level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_DEBUG);
this.logger = new ConsoleLogger(Integer.parseInt(level));
- objectmodel.clear();
-
- request.reset();
- objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request);
-
- response.reset();
- objectmodel.put(ObjectModelHelper.RESPONSE_OBJECT, response);
-
- environmentContext.reset();
- objectmodel.put(ObjectModelHelper.CONTEXT_OBJECT, environmentContext);
-
this.classDir = this.getClassDirURL().toExternalForm();
- PropertyProvider env = new TestPropertyProvider();
-
- this.container = null; //CoreUtil.createRootContainer(new MockContext(), env);
- this.serviceManager = (ServiceManager)this.container.getBean(ServiceManager.class.getName());
- this.rootProcessor = (Processor)this.container.getBean(Processor.ROLE);
}
/**
- * @see junit.framework.TestCase#tearDown()
+ * @see org.apache.cocoon.AbstractTestCase#initBeanFactory()
*/
- protected void tearDown() throws Exception {
- if ( this.container != null ) {
- this.container.destroySingletons();
- }
- super.tearDown();
+ protected void initBeanFactory() {
+ super.initBeanFactory();
+ this.serviceManager = (ServiceManager)this.getBeanFactory().getBean(ServiceManager.class.getName());
+ this.rootProcessor = (Processor)this.getBeanFactory().getBean(Processor.ROLE);
}
/** Return the logger */
@@ -118,19 +84,19 @@
protected URL getClassDirURL() throws RuntimeException {
String className = getClass().getName().replace( '.', '/' ) + ".class";
String classURL = null;
- String classDir = null;
+ String localClassDir = null;
try {
classURL =
getClass().getClassLoader().getResource( className ).toExternalForm();
getLogger().debug("classURL=" + classURL);
- classDir = classURL.substring(0, classURL.lastIndexOf('/') + 1);
- getLogger().debug("classDir=" + classDir);
- return new URL(classDir);
+ localClassDir = classURL.substring(0, classURL.lastIndexOf('/') + 1);
+ getLogger().debug("classDir=" + localClassDir);
+ return new URL(localClassDir);
} catch (SecurityException e) {
throw new RuntimeException("Not allowed to access classloader for " + className, e);
} catch (MalformedURLException e) {
throw new RuntimeException("Malformed URL for className=" + className +
- " classURL=" + classURL + " classDir=" + classDir, e);
+ " classURL=" + classURL + " classDir=" + localClassDir, e);
} catch (Exception e) {
throw new RuntimeException("Couldn't create URL for " + className, e);
}
@@ -194,8 +160,8 @@
protected MockEnvironment getEnvironment(String uri) {
MockEnvironment env = new MockEnvironment();
env.setURI("", uri);
- this.request.setEnvironment(env);
- env.setObjectModel(this.objectmodel);
+ this.getRequest().setEnvironment(env);
+ env.setObjectModel(this.getObjectModel());
return env;
}
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=489752&r1=489751&r2=489752
==============================================================================
--- 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 Fri Dec 22 12:46:26 2006
@@ -24,8 +24,6 @@
import java.util.Iterator;
import java.util.Map;
-import junit.framework.TestCase;
-
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
@@ -40,9 +38,8 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.AbstractTestCase;
import org.apache.cocoon.Constants;
-import org.apache.cocoon.configuration.Settings;
-import org.apache.cocoon.configuration.impl.MutableSettings;
import org.apache.cocoon.core.container.spring.avalon.AvalonBeanPostProcessor;
import org.apache.cocoon.core.container.spring.avalon.AvalonElementParser;
import org.apache.cocoon.core.container.spring.avalon.AvalonUtils;
@@ -50,11 +47,7 @@
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;
@@ -139,7 +132,7 @@
*
* @version $Id$
*/
-public abstract class ContainerTestCase extends TestCase {
+public abstract class ContainerTestCase extends AbstractTestCase {
/** The default logger */
private Logger logger;
@@ -152,9 +145,6 @@
/** The context */
private Context context;
- /** The bean factory containing the avalon components. */
- private ConfigurableListableBeanFactory beanFactory;
-
/** Return the logger */
protected Logger getLogger() {
return logger;
@@ -165,20 +155,14 @@
return this.manager;
}
- /** Return the bean factory. */
- protected ConfigurableListableBeanFactory getBeanFactory() {
- return this.beanFactory;
- }
-
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
- super.setUp();
-
String level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_WARN);
this.logger = new ConsoleLogger(Integer.parseInt(level));
- this.prepare();
+
+ super.setUp();
}
/**
@@ -202,6 +186,15 @@
}
/**
+ * @see org.apache.cocoon.AbstractTestCase#createBeanFactory()
+ */
+ protected void createBeanFactory()
+ throws Exception {
+ super.createBeanFactory();
+ this.prepare();
+ }
+
+ /**
* Initializes the ComponentLocator
*
* @param testconf The configuration file is passed as a <code>InputStream</code>
@@ -250,10 +243,6 @@
* Disposes the <code>ComponentLocator</code>
*/
final private void done() {
- if( this.beanFactory != null ) {
- this.beanFactory.destroySingletons();
- this.beanFactory = null;
- }
this.manager = null;
this.context = null;
this.logger = null;
@@ -320,38 +309,31 @@
// subclasses can add components here
}
- 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 {
// read roles and components
ConfigurationInfo rolesInfo = ConfigurationReader.readConfiguration(confRM, confCM, null, null);
this.addComponents( rolesInfo );
- this.beanFactory = new DefaultListableBeanFactory();
- this.addSettings((DefaultListableBeanFactory)this.beanFactory);
final AvalonInstantiator aep = new AvalonInstantiator(this.contextProperties);
- aep.createComponents(null, rolesInfo, (DefaultListableBeanFactory)this.beanFactory, null, new DefaultResourceLoader());
+ aep.createComponents(null, rolesInfo, (BeanDefinitionRegistry) this.getBeanFactory(), null, new DefaultResourceLoader());
AvalonBeanPostProcessor postProcessor = new AvalonBeanPostProcessor();
postProcessor.setLogger(new ConsoleLogger());
postProcessor.setContext(this.context);
postProcessor.setConfigurationInfo(rolesInfo);
- postProcessor.setBeanFactory(this.beanFactory);
- this.beanFactory.addBeanPostProcessor(postProcessor);
+ postProcessor.setBeanFactory(this.getBeanFactory());
+ this.getBeanFactory().addBeanPostProcessor(postProcessor);
+ }
- ((DefaultListableBeanFactory)this.beanFactory).preInstantiateSingletons();
- this.manager = (ServiceManager)this.beanFactory.getBean(ServiceManager.class.getName());
- this.context = (Context)this.beanFactory.getBean(AvalonUtils.CONTEXT_ROLE);
+ /**
+ * @see org.apache.cocoon.AbstractTestCase#initBeanFactory()
+ */
+ protected void initBeanFactory() {
+ super.initBeanFactory();
+ this.manager = (ServiceManager)this.getBeanFactory().getBean(ServiceManager.class.getName());
+ this.context = (Context)this.getBeanFactory().getBean(AvalonUtils.CONTEXT_ROLE);
}
protected final Object lookup( final String key )
Added: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java?view=auto&rev=489752
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java (added)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java Fri Dec 22 12:46:26 2006
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.environment.mock;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.Session;
+import org.springframework.web.context.request.RequestAttributes;
+
+/**
+ * This is an implementation of Springs {@link RequestAttributes} based
+ * on the Cocoon request interface.
+ * Request scope is mapped to Cocoon request scope, session scope to Cocoon's
+ * request global scope and global session scope to session scope!
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public class MockRequestAttributes
+ implements RequestAttributes {
+
+ final protected Request request;
+
+ final protected Map callbacks = new HashMap();
+
+ public MockRequestAttributes(Request r) {
+ this.request = r;
+ }
+
+ /**
+ * @see org.springframework.web.context.scope.RequestAttributes#getAttribute(java.lang.String, int)
+ */
+ public Object getAttribute(String key, int scope) {
+ if ( scope == RequestAttributes.SCOPE_REQUEST ) {
+ return this.request.getAttribute(key, Request.REQUEST_SCOPE);
+ }
+ if ( scope == RequestAttributes.SCOPE_SESSION ) {
+ return this.request.getAttribute(key, Request.GLOBAL_SCOPE);
+ }
+ final Session session = this.request.getSession(false);
+ if ( session != null ) {
+ return session.getAttribute(key);
+ }
+ return null;
+ }
+
+ /**
+ * @see org.springframework.web.context.scope.RequestAttributes#getSessionMutex()
+ */
+ public Object getSessionMutex() {
+ return this.request.getSession();
+ }
+
+ /**
+ * @see org.springframework.web.context.scope.RequestAttributes#removeAttribute(java.lang.String, int)
+ */
+ public void removeAttribute(String key, int scope) {
+ if ( scope == RequestAttributes.SCOPE_REQUEST ) {
+ this.request.removeAttribute(key, Request.REQUEST_SCOPE);
+ }
+ if ( scope == RequestAttributes.SCOPE_SESSION ) {
+ this.request.removeAttribute(key, Request.GLOBAL_SCOPE);
+ }
+ if ( scope == RequestAttributes.SCOPE_GLOBAL_SESSION ) {
+ final Session session = this.request.getSession(false);
+ if ( session != null ) {
+ session.removeAttribute(key);
+ }
+ }
+ }
+
+ /**
+ * @see org.springframework.web.context.scope.RequestAttributes#setAttribute(java.lang.String, java.lang.Object, int)
+ */
+ public void setAttribute(String key, Object value, int scope) {
+ if ( scope == RequestAttributes.SCOPE_REQUEST ) {
+ this.request.setAttribute(key, value, Request.REQUEST_SCOPE);
+ }
+ if ( scope == RequestAttributes.SCOPE_SESSION ) {
+ this.request.setAttribute(key, value, Request.GLOBAL_SCOPE);
+ }
+ if ( scope == RequestAttributes.SCOPE_GLOBAL_SESSION ) {
+ final Session session = this.request.getSession(true);
+ session.setAttribute(key, value);
+ }
+ }
+
+ /**
+ * @see org.springframework.web.context.request.RequestAttributes#getSessionId()
+ */
+ public String getSessionId() {
+ return this.request.getSession().getId();
+ }
+
+ /**
+ * @see org.springframework.web.context.request.RequestAttributes#registerDestructionCallback(java.lang.String, java.lang.Runnable, int)
+ */
+ public void registerDestructionCallback(String name, Runnable task, int scope) {
+ this.callbacks.put(name, task);
+ }
+
+ public void requestCompleted() {
+ final Iterator i = this.callbacks.values().iterator();
+ while ( i.hasNext() ) {
+ final Runnable task = (Runnable)i.next();
+ task.run();
+ }
+ }
+}
Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/environment/mock/MockRequestAttributes.java
------------------------------------------------------------------------------
svn:keywords = Id