Author: danielf Date: Sun Apr 24 00:12:35 2005 New Revision: 164455 URL: http://svn.apache.org/viewcvs?rev=164455&view=rev Log: Refactored SitemapTestCase to be based on Core, CoreUtil and Cocoon.
Added: cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java (with props) cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java (with props) Modified: cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java cocoon/trunk/src/test/org/apache/cocoon/components/blocks/BlockManagerTestCase.xconf cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockRequest.java cocoon/trunk/src/test/org/apache/cocoon/generation/VirtualPipelineGeneratorTestCase.xconf cocoon/trunk/src/test/org/apache/cocoon/reading/VirtualPipelineReaderTestCase.xconf cocoon/trunk/src/test/org/apache/cocoon/serialization/VirtualPipelineSerializerTestCase.xconf cocoon/trunk/src/test/org/apache/cocoon/transformation/VirtualPipelineTransformerTestCase.xconf Modified: cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java (original) +++ cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java Sun Apr 24 00:12:35 2005 @@ -15,50 +15,89 @@ */ package org.apache.cocoon; +import java.io.InputStream; import java.net.URL; +import java.util.HashMap; +import java.util.Map; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.DefaultConfiguration; -import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; -import org.apache.avalon.framework.context.DefaultContext; -import org.apache.cocoon.Constants; -import org.apache.cocoon.Processor; -import org.apache.cocoon.components.ContextHelper; -import org.apache.cocoon.environment.internal.EnvironmentHelper; +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.cocoon.Cocoon; +import org.apache.cocoon.core.BootstrapEnvironment; +import org.apache.cocoon.core.CoreUtil; +import org.apache.cocoon.core.TestBootstrapEnvironment; +import org.apache.cocoon.core.TestCoreUtil; +import org.apache.cocoon.environment.ObjectModelHelper; +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.apache.excalibur.source.Source; +import org.apache.excalibur.source.SourceResolver; + +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(); + + private Logger logger; + private CoreUtil coreUtil; + private Cocoon cocoon; + private String classDir; -public class SitemapTestCase extends SitemapComponentTestCase { + protected void setUp() throws Exception { + super.setUp(); - protected String classDir; - protected URL classDirURL; + String level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_WARN); + this.logger = new ConsoleLogger(Integer.parseInt(level)); - public void setUp() throws Exception { - this.classDirURL = getClassDirURL(); - this.classDir = this.classDirURL.toExternalForm(); - super.setUp(); + 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); + + String className = this.getClass().getName(); + this.classDir = this.getClassDirURL().toExternalForm(); + BootstrapEnvironment env = + new TestBootstrapEnvironment(className.substring(className.lastIndexOf('.') + 1) + ".xconf", + this.getClass().getClassLoader(), + this.classDir, + environmentContext, + this.logger); + + this.coreUtil = new TestCoreUtil(env); + this.cocoon = this.coreUtil.createCocoon(); } - protected void prepare() - throws Exception { - // The context is set in addContext instead - Configuration context = new DefaultConfiguration("", "-"); - - URL rolesURL = - getClass().getClassLoader().getResource("org/apache/cocoon/cocoon.roles"); - DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); - Configuration roles = builder.build( rolesURL.openStream() ); - - String componentsName = getClass().getName().replace( '.', '/' ) + ".xconf"; - URL componentsURL = getClass().getClassLoader().getResource( componentsName ); - Configuration components; - if ( componentsURL != null ) { - components = builder.build( componentsURL.openStream() ); - } else { - components = new DefaultConfiguration("", "-"); - } - prepare(context, roles, components); + protected void tearDown() throws Exception { + this.coreUtil.destroy(); + super.tearDown(); } + /** Return the logger */ + protected Logger getLogger() { + return this.logger; + } + + protected final Object lookup( final String key ) + throws ServiceException { + return this.cocoon.getServiceManager().lookup( key ); + } + + protected final void release( final Object object ) { + this.cocoon.getServiceManager().release( object ); + } + /** * Utility method for geting the URL to the directory that this class is in */ @@ -75,33 +114,79 @@ } } - protected void addContext(DefaultContext context) { - super.addContext(context); - context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, getContext()); - context.put(ContextHelper.CONTEXT_ROOT_URL, this.classDirURL); + /** + * Load a binary document. + * + * @param source Source location. + * + * @return Binary data. + */ + public final byte[] loadByteArray(String source) { + + SourceResolver resolver = null; + Source assertionsource = null; + byte[] assertiondocument = null; + + try { + resolver = (SourceResolver) this.cocoon.getSourceResolver(); + assertNotNull("Test lookup of source resolver", resolver); + + assertionsource = resolver.resolveURI(source); + assertNotNull("Test lookup of assertion source", assertionsource); + assertTrue("Test if source exist", assertionsource.exists()); + + assertNotNull("Test if inputstream of the assertion source is not null", + assertionsource.getInputStream()); + + InputStream input = assertionsource.getInputStream(); + long size = assertionsource.getContentLength(); + + assertiondocument = new byte[(int) size]; + int i = 0; + int c; + + while ((c = input.read())!=-1) { + assertiondocument[i] = (byte) c; + i++; + } + + } catch (Exception e) { + getLogger().error("Could not execute test", e); + fail("Could not execute test: "+e); + } + + return assertiondocument; } - protected boolean addSourceFactories() { - return false; + /** + * Assert that the result of a byte comparison is identical. + * + * @param expected The expected byte array + * @param actual The actual byte array + */ + public final void assertIdentical(byte[] expected, byte[] actual) { + assertEquals("Byte arrays of differing sizes, ", expected.length, + actual.length); + + if (expected.length>0) { + for (int i = 0; i<expected.length; i++) { + assertEquals("Byte array differs at index "+i, expected[i], + actual[i]); + } + } + } protected byte[] process(String uri) throws Exception { - Processor processor = (Processor)this.lookup(Processor.ROLE); MockEnvironment env = new MockEnvironment(); env.setURI("", uri); - getRequest().setEnvironment(env); - env.setObjectModel(getObjectModel()); + this.request.setEnvironment(env); + env.setObjectModel(this.objectmodel); - EnvironmentHelper.enterProcessor(processor, this.getManager(), env); - try { - processor.process(env); - getLogger().info("Output: " + new String(env.getOutput(), "UTF-8")); + this.cocoon.process(env); + getLogger().info("Output: " + new String(env.getOutput(), "UTF-8")); - return env.getOutput(); - } finally { - EnvironmentHelper.leaveProcessor(); - this.release(processor); - } + return env.getOutput(); } protected void pipeTest(String uri, String expectedSource) throws Exception { Modified: cocoon/trunk/src/test/org/apache/cocoon/components/blocks/BlockManagerTestCase.xconf URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/components/blocks/BlockManagerTestCase.xconf?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/components/blocks/BlockManagerTestCase.xconf (original) +++ cocoon/trunk/src/test/org/apache/cocoon/components/blocks/BlockManagerTestCase.xconf Sun Apr 24 00:12:35 2005 @@ -15,7 +15,10 @@ limitations under the License. --> -<components> +<cocoon version="2.2"> + + <include src="resource://org/apache/cocoon/cocoon.roles"/> + <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser"> <parameter name="validate" value="false"/> <parameter name="namespace-prefixes" value="false"/> @@ -45,4 +48,4 @@ </properties> </component> -</components> +</cocoon> Added: cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java?rev=164455&view=auto ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java (added) +++ cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java Sun Apr 24 00:12:35 2005 @@ -0,0 +1,149 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.core; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; + +import org.apache.avalon.framework.context.DefaultContext; +import org.apache.avalon.framework.logger.Logger; +import org.apache.cocoon.core.BootstrapEnvironment; +import org.apache.cocoon.core.Settings; +import org.apache.cocoon.environment.Context; +import org.apache.cocoon.util.log.DeprecationLogger; +import org.apache.log.LogTarget; + +public class TestBootstrapEnvironment + implements BootstrapEnvironment { + + private final String configuration; + private final ClassLoader classLoader; + private final String contextPath; + public Logger logger; + private final Context environmentContext; + + public TestBootstrapEnvironment(String configuration, + ClassLoader classLoader, + String contextPath, + Context environmentContext, + Logger logger) { + this.configuration = configuration; + this.classLoader = classLoader; + this.contextPath = contextPath; + this.environmentContext = environmentContext; + + this.logger = logger; + DeprecationLogger.logger = this.logger; + } + + /** Log a message during bootstrapping. This is used to log + * information before the logging system is setup. + * @param message A message. + */ + public void log(String message) { + this.logger.info(message); + } + + /** Log a message during bootstrapping. This is used to log + * information before the logging system is setup. + * @param message A message. + * @param error An error. + */ + public void log(String message, Throwable error) { + this.logger.info(message, error); + } + + /** + * Pass the root logger back to the environment. As soon as the + * logging system is set up, this method is called. + * @param rootLogger The root logger. + */ + public void setLogger(Logger rootLogger) { + this.logger = rootLogger; + } + + /** + * Get the input stream from a resource at the given + * path. Only paths relative to the bootstrap context are + * supported. Returns null if no resource exists at the + * specified path + */ + public InputStream getInputStream(String path) { + try { + return (new URL(this.contextPath + path)).openStream(); + } catch (IOException e) { + this.log("Couldn't open " + this.contextPath + path); + return null; + } + } + + public void configure(Settings settings) { + settings.setConfiguration(this.configuration); + settings.setWorkDirectory("work"); + } + + public void configureLoggingContext(DefaultContext context) { + throw new UnsupportedOperationException(); + } + + public void configure(DefaultContext context) { + } + + public ClassLoader getInitClassLoader() { + return this.classLoader; + } + + public Context getEnvironmentContext() { + return this.environmentContext; + } + + /** + * Returns the URL to the application context. + */ + public String getContextURL() { + return this.contextPath; + } + + /** + * Returns a file to the application context. + * @return A file pointing to the context or null if the context is not + * writeable. + */ + public File getContextForWriting() { + return null; + } + + public LogTarget getDefaultLogTarget() { + throw new UnsupportedOperationException(); + } + + /** + * Set the ConfigFile for the Cocoon object. + * + * @param configFileName The file location for the cocoon.xconf + * + * @throws Exception + */ + public URL getConfigFile(String configFileName) throws Exception { + return new URL(this.contextPath + configFileName); + } + + public String getClassPath(Settings settings) { + throw new UnsupportedOperationException(); + } +} Propchange: cocoon/trunk/src/test/org/apache/cocoon/core/TestBootstrapEnvironment.java ------------------------------------------------------------------------------ svn:eol-style = native Added: cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java?rev=164455&view=auto ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java (added) +++ cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java Sun Apr 24 00:12:35 2005 @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.core; + +import org.apache.avalon.excalibur.logger.LoggerManager; +import org.apache.avalon.framework.logger.Logger; +import org.apache.cocoon.core.BootstrapEnvironment; +import org.apache.cocoon.core.CoreUtil; + +public class TestCoreUtil extends CoreUtil { + public TestCoreUtil(BootstrapEnvironment env) throws Exception { + super(env); + } + + // Simplified logging + protected void initLogger() { + this.log = ((TestBootstrapEnvironment)this.env).logger; + this.loggerManager = new DefaultLoggerManager(this.log); + } + + // Simplified classloader handling + protected void updateEnvironment() throws Exception {} + + /** + * We use this simple logger manager that sends all output to the console (logger) + */ + protected static class DefaultLoggerManager implements LoggerManager { + + private Logger logger; + + public DefaultLoggerManager(Logger logger) { + this.logger = logger; + } + /* (non-Javadoc) + * @see org.apache.avalon.excalibur.logger.LoggerManager#getDefaultLogger() + */ + public Logger getDefaultLogger() { + return this.logger; + } + /* (non-Javadoc) + * @see org.apache.avalon.excalibur.logger.LoggerManager#getLoggerForCategory(java.lang.String) + */ + public Logger getLoggerForCategory(String arg0) { + return this.logger; + } + } +} Propchange: cocoon/trunk/src/test/org/apache/cocoon/core/TestCoreUtil.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java (original) +++ cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockEnvironment.java Sun Apr 24 00:12:35 2005 @@ -130,19 +130,19 @@ } public boolean tryResetResponse() throws IOException { - throw new AssertionFailedError("Not implemented"); + return false; } public void commitResponse() throws IOException { - throw new AssertionFailedError("Not implemented"); + // do nothing } public void startingProcessing() { - throw new AssertionFailedError("Not implemented"); + // do nothing } public void finishingProcessing() { - throw new AssertionFailedError("Not implemented"); + // do nothing } /** Modified: cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockRequest.java URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockRequest.java?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockRequest.java (original) +++ cocoon/trunk/src/test/org/apache/cocoon/environment/mock/MockRequest.java Sun Apr 24 00:12:35 2005 @@ -49,7 +49,7 @@ private String queryString; private String method = "GET"; private String contentType; - private Locale locale; + private Locale locale = Locale.US; private Principal principal; private String remoteAddr; private String remoteHost; @@ -339,7 +339,7 @@ queryString = null; method = "GET"; contentType = null; - locale = null; + locale = Locale.US; principal = null; remoteAddr = null; remoteHost = null; Modified: cocoon/trunk/src/test/org/apache/cocoon/generation/VirtualPipelineGeneratorTestCase.xconf URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/generation/VirtualPipelineGeneratorTestCase.xconf?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/generation/VirtualPipelineGeneratorTestCase.xconf (original) +++ cocoon/trunk/src/test/org/apache/cocoon/generation/VirtualPipelineGeneratorTestCase.xconf Sun Apr 24 00:12:35 2005 @@ -15,7 +15,10 @@ limitations under the License. --> -<components> +<cocoon version="2.2"> + + <include src="resource://org/apache/cocoon/cocoon.roles"/> + <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser"> <parameter name="validate" value="false"/> <parameter name="namespace-prefixes" value="false"/> @@ -43,4 +46,4 @@ give exceptions during decommissioning --> <sitemap file="resource://org/apache/cocoon/generation/vpc-sitemap.xmap"/> -</components> \ No newline at end of file +</cocoon> \ No newline at end of file Modified: cocoon/trunk/src/test/org/apache/cocoon/reading/VirtualPipelineReaderTestCase.xconf URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/reading/VirtualPipelineReaderTestCase.xconf?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/reading/VirtualPipelineReaderTestCase.xconf (original) +++ cocoon/trunk/src/test/org/apache/cocoon/reading/VirtualPipelineReaderTestCase.xconf Sun Apr 24 00:12:35 2005 @@ -15,7 +15,10 @@ limitations under the License. --> -<components> +<cocoon version="2.2"> + + <include src="resource://org/apache/cocoon/cocoon.roles"/> + <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser"> <parameter name="validate" value="false"/> <parameter name="namespace-prefixes" value="false"/> @@ -43,4 +46,4 @@ give exceptions during decommissioning --> <sitemap file="resource://org/apache/cocoon/reading/vpc-sitemap.xmap"/> -</components> \ No newline at end of file +</cocoon> \ No newline at end of file Modified: cocoon/trunk/src/test/org/apache/cocoon/serialization/VirtualPipelineSerializerTestCase.xconf URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/serialization/VirtualPipelineSerializerTestCase.xconf?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/serialization/VirtualPipelineSerializerTestCase.xconf (original) +++ cocoon/trunk/src/test/org/apache/cocoon/serialization/VirtualPipelineSerializerTestCase.xconf Sun Apr 24 00:12:35 2005 @@ -15,7 +15,10 @@ limitations under the License. --> -<components> +<cocoon version="2.2"> + + <include src="resource://org/apache/cocoon/cocoon.roles"/> + <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser"> <parameter name="validate" value="false"/> <parameter name="namespace-prefixes" value="false"/> @@ -55,4 +58,4 @@ give exceptions during decommissioning --> <sitemap file="resource://org/apache/cocoon/serialization/vpc-sitemap.xmap"/> -</components> \ No newline at end of file +</cocoon> \ No newline at end of file Modified: cocoon/trunk/src/test/org/apache/cocoon/transformation/VirtualPipelineTransformerTestCase.xconf URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/transformation/VirtualPipelineTransformerTestCase.xconf?rev=164455&r1=164454&r2=164455&view=diff ============================================================================== --- cocoon/trunk/src/test/org/apache/cocoon/transformation/VirtualPipelineTransformerTestCase.xconf (original) +++ cocoon/trunk/src/test/org/apache/cocoon/transformation/VirtualPipelineTransformerTestCase.xconf Sun Apr 24 00:12:35 2005 @@ -15,7 +15,10 @@ limitations under the License. --> -<components> +<cocoon version="2.2"> + + <include src="resource://org/apache/cocoon/cocoon.roles"/> + <xml-parser class="org.apache.excalibur.xml.impl.JaxpParser"> <parameter name="validate" value="false"/> <parameter name="namespace-prefixes" value="false"/> @@ -55,4 +58,4 @@ give exceptions during decommissioning --> <sitemap file="resource://org/apache/cocoon/transformation/vpc-sitemap.xmap"/> -</components> \ No newline at end of file +</cocoon> \ No newline at end of file