You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2006/08/23 14:39:04 UTC

svn commit: r434032 - in /cocoon/trunk/blocks: cocoon-repository/cocoon-repository-impl/ cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/ cocoon-repository/cocoon-repository-impl/src/test/ cocoon-reposito...

Author: vgritsenko
Date: Wed Aug 23 05:39:03 2006
New Revision: 434032

URL: http://svn.apache.org/viewvc?rev=434032&view=rev
Log:
move caching source test to repository block.
enhance the test.

Added:
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/
      - copied from r433766, cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/src/test/
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/
      - copied from r434017, cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/src/test/java/
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/
      - copied from r434017, cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/src/test/resources/
Removed:
    cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/src/test/java/
    cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/src/test/resources/
Modified:
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/pom.xml
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
    cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest
    cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/pom.xml

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/pom.xml?rev=434032&r1=434031&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/pom.xml (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/pom.xml Wed Aug 23 05:39:03 2006
@@ -17,9 +17,9 @@
   specific language governing permissions and limitations
   under the License.
 -->
+
 <!--+
     | @version $Id$
-    |
     +-->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <parent>
@@ -34,6 +34,7 @@
   <packaging>jar</packaging>
   <name>repository Block Implementation</name>
   <description/>
+
   <dependencies>
     <dependency>
       <groupId>org.apache.cocoon</groupId>
@@ -108,5 +109,38 @@
         </exclusion>
       </exclusions>
     </dependency>
+    <!-- TEST -->
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.3</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.cocoon</groupId>
+      <artifactId>cocoon-core</artifactId>
+      <version>2.2.0-M2-SNAPSHOT</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <includes>
+            <include>**/*TestCase.class</include>
+            <include>**/*Test.class</include>
+          </includes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
 </project>

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java?rev=434032&r1=434031&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/CachingSource.java Wed Aug 23 05:39:03 2006
@@ -77,7 +77,7 @@
                                       Source {
 
     // TODO: Decouple from eventcache block.
-    
+
     // ---------------------------------------------------- Constants
 
     public static final String CACHE_EXPIRES_PARAM = "cache-expires";

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java?rev=434032&r1=434031&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/main/java/org/apache/cocoon/components/source/impl/TraversableCachingSource.java Wed Aug 23 05:39:03 2006
@@ -77,6 +77,10 @@
     }
 
     public Source getChild(String name) throws SourceException {
+        if (!isCollection()) {
+            throw new SourceException("Source is not a collection");
+        }
+
         Source child;
         try {
             getResponseMeta();
@@ -87,11 +91,12 @@
             throw new SourceException("Failure getting child", e);
         }
 
-        if (!isCollection()) {
-            throw new SourceException("Source is not a collection");
+        boolean isCollection = false;
+        if (child instanceof TraversableSource) {
+            isCollection = ((TraversableSource) child).isCollection();
         }
 
-        return createSource(getChildURI(super.uri, name), getChildURI(super.sourceUri, name), child);
+        return createSource(getChildURI(super.uri, isCollection, name), getChildURI(super.sourceUri, isCollection, name), child);
     }
 
     public Collection getChildren() throws SourceException {
@@ -117,7 +122,13 @@
             } catch (IOException e) {
                 throw new SourceException("Failure getting child", e);
             }
-            result.add(createSource(getChildURI(super.uri, children[i]), getChildURI(super.sourceUri, children[i]), child));
+
+            boolean isCollection = false;
+            if (child instanceof TraversableSource) {
+                isCollection = ((TraversableSource) child).isCollection();
+            }
+
+            result.add(createSource(getChildURI(super.uri, isCollection, children[i]), getChildURI(super.sourceUri, isCollection, children[i]), child));
         }
 
         return result;
@@ -153,7 +164,7 @@
      * Calculate the cached child URI based on a parent URI
      * and a child name.
      */
-    private static String getChildURI(String parentURI, String childName) {
+    private static String getChildURI(String parentURI, boolean isCollection, String childName) {
         // separate query string from rest of parentURI
         String rest, qs;
         int index = parentURI.indexOf('?');
@@ -165,15 +176,17 @@
             qs = "";
         }
 
-        // calculate qs-less child uri
-        String childURI;
-        if (rest.charAt(rest.length()-1) == '/') {
-            childURI = rest + childName;
-        } else {
-            childURI = rest + "/" + childName;
+        // calculate child uri
+        StringBuffer childURI = new StringBuffer(rest);
+        if (rest.charAt(rest.length()-1) != '/') {
+            childURI.append('/');
+        }
+        childURI.append(childName);
+        if (isCollection) {
+            childURI.append('/');
         }
-
-        return childURI + qs;
+        childURI.append(qs);
+        return childURI.toString();
     }
 
     /**
@@ -186,19 +199,17 @@
         if (index != -1) {
             rest = childURI.substring(0, index);
             qs = childURI.substring(index);
-        }
-        else {
+        } else {
             rest = childURI;
             qs = "";
         }
 
         // calculate qs-less parent uri
         String parentUri;
-        index = rest.lastIndexOf('/',rest.length()-2);
+        index = rest.lastIndexOf('/', rest.length() - 2);
         if (index != -1) {
-            parentUri = rest.substring(0,index);
-        }
-        else {
+            parentUri = rest.substring(0, index + 1);
+        } else {
             parentUri = rest;
         }
 

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java?rev=434032&r1=434017&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/java/org/apache/cocoon/components/source/impl/CachingSourceTestCase.java Wed Aug 23 05:39:03 2006
@@ -21,7 +21,6 @@
 
 import org.apache.avalon.framework.context.DefaultContext;
 import org.apache.cocoon.Constants;
-import org.apache.cocoon.caching.IdentifierCacheKey;
 import org.apache.cocoon.core.container.ContainerTestCase;
 import org.apache.cocoon.environment.mock.MockContext;
 import org.apache.cocoon.xml.LoggingContentHandler;
@@ -31,71 +30,155 @@
 
 /**
  * TODO describe class
- *
  */
 public class CachingSourceTestCase extends ContainerTestCase {
 
-    public void testResolveURI() throws Exception {
+    private static final String URI = "resource://org/apache/cocoon/components/source/impl/cachingsourcetest.xml?foo=bar";
+
+    SourceResolver resolver;
 
-        SourceResolver resolver = (SourceResolver) lookup(SourceResolver.ROLE);
+    protected void setUp() throws Exception {
+        super.setUp();
+        resolver = (SourceResolver) lookup(SourceResolver.ROLE);
+    }
 
-        String scheme = "async-caching";
-        String uri = "resource://org/apache/cocoon/components/" +
-            "source/impl/cachingsourcetest.xml?foo=bar";
+    public void testResolveURI() throws Exception {
+        testResolveURI("caching", URI);
+    }
 
+    public void testResolveAsyncURI() throws Exception {
+        testResolveURI("async-caching", URI);
+    }
+
+    private void testResolveURI(final String scheme, final String uri) throws Exception {
         // resolve CachingSource
-        Source source = resolver.resolveURI(
-            scheme + ":" + uri + "&cocoon:cache-expires=10");
+        Source source = resolver.resolveURI(scheme + ":" + uri + "&cocoon:cache-expires=10");
         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());
+        assertEquals("source:" + uri, cachingSource.getCacheKey());
+        resolver.release(source);
 
+        // resolve CachingSource with specified cache name
         cachingSource = (CachingSource) resolver.resolveURI(scheme + ":" + uri + "&cocoon:cache-name=test");
+        assertEquals("source:" + uri + ":test", cachingSource.getCacheKey());
+        resolver.release(source);
+    }
+
+    public void testCachingURI() throws Exception {
+        String uri = "caching:http://www.google.com/?cocoon:cache-expires=1";
+
+        CachingSource source;
+
+        source = (CachingSource) resolver.resolveURI(uri);
+        CachingSource.SourceMeta meta1 = source.getResponseMeta();
+        resolver.release(source);
+
+        source = (CachingSource) resolver.resolveURI(uri);
+        CachingSource.SourceMeta meta2 = source.getResponseMeta();
+        resolver.release(source);
+
+        assertTrue(meta1 == meta2);
+
+        source = (CachingSource) resolver.resolveURI(uri);
+        source.refresh();
+        CachingSource.SourceMeta meta3 = source.getResponseMeta();
+        resolver.release(source);
+
+        assertTrue(meta1 == meta3);
+
+        Thread.sleep(1100);
 
-        assertEquals(cachingSource.getCacheKey(), new IdentifierCacheKey("source:"+uri+":test",false));
+        source = (CachingSource) resolver.resolveURI(uri);
+        source.refresh();
+        CachingSource.SourceMeta meta4 = source.getResponseMeta();
+        resolver.release(source);
+        assertTrue(meta1 != meta4);
+        assertTrue(meta1.getContentLength() == meta4.getContentLength());
+        assertTrue(meta1.getLastModified() != meta4.getLastModified());
+        assertEquals(meta1.getMimeType(), meta4.getMimeType());
+    }
+
+    public void testRefreshSyncURI() throws Exception {
+        testRefreshURI("caching", "http://www.google.com/");
+    }
+
+//    public void testRefreshAsyncURI() throws Exception {
+//        testRefreshURI("async-caching", "http://www.google.com/");
+//    }
 
+    private void testRefreshURI(final String scheme, final String uri) throws Exception {
+        CachingSource source;
+
+        source = (CachingSource) resolver.resolveURI(scheme + ":" + uri + "?cocoon:cache-expires=1");
+        CachingSource.SourceMeta meta1 = source.getResponseMeta();
+        resolver.release(source);
+
+        source = (CachingSource) resolver.resolveURI(scheme + ":" + uri + "?cocoon:cache-expires=1");
+        CachingSource.SourceMeta meta2 = source.getResponseMeta();
         resolver.release(source);
 
-        String parentURI = "file://c:/temp";
-        String childURI = parentURI + "/test";
+        assertTrue(meta1 == meta2);
+        assertTrue(meta1.getContentLength() == meta2.getContentLength());
+        assertTrue(meta1.getLastModified() == meta2.getLastModified());
+
+        Thread.sleep(1200);
+
+        source = (CachingSource) resolver.resolveURI(scheme + ":" + uri + "?cocoon:cache-expires=1");
+        CachingSource.SourceMeta meta3 = source.getResponseMeta();
+        resolver.release(source);
+
+        assertTrue(meta1 != meta3);
+        assertTrue(meta1.getContentLength() == meta3.getContentLength());
+        assertTrue(meta1.getLastModified() != meta3.getLastModified());
+        assertEquals(meta1.getMimeType(), meta3.getMimeType());
+    }
+
+    public void testCachingTraversableSource() throws Exception {
+        String scheme = "caching";
+        File cwd = new File(".").getCanonicalFile();
+        String childURI = cwd.toURL().toString();
+        String parentURI = cwd.getParentFile().toURL().toString();
+        String childName = cwd.getName();
+        String parentName = cwd.getParentFile().getName();
 
         // resolve TraversableCachingSource
-        source = resolver.resolveURI(scheme + ":" + childURI + "?cocoon:cache-expires=1");
+        Source source = resolver.resolveURI(scheme + ":" + childURI + "?cocoon:cache-expires=1");
         assertTrue(source instanceof TraversableCachingSource);
 
         TraversableCachingSource child = (TraversableCachingSource) source;
-        assertEquals("test",child.getName());
+        assertEquals(childName, child.getName());
+        assertTrue(child.getParent() instanceof TraversableCachingSource);
 
-        assertTrue( child.getParent() instanceof TraversableCachingSource);
         TraversableCachingSource parent = (TraversableCachingSource) child.getParent();
-        assertEquals("temp",parent.getName());
-        //assertEquals(parentURI, parent.getSourceURI());
+        assertEquals(parentName, parent.getName());
+        assertEquals(parentURI, parent.getSourceURI());
         assertTrue(parent.isCollection());
 
-        child = (TraversableCachingSource) parent.getChild("test");
-        assertEquals("test", child.getName());
-        //assertEquals(childURI, child.getSourceURI());
+        child = (TraversableCachingSource) parent.getChild(childName);
+        assertEquals(childName, child.getName());
+        assertEquals(childURI, child.getSourceURI());
 
+        boolean found = false;
         Iterator children = parent.getChildren().iterator();
         while (children.hasNext()) {
             child = (TraversableCachingSource) children.next();
+            if (child.getName().equals(childName)) {
+                found = true;
+            }
         }
+        assertTrue(found);
 
         resolver.release(source);
     }
 
     public void testGetContents() throws Exception {
-
-        SourceResolver resolver = (SourceResolver) lookup(SourceResolver.ROLE);
-
-        // resolve AsyncCachingSource
+        // resolve CachingSource
         String scheme = "caching";
-        String uri = "resource://org/apache/cocoon/components/" +
-            "source/impl/cachingsourcetest.xml";
+        String uri = "resource://org/apache/cocoon/components/source/impl/cachingsourcetest.xml";
         CachingSource source = (CachingSource) resolver.resolveURI(scheme + ":" + uri);
 
         InputStream stream = source.getInputStream();
@@ -103,7 +186,7 @@
         byte[] buffer = new byte[1024];
         int len;
         while ((len = stream.read(buffer)) > 0) {
-            contents += new String(buffer,0,len);
+            contents += new String(buffer, 0, len);
         }
 
         resolver.release(source);
@@ -112,7 +195,7 @@
         source = (CachingSource) resolver.resolveURI(scheme + ":" + uri);
 
         SaxBuffer saxbuffer = new SaxBuffer();
-        LoggingContentHandler handler = new LoggingContentHandler("test",saxbuffer);
+        LoggingContentHandler handler = new LoggingContentHandler("test", saxbuffer);
         handler.enableLogging(getLogger().getChildLogger("handler"));
         source.toSAX(handler);
     }
@@ -128,19 +211,8 @@
 //                          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());
+        ctx.put("work-directory", new File("build/work"));
+        ctx.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, new MockContext());
     }
-
 }

Modified: cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest?rev=434032&r1=434017&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest (original)
+++ cocoon/trunk/blocks/cocoon-repository/cocoon-repository-impl/src/test/resources/org/apache/cocoon/components/source/impl/CachingSourceTestCase.xtest Wed Aug 23 05:39:03 2006
@@ -70,7 +70,6 @@
      <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="*"/>
@@ -87,7 +86,7 @@
   </delay-refresher>
 
   <xmlizer logger="core.xmlizer">
-      <parser mime-type="text/xml" role="org.apache.excalibur.xml.sax.SAXParser"/>
+    <parser mime-type="text/xml" role="org.apache.excalibur.xml.sax.SAXParser"/>
   </xmlizer>
 
  </components>

Modified: cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/pom.xml?rev=434032&r1=434031&r2=434032&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/pom.xml (original)
+++ cocoon/trunk/blocks/cocoon-scratchpad/cocoon-scratchpad-impl/pom.xml Wed Aug 23 05:39:03 2006
@@ -121,9 +121,6 @@
             <include>**/*TestCase.class</include>
             <include>**/*Test.class</include>
           </includes>
-          <excludes>
-            <exclude>**/CachingSourceTestCase.class</exclude>
-          </excludes>
         </configuration>
       </plugin>
     </plugins>