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 2009/09/14 18:25:53 UTC

svn commit: r814725 - in /labs/magma/trunk/website-velocity/src: main/java/org/apache/magma/website/velocity/ test/java/org/apache/magma/website/velocity/ test/resources/org/apache/magma/website/velocity/

Author: simoneg
Date: Mon Sep 14 16:25:53 2009
New Revision: 814725

URL: http://svn.apache.org/viewvc?rev=814725&view=rev
Log:
LABS-458 : check for primitive types in formatting hook

Added:
    labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPrimitiveFormatting.vm
Modified:
    labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java
    labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java

Modified: labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj?rev=814725&r1=814724&r2=814725&view=diff
==============================================================================
--- labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj (original)
+++ labs/magma/trunk/website-velocity/src/main/java/org/apache/magma/website/velocity/FormatFields.aj Mon Sep 14 16:25:53 2009
@@ -34,6 +34,7 @@
 		&& this(bean);
 	
 	private Object ret = null;
+	private boolean nativeType = false;
 	private String doreturn = null;
 
     after (MagmaBeanSupport bean) returning (Object val): calledForRendering(bean) {
@@ -43,6 +44,7 @@
     	if (property == null) {    		
     		return;
     	}
+    	nativeType = property.getType().isPrimitive();
     	if (val instanceof String && property.getType().equals(String.class)) {
 			doreturn = StringEscapeUtils.escapeHtml((String)val);
     	} else {
@@ -53,7 +55,18 @@
     Object around(Object acval) : call(* Object+.toString()) && withincode(* ASTReference.render(..)) &&
     		target(acval) {
     	Object unfiltered = proceed(acval);
-    	if (acval == ret && doreturn != null) return doreturn;
+    	// Check if we have a formatted/converted value
+    	if (doreturn != null) {
+    		// Check if it is the same value, just in case
+    		if (acval == ret) return doreturn;
+    		// Native types are wrapped, so they don't result identity equals
+    		if (nativeType) {
+    			// The case were both are null is already covered
+    			if (acval != null && ret != null) {
+    				if (acval.equals(ret)) return doreturn;
+    			}
+    		}
+    	}
     	return unfiltered;
     }
 	

Modified: labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java?rev=814725&r1=814724&r2=814725&view=diff
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java (original)
+++ labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/FakeBean.java Mon Sep 14 16:25:53 2009
@@ -29,6 +29,7 @@
 	private Date date = null;
 	private String string = null;
 	private String richtext = null;
+	private double natval = 0.5;
 
 	@Format(format="yyyy/MM/dd")
 	public Date getDate() {
@@ -60,4 +61,15 @@
 	public void setRichtext(String richtext) {
 		this.richtext = richtext;
 	}
+
+	@Format(format="percent-2")
+	public double getNatval() {
+		return natval;
+	}
+
+	public void setNatval(double natval) {
+		this.natval = natval;
+	}
+
+	
 }

Modified: labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java?rev=814725&r1=814724&r2=814725&view=diff
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java (original)
+++ labs/magma/trunk/website-velocity/src/test/java/org/apache/magma/website/velocity/TestGettersFormatting.java Mon Sep 14 16:25:53 2009
@@ -67,6 +67,17 @@
 		String str = new String(baos.toByteArray(), "UTF-8");
 		assertEquals("1979/03/05", str.trim());
 	}
+
+	@Test
+	public void formattedPrimitive() throws Exception {
+		FakeBean fb = new FakeBean();
+		VelocityHtmlProducer prod = new VelocityHtmlProducer("testPrimitiveFormatting.vm");
+		prod.addParameter("bean", fb);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		prod.produce(baos);
+		String str = new String(baos.toByteArray(), "UTF-8");
+		assertEquals("50%", str.trim());
+	}
 	
 	@Test
 	public void escapeHtml() throws Exception {

Added: labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPrimitiveFormatting.vm
URL: http://svn.apache.org/viewvc/labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPrimitiveFormatting.vm?rev=814725&view=auto
==============================================================================
--- labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPrimitiveFormatting.vm (added)
+++ labs/magma/trunk/website-velocity/src/test/resources/org/apache/magma/website/velocity/testPrimitiveFormatting.vm Mon Sep 14 16:25:53 2009
@@ -0,0 +1,16 @@
+## 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.
+
+$bean.natval



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