Skip to content

Commit

Permalink
Added tests for the implementation of #809 in OGNL-based scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfernandez committed Dec 13, 2020
1 parent bbe4567 commit 0fe0a8a
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 4 deletions.
56 changes: 56 additions & 0 deletions src/test/java/org/thymeleaf/util/StandardExpressionUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,60 @@ public void testMightNeedExpressionObjects() {

}


@Test
public void testContainsOGNLInstantiationOrStatic() {

Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abcnew"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abcnew "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc3new "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc_new "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc$new "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc-new "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc new "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc.new "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc newnew"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abcnew ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc new ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc new w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc new w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc (new )w ewnew"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc (new)w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("abc +new )w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("newnew"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("(new )w ewnew"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("(new)w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("+new )w ewnew"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("!new )w ewnew"));

Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.SomeClass@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.SomenewClass@"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.Some Class@"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.Some newClass@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.Some new Class@"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.Some newClass@new"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("@a.b.Some newClass@new "));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("new@a.b.Some newClass@new"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a@a.b.Some newClass@a"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.Some newClass@ a"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic(" a@a.b.Some newClass@a "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a@a.b.SomeClass@a"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.SomeClass@ a"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic(" a@a.b.SomeClass@a "));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.SomeClass@ a @a.b.Some Class@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.Some Class@ a @a.b.SomeClass@"));
Assert.assertFalse(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.Some Class@ a @a.b.Some Class@"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.SomeClass@ @"));
Assert.assertTrue(StandardExpressionUtils.containsOGNLInstantiationOrStatic("a @a.b.SomeClass@@"));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="${new java.lang.String('hello')}">ALLOWED</p>
# ------------------------------------------------------------
%OUTPUT
<p>hello</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="${__${new java.lang.String('hello')}__}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:at0="${new java.lang.String('hello')}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:attr="at0=${new java.lang.String('hello')}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:href="~{${new java.lang.String('hello')}}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:insert="${new java.lang.String('hello')}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="@{${new java.lang.String('hello')}}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="${@org.thymeleaf.TemplateEngine@TIMER_LOGGER_NAME}">ALLOWED</p>
# ------------------------------------------------------------
%OUTPUT
<p>org.thymeleaf.TemplateEngine.TIMER</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="${__${@org.thymeleaf.TemplateEngine@TIMER_LOGGER_NAME}__}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:at0="${@org.thymeleaf.TemplateEngine@TIMER_LOGGER_NAME}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:href="~{${@org.thymeleaf.TemplateEngine@TIMER_LOGGER_NAME}}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%EXTENDS ../html5.01.common
# ------------------------------------------------------------
%INPUT
<p th:text="@{${@org.thymeleaf.TemplateEngine@TIMER_LOGGER_NAME}}">NOT ALLOWED</p>
# ------------------------------------------------------------
%EXCEPTION org.thymeleaf.exceptions.TemplateProcessingException
%EXCEPTION_MESSAGE_PATTERN (.*)\Qforbidden in this context\E(.*)
14 changes: 10 additions & 4 deletions src/test/resources/templateengine/features/util/util02.thtest
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
%TEMPLATE_MODE TEXT
# ------------------------------------------------------------
%CONTEXT
hourOfDayField = @java.util.Calendar@HOUR_OF_DAY
minuteField = @java.util.Calendar@MINUTE
secondField = @java.util.Calendar@SECOND
millisecondField = @java.util.Calendar@MILLISECOND
# ------------------------------------------------------------
%INPUT
[# th:with="date = ${#calendars.createToday()}"
th:text="${date.get(@java.util.Calendar@HOUR_OF_DAY) + ' ' +
date.get(@java.util.Calendar@MINUTE) + ' ' +
date.get(@java.util.Calendar@SECOND) + ' ' +
date.get(@java.util.Calendar@MILLISECOND)}"]should all be zero[/]
th:text="${date.get(hourOfDayField) + ' ' +
date.get(minuteField) + ' ' +
date.get(secondField) + ' ' +
date.get(millisecondField)}"]should all be zero[/]
# ------------------------------------------------------------
%OUTPUT
0 0 0 0

0 comments on commit 0fe0a8a

Please sign in to comment.