cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r701884 - /cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
Date Sun, 05 Oct 2008 20:44:32 GMT
Author: gkossakowski
Date: Sun Oct  5 13:44:32 2008
New Revision: 701884

URL: http://svn.apache.org/viewvc?rev=701884&view=rev
Log:
Remove TestCase dependency on internet connection and slashdot.org site.

Introduced mock Source that mimics intended behaviour but removes dependency on slashdot.org
site.
At the same time, due to fact that TestCase is ran in more controlled environment it was possible
to reduce its execution time by factor of 4.

With this change it's finally possible to work with Cocoon offline.

Modified:
    cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java

Modified: cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java?rev=701884&r1=701883&r2=701884&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
Sun Oct  5 13:44:32 2008
@@ -17,17 +17,27 @@
 package org.apache.cocoon.components.source.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.Date;
 import java.util.Iterator;
+import java.util.Map;
 
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
 import org.apache.avalon.framework.context.DefaultContext;
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.SitemapComponentTestCase;
+import org.apache.cocoon.core.container.spring.avalon.ComponentInfo;
+import org.apache.cocoon.core.container.spring.avalon.ConfigurationInfo;
 import org.apache.cocoon.environment.mock.MockContext;
 import org.apache.cocoon.xml.LoggingContentHandler;
 import org.apache.cocoon.xml.SaxBuffer;
 import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
 import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.SourceUtil;
+import org.apache.excalibur.source.impl.AbstractSource;
 
 /**
  * CachingSource (cached: protocol) tests
@@ -44,6 +54,19 @@
         super.setUp();
         resolver = (SourceResolver) lookup(SourceResolver.ROLE);
     }
+    
+    @Override
+    protected void addComponents(ConfigurationInfo info) throws Exception {
+        super.addComponents(info);
+        
+        //register MockSourceFactory
+        ComponentInfo componentInfo = new ComponentInfo();
+        componentInfo.setComponentClassName(MockSourceFactory.class.getName());
+        componentInfo.setRole(SourceFactory.ROLE + "/mock");
+        componentInfo.setConfiguration(new DefaultConfiguration("-"));
+        
+        info.addComponent(componentInfo);
+    }
 
     public void testResolveURI() throws Exception {
         testResolveURI("cached", URI);
@@ -72,7 +95,7 @@
     }
 
     public void testCachingURI() throws Exception {
-        String uri = "cached:http://slashdot.org/?cocoon:cache-expires=5";
+        String uri = "cached:mock://dummyaddress.org/?cocoon:cache-expires=1";
 
         CachingSource source;
 
@@ -95,7 +118,7 @@
         // Source is still cached -- still same meta data
         assertSame(meta1, meta3);
 
-        Thread.sleep(5100);
+        Thread.sleep(1100);
 
         source = (CachingSource) resolver.resolveURI(uri);
         source.refresh();
@@ -110,7 +133,7 @@
     }
 
     public void testRefreshSyncURI() throws Exception {
-        testRefreshURI("cached", "http://slashdot.org/");
+        testRefreshURI("cached", "mock://dummyaddress.org/");
     }
 
 //    public void testRefreshAsyncURI() throws Exception {
@@ -221,4 +244,48 @@
         ctx.put("work-directory", new File("build/work"));
         ctx.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new MockContext());
     }
+    
+    //------------------------ Mock objects ---------------------------
+    
+    /**
+     * Simple source that does not provide anything meaningful apart from lastModification
date which is passed
+     * to it from outside.
+     */
+    private static class MockSource extends AbstractSource {
+        
+        public MockSource(String uri, long lastModified) {
+            setSystemId(uri);
+            setScheme(SourceUtil.getScheme(uri));
+            setLastModified(lastModified);
+        }
+
+        public boolean exists() { 
+            return true; 
+        }
+        
+        @Override
+        protected void getInfos() {
+            //overrode this method because it wipes out last modification date
+        }
+    }
+    
+    /**
+     * This simple SourceFactory produces instances of MockSource with current lastModification
date.
+     */
+    private static final class MockSourceFactory implements SourceFactory {
+        
+        public MockSourceFactory() { }
+
+        @SuppressWarnings("unchecked")
+        public Source getSource(String location, Map parameters)
+                throws IOException, MalformedURLException {
+            return new MockSource(location, (new Date()).getTime());
+        }
+
+        public void release(Source source) {
+            //it's mock source, do nothing
+        }
+        
+    }
+    
 }



Mime
View raw message