You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2008/10/28 03:19:49 UTC

svn commit: r708416 - in /labs/magma/trunk/foundation-website/src: main/java/org/apache/magma/website/ test/java/org/apache/magma/website/ test/java/org/apache/magma/website/templating/ test/java/org/apache/magma/website/teststuff/ test/resources/org/a...

Author: simoneg
Date: Mon Oct 27 19:19:48 2008
New Revision: 708416

URL: http://svn.apache.org/viewvc?rev=708416&view=rev
Log:
LABS-193 : reuse last template by default

Added:
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleSecondTemplate.java
    labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/second.html
Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java?rev=708416&r1=708415&r2=708416&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java Mon Oct 27 19:19:48 2008
@@ -94,13 +94,26 @@
 			mybaseurl += "/" + req.getServletPath();
 		}
 		
+		HttpSession session = req.getSession();
 		Template t = producer.getTemplate();
 		if (t == null) {
-			t = getDefaultTemplate();
+			Class<Template> tplcl = (Class<Template>) session.getAttribute("__magma_lasttemplate");
+			if (tplcl != null) {
+				try {
+					t = tplcl.newInstance();
+				} catch (InstantiationException e) {
+					throw new MagmaException(e, "Could not instantiate the template {0}, maybe missing a default constructor?", tplcl);
+				} catch (IllegalAccessException e) {
+					throw new MagmaException(e, "Security is not correctly configured, cannot create an instance of {0}", tplcl);
+				}
+			}
+			if (t == null) {
+				t = getDefaultTemplate();
+			}
 		}
+		session.setAttribute("__magma_lasttemplate", t.getClass());
 		t.setCurrent(producer);
 		
-		HttpSession session = req.getSession();
 		final String last = (String) session.getAttribute("__magma_lastmain");
 		if (last != null) {
 			t.setPrevious(new PreviousGiver() {

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java?rev=708416&r1=708415&r2=708416&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java Mon Oct 27 19:19:48 2008
@@ -47,6 +47,8 @@
 		one(req).setAttribute("__magma_url",path);
 		allowing(req).getAttribute("__magma_url"); will(returnValue(path));
 		atMost(1).of(session).getAttribute("__magma_lastmain"); will(returnValue(null));			
+		atMost(1).of(session).getAttribute("__magma_lasttemplate"); will(returnValue(null));			
+		allowing(session).setAttribute(with(equal("__magma_lasttemplate")), with(any(Class.class))); 			
 		atMost(1).of(session).setAttribute("__magma_lastmain",path);
 		allowing(session).getAttribute("name"); will(returnValue("test name"));
 		allowing(req).getHeader("User-Agent"); will(returnValue("Mozilla"));
@@ -71,6 +73,8 @@
 		allowing(req).getAttribute("__magma_url"); will(returnValue(path));
 		one(req).setAttribute("__magma_url",path);
 		atMost(1).of(session).getAttribute("__magma_lastmain"); will(returnValue(null));						
+		atMost(1).of(session).getAttribute("__magma_lasttemplate"); will(returnValue(null));			
+		allowing(session).setAttribute(with(equal("__magma_lasttemplate")), with(any(Class.class))); 			
 		allowing(session).setAttribute("__magma_lastmain", path);
 		one(resp).setContentType("text/html; charset=utf-8");			
 		allowing(resp).getOutputStream(); will(returnValue(out));

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java?rev=708416&r1=708415&r2=708416&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/templating/TemplatingTest.java Mon Oct 27 19:19:48 2008
@@ -25,6 +25,7 @@
 import org.apache.magma.website.MockServletOutputStream;
 import org.apache.magma.website.UnitTestDispatch;
 import org.apache.magma.website.teststuff.Person;
+import org.apache.magma.website.teststuff.SimpleSecondTemplate;
 import org.apache.magma.website.teststuff.SimpleTemplate;
 
 import javax.servlet.http.Cookie;
@@ -51,7 +52,24 @@
 		def.start();
 		
 		assertStructure(def.out.toString(), "<div id=\"zoneMain", "ConvertedSessionHandler.doSetUser]", "<div id=\"zoneSide", "AuxiliaryWebHandler.doAuxSearch]", "AuxiliaryWebHandler.doAuxPoll]");
+	}
+
+	@Test
+	public void reusingTemplate() throws Exception {
+		DefaultExpectations def = new DefaultExpectations() {{
+			one(session).getAttribute("__magma_lasttemplate"); will(returnValue(SimpleSecondTemplate.class));
+			one(session).setAttribute("user", "Simone");
+			one(session).getAttribute("user"); will(returnValue("Massimo"));						
+		}};
+		UnitTestDispatch disp = new UnitTestDispatch();
+		disp.setTemplate(new SimpleTemplate());
+		def.disp = disp;
+		def.setupMinimal("/testSession/setUser");
+		def.start();
+		
+		assertStructure(def.out.toString(), "Second template");
 	}	
+	
 
 	@Test
 	public void simpleTemplateWithReplacedAuxiliary() throws Exception {

Added: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleSecondTemplate.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleSecondTemplate.java?rev=708416&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleSecondTemplate.java (added)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/SimpleSecondTemplate.java Mon Oct 27 19:19:48 2008
@@ -0,0 +1,14 @@
+package org.apache.magma.website.teststuff;
+
+import org.apache.magma.website.templating.Template;
+
+public class SimpleSecondTemplate extends Template {
+
+	public SimpleSecondTemplate() {
+		super();
+		super.fileName = "second.html";
+	}
+
+	
+	
+}

Added: labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/second.html
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/second.html?rev=708416&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/second.html (added)
+++ labs/magma/trunk/foundation-website/src/test/resources/org/apache/magma/website/teststuff/second.html Mon Oct 27 19:19:48 2008
@@ -0,0 +1,25 @@
+<html>
+<!-- 
+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.
+-->
+  <head>
+  </head>
+  <body>
+    Second template
+    <div id="zoneMain">
+    </div>
+  </body>
+</html>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org