|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 24 |
|
25 | 25 | import java.io.InputStream; |
| 26 | +import java.util.HashMap; |
26 | 27 | import java.util.Locale; |
| 28 | +import java.util.Map; |
27 | 29 |
|
28 | 30 | import org.junit.jupiter.api.AfterEach; |
29 | 31 | import org.junit.jupiter.api.BeforeEach; |
@@ -108,6 +110,43 @@ void testOrder() { |
108 | 110 |
|
109 | 111 | } |
110 | 112 |
|
| 113 | + /** |
| 114 | + * A form-set constant overrides a global constant of the same name, because {@link Field#process} applies the |
| 115 | + * form-set constants before the global ones. This precedence must also hold for a form reached through extension: |
| 116 | + * when {@link Form#process} recurses into the parent form it has to pass the global and form-set constants in the |
| 117 | + * same order as for a non-extended form. With the arguments swapped the parent's fields resolve a shared constant |
| 118 | + * to the global value instead of the form-set value. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + void testConstantOrderWhenExtending() { |
| 122 | + final Map<String, String> globalConstants = new HashMap<>(); |
| 123 | + globalConstants.put("greeting", "global"); |
| 124 | + final Map<String, String> constants = new HashMap<>(); |
| 125 | + constants.put("greeting", "formset"); |
| 126 | + |
| 127 | + final Field baseField = new Field(); |
| 128 | + baseField.setProperty("name"); |
| 129 | + baseField.addVar("msg", "${greeting}", null); |
| 130 | + |
| 131 | + final Form baseForm = new Form(); |
| 132 | + baseForm.setName("baseForm"); |
| 133 | + baseForm.addField(baseField); |
| 134 | + |
| 135 | + final Form childForm = new Form(); |
| 136 | + childForm.setName("childForm"); |
| 137 | + childForm.setExtends("baseForm"); |
| 138 | + |
| 139 | + final Map<String, Form> forms = new HashMap<>(); |
| 140 | + forms.put("baseForm", baseForm); |
| 141 | + forms.put("childForm", childForm); |
| 142 | + |
| 143 | + // Processing the child reaches the still-unprocessed parent through the extension branch of Form.process. |
| 144 | + childForm.process(globalConstants, constants, forms); |
| 145 | + |
| 146 | + assertEquals("formset", baseForm.getField("name").getVar("msg").getValue(), |
| 147 | + "the form-set constant should override the global constant for an extended form"); |
| 148 | + } |
| 149 | + |
111 | 150 | /** |
112 | 151 | * Tests if we can override a rule. We "can" override a rule if the message shown when the firstName required test fails and the lastName test is null. |
113 | 152 | */ |
|
0 commit comments