|
| 1 | +/** |
| 2 | + * Copyright (C) 2011-2015 The XDocReport Team <[email protected]> |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | + * a copy of this software and associated documentation files (the |
| 8 | + * "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to |
| 12 | + * the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | +package fr.opensagres.xdocreport.template.freemarker; |
| 26 | + |
| 27 | +import static org.junit.Assert.*; |
| 28 | + |
| 29 | +import java.io.StringWriter; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import fr.opensagres.xdocreport.template.freemarker.internal.XDocFreemarkerContext; |
| 38 | +import freemarker.template.Configuration; |
| 39 | +import freemarker.template.Template; |
| 40 | + |
| 41 | +/** |
| 42 | + * Test case to verify that object rendering still works with security fixes |
| 43 | + */ |
| 44 | +public class ObjectRenderingTestCase |
| 45 | +{ |
| 46 | + |
| 47 | + // Test classes |
| 48 | + public static class Person { |
| 49 | + private String name; |
| 50 | + private int age; |
| 51 | + private Address address; |
| 52 | + |
| 53 | + public Person(String name, int age) { |
| 54 | + this.name = name; |
| 55 | + this.age = age; |
| 56 | + } |
| 57 | + |
| 58 | + public Person(String name, int age, Address address) { |
| 59 | + this.name = name; |
| 60 | + this.age = age; |
| 61 | + this.address = address; |
| 62 | + } |
| 63 | + |
| 64 | + public String getName() { return name; } |
| 65 | + public int getAge() { return age; } |
| 66 | + public Address getAddress() { return address; } |
| 67 | + } |
| 68 | + |
| 69 | + public static class Address { |
| 70 | + private String city; |
| 71 | + private String country; |
| 72 | + |
| 73 | + public Address(String city, String country) { |
| 74 | + this.city = city; |
| 75 | + this.country = country; |
| 76 | + } |
| 77 | + |
| 78 | + public String getCity() { return city; } |
| 79 | + public String getCountry() { return country; } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testObjectPropertiesRendering() |
| 84 | + { |
| 85 | + FreemarkerTemplateEngine engine = new FreemarkerTemplateEngine(); |
| 86 | + XDocFreemarkerContext context = new XDocFreemarkerContext(); |
| 87 | + |
| 88 | + // Test 1: Simple object properties |
| 89 | + Person cuong = new Person("Cuong", 30); |
| 90 | + context.put("cuong", cuong); |
| 91 | + |
| 92 | + assertEquals("Cuong", context.get("cuong")); |
| 93 | + |
| 94 | + // Test 2: Nested object properties |
| 95 | + Person user = new Person("John", 25, new Address("Hanoi", "Vietnam")); |
| 96 | + context.put("user", user); |
| 97 | + |
| 98 | + assertEquals("John", context.get("user")); |
| 99 | + |
| 100 | + // Test 3: List of objects |
| 101 | + List<Person> employees = Arrays.asList( |
| 102 | + new Person("Alice", 28), |
| 103 | + new Person("Bob", 32), |
| 104 | + new Person("Charlie", 29) |
| 105 | + ); |
| 106 | + context.put("employees", employees); |
| 107 | + |
| 108 | + assertEquals(3, ((List<?>)context.get("employees")).size()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testMapPropertiesRendering() |
| 113 | + { |
| 114 | + XDocFreemarkerContext context = new XDocFreemarkerContext(); |
| 115 | + |
| 116 | + // Test with Map |
| 117 | + Map<String, Object> personMap = new HashMap<String, Object>(); |
| 118 | + personMap.put("name", "Cuong"); |
| 119 | + personMap.put("age", 30); |
| 120 | + personMap.put("department", "IT"); |
| 121 | + |
| 122 | + context.put("person", personMap); |
| 123 | + |
| 124 | + assertEquals("Cuong", ((Map<?, ?>)context.get("person")).get("name")); |
| 125 | + assertEquals(30, ((Map<?, ?>)context.get("person")).get("age")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testFreeMarkerSafeBuiltins() |
| 130 | + { |
| 131 | + XDocFreemarkerContext context = new XDocFreemarkerContext(); |
| 132 | + |
| 133 | + // Test that safe FreeMarker built-ins are still accessible in context |
| 134 | + context.put("userName", "john.doe"); |
| 135 | + context.put("safeBuiltin", "${userName?upper_case}"); |
| 136 | + context.put("lengthBuiltin", "${userName?length}"); |
| 137 | + context.put("defaultBuiltin", "${userName!'default'}"); |
| 138 | + |
| 139 | + // These should not throw exceptions |
| 140 | + assertNotNull(context.get("safeBuiltin")); |
| 141 | + assertNotNull(context.get("lengthBuiltin")); |
| 142 | + assertNotNull(context.get("defaultBuiltin")); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testTemplateProcessingWithObjects() |
| 147 | + { |
| 148 | + FreemarkerTemplateEngine engine = new FreemarkerTemplateEngine(); |
| 149 | + XDocFreemarkerContext context = new XDocFreemarkerContext(); |
| 150 | + |
| 151 | + // Add test data |
| 152 | + Person cuong = new Person("Cuong", 30, new Address("Hanoi", "Vietnam")); |
| 153 | + context.put("cuong", cuong); |
| 154 | + context.put("title", "Employee Report"); |
| 155 | + |
| 156 | + try |
| 157 | + { |
| 158 | + // Create a simple template |
| 159 | + String templateContent = "Hello ${cuong.name}, you are ${cuong.age} years old and live in ${cuong.address.city}."; |
| 160 | + Template template = new Template("test", templateContent, engine.getFreemarkerConfiguration()); |
| 161 | + |
| 162 | + // Process template |
| 163 | + StringWriter writer = new StringWriter(); |
| 164 | + template.process(context.getContextMap(), writer); |
| 165 | + String result = writer.toString(); |
| 166 | + |
| 167 | + // Verify the result contains our data |
| 168 | + assertTrue("Template should contain name", result.contains("Cuong")); |
| 169 | + assertTrue("Template should contain age", result.contains("30")); |
| 170 | + assertTrue("Template should contain city", result.contains("Hanoi")); |
| 171 | + } |
| 172 | + catch (Exception e) |
| 173 | + { |
| 174 | + fail("Template processing should work with objects: " + e.getMessage()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testListProcessing() |
| 180 | + { |
| 181 | + FreemarkerTemplateEngine engine = new FreemarkerTemplateEngine(); |
| 182 | + XDocFreemarkerContext context = new XDocFreemarkerContext(); |
| 183 | + |
| 184 | + // Add list data |
| 185 | + List<Person> employees = Arrays.asList( |
| 186 | + new Person("Alice", 28), |
| 187 | + new Person("Bob", 32) |
| 188 | + ); |
| 189 | + context.put("employees", employees); |
| 190 | + |
| 191 | + try |
| 192 | + { |
| 193 | + // Create a template with list processing |
| 194 | + String templateContent = "[#list employees as emp]${emp.name} (${emp.age})[/#list]"; |
| 195 | + Template template = new Template("test", templateContent, engine.getFreemarkerConfiguration()); |
| 196 | + |
| 197 | + // Process template |
| 198 | + StringWriter writer = new StringWriter(); |
| 199 | + template.process(context.getContextMap(), writer); |
| 200 | + String result = writer.toString(); |
| 201 | + |
| 202 | + // Verify the result |
| 203 | + assertTrue("Template should contain Alice", result.contains("Alice")); |
| 204 | + assertTrue("Template should contain Bob", result.contains("Bob")); |
| 205 | + assertTrue("Template should contain ages", result.contains("28")); |
| 206 | + assertTrue("Template should contain ages", result.contains("32")); |
| 207 | + } |
| 208 | + catch (Exception e) |
| 209 | + { |
| 210 | + fail("List processing should work: " + e.getMessage()); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments