Parent Directory | Revision Log | Patch
--- cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java 2005/04/24 06:59:57 164454 +++ cocoon/trunk/src/test/org/apache/cocoon/SitemapTestCase.java 2005/04/24 07:12:35 164455 @@ -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 @@ public class SitemapTestCase extends Sit } } - 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 {
infrastructure at apache.org | ViewVC Help |
Powered by ViewVC 1.1.26 |