Author: apetrelli
Date: Sun Jun 29 08:02:15 2008
New Revision: 672628

URL: http://svn.apache.org/viewvc?rev=672628&view=rev
Log:
TILES-157
Added UrlPreparer and CompatibilityPreparerFactory to act as in Struts 1/Tiles 
1.

Added:
    
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/
    
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java
   (with props)
    
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
   (with props)
    
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
   (with props)
    
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/
    
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
   (with props)
    
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
   (with props)

Added: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java?rev=672628&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java
 (added)
+++ 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java
 Sun Jun 29 08:02:15 2008
@@ -0,0 +1,48 @@
+/*
+ * $Id: StrutsPreparerFactory.java 603355 2007-12-11 20:48:27Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.compat.preparer;
+
+import org.apache.tiles.preparer.BasicPreparerFactory;
+import org.apache.tiles.preparer.ViewPreparer;
+
+/**
+ * Factory used to instantiate preparers in a Struts 1 / Tiles 2 environment.
+ *
+ * @version $Rev: 603355 $ $Date: 2007-12-11 21:48:27 +0100 (mar, 11 dic 2007) 
$
+ * @since 2.1.0
+ */
+public class CompatibilityPreparerFactory extends BasicPreparerFactory {
+
+    /** [EMAIL PROTECTED] */
+    protected ViewPreparer createPreparer(String name) {
+        ViewPreparer retValue;
+
+        if (name.startsWith("/")) {
+            retValue = new UrlPreparer(name);
+        } else {
+            retValue = super.createPreparer(name);
+        }
+
+        return retValue;
+    }
+
+}

Propchange: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactory.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java?rev=672628&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
 (added)
+++ 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
 Sun Jun 29 08:02:15 2008
@@ -0,0 +1,95 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.compat.preparer;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.preparer.PreparerException;
+import org.apache.tiles.preparer.ViewPreparerSupport;
+import org.apache.tiles.servlet.context.ServletTilesRequestContext;
+
+/**
+ * Uses a URL that acts as a preparer. When
+ * [EMAIL PROTECTED] 
org.apache.tiles.preparer.ViewPreparer#execute(TilesRequestContext, 
AttributeContext)}
+ * is called, the URL is got, but its response is discarded.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public class UrlPreparer extends ViewPreparerSupport {
+
+    /**
+     * The URL to be used as a preparer.
+     */
+    private String url;
+
+    /**
+     * Constructor.
+     *
+     * @param url The URL to be used as a preparer.
+     */
+    public UrlPreparer(String url) {
+        this.url = url;
+    }
+
+    /** [EMAIL PROTECTED] */
+    @Override
+    public void execute(TilesRequestContext tilesContext,
+            AttributeContext attributeContext) {
+
+        if (tilesContext instanceof ServletTilesRequestContext) {
+            ServletTilesRequestContext servletTilesContext =
+                (ServletTilesRequestContext) tilesContext;
+            HttpServletRequest request = servletTilesContext.getRequest();
+            HttpServletResponse response = servletTilesContext.getResponse();
+            ServletContext servletContext = (ServletContext) 
servletTilesContext
+                    .getContext();
+            RequestDispatcher rd = servletContext.getRequestDispatcher(url);
+            if (rd == null) {
+                throw new PreparerException(
+                    "Controller can't find url '" + url + "'.");
+            }
+
+            try {
+                rd.include(request, response);
+            } catch (ServletException e) {
+                throw new PreparerException(
+                        "The request dispatcher threw an exception", e);
+            } catch (IOException e) {
+                throw new PreparerException(
+                        "The request dispatcher threw an I/O exception", e);
+            }
+        } else {
+            throw new PreparerException("Cannot dispatch url '" + url
+                    + "' since this preparer has not been called under a 
servlet environment");
+        }
+    }
+
+}

Propchange: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/UrlPreparer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html?rev=672628&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
 (added)
+++ 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
 Sun Jun 29 08:02:15 2008
@@ -0,0 +1,33 @@
+<!--
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+-->
+<html>
+<head>
+    <title>Tiles compatibility view preparers package</title>
+</head>
+<body>
+<p>"View preparers" are objects that allows the "preparation" of a Tiles 
artifact
+(definition, template or attribute) before it is rendered.</p>
+<p>This package contains "compatibility" preparers, i.e. ViewPreparers that are
+compatibile with Tiles 1 "controllers".</p>
+</body>
+</html>
\ No newline at end of file

Propchange: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/framework/trunk/tiles-compat/src/main/java/org/apache/tiles/compat/preparer/package.html
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java?rev=672628&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
 (added)
+++ 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
 Sun Jun 29 08:02:15 2008
@@ -0,0 +1,67 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.compat.preparer;
+
+import org.apache.tiles.preparer.ViewPreparer;
+import org.apache.tiles.preparer.ViewPreparerSupport;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests [EMAIL PROTECTED] CompatibilityPreparerFactory}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class CompatibilityPreparerFactoryTest extends TestCase {
+
+    /**
+     * The factory to test.
+     */
+    private CompatibilityPreparerFactory factory;
+
+    /** [EMAIL PROTECTED] */
+    @Override
+    protected void setUp() throws Exception {
+        factory = new CompatibilityPreparerFactory();
+    }
+
+    /**
+     * Test method for
+     * [EMAIL PROTECTED] 
org.apache.tiles.compat.preparer.CompatibilityPreparerFactory#createPreparer(java.lang.String)}
+     * .
+     */
+    public void testCreatePreparer() {
+        ViewPreparer preparer = factory.createPreparer("/my/url.do");
+        assertTrue("The preparer is not an UrlPreparer",
+                preparer instanceof UrlPreparer);
+        preparer = factory.createPreparer(MockViewPreparer.class.getName());
+        assertTrue("The preparer is not an class ViewPreparer",
+                preparer instanceof MockViewPreparer);
+    }
+
+    /**
+     * Mock view preparer to test preparer instantiation.
+     */
+    public static final class MockViewPreparer extends ViewPreparerSupport {
+        // Nothing here.
+    }
+}

Propchange: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/CompatibilityPreparerFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java?rev=672628&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
 (added)
+++ 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
 Sun Jun 29 08:02:15 2008
@@ -0,0 +1,85 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.compat.preparer;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.tiles.AttributeContext;
+import org.apache.tiles.context.TilesRequestContext;
+import org.apache.tiles.servlet.context.ServletTilesRequestContext;
+import org.easymock.EasyMock;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests [EMAIL PROTECTED] UrlPreparer}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class UrlPreparerTest extends TestCase {
+
+    /**
+     * The preparer to test.
+     */
+    private UrlPreparer preparer;
+
+    /** [EMAIL PROTECTED] */
+    @Override
+    protected void setUp() throws Exception {
+        preparer = new UrlPreparer("/my/url.do");
+    }
+
+    /**
+     * Test method for
+     * [EMAIL PROTECTED] org.apache.tiles.compat.preparer.UrlPreparer#execute(
+     * org.apache.tiles.context.TilesRequestContext, 
org.apache.tiles.AttributeContext)}.
+     * @throws IOException If something goes wrong.
+     * @throws ServletException If something goes wrong.
+     */
+    public void testExecute() throws ServletException, IOException {
+        HttpServletRequest request = EasyMock
+                .createMock(HttpServletRequest.class);
+        HttpServletResponse response = EasyMock
+                .createMock(HttpServletResponse.class);
+        ServletContext servletContext = EasyMock
+                .createMock(ServletContext.class);
+        RequestDispatcher rd = EasyMock.createMock(RequestDispatcher.class);
+        TilesRequestContext requestContext = new ServletTilesRequestContext(
+                servletContext, request, response);
+        AttributeContext attributeContext = EasyMock
+                .createMock(AttributeContext.class);
+
+        EasyMock.expect(servletContext.getRequestDispatcher("/my/url.do"))
+                .andReturn(rd);
+        rd.include(request, response);
+        EasyMock
+                .replay(request, response, servletContext, attributeContext, 
rd);
+        preparer.execute(requestContext, attributeContext);
+        EasyMock.verify(rd);
+    }
+}

Propchange: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/framework/trunk/tiles-compat/src/test/java/org/apache/tiles/compat/preparer/UrlPreparerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL


Reply via email to