Skip to content

Commit

Permalink
Fixes bugs: looping over wrapped map, looping over POJO with null values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McClure committed Oct 30, 2015
1 parent 025c872 commit b19a313
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Release History

v3.0.2 - 2015-10-30 tmcclure

Fixes bugs: looping over wrapped map, looping over POJO with null values

v3.0.1 - 2015-07-16 tmcclure

Better handling of nested POJOs.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/x5/template/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@
* Updates: <A href="http://www.x5software.com/chunk/">Chunk Documentation</A><BR>
*
* @author Tom McClure
* @version 3.0.1
* @version 3.0.2
*/

public class Chunk implements Map<String,Object>
{
public static final int HASH_THRESH = 8;
public static final int DEPTH_LIMIT = 17;

public static final String VERSION = "3.0.1";
public static final String VERSION = "3.0.2";

private static final String TRUE = "TRUE";

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/x5/template/LoopTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ private TableData fetchData(String dataVar, String origin)
data = TableOfMaps.boxEnumeration((java.util.Enumeration)unwrapped);
} else if (unwrapped instanceof java.util.Iterator) {
data = TableOfMaps.boxIterator((java.util.Iterator)unwrapped);
} else if (unwrapped instanceof Map) {
data = new ObjectTable((Map)unwrapped);
}
}
if (data == null) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/x5/util/ObjectDataMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,13 @@ public Map<String,Object> mapifyPOJO(Object pojo)
try {
paramValue = field.get(pojo);
} catch (IllegalAccessException e) {
continue;
}

if (paramValue != null) {
if (pickle == null) pickle = new HashMap<String,Object>();
// convert isActive to is_active
paramName = splitCamelCase(paramName);
storeValue(pickle, paramClass, paramName, paramValue, isBean);
}
if (pickle == null) pickle = new HashMap<String,Object>();
// convert isActive to is_active
paramName = splitCamelCase(paramName);
storeValue(pickle, paramClass, paramName, paramValue, isBean);
}

return pickle;
Expand Down Expand Up @@ -201,7 +200,9 @@ private Map<String,Object> mapifyCapsule(DataCapsule capsule)
private static void storeValue(Map<String,Object> pickle, Class paramClass,
String paramName, Object paramValue, boolean isBean)
{
if (paramClass.isArray() || paramValue instanceof List) {
if (paramValue == null) {
pickle.put(paramName, null);
} else if (paramClass.isArray() || paramValue instanceof List) {
pickle.put(paramName, paramValue);
} else if (paramClass == String.class) {
pickle.put(paramName, paramValue);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/x5/template/ChunkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;

Expand Down Expand Up @@ -922,6 +923,7 @@ public static class CircularThing
boolean isActive;
CircularThing boss;
CircularThing[] children;
Map<String,String> map;

public CircularThing(String name, int age, boolean isActive)
{
Expand All @@ -933,6 +935,10 @@ public CircularThing(String name, int age, boolean isActive)
// I traveled back in time, I am my own dad!
this.children = new CircularThing[]{this,this};
}

public void setMap(Map<String,String> map) {
this.map = map;
}
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/test/java/com/x5/template/IfTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ public void testNestedParens()
assertEquals("ODDEVEN",c.toString()+d.toString());
}

@Test
public void testLengthFilter()
{
Chunk c = new Chunk();
c.set("x", new String[]{});
c.append("{% if ($x|length == 0) %}pass{% else %}fail{% endif %}");

assertEquals("pass", c.toString());
}

@Test
public void testOnEmptyFilter()
{
Expand All @@ -344,10 +354,10 @@ public void testOnEmptyFilter()
c.setOrDelete("b",null);
c.set("c","non-empty");
c.set("d"," \n ");
c.append("{^if (~a|onempty(EMPTY) == EMPTY) }EMPTY{^else}FULL{/if} ");
c.append("{^if (~b|onempty(EMPTY) == EMPTY) }EMPTY{^else}FULL{/if} ");
c.append("{^if (~c|onempty(EMPTY) == EMPTY) }EMPTY{^else}FULL{/if} ");
c.append("{^if (~d|onempty(EMPTY) == EMPTY) }EMPTY{^else}FULL{/if}");
c.append("{% if ($a|onempty(EMPTY) == EMPTY) }EMPTY{% else %}FULL{% endif %} ");
c.append("{% if ($b|onempty(EMPTY) == EMPTY) }EMPTY{% else %}FULL{% endif %} ");
c.append("{% if ($c|onempty(EMPTY) == EMPTY) }EMPTY{% else %}FULL{% endif %} ");
c.append("{% if ($d|onempty(EMPTY) == EMPTY) }EMPTY{% else %}FULL{% endif %}");

assertEquals("EMPTY EMPTY FULL EMPTY", c.toString());
}
Expand Down
61 changes: 60 additions & 1 deletion src/test/java/com/x5/template/LoopTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.x5.template;

import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

Expand Down Expand Up @@ -385,6 +386,64 @@ public void testLoopOverNestedEnumeration()
assertEquals("a-b-c-d", c.toString());
}

@Test
public void testLoopOverMap()
{
Theme theme = new Theme();
Chunk c = theme.makeChunk();
Map<String,Object> map = new java.util.HashMap<String,Object>();
map.put("a", "1");
map.put("b", "2");
c.set("map", map);
c.append("{% loop in $map as $key:$value divider='-' %}{$key}:{$value}{% endloop %}");

assertEquals("a:1-b:2", c.toString());
}

@Test
public void testLoopOverNestedMap()
{
Theme theme = new Theme();
Chunk c = theme.makeChunk();
Map<String,Object> mapA = new java.util.HashMap<String,Object>();
Map<String,Object> mapB = new java.util.HashMap<String,Object>();
mapB.put("a", "1");
mapB.put("b", "2");
mapA.put("child", mapB);
c.set("parent_map", mapA);
c.append("{% loop in $parent_map.child as $key:$value divider='-' %}{$key}:{$value}{% endloop %}");

assertEquals("a:1-b:2", c.toString());
}

@Test
public void testLoopOverChildObject()
{
Theme theme = new Theme();
Chunk c = theme.makeChunk();
ChunkTest.CircularThing bob = new ChunkTest.CircularThing("Bob", 28, false);
c.set("bob", bob);
c.append("{% loop in $bob.boss as $key:$value divider='-' %}{$key}:{$value|type}{% endloop %}");

assertEquals("age:STRING-boss:OBJECT-children:LIST-map:NULL-name:STRING-pi:STRING", c.toString());
}

@Test
public void testLoopOverChildMap()
{
Theme theme = new Theme();
Chunk c = theme.makeChunk();
ChunkTest.CircularThing bob = new ChunkTest.CircularThing("Bob", 28, false);
Map map = new HashMap<String,String>();
map.put("a", "1");
map.put("b", "2");
bob.setMap(map);
c.set("bob", bob);
c.append("{% loop in $bob.map as $key:$value divider='-' %}{$key}:{$value}{% endloop %}");

assertEquals("a:1-b:2", c.toString());
}

@Test
public void testLoopOverEnumeration()
{
Expand Down Expand Up @@ -517,7 +576,7 @@ public void testDataResetAlt()
String widgets = "[[widget_id,widget_name],[1,thingamabob],[2,doodad]]";

Chunk c = theme.makeChunk();
c.append("{.loop in ~widgets as w}{$w.widget_id}{.onEmpty}EMPTY{/loop}");
c.append("{.loop in $widgets as $w}{$w.widget_id}{.onEmpty}EMPTY{/loop}");

c.set("widgets", widgets);

Expand Down

0 comments on commit b19a313

Please sign in to comment.