Author: mcucchiara
Date: Tue Mar  8 14:24:16 2011
New Revision: 1079368

URL: http://svn.apache.org/viewvc?rev=1079368&view=rev
Log:
WW-3524 - Added field bridge support

Added:
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONParameter.java
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/FieldBridge.java
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/ParameterizedBridge.java
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/StringBridge.java
    
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
    
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
    
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-02.txt
Modified:
    
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
    
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java

Modified: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java?rev=1079368&r1=1079367&r2=1079368&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
 (original)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
 Tue Mar  8 14:24:16 2011
@@ -23,6 +23,10 @@ package org.apache.struts2.json;
 import com.opensymphony.xwork2.util.logging.Logger;
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 import org.apache.struts2.json.annotations.JSON;
+import org.apache.struts2.json.annotations.JSONFieldBridge;
+import org.apache.struts2.json.annotations.JSONParameter;
+import org.apache.struts2.json.bridge.FieldBridge;
+import org.apache.struts2.json.bridge.ParameterizedBridge;
 
 import java.beans.BeanInfo;
 import java.beans.Introspector;
@@ -33,13 +37,7 @@ import java.text.CharacterIterator;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.text.StringCharacterIterator;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Stack;
+import java.util.*;
 import java.util.regex.Pattern;
 
 /**
@@ -70,8 +68,7 @@ class JSONWriter {
     private boolean excludeNullProperties;
 
     /**
-     * @param object
-     *            Object to be serialized into JSON
+     * @param object Object to be serialized into JSON
      * @return JSON string for object
      * @throws JSONException
      */
@@ -80,13 +77,12 @@ class JSONWriter {
     }
 
     /**
-     * @param object
-     *            Object to be serialized into JSON
+     * @param object Object to be serialized into JSON
      * @return JSON string for object
      * @throws JSONException
      */
     public String write(Object object, Collection<Pattern> excludeProperties,
-            Collection<Pattern> includeProperties, boolean 
excludeNullProperties) throws JSONException {
+                        Collection<Pattern> includeProperties, boolean 
excludeNullProperties) throws JSONException {
         this.excludeNullProperties = excludeNullProperties;
         this.buf.setLength(0);
         this.root = object;
@@ -139,7 +135,7 @@ class JSONWriter {
         if (object instanceof Class) {
             this.string(object);
         } else if (object instanceof Boolean) {
-            this.bool(((Boolean) object).booleanValue());
+            this.bool((Boolean) object);
         } else if (object instanceof Number) {
             this.add(object);
         } else if (object instanceof String) {
@@ -184,42 +180,22 @@ class JSONWriter {
             PropertyDescriptor[] props = info.getPropertyDescriptors();
 
             boolean hasData = false;
-            for (int i = 0; i < props.length; ++i) {
-                PropertyDescriptor prop = props[i];
+            for (PropertyDescriptor prop : props) {
                 String name = prop.getName();
                 Method accessor = prop.getReadMethod();
-                Method baseAccessor = null;
-                if (clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1) {
-                    try {
-                        baseAccessor = Class.forName(
-                                clazz.getName().substring(0, 
clazz.getName().indexOf("$$"))).getMethod(
-                                accessor.getName(), 
accessor.getParameterTypes());
-                    } catch (Exception ex) {
-                        LOG.debug(ex.getMessage(), ex);
-                    }
-                } else if (clazz.getName().indexOf("$$_javassist") > -1) {
-                       try {
-                        baseAccessor = Class.forName(
-                                clazz.getName().substring(0, 
clazz.getName().indexOf("_$$")))
-                                .getMethod(accessor.getName(), 
accessor.getParameterTypes());
-                    } catch (Exception ex) {
-                        LOG.debug(ex.getMessage(), ex);
-                    }
-                } else
-                    baseAccessor = accessor;
+                Method baseAccessor = findBaseAccessor(clazz, accessor);
 
                 if (baseAccessor != null) {
+                    if (baseAccessor.isAnnotationPresent(JSON.class)) {
+                        JSONAnnotationFinder jsonFinder = new 
JSONAnnotationFinder(baseAccessor).invoke();
 
-                    JSON json = baseAccessor.getAnnotation(JSON.class);
-                    if (json != null) {
-                        if (!json.serialize())
-                            continue;
-                        else if (json.name().length() > 0)
-                            name = json.name();
+                        if (!jsonFinder.shouldSerialize()) continue;
+                        if (jsonFinder.getName() != null) {
+                            name = jsonFinder.getName();
+                        }
                     }
-
                     // ignore "class" and others
-                    if (this.shouldExcludeProperty(clazz, prop)) {
+                    if (this.shouldExcludeProperty(prop)) {
                         continue;
                     }
                     String expr = null;
@@ -231,7 +207,11 @@ class JSONWriter {
                         expr = this.setExprStack(expr);
                     }
 
-                    Object value = accessor.invoke(object, new Object[0]);
+                    Object value = accessor.invoke(object);
+                    if 
(baseAccessor.isAnnotationPresent(JSONFieldBridge.class)) {
+                        value = getBridgedValue(baseAccessor, value);
+                    }
+
                     boolean propertyPrinted = this.add(name, value, accessor, 
hasData);
                     hasData = hasData || propertyPrinted;
                     if (this.buildExpr) {
@@ -253,6 +233,48 @@ class JSONWriter {
         this.add("}");
     }
 
+    private Object getBridgedValue(Method baseAccessor, Object value) throws 
InstantiationException, IllegalAccessException {
+        JSONFieldBridge fieldBridgeAnn = 
baseAccessor.getAnnotation(JSONFieldBridge.class);
+        if (fieldBridgeAnn != null) {
+            Class impl = fieldBridgeAnn.impl();
+            FieldBridge instance = (FieldBridge) impl.newInstance();
+
+            if (fieldBridgeAnn.params().length > 0 && 
ParameterizedBridge.class.isAssignableFrom(impl)) {
+                Map<String, String> params = new HashMap<String, 
String>(fieldBridgeAnn.params().length);
+                for (JSONParameter param : fieldBridgeAnn.params()) {
+                    params.put(param.name(), param.value());
+                }
+                ((ParameterizedBridge) instance).setParameterValues(params);
+            }
+            value = instance.objectToString(value);
+        }
+        return value;
+    }
+
+    private Method findBaseAccessor(Class clazz, Method accessor) {
+        Method baseAccessor = null;
+        if (clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1) {
+            try {
+                baseAccessor = Class.forName(
+                        clazz.getName().substring(0, 
clazz.getName().indexOf("$$"))).getMethod(
+                        accessor.getName(), accessor.getParameterTypes());
+            } catch (Exception ex) {
+                LOG.debug(ex.getMessage(), ex);
+            }
+        } else if (clazz.getName().indexOf("$$_javassist") > -1) {
+            try {
+                baseAccessor = Class.forName(
+                        clazz.getName().substring(0, 
clazz.getName().indexOf("_$$")))
+                        .getMethod(accessor.getName(), 
accessor.getParameterTypes());
+            } catch (Exception ex) {
+                LOG.debug(ex.getMessage(), ex);
+            }
+        } else {
+            return accessor;
+        }
+        return baseAccessor;
+    }
+
     /**
      * Instrospect an Enum and serialize it as a name/value pair or as a bean
      * including all its own properties
@@ -265,19 +287,13 @@ class JSONWriter {
         }
     }
 
-    /**
-     * Ignore "class" field
-     */
-    private boolean shouldExcludeProperty(Class clazz, PropertyDescriptor 
prop) throws SecurityException,
+    private boolean shouldExcludeProperty(PropertyDescriptor prop) throws 
SecurityException,
             NoSuchFieldException {
         String name = prop.getName();
 
-        if (name.equals("class") || name.equals("declaringClass") || 
name.equals("cachedSuperClass")
-                || name.equals("metaClass")) {
-            return true;
-        }
+        return name.equals("class") || name.equals("declaringClass") || 
name.equals("cachedSuperClass")
+                || name.equals("metaClass");
 
-        return false;
     }
 
     private String expandExpr(int i) {
@@ -520,9 +536,8 @@ class JSONWriter {
 
     /**
      * Represent as unicode
-     * 
-     * @param c
-     *            character to be encoded
+     *
+     * @param c character to be encoded
      */
     private void unicode(char c) {
         this.add("\\u");
@@ -545,12 +560,39 @@ class JSONWriter {
      * If true, an Enum is serialized as a bean with a special property
      * _name=name() as all as all other properties defined within the 
enum.<br/>
      * If false, an Enum is serialized as a name=value pair (name=name())
-     * 
-     * @param enumAsBean
-     *            true to serialize an enum as a bean instead of as a 
name=value
-     *            pair (default=false)
+     *
+     * @param enumAsBean true to serialize an enum as a bean instead of as a 
name=value
+     *                   pair (default=false)
      */
     public void setEnumAsBean(boolean enumAsBean) {
         this.enumAsBean = enumAsBean;
     }
+
+    private static class JSONAnnotationFinder {
+        private boolean serialize = true;
+        private Method accessor;
+        private String name;
+
+        public JSONAnnotationFinder(Method accessor) {
+            this.accessor = accessor;
+        }
+
+        boolean shouldSerialize() {
+            return serialize;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+
+        public JSONAnnotationFinder invoke() {
+            JSON json = accessor.getAnnotation(JSON.class);
+            serialize = json.serialize();
+            if (serialize && json.name().length() > 0) {
+                name = json.name();
+            }
+            return this;
+        }
+    }
 }

Added: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,38 @@
+/*
+ * $Id: JSONFieldBridge.java 2010-10-28 10:25:38Z maurizio.cucchiara $
+ *
+ * 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.struts2.json.annotations;
+
+import org.apache.struts2.json.bridge.FieldBridge;
+import org.apache.struts2.json.bridge.StringBridge;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface JSONFieldBridge {
+    Class<? extends FieldBridge> impl() default StringBridge.class;
+
+    public JSONParameter[] params() default {};
+}

Added: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONParameter.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONParameter.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONParameter.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONParameter.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,41 @@
+/*
+ * $Id: JSONParameter.java 2010-10-28 10:23:32Z maurizio.cucchiara $
+ *
+ * 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.struts2.json.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * User: maurizio.cucchiara
+ * Date: Oct 28, 2010
+ * Time: 2:16:44 AM
+ * key/value parameter pattern
+ */
+@Target(ElementType.PARAMETER)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface JSONParameter {
+    String name();
+
+    String value();
+}

Added: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/FieldBridge.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/FieldBridge.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/FieldBridge.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/FieldBridge.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,31 @@
+/*
+ * $Id: FieldBridge.java 2010-10-28 10:30:59Z maurizio.cucchiara $
+ *
+ * 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.struts2.json.bridge;
+
+/**
+ * User: maurizio.cucchiara
+ * Date: Oct 28, 2010
+ * Time: 12:06:13 AM
+ */
+public interface FieldBridge {
+    String objectToString(Object object);
+}

Added: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/ParameterizedBridge.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/ParameterizedBridge.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/ParameterizedBridge.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/ParameterizedBridge.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,34 @@
+/*
+ * $Id: ParameterizedBridge.java 2010-10-28 11:29:19Z maurizio.cucchiara $
+ *
+ * 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.struts2.json.bridge;
+
+import java.util.Map;
+
+/**
+ * User: maurizio.cucchiara
+ * Date: Oct 28, 2010
+ * Time: 2:32:08 AM
+ */
+public interface ParameterizedBridge {
+
+    void setParameterValues(Map<String,String> parameters);
+}

Added: 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/StringBridge.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/StringBridge.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/StringBridge.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/main/java/org/apache/struts2/json/bridge/StringBridge.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,35 @@
+/*
+ * $Id: StringBridge.java 2010-10-28 10:30:59Z maurizio.cucchiara $
+ *
+ * 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.struts2.json.bridge;
+
+/**
+ * User: maurizio.cucchiara
+ * Date: Oct 28, 2010
+ * Time: 12:46:16 AM
+ */
+public class StringBridge implements FieldBridge{
+
+    public String objectToString(Object object) {
+        if(object==null) return null;
+        return object.toString();
+    }
+}

Added: 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/JSONWriterTest.java
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,63 @@
+package org.apache.struts2.json;
+
+import org.apache.struts2.StrutsTestCase;
+import org.apache.struts2.json.annotations.JSONFieldBridge;
+import org.apache.struts2.json.bridge.StringBridge;
+import org.junit.Test;
+
+import java.net.URL;
+
+public class JSONWriterTest extends StrutsTestCase{
+    @Test
+    public void testWrite() throws Exception {
+        Bean bean1=new Bean();
+        bean1.setStringField("str");
+        bean1.setBooleanField(true);
+        bean1.setCharField('s');
+        bean1.setDoubleField(10.1);
+        bean1.setFloatField(1.5f);
+        bean1.setIntField(10);
+        bean1.setLongField(100);
+        bean1.setEnumField(AnEnum.ValueA);
+        bean1.setEnumBean(AnEnumBean.Two);
+
+        JSONWriter jsonWriter = new JSONWriter();
+        jsonWriter.setEnumAsBean(false);
+        String json = jsonWriter.write(bean1);
+        
TestUtils.assertEquals(JSONWriter.class.getResource("jsonwriter-write-bean-01.txt"),
 json);
+    }
+
+    @Test
+    public void testWriteAnnotatedBean() throws Exception {
+        AnnotatedBean bean1=new AnnotatedBean();
+        bean1.setStringField("str");
+        bean1.setBooleanField(true);
+        bean1.setCharField('s');
+        bean1.setDoubleField(10.1);
+        bean1.setFloatField(1.5f);
+        bean1.setIntField(10);
+        bean1.setLongField(100);
+        bean1.setEnumField(AnEnum.ValueA);
+        bean1.setEnumBean(AnEnumBean.Two);
+        bean1.setUrl(new URL("http://www.google.com";));
+
+        JSONWriter jsonWriter = new JSONWriter();
+        jsonWriter.setEnumAsBean(false);
+        jsonWriter.setIgnoreHierarchy(false);
+        String json = jsonWriter.write(bean1);
+        
TestUtils.assertEquals(JSONWriter.class.getResource("jsonwriter-write-bean-02.txt"),
 json);
+    }
+
+    private class AnnotatedBean extends Bean{
+        private URL url;
+
+        @JSONFieldBridge(impl = StringBridge.class)
+        public URL getUrl() {
+            return url;
+        }
+
+        public void setUrl(URL url) {
+            this.url = url;
+        }
+    }
+}

Modified: 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java?rev=1079368&r1=1079367&r2=1079368&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java
 (original)
+++ 
struts/struts2/trunk/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java
 Tue Mar  8 14:24:16 2011
@@ -20,6 +20,8 @@
  */
 package org.apache.struts2.json;
 
+import org.junit.Assert;
+
 import java.io.InputStream;
 import java.net.URL;
 import java.util.StringTokenizer;
@@ -75,6 +77,12 @@ public class TestUtils {
         return bufferString.equals(writerString);
     }
 
+    public static void assertEquals(URL source, String text) throws Exception {
+        String writerString = TestUtils.normalize(text, true);
+        String bufferString = TestUtils.normalize(readContent(source), true);
+        Assert.assertEquals(bufferString,writerString);
+    }
+
     public static String readContent(URL url) throws Exception {
         if (url == null)
             throw new Exception("unable to verify a null URL");

Added: 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-01.txt
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,15 @@
+{
+    "bigDecimal":null,
+    "bigInteger":null,
+    "booleanField":true,
+    "byteField":0,
+    "charField":"s",
+    "doubleField":10.1,
+    "enumBean":"Two",
+    "enumField":"ValueA",
+    "floatField":1.5,
+    "intField":10,
+    "longField":100,
+    "objectField":null,
+    "stringField":"str"
+}

Added: 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-02.txt
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-02.txt?rev=1079368&view=auto
==============================================================================
--- 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-02.txt
 (added)
+++ 
struts/struts2/trunk/plugins/json/src/test/resources/org/apache/struts2/json/jsonwriter-write-bean-02.txt
 Tue Mar  8 14:24:16 2011
@@ -0,0 +1,16 @@
+{
+    "bigDecimal":null,
+    "bigInteger":null,
+    "booleanField":true,
+    "byteField":0,
+    "charField":"s",
+    "doubleField":10.1,
+    "enumBean":"Two",
+    "enumField":"ValueA",
+    "floatField":1.5,
+    "intField":10,
+    "longField":100,
+    "objectField":null,
+    "stringField":"str",
+    "url":"http:\/\/www.google.com"
+}


Reply via email to