Skip to content

Commit bc40f17

Browse files
committed
Refactor inner test object classes into one package to make allowlisting
simpler for tests
1 parent f704595 commit bc40f17

32 files changed

Lines changed: 805 additions & 687 deletions

src/test/java/com/hubspot/jinjava/FilterOverrideTest.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5-
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
6-
import com.hubspot.jinjava.lib.filter.Filter;
5+
import com.hubspot.jinjava.testobjects.FilterOverrideTestObjects;
76
import java.util.HashMap;
87
import org.junit.Test;
98

@@ -16,26 +15,7 @@ public void itAllowsUsersToOverrideBuiltInFilters() {
1615

1716
assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("11");
1817

19-
jinjava.getGlobalContext().registerClasses(DescriptiveAddFilter.class);
18+
jinjava.getGlobalContext().registerClasses(FilterOverrideTestObjects.DescriptiveAddFilter.class);
2019
assertThat(jinjava.render(template, new HashMap<>())).isEqualTo("5 + 6 = 11");
2120
}
22-
23-
public static class DescriptiveAddFilter implements Filter {
24-
25-
@Override
26-
public String getName() {
27-
return "add";
28-
}
29-
30-
@Override
31-
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
32-
return (
33-
var +
34-
" + " +
35-
args[0] +
36-
" = " +
37-
(Integer.parseInt(var.toString()) + Integer.parseInt(args[0]))
38-
);
39-
}
40-
}
4121
}

src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java

Lines changed: 18 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.Assert.assertEquals;
55

6-
import com.google.common.collect.ForwardingList;
76
import com.google.common.collect.ImmutableMap;
87
import com.google.common.collect.ImmutableSet;
98
import com.google.common.collect.Lists;
@@ -18,10 +17,9 @@
1817
import com.hubspot.jinjava.interpret.TemplateError;
1918
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
2019
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
21-
import com.hubspot.jinjava.objects.PyWrapper;
2220
import com.hubspot.jinjava.objects.date.PyishDate;
21+
import com.hubspot.jinjava.testobjects.ExpressionResolverTestObjects;
2322
import java.math.BigDecimal;
24-
import java.util.Collection;
2523
import java.util.Date;
2624
import java.util.List;
2725
import java.util.Map;
@@ -163,7 +161,7 @@ public void itResolvesDictValWithDotParam() {
163161

164162
@Test
165163
public void itResolvesMapValOnCustomObject() {
166-
MyCustomMap dict = new MyCustomMap();
164+
ExpressionResolverTestObjects.MyCustomMap dict = new ExpressionResolverTestObjects.MyCustomMap();
167165
context.put("thedict", dict);
168166

169167
Object val = interpreter.resolveELExpression("thedict['foo']", -1);
@@ -177,7 +175,7 @@ public void itResolvesMapValOnCustomObject() {
177175

178176
@Test
179177
public void itResolvesOtherMethodsOnCustomMapObject() {
180-
MyCustomMap dict = new MyCustomMap();
178+
ExpressionResolverTestObjects.MyCustomMap dict = new ExpressionResolverTestObjects.MyCustomMap();
181179
context.put("thedict", dict);
182180

183181
Object val = interpreter.resolveELExpression("thedict.size", -1);
@@ -190,67 +188,6 @@ public void itResolvesOtherMethodsOnCustomMapObject() {
190188
assertThat(val2.toString()).isEqualTo("[foo=bar, two=2, size=777]");
191189
}
192190

193-
public static final class MyCustomMap implements Map<String, String> {
194-
195-
Map<String, String> data = ImmutableMap.of("foo", "bar", "two", "2", "size", "777");
196-
197-
@Override
198-
public int size() {
199-
return data.size();
200-
}
201-
202-
@Override
203-
public boolean isEmpty() {
204-
return data.isEmpty();
205-
}
206-
207-
@Override
208-
public boolean containsKey(Object key) {
209-
return data.containsKey(key);
210-
}
211-
212-
@Override
213-
public boolean containsValue(Object value) {
214-
return data.containsValue(value);
215-
}
216-
217-
@Override
218-
public String get(Object key) {
219-
return data.get(key);
220-
}
221-
222-
@Override
223-
public String put(String key, String value) {
224-
return null;
225-
}
226-
227-
@Override
228-
public String remove(Object key) {
229-
return null;
230-
}
231-
232-
@Override
233-
public void putAll(Map<? extends String, ? extends String> m) {}
234-
235-
@Override
236-
public void clear() {}
237-
238-
@Override
239-
public Set<String> keySet() {
240-
return data.keySet();
241-
}
242-
243-
@Override
244-
public Collection<String> values() {
245-
return data.values();
246-
}
247-
248-
@Override
249-
public Set<Entry<String, String>> entrySet() {
250-
return data.entrySet();
251-
}
252-
}
253-
254191
@Test
255192
public void itResolvesInnerDictVal() {
256193
Map<String, Object> dict = Maps.newHashMap();
@@ -274,24 +211,6 @@ public void itResolvesInnerListVal() {
274211
assertThat(val).isEqualTo("val");
275212
}
276213

277-
public static class MyCustomList<T> extends ForwardingList<T> implements PyWrapper {
278-
279-
private final List<T> list;
280-
281-
public MyCustomList(List<T> list) {
282-
this.list = list;
283-
}
284-
285-
@Override
286-
protected List<T> delegate() {
287-
return list;
288-
}
289-
290-
public int getTotalCount() {
291-
return list.size();
292-
}
293-
}
294-
295214
@Test
296215
public void itRecordsFilterNames() {
297216
Object val = interpreter.resolveELExpression("2.3 | round", -1);
@@ -301,7 +220,7 @@ public void itRecordsFilterNames() {
301220

302221
@Test
303222
public void callCustomListProperty() {
304-
List<Integer> myList = new MyCustomList<>(Lists.newArrayList(1, 2, 3, 4));
223+
List<Integer> myList = new ExpressionResolverTestObjects.MyCustomList<>(Lists.newArrayList(1, 2, 3, 4));
305224

306225
context.put("mylist", myList);
307226
Object val = interpreter.resolveELExpression("mylist.total_count", -1);
@@ -353,15 +272,15 @@ public void syntaxError() {
353272

354273
@Test
355274
public void itWrapsDates() {
356-
context.put("myobj", new MyClass(new Date(0)));
275+
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
357276
Object result = interpreter.resolveELExpression("myobj.date", -1);
358277
assertThat(result).isInstanceOf(PyishDate.class);
359278
assertThat(result.toString()).isEqualTo("1970-01-01 00:00:00");
360279
}
361280

362281
@Test
363282
public void blackListedProperties() {
364-
context.put("myobj", new MyClass(new Date(0)));
283+
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
365284
interpreter.resolveELExpression("myobj.class.methods[0]", -1);
366285

367286
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
@@ -373,14 +292,14 @@ public void blackListedProperties() {
373292

374293
@Test
375294
public void itWillNotReturnClassObjectProperties() {
376-
context.put("myobj", new MyClass(new Date(0)));
295+
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
377296
Object clazz = interpreter.resolveELExpression("myobj.clazz", -1);
378297
assertThat(clazz).isNull();
379298
}
380299

381300
@Test
382301
public void blackListedMethods() {
383-
context.put("myobj", new MyClass(new Date(0)));
302+
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
384303
interpreter.resolveELExpression("myobj.wait()", -1);
385304

386305
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
@@ -390,7 +309,7 @@ public void blackListedMethods() {
390309

391310
@Test
392311
public void itWillNotReturnClassObjects() {
393-
context.put("myobj", new MyClass(new Date(0)));
312+
context.put("myobj", new ExpressionResolverTestObjects.MyClass(new Date(0)));
394313
interpreter.resolveELExpression("myobj.getClass()", -1);
395314

396315
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
@@ -517,29 +436,29 @@ public void itStoresResolvedFunctions() {
517436

518437
@Test
519438
public void presentOptionalProperty() {
520-
context.put("myobj", new OptionalProperty(null, "foo"));
439+
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, "foo"));
521440
assertThat(interpreter.resolveELExpression("myobj.val", -1)).isEqualTo("foo");
522441
assertThat(interpreter.getErrorsCopy()).isEmpty();
523442
}
524443

525444
@Test
526445
public void emptyOptionalProperty() {
527-
context.put("myobj", new OptionalProperty(null, null));
446+
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, null));
528447
assertThat(interpreter.resolveELExpression("myobj.val", -1)).isNull();
529448
assertThat(interpreter.getErrorsCopy()).isEmpty();
530449
}
531450

532451
@Test
533452
public void presentNestedOptionalProperty() {
534-
context.put("myobj", new OptionalProperty(new MyClass(new Date(0)), "foo"));
453+
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(new ExpressionResolverTestObjects.MyClass(new Date(0)), "foo"));
535454
assertThat(Objects.toString(interpreter.resolveELExpression("myobj.nested.date", -1)))
536455
.isEqualTo("1970-01-01 00:00:00");
537456
assertThat(interpreter.getErrorsCopy()).isEmpty();
538457
}
539458

540459
@Test
541460
public void emptyNestedOptionalProperty() {
542-
context.put("myobj", new OptionalProperty(null, null));
461+
context.put("myobj", new ExpressionResolverTestObjects.OptionalProperty(null, null));
543462
assertThat(interpreter.resolveELExpression("myobj.nested.date", -1)).isNull();
544463
assertThat(interpreter.getErrorsCopy()).isEmpty();
545464
}
@@ -548,7 +467,7 @@ public void emptyNestedOptionalProperty() {
548467
public void presentNestedNestedOptionalProperty() {
549468
context.put(
550469
"myobj",
551-
new NestedOptionalProperty(new OptionalProperty(new MyClass(new Date(0)), "foo"))
470+
new ExpressionResolverTestObjects.NestedOptionalProperty(new ExpressionResolverTestObjects.OptionalProperty(new ExpressionResolverTestObjects.MyClass(new Date(0)), "foo"))
552471
);
553472
assertThat(
554473
Objects.toString(interpreter.resolveELExpression("myobj.nested.nested.date", -1))
@@ -559,7 +478,7 @@ public void presentNestedNestedOptionalProperty() {
559478

560479
@Test
561480
public void itResolvesLazyExpressionsToTheirUnderlyingValue() {
562-
TestClass testClass = new TestClass();
481+
ExpressionResolverTestObjects.TestClass testClass = new ExpressionResolverTestObjects.TestClass();
563482
Supplier<String> lazyString = () -> result("hallelujah", testClass);
564483

565484
context.put("myobj", ImmutableMap.of("test", LazyExpression.of(lazyString, "")));
@@ -580,7 +499,7 @@ public void itResolvesNullLazyExpressions() {
580499

581500
@Test
582501
public void itResolvesSuppliersOnlyIfResolved() {
583-
TestClass testClass = new TestClass();
502+
ExpressionResolverTestObjects.TestClass testClass = new ExpressionResolverTestObjects.TestClass();
584503
Supplier<String> lazyString = () -> result("hallelujah", testClass);
585504

586505
context.put(
@@ -596,7 +515,7 @@ public void itResolvesSuppliersOnlyIfResolved() {
596515

597516
@Test
598517
public void itResolvesLazyExpressionsInNested() {
599-
Supplier<TestClass> lazyObject = TestClass::new;
518+
Supplier<ExpressionResolverTestObjects.TestClass> lazyObject = ExpressionResolverTestObjects.TestClass::new;
600519

601520
context.put("myobj", ImmutableMap.of("test", LazyExpression.of(lazyObject, "")));
602521

@@ -675,75 +594,9 @@ public void itAddsInvalidInputErrorWhenArithmeticExceptionIsThrown() {
675594
.isEqualTo(ErrorReason.INVALID_INPUT);
676595
}
677596

678-
public String result(String value, TestClass testClass) {
597+
public String result(String value, ExpressionResolverTestObjects.TestClass testClass) {
679598
testClass.touch();
680599
return value;
681600
}
682601

683-
public static class TestClass {
684-
685-
private boolean touched = false;
686-
private String name = "Amazing test class";
687-
688-
public boolean isTouched() {
689-
return touched;
690-
}
691-
692-
public void touch() {
693-
this.touched = true;
694-
}
695-
696-
public String getName() {
697-
return name;
698-
}
699-
}
700-
701-
public static final class MyClass {
702-
703-
private Date date;
704-
705-
MyClass(Date date) {
706-
this.date = date;
707-
}
708-
709-
public Class getClazz() {
710-
return this.getClass();
711-
}
712-
713-
public Date getDate() {
714-
return date;
715-
}
716-
}
717-
718-
public static final class OptionalProperty {
719-
720-
private MyClass nested;
721-
private String val;
722-
723-
OptionalProperty(MyClass nested, String val) {
724-
this.nested = nested;
725-
this.val = val;
726-
}
727-
728-
public Optional<MyClass> getNested() {
729-
return Optional.ofNullable(nested);
730-
}
731-
732-
public Optional<String> getVal() {
733-
return Optional.ofNullable(val);
734-
}
735-
}
736-
737-
public static final class NestedOptionalProperty {
738-
739-
private OptionalProperty nested;
740-
741-
public NestedOptionalProperty(OptionalProperty nested) {
742-
this.nested = nested;
743-
}
744-
745-
public Optional<OptionalProperty> getNested() {
746-
return Optional.ofNullable(nested);
747-
}
748-
}
749602
}

0 commit comments

Comments
 (0)