juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject incubator-juneau git commit: DynaBeans - checkpoint
Date Thu, 04 May 2017 00:55:18 GMT
Repository: incubator-juneau
Updated Branches:
  refs/heads/master f27bfcb46 -> ef55eca75


DynaBeans - checkpoint

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/ef55eca7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/ef55eca7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/ef55eca7

Branch: refs/heads/master
Commit: ef55eca75776f2e4ee631daa6893eeda854223da
Parents: f27bfcb
Author: JamesBognar <jamesbognar@apache.org>
Authored: Wed May 3 20:55:15 2017 -0400
Committer: JamesBognar <jamesbognar@apache.org>
Committed: Wed May 3 20:55:15 2017 -0400

----------------------------------------------------------------------
 .../test/java/org/apache/juneau/ComboTest.java  |  1 +
 .../org/apache/juneau/DynaBeanComboTest.java    | 98 ++++++++++++++++++++
 .../main/java/org/apache/juneau/BeanMap.java    |  7 +-
 .../main/java/org/apache/juneau/BeanMeta.java   |  5 +-
 .../org/apache/juneau/BeanPropertyMeta.java     | 14 ++-
 .../java/org/apache/juneau/xml/XmlBeanMeta.java |  2 +
 .../org/apache/juneau/xml/XmlSerializer.java    |  4 +-
 7 files changed, 123 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core-test/src/test/java/org/apache/juneau/ComboTest.java
----------------------------------------------------------------------
diff --git a/juneau-core-test/src/test/java/org/apache/juneau/ComboTest.java b/juneau-core-test/src/test/java/org/apache/juneau/ComboTest.java
index 00535bf..8a13997 100644
--- a/juneau-core-test/src/test/java/org/apache/juneau/ComboTest.java
+++ b/juneau-core-test/src/test/java/org/apache/juneau/ComboTest.java
@@ -177,6 +177,7 @@ public abstract class ComboTest {
 		} catch (AssertionError e) {
 			throw e;
 		} catch (Exception e) {
+			e.printStackTrace();
 			throw new AssertionError(comboInput.label + "/" + testName + " failed.  exception=" +
e.getLocalizedMessage());
 		}
 	}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core-test/src/test/java/org/apache/juneau/DynaBeanComboTest.java
----------------------------------------------------------------------
diff --git a/juneau-core-test/src/test/java/org/apache/juneau/DynaBeanComboTest.java b/juneau-core-test/src/test/java/org/apache/juneau/DynaBeanComboTest.java
new file mode 100644
index 0000000..bb9ac79
--- /dev/null
+++ b/juneau-core-test/src/test/java/org/apache/juneau/DynaBeanComboTest.java
@@ -0,0 +1,98 @@
+// ***************************************************************************************************************************
+// * 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.juneau;
+
+import static org.apache.juneau.TestUtils.*;
+
+import java.util.*;
+
+import org.apache.juneau.annotation.*;
+import org.apache.juneau.parser.*;
+import org.apache.juneau.serializer.*;
+import org.junit.runner.*;
+import org.junit.runners.*;
+
+/**
+ * Exhaustive serialization tests DynaBean support.
+ */
+@RunWith(Parameterized.class)
+@SuppressWarnings({"javadoc"})
+public class DynaBeanComboTest extends ComboTest {
+
+	@Parameterized.Parameters
+	public static Collection<Object[]> getParameters() {
+		return Arrays.asList(new Object[][] {
+			{ 	/* 0 */
+				new ComboInput<A>(
+					"A",
+					A.class,
+					new A().init(),
+					/* Json */		"{f1:1,f2a:'a',f2b:'b',f3:3}",
+					/* JsonT */		"{f1:1,f2a:'a',f2b:'b',f3:3}",
+					/* JsonR */		"{\n\tf1: 1,\n\tf2a: 'a',\n\tf2b: 'b',\n\tf3: 3\n}",
+					/* Xml */		"<object><f1>1</f1><f2a>a</f2a><f2b>b</f2b><f3>3</f3></object>",
+					/* XmlT */		"<object><f1>1</f1><f2a>a</f2a><f2b>b</f2b><f3>3</f3></object>",
+					/* XmlR */		"<object>\n\t<f1>1</f1>\n\t<f2a>a</f2a>\n\t<f2b>b</f2b>\n\t<f3>3</f3>\n</object>\n",
+					/* XmlNs */		"<object><f1>1</f1><f2a>a</f2a><f2b>b</f2b><f3>3</f3></object>",
+					/* Html */		"<table><tr><td>f1</td><td>1</td></tr><tr><td>f2a</td><td>a</td></tr><tr><td>f2b</td><td>b</td></tr><tr><td>f3</td><td>3</td></tr></table>",
+					/* HtmlT */		"<table><tr><td>f1</td><td>1</td></tr><tr><td>f2a</td><td>a</td></tr><tr><td>f2b</td><td>b</td></tr><tr><td>f3</td><td>3</td></tr></table>",
+					/* HtmlR */		"<table>\n\t<tr>\n\t\t<td>f1</td>\n\t\t<td>1</td>\n\t</tr>\n\t<tr>\n\t\t<td>f2a</td>\n\t\t<td>a</td>\n\t</tr>\n\t<tr>\n\t\t<td>f2b</td>\n\t\t<td>b</td>\n\t</tr>\n\t<tr>\n\t\t<td>f3</td>\n\t\t<td>3</td>\n\t</tr>\n</table>\n",
+					/* Uon */		"(f1=1,f2a=a,f2b=b,f3=3)",
+					/* UonT */		"(f1=1,f2a=a,f2b=b,f3=3)",
+					/* UonR */		"(\n\tf1=1,\n\tf2a=a,\n\tf2b=b,\n\tf3=3\n)",
+					/* UrlEnc */	"f1=1&f2a=a&f2b=b&f3=3",
+					/* UrlEncT */	"f1=1&f2a=a&f2b=b&f3=3",
+					/* UrlEncR */	"f1=1\n&f2a=a\n&f2b=b\n&f3=3",
+					/* MsgPack */	"84A2663101A3663261A161A3663262A162A2663303",
+					/* MsgPackT */	"84A2663101A3663261A161A3663262A162A2663303",
+					/* RdfXml */	"<rdf:RDF>\n<rdf:Description>\n<jp:f1>1</jp:f1>\n<jp:f2a>a</jp:f2a>\n<jp:f2b>b</jp:f2b>\n<jp:f3>3</jp:f3>\n</rdf:Description>\n</rdf:RDF>\n",
+					/* RdfXmlT */	"<rdf:RDF>\n<rdf:Description>\n<jp:f1>1</jp:f1>\n<jp:f2a>a</jp:f2a>\n<jp:f2b>b</jp:f2b>\n<jp:f3>3</jp:f3>\n</rdf:Description>\n</rdf:RDF>\n",
+					/* RdfXmlR */	"<rdf:RDF>\n  <rdf:Description>\n    <jp:f1>1</jp:f1>\n
   <jp:f2a>a</jp:f2a>\n    <jp:f2b>b</jp:f2b>\n    <jp:f3>3</jp:f3>\n
 </rdf:Description>\n</rdf:RDF>\n"
+				)
+				{
+					public void verify(A o) {
+						assertType(A.class, o);
+					}
+				}
+			},
+		});
+	}
+	
+	public DynaBeanComboTest(ComboInput<?> comboInput) {
+		super(comboInput);
+	}
+	
+	@Override
+	protected Serializer applySettings(Serializer s) throws Exception {
+		return s.builder().trimNullProperties(false).build();
+	}
+	
+	@Override
+	protected Parser applySettings(Parser p) throws Exception {
+		return p.builder().build();
+	}
+	
+	public static class A {
+		public int f1;
+		@BeanProperty(name="*")
+		public Map<String,Object> f2 = new LinkedHashMap<String,Object>();
+		public int f3;
+	
+		public A init() {
+			this.f1 = 1;
+			this.f2 = new ObjectMap().append("f2a", "a").append("f2b", "b");
+			this.f3 = 3;
+			return this;
+		}
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core/src/main/java/org/apache/juneau/BeanMap.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanMap.java b/juneau-core/src/main/java/org/apache/juneau/BeanMap.java
index 7e69c6c..85e1670 100644
--- a/juneau-core/src/main/java/org/apache/juneau/BeanMap.java
+++ b/juneau-core/src/main/java/org/apache/juneau/BeanMap.java
@@ -417,8 +417,11 @@ public class BeanMap<T> extends AbstractMap<String,Object>
implements Delegate<T
 		for (BeanPropertyMeta bpm : properties) {
 			try {
 				if (bpm.isDyna()) {
-					for (Map.Entry<String,Object> e : bpm.getDynaMap(this).entrySet())
-						l.add(new BeanPropertyValue(bpm, e.getKey(), e.getValue(), null));
+					for (Map.Entry<String,Object> e : bpm.getDynaMap(bean).entrySet()) {
+						Object val = e.getValue();
+						if (val != null || ! ignoreNulls)
+							l.add(new BeanPropertyValue(bpm, e.getKey(), val, null));
+					}
 				} else {
 					Object val = bpm.get(this, null);
 					if (val != null || ! ignoreNulls)

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core/src/main/java/org/apache/juneau/BeanMeta.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanMeta.java b/juneau-core/src/main/java/org/apache/juneau/BeanMeta.java
index ca2cfc6..bfa5246 100644
--- a/juneau-core/src/main/java/org/apache/juneau/BeanMeta.java
+++ b/juneau-core/src/main/java/org/apache/juneau/BeanMeta.java
@@ -660,7 +660,10 @@ public class BeanMeta<T> {
 	 * 	on this bean.
 	 */
 	public BeanPropertyMeta getPropertyMeta(String name) {
-		return this.properties.get(name);
+		BeanPropertyMeta bpm = properties.get(name);
+		if (bpm == null)
+			bpm = dynaProperty;
+		return bpm;
 	}
 
 	/**

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core/src/main/java/org/apache/juneau/BeanPropertyMeta.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanPropertyMeta.java b/juneau-core/src/main/java/org/apache/juneau/BeanPropertyMeta.java
index eb3efc6..ca20da7 100644
--- a/juneau-core/src/main/java/org/apache/juneau/BeanPropertyMeta.java
+++ b/juneau-core/src/main/java/org/apache/juneau/BeanPropertyMeta.java
@@ -166,6 +166,11 @@ public class BeanPropertyMeta {
 
 			isDyna = "*".equals(name);
 
+			if (isDyna)
+				rawTypeMeta = rawTypeMeta.getValueType();
+			if (rawTypeMeta == null)
+				return false;
+
 			// Do some annotation validation.
 			Class<?> c = rawTypeMeta.getInnerClass();
 			if (getter != null) {
@@ -668,7 +673,7 @@ public class BeanPropertyMeta {
 				m = (Map<String,Object>)field.get(bean);
 			else
 				throw new BeanRuntimeException(beanMeta.c, "Cannot set property ''{0}'' of type ''{1}''
to object of type ''{2}'' because no setter is defined on this property, and the existing
property value is null", name, this.getClassMeta().getInnerClass().getName(), findClassName(val));
-			return (m == null ? null : m.get(pName));
+			return (m == null ? null : m.put(pName, val));
 		}
 		if (setter != null)
 			return setter.invoke(bean, val);
@@ -692,8 +697,11 @@ public class BeanPropertyMeta {
 	 */
 	public Map<String,Object> getDynaMap(Object bean) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
 		if (isDyna) {
-			Map<String,Object> m = (Map<String,Object>)getter.invoke(bean);
-			return m == null ? Collections.EMPTY_MAP : m;
+			if (getter != null)
+				return (Map)getter.invoke(bean);
+			if (field != null)
+				return (Map)field.get(bean);
+			throw new BeanRuntimeException(beanMeta.c, "Getter or public field not defined on property
''{0}''", name);
 		}
 		return Collections.EMPTY_MAP;
 	}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core/src/main/java/org/apache/juneau/xml/XmlBeanMeta.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlBeanMeta.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlBeanMeta.java
index 7b00113..ed7ebb8 100644
--- a/juneau-core/src/main/java/org/apache/juneau/xml/XmlBeanMeta.java
+++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlBeanMeta.java
@@ -258,6 +258,8 @@ public class XmlBeanMeta extends BeanMetaExtended {
 	protected BeanPropertyMeta getPropertyMeta(String fieldName) {
 		if (collapsedProperties != null) {
 			BeanPropertyMeta bpm = collapsedProperties.get(fieldName);
+			if (bpm == null)
+				bpm = collapsedProperties.get("*");
 			if (bpm != null)
 				return bpm;
 		}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/ef55eca7/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
----------------------------------------------------------------------
diff --git a/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java b/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
index c16fcba..4b63fa5 100644
--- a/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
+++ b/juneau-core/src/main/java/org/apache/juneau/xml/XmlSerializer.java
@@ -629,7 +629,7 @@ public class XmlSerializer extends WriterSerializer {
 		ClassMeta<?> contentType = null;
 		for (BeanPropertyValue p : lp) {
 			String n = p.getName();
-			if (attrs.contains(n) || n.equals(attrsProperty)) {
+			if (attrs.contains(n) || attrs.contains("*") || n.equals(attrsProperty)) {
 				BeanPropertyMeta pMeta = p.getMeta();
 				ClassMeta<?> cMeta = p.getClassMeta();
 
@@ -692,7 +692,7 @@ public class XmlSerializer extends WriterSerializer {
 					hasContent = false;
 				else if (contentType.isArray() && Array.getLength(content) == 0)
 					hasContent = false;
-			} else if (elements.contains(n) || collapsedElements.contains(n)) {
+			} else if (elements.contains(n) || collapsedElements.contains(n) || elements.contains("*")
|| collapsedElements.contains("*") ) {
 				String key = p.getName();
 				Object value = p.getValue();
 				Throwable t = p.getThrown();


Mime
View raw message