unico 2004/07/01 07:32:37
Added:
src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl
XMLizableSourceFactory.java XMLizableSource.java
CachingSourceTestCase.java
CachingSourceTestCase.xtest cachingsourcetest.xml
Log:
CachingSource test case and supporting classes and resources
Revision Changes Path
1.1
cocoon-2.1/src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl/XMLizableSourceFactory.java
Index: XMLizableSourceFactory.java
===================================================================
/*
* 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.components.source.impl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Map;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceFactory;
import org.apache.excalibur.source.SourceResolver;
public class XMLizableSourceFactory extends AbstractLogEnabled
implements SourceFactory, Serviceable, ThreadSafe {
private ServiceManager m_manager;
private SourceResolver m_resolver;
private boolean m_initialized;
public void service(ServiceManager manager) throws ServiceException {
m_manager = manager;
}
private synchronized void lazyInitialize() throws SourceException {
if (m_initialized) {
return;
}
try {
m_resolver = (SourceResolver)
m_manager.lookup(SourceResolver.ROLE);
}
catch (ServiceException e) {
throw new SourceException("Missing service dependency:
SourceResolver",e);
}
m_initialized = true;
}
public Source getSource(String location, Map parameters)
throws IOException, MalformedURLException {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Creating source object for " + location);
}
if (!m_initialized) {
lazyInitialize();
}
final String uri =
location.substring(XMLizableSource.SCHEME.length()+1);
final Source delegate = m_resolver.resolveURI(uri,null,parameters);
return new XMLizableSource(delegate,m_manager);
}
public void release(Source source) {
if (source instanceof XMLizableSource) {
m_resolver.release(((XMLizableSource) source).getSource());
}
}
}
1.1
cocoon-2.1/src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl/XMLizableSource.java
Index: XMLizableSource.java
===================================================================
/*
* 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.components.source.impl;
import java.io.IOException;
import java.io.InputStream;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.cocoon.components.source.SourceUtil;
import org.apache.excalibur.xml.sax.XMLizable;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceNotFoundException;
import org.apache.excalibur.source.SourceValidity;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
* Proxy like source adding XMLizable.
*/
public class XMLizableSource implements XMLizable, Source {
public static final String SCHEME = "xml";
private Source m_source;
private ServiceManager m_manager;
public XMLizableSource(Source source, ServiceManager manager) {
m_source = source;
m_manager = manager;
}
public Source getSource() {
return m_source;
}
public String getSourceURI() {
return m_source.getURI();
}
public boolean exists() {
return m_source.exists();
}
public InputStream getInputStream()
throws IOException, SourceNotFoundException {
return m_source.getInputStream();
}
public String getURI() {
return SCHEME + ":" + m_source.getURI();
}
public String getScheme() {
return SCHEME;
}
public SourceValidity getValidity() {
return m_source.getValidity();
}
public void refresh() {
m_source.refresh();
}
public String getMimeType() {
return m_source.getMimeType();
}
public long getContentLength() {
return m_source.getContentLength();
}
public long getLastModified() {
return m_source.getLastModified();
}
public void toSAX(ContentHandler handler) throws SAXException {
try {
SourceUtil.toSAX(m_manager,m_source,"text/xml",handler);
}
catch (Exception e) {
throw new SAXException("Failure during toSAX",e);
}
}
}
1.1
cocoon-2.1/src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
Index: CachingSourceTestCase.java
===================================================================
/*
* 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.components.source.impl;
import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.LogKitLogger;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.Constants;
import org.apache.cocoon.caching.Cache;
import org.apache.cocoon.caching.IdentifierCacheKey;
import org.apache.cocoon.environment.mock.MockContext;
import org.apache.cocoon.xml.LoggingContentHandler;
import org.apache.cocoon.xml.SaxBuffer;
import org.apache.excalibur.source.ModifiableSource;
import org.apache.excalibur.source.ModifiableTraversableSource;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.source.TraversableSource;
/**
* TODO describe class
*
* @author <a href="mailto:[EMAIL PROTECTED]">Unico Hommes</a>
*/
public class CachingSourceTestCase extends ExcaliburTestCase {
public CachingSourceTestCase(String name) {
super(name);
}
public void testResolveURI() throws Exception {
SourceResolver resolver = (SourceResolver)
lookup(SourceResolver.ROLE);
String scheme = "async-caching";
String uri = "resource://org/apache/cocoon/components/" +
"source/impl/cachingsourcetest.xml?foo=bar";
// resolve CachingSource
Source source = resolver.resolveURI(
scheme + ":" + uri + "&cocoon:cache-expires=10");
super.assertTrue(source instanceof CachingSource);
CachingSource cachingSource = (CachingSource) source;
assertEquals(uri, cachingSource.getSourceURI());
assertEquals(scheme, cachingSource.getScheme());
assertEquals(10 * 1000, cachingSource.getExpiration());
assertEquals(new IdentifierCacheKey("source:" + uri,false),
cachingSource.getCacheKey());
cachingSource = (CachingSource) resolver.resolveURI(scheme + ":" +
uri + "&cocoon:cache-name=test");
assertEquals(cachingSource.getCacheKey(),new
IdentifierCacheKey("source:"+uri+":test",false));
resolver.release(source);
String parentURI = "file://c:/temp";
String childURI = parentURI + "/test";
// resolve TraversableCachingSource
source = resolver.resolveURI(scheme + ":" + childURI +
"?cocoon:cache-expires=1");
assertTrue(source instanceof TraversableCachingSource);
TraversableCachingSource child = (TraversableCachingSource) source;
assertEquals("test",child.getName());
TraversableCachingSource parent = (TraversableCachingSource)
child.getParent();
assertTrue(parent instanceof TraversableCachingSource);
assertEquals("temp",parent.getName());
//assertEquals(parentURI, parent.getSourceURI());
assertTrue(parent.isCollection());
child = (TraversableCachingSource) parent.getChild("test");
assertEquals("test", child.getName());
//assertEquals(childURI, child.getSourceURI());
Iterator children = parent.getChildren().iterator();
while (children.hasNext()) {
child = (TraversableCachingSource) children.next();
}
resolver.release(source);
}
public void testGetContents() throws Exception {
SourceResolver resolver = (SourceResolver)
lookup(SourceResolver.ROLE);
// resolve AsyncCachingSource
String scheme = "caching";
String uri = "resource://org/apache/cocoon/components/" +
"source/impl/cachingsourcetest.xml";
CachingSource source = (CachingSource) resolver.resolveURI(scheme +
":" + uri);
InputStream stream = source.getInputStream();
String contents = new String();
byte[] buffer = new byte[1024];
int len;
while ((len = stream.read(buffer)) > 0) {
contents += new String(buffer,0,len);
}
resolver.release(source);
uri = "xml:" + uri;
source = (CachingSource) resolver.resolveURI(scheme + ":" + uri);
SaxBuffer saxbuffer = new SaxBuffer();
LoggingContentHandler handler = new
LoggingContentHandler("test",saxbuffer);
handler.enableLogging(new
LogKitLogger(getLogger().getChildLogger("handler")));
source.toSAX(handler);
}
// public void testDelayRefresher() throws Exception {
// Parameters parameters = new Parameters();
// parameters.setParameter("cache-expires",String.valueOf(10));
//
// Refresher refresher = (Refresher) lookup(Refresher.ROLE);
// refresher.refresh(new SimpleCacheKey("test",false),
// "http://www.hippo.nl/index.html",
// Cache.ROLE,
// parameters);
// }
//source.getSource();
// InputStream stream = source.getInputStream();
// String contents = new String();
// byte[] buffer = new byte[1024];
// int len;
// while((len = stream.read(buffer)) > 0) {
// contents += new String(buffer,0,len);
// }
// getLogger().debug("contents: " + contents);
//getLogger().debug("");
protected void addContext(DefaultContext ctx) {
ctx.put("work-directory",new File("build/work"));
ctx.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT,new MockContext());
}
}
1.1
cocoon-2.1/src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest
Index: CachingSourceTestCase.xtest
===================================================================
<?xml version="1.0" ?>
<!--
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.
-->
<testcase>
<annotation>
Test Cases: CachingSource
</annotation>
<logkit>
<factories>
<factory type="stream"
class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/>
</factories>
<targets>
<stream id="root">
<stream>System.out</stream>
<format type="extended">
%7.7{priority} %5.5{time} [%{category}] (%{context}):
%{message}\n%{throwable}
</format>
</stream>
</targets>
<categories>
<category name="CachingSourceTestCase" log-level="DEBUG">
<log-target id-ref="root"/>
</category>
<category name="test" log-level="DEBUG">
<log-target id-ref="root"/>
</category>
<category name="core" log-level="DEBUG">
<log-target id-ref="root"/>
</category>
</categories>
</logkit>
<roles>
<role name="org.apache.excalibur.source.SourceFactorySelector"
shorthand="source-factories"
default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector"
/>
<role name="org.apache.excalibur.source.SourceResolver"
shorthand="source-resolver"
default-class="org.apache.excalibur.source.impl.SourceResolverImpl"
/>
<role name="org.apache.excalibur.store.Store"
shorthand="store"
default-class="org.apache.excalibur.store.impl.MRUMemoryStore"
/>
<role name="org.apache.excalibur.store.StoreJanitor"
shorthand="store-janitor"
default-class="org.apache.excalibur.store.impl.StoreJanitorImpl"
/>
<role name="org.apache.cocoon.caching.Cache"
shorthand="cache"
default-class="org.apache.cocoon.caching.impl.CacheImpl"
/>
<role name="org.apache.cocoon.components.source.impl.Refresher"
shorthand="delay-refresher"
default-class="org.apache.cocoon.components.source.impl.DelayRefresher"
/>
<role name="org.apache.cocoon.components.cron.JobScheduler"
shorthand="job-scheduler"
default-class="org.apache.cocoon.components.cron.QuartzJobScheduler"
/>
<role name="org.apache.cocoon.components.cron.CronJob/UpdateTarget"
shorthand="update-target"
default-class="org.apache.cocoon.components.source.impl.UpdateTarget"
/>
<role name="org.apache.cocoon.components.sax.XMLSerializer"
shorthand="xml-serializer"
default-class="org.apache.cocoon.components.sax.XMLByteStreamCompiler"
/>
<role name="org.apache.cocoon.components.sax.XMLDeserializer"
shorthand="xml-deserializer"
default-class="org.apache.cocoon.components.sax.XMLByteStreamInterpreter"
/>
<role name="org.apache.excalibur.xml.sax.SAXParser"
shorthand="xml-parser"
default-class="org.apache.excalibur.xml.impl.JaxpParser"
/>
<role name="org.apache.excalibur.xmlizer.XMLizer"
shorthand="xmlizer"
default-class="org.apache.excalibur.xmlizer.DefaultXMLizer"
/>
</roles>
<components>
<source-factories>
<component-instance
class="org.apache.cocoon.components.source.impl.CachingSourceFactory"
name="caching" logger="core.cachingsource"/>
<component-instance
class="org.apache.cocoon.components.source.impl.CachingSourceFactory"
name="async-caching" logger="core.asynchcachingsource">
<parameter name="async" value="true"/>
</component-instance>
<component-instance
class="org.apache.cocoon.components.source.impl.XMLizableSourceFactory"
name="xml" logger="core.xmlsource"/>
<!--component-instance
class="org.apache.cocoon.components.source.impl.WebDAVSourceFactory"
name="webdav" logger="core.webdavsource"/-->
<component-instance
class="org.apache.excalibur.source.impl.FileSourceFactory" name="file"
logger="core.filesource"/>
<component-instance
class="org.apache.excalibur.source.impl.ResourceSourceFactory" name="resource"
logger="core.resourcesource"/>
<component-instance
class="org.apache.excalibur.source.impl.URLSourceFactory" name="*"/>
</source-factories>
<persistent-store logger="core.persistentstore">
<parameter name="directory" value="cachingsourcetest"/>
</persistent-store>
<transient-store logger="core.transientstore"/>
<cache logger="core.cache"/>
<delay-refresher logger="core.refresher">
<parameter name="write-interval" value="10"/>
<parameter name="scheduler-target"
value="org.apache.cocoon.components.cron.CronJob/update"/>
</delay-refresher>
<update-target logger="core.updatetarget"/>
<xmlizer logger="core.xmlizer">
<parser mime-type="text/xml"
role="org.apache.excalibur.xml.sax.SAXParser"/>
</xmlizer>
</components>
</testcase>
1.1
cocoon-2.1/src/blocks/scratchpad/test/org/apache/cocoon/components/source/impl/cachingsourcetest.xml
Index: cachingsourcetest.xml
===================================================================
<?xml version="1.0"?>
<!--
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.
-->
<test/>