Author: ugo
Date: Fri Oct 22 10:09:48 2004
New Revision: 55322

Added:
   cocoon/trunk/legal/jmock-1.0.1.jar.license.txt
   cocoon/trunk/src/test/org/apache/cocoon/MockLogger.java
   cocoon/trunk/tools/lib/jmock-1.0.1.jar   (contents, props changed)
Removed:
   
cocoon/trunk/src/test/org/apache/cocoon/acting/ResourceExistsActionTestCase.xtest
Modified:
   
cocoon/trunk/src/test/org/apache/cocoon/acting/RequestParamActionTestCase.java
   
cocoon/trunk/src/test/org/apache/cocoon/acting/ResourceExistsActionTestCase.java
Log:
Introducing mock objects, refactoring action testcases

Added: cocoon/trunk/legal/jmock-1.0.1.jar.license.txt
==============================================================================
--- (empty file)
+++ cocoon/trunk/legal/jmock-1.0.1.jar.license.txt      Fri Oct 22 10:09:48 2004
@@ -0,0 +1,25 @@
+Copyright (c) 2000-2003, jMock.org
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this 
list of
+conditions and the following disclaimer. Redistributions in binary form must 
reproduce
+the above copyright notice, this list of conditions and the following 
disclaimer in
+the documentation and/or other materials provided with the distribution.
+
+Neither the name of jMock nor the names of its contributors may be used to 
endorse
+or promote products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
EVENT
+SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
LIMITED
+TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY
+WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.

Added: cocoon/trunk/src/test/org/apache/cocoon/MockLogger.java
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/test/org/apache/cocoon/MockLogger.java     Fri Oct 22 
10:09:48 2004
@@ -0,0 +1,152 @@
+/*
+ * 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;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Mock logger for test cases. Delegates to a Commons Logging logger.
+ * 
+ * @version CVS $Id$
+ */
+public class MockLogger implements Logger {
+    protected Log logger;
+    
+    /**
+     * Create a new logger for a given class.
+     * 
+     * @param clazz The class.
+     */
+    public MockLogger(Class clazz) {
+        this.logger = LogFactory.getLog(clazz);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#debug(java.lang.String)
+     */
+    public void debug(String msg) {
+        this.logger.debug(msg);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#debug(java.lang.String, 
java.lang.Throwable)
+     */
+    public void debug(String msg, Throwable t) {
+        this.logger.debug(msg, t);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#isDebugEnabled()
+     */
+    public boolean isDebugEnabled() {
+        return this.logger.isDebugEnabled();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#info(java.lang.String)
+     */
+    public void info(String msg) {
+        this.logger.info(msg);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#info(java.lang.String, 
java.lang.Throwable)
+     */
+    public void info(String msg, Throwable t) {
+        this.logger.info(msg, t);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#isInfoEnabled()
+     */
+    public boolean isInfoEnabled() {
+        return this.logger.isInfoEnabled();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#warn(java.lang.String)
+     */
+    public void warn(String msg) {
+        this.logger.warn(msg);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#warn(java.lang.String, 
java.lang.Throwable)
+     */
+    public void warn(String msg, Throwable t) {
+        this.logger.warn(msg, t);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#isWarnEnabled()
+     */
+    public boolean isWarnEnabled() {
+        return this.logger.isWarnEnabled();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#error(java.lang.String)
+     */
+    public void error(String msg) {
+        this.logger.error(msg);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#error(java.lang.String, 
java.lang.Throwable)
+     */
+    public void error(String msg, Throwable t) {
+        this.logger.error(msg, t);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#isErrorEnabled()
+     */
+    public boolean isErrorEnabled() {
+        return this.logger.isErrorEnabled();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.avalon.framework.logger.Logger#fatalError(java.lang.String)
+     */
+    public void fatalError(String msg) {
+        this.logger.fatal(msg);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.avalon.framework.logger.Logger#fatalError(java.lang.String, 
java.lang.Throwable)
+     */
+    public void fatalError(String msg, Throwable t) {
+        this.logger.fatal(msg, t);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.logger.Logger#isFatalErrorEnabled()
+     */
+    public boolean isFatalErrorEnabled() {
+        return this.logger.isFatalEnabled();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.avalon.framework.logger.Logger#getChildLogger(java.lang.String)
+     */
+    public Logger getChildLogger(String arg0) {
+        return null;
+    }
+
+}

Modified: 
cocoon/trunk/src/test/org/apache/cocoon/acting/RequestParamActionTestCase.java
==============================================================================
--- 
cocoon/trunk/src/test/org/apache/cocoon/acting/RequestParamActionTestCase.java  
    (original)
+++ 
cocoon/trunk/src/test/org/apache/cocoon/acting/RequestParamActionTestCase.java  
    Fri Oct 22 10:09:48 2004
@@ -16,18 +16,23 @@
 
 package org.apache.cocoon.acting;
 
+import java.util.HashMap;
 import java.util.Map;
 
+import junit.framework.TestCase;
+
 import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.cocoon.SitemapComponentTestCase;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.mock.MockRequest;
 
 /**
  *
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Stephan Michels </a>
- * @version CVS $Id: RequestParamActionTestCase.java,v 1.5 2004/03/05 13:03:03 
bdelacretaz Exp $
+ * @version CVS $Id$
  */
-public class RequestParamActionTestCase extends SitemapComponentTestCase {
+public class RequestParamActionTestCase extends TestCase {
+    private Map objectModel = new HashMap();
 
     public RequestParamActionTestCase(String name) {
         super(name);
@@ -35,15 +40,18 @@
 
     public void testRequestAction() throws Exception {
 
-        getRequest().setRequestURI("test.xml?abc=def&ghi=jkl");
-        getRequest().setQueryString("abc=def&ghi=jkl");
-        getRequest().setContextPath("servlet");
-        getRequest().addParameter("abc", "def");
+        MockRequest request = new MockRequest();
+        request.setRequestURI("test.xml?abc=def&ghi=jkl");
+        request.setQueryString("abc=def&ghi=jkl");
+        request.setContextPath("servlet");
+        request.addParameter("abc", "def");
+        objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
 
         Parameters parameters = new Parameters();
         parameters.setParameter("parameters", "true");
 
-        Map result = act("request", null, parameters);
+        RequestParamAction action = new RequestParamAction();
+        Map result = action.act(null, null, objectModel, null, parameters); 
 
         assertNotNull("Test if resource exists", result);
         assertEquals("Test for parameter", "test.xml?abc=def&ghi=jkl", 
result.get("requestURI"));

Modified: 
cocoon/trunk/src/test/org/apache/cocoon/acting/ResourceExistsActionTestCase.java
==============================================================================
--- 
cocoon/trunk/src/test/org/apache/cocoon/acting/ResourceExistsActionTestCase.java
    (original)
+++ 
cocoon/trunk/src/test/org/apache/cocoon/acting/ResourceExistsActionTestCase.java
    Fri Oct 22 10:09:48 2004
@@ -16,34 +16,69 @@
 
 package org.apache.cocoon.acting;
 
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.cocoon.SitemapComponentTestCase;
+import org.apache.cocoon.MockLogger;
+import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.environment.mock.MockRequest;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceNotFoundException;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
 
 /**
  *
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Stephan Michels </a>
- * @version CVS $Id: ResourceExistsActionTestCase.java,v 1.4 2004/03/05 
13:03:03 bdelacretaz Exp $
+ * @version CVS $Id$
  */
-public class ResourceExistsActionTestCase extends SitemapComponentTestCase {
+public class ResourceExistsActionTestCase extends MockObjectTestCase {
+    private Map objectModel = new HashMap();
 
     public ResourceExistsActionTestCase(String name) {
         super(name);
     }
 
-    public void testExistAction() throws Exception {
+    public void testExists() throws Exception {
 
-        String src = 
"resource://org/apache/cocoon/acting/ResourceExistsActionTestCase.xtest";
+        MockRequest request = new MockRequest();
+
+        String src = "don't care";
         Parameters parameters = new Parameters();
 
-        Map result = act("exist", src, parameters);
-        assertNotNull("Test if resource exists", result);
+        ResourceExistsAction action = new ResourceExistsAction();
+        action.enableLogging(new MockLogger(action.getClass()));
+        Mock resolver = new Mock(SourceResolver.class);
+        Mock source = new Mock(Source.class);
+        resolver.expects(once()).method("resolveURI").with(same(src)).
+                will(returnValue(source.proxy()));
+        resolver.expects(once()).method("release").with(same(source.proxy()));
+        source.expects(atLeastOnce()).method("exists").will(returnValue(true));
+        Map result = action.act(null, (SourceResolver) resolver.proxy(), 
+                objectModel, src, parameters);
+        assertSame("Test if resource exists", AbstractAction.EMPTY_MAP, 
result);
+        resolver.verify();
+        source.verify();
+    }
+
+    public void testNotExists() throws Exception {
 
-        src = 
"resource://org/apache/cocoon/acting/ResourceExistsActionTestCase.abc";
+        MockRequest request = new MockRequest();
+
+        String src = "don't care";
+        Parameters parameters = new Parameters();
 
-        result = act("exist", src, parameters);
+        ResourceExistsAction action = new ResourceExistsAction();
+        action.enableLogging(new MockLogger(action.getClass()));
+        Mock resolver = new Mock(SourceResolver.class);
+        resolver.expects(once()).method("resolveURI").with(same(src)).
+                will(this.throwException(new SourceNotFoundException("don't 
care")));
+        Map result = action.act(null, (SourceResolver) resolver.proxy(), 
+                objectModel, src, parameters);
         assertNull("Test if resource not exists", result);
+        resolver.verify();
     }
 }

Added: cocoon/trunk/tools/lib/jmock-1.0.1.jar
==============================================================================
Binary file. No diff available.

Reply via email to