Search in sources :

Example 31 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class EngineContextTest method test05.

@Test
public void test05() {
    final IEngineConfiguration configuration = TestTemplateEngineConfigurationBuilder.build();
    final TemplateData templateData1 = TestTemplateDataConfigurationBuilder.build("test01", TemplateMode.HTML);
    final EngineContext vm = new EngineContext(configuration, templateData1, null, LOCALE, null);
    vm.setVariable("one", "a value");
    Assert.assertEquals("{0:{one=a value}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.setVariable("one", "two values");
    Assert.assertEquals("{0:{one=two values}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.removeVariable("one");
    Assert.assertEquals("{0:{}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{}(test01)", vm.toString());
    Assert.assertEquals(Collections.emptySet(), vm.getVariableNames());
    vm.setVariable("one", "two values");
    Assert.assertEquals("{0:{one=two values}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.increaseLevel();
    vm.setVariable("one", "hello");
    Assert.assertEquals("{1:{one=hello},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.removeVariable("one");
    Assert.assertEquals("{1:{one=(*removed*)},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{}(test01)", vm.toString());
    Assert.assertEquals(Collections.emptySet(), vm.getVariableNames());
    vm.setVariable("one", "hello");
    Assert.assertEquals("{1:{one=hello},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.removeVariable("two");
    Assert.assertEquals("{1:{one=hello},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.setVariable("two", "twello");
    vm.setInliner(new StandardTextInliner(configuration));
    Assert.assertEquals("{1:{one=hello, two=twello}[StandardTextInliner],0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello, two=twello}[StandardTextInliner](test01)", vm.toString());
    Assert.assertEquals(createSet("one", "two"), vm.getVariableNames());
    vm.removeVariable("two");
    Assert.assertEquals("{1:{one=hello}[StandardTextInliner],0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello}[StandardTextInliner](test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.removeVariable("one");
    Assert.assertEquals("{1:{one=(*removed*)}[StandardTextInliner],0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{}[StandardTextInliner](test01)", vm.toString());
    Assert.assertEquals(Collections.emptySet(), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{0:{one=two values}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
    vm.increaseLevel();
    vm.setVariable("two", "twellor");
    Assert.assertEquals("{1:{two=twellor},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values, two=twellor}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "two"), vm.getVariableNames());
    vm.increaseLevel();
    vm.setVariable("three", "twelloree");
    Assert.assertEquals("{2:{three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values, two=twellor, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three", "two"), vm.getVariableNames());
    vm.setVariable("one", "atwe");
    Assert.assertEquals("{2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, two=twellor, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three", "two"), vm.getVariableNames());
    vm.increaseLevel();
    vm.removeVariable("two");
    Assert.assertEquals("{3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[3]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three"), vm.getVariableNames());
    vm.increaseLevel();
    vm.removeVariable("two");
    Assert.assertEquals("{3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[4]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three"), vm.getVariableNames());
    vm.increaseLevel();
    Assert.assertEquals("{3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[5]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three"), vm.getVariableNames());
    vm.setVariable("four", "lotwss");
    Assert.assertEquals("{5:{four=lotwss},3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[5]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree, four=lotwss}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three", "four"), vm.getVariableNames());
    vm.setVariable("two", "itwiii");
    Assert.assertEquals("{5:{four=lotwss, two=itwiii},3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[5]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree, four=lotwss, two=itwiii}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three", "four", "two"), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[4]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three"), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{3:{two=(*removed*)},2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[3]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three"), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{2:{one=atwe, three=twelloree},1:{two=twellor},0:{one=two values}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, two=twellor, three=twelloree}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "three", "two"), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{1:{two=twellor},0:{one=two values}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values, two=twellor}(test01)", vm.toString());
    Assert.assertEquals(createSet("one", "two"), vm.getVariableNames());
    vm.decreaseLevel();
    Assert.assertEquals("{0:{one=two values}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=two values}(test01)", vm.toString());
    Assert.assertEquals(createSet("one"), vm.getVariableNames());
}
Also used : StandardTextInliner(org.thymeleaf.standard.inline.StandardTextInliner) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) EngineContext(org.thymeleaf.context.EngineContext) Test(org.junit.Test)

Example 32 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class WebEngineContextTest method test11.

@Test
public void test11() {
    final IEngineConfiguration configuration = TestTemplateEngineConfigurationBuilder.build();
    final TemplateData templateData1 = TestTemplateDataConfigurationBuilder.build("test01", TemplateMode.HTML);
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, LOCALE);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final WebEngineContext vm = new WebEngineContext(configuration, templateData1, null, mockRequest, mockResponse, mockServletContext, LOCALE, null);
    vm.setVariable("one", "a value");
    Assert.assertFalse(vm.hasSelectionTarget());
    Assert.assertNull(vm.getSelectionTarget());
    Assert.assertEquals("{0:{one=a value}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}(test01)", vm.toString());
    vm.increaseLevel();
    vm.setVariable("one", "hello");
    vm.removeVariable("one");
    vm.setVariable("one", "hello");
    vm.removeVariable("two");
    vm.setVariable("two", "twello");
    vm.setVariable("two", "twellor");
    Assert.assertFalse(vm.hasSelectionTarget());
    Assert.assertNull(vm.getSelectionTarget());
    Assert.assertEquals("{1:{one=hello, two=twellor},0:{one=a value}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello, two=twellor}(test01)", vm.toString());
    vm.increaseLevel();
    vm.setVariable("three", "twelloree");
    vm.setVariable("one", "atwe");
    Assert.assertFalse(vm.hasSelectionTarget());
    Assert.assertNull(vm.getSelectionTarget());
    Assert.assertEquals("{2:{three=twelloree, one=atwe},1:{one=hello, two=twellor},0:{one=a value}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, two=twellor, three=twelloree}(test01)", vm.toString());
    vm.setSelectionTarget("BIGFORM");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertEquals("twellor", vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("BIGFORM", vm.getSelectionTarget());
    Assert.assertEquals("{2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, two=twellor, three=twelloree}<BIGFORM>(test01)", vm.toString());
    vm.increaseLevel();
    vm.removeVariable("two");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("BIGFORM", vm.getSelectionTarget());
    Assert.assertEquals("{3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[3]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}<BIGFORM>(test01)", vm.toString());
    vm.increaseLevel();
    vm.removeVariable("two");
    vm.setSelectionTarget("SMALLFORM");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("SMALLFORM", vm.getSelectionTarget());
    Assert.assertEquals("{4:<SMALLFORM>,3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[4]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}<SMALLFORM>(test01)", vm.toString());
    vm.increaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("SMALLFORM", vm.getSelectionTarget());
    Assert.assertEquals("{4:<SMALLFORM>,3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[5]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}<SMALLFORM>(test01)", vm.toString());
    vm.setVariable("four", "lotwss");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertTrue(vm.containsVariable("four"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertEquals("lotwss", vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("SMALLFORM", vm.getSelectionTarget());
    Assert.assertEquals("{5:{four=lotwss},4:<SMALLFORM>,3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[5]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree, four=lotwss}<SMALLFORM>(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertFalse(vm.containsVariable("four"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertNull(vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("SMALLFORM", vm.getSelectionTarget());
    Assert.assertEquals("{4:<SMALLFORM>,3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[4]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}<SMALLFORM>(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertFalse(vm.containsVariable("four"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertNull(vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("BIGFORM", vm.getSelectionTarget());
    Assert.assertEquals("{3:{two=null},2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[3]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree}<BIGFORM>(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertFalse(vm.containsVariable("four"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertEquals("twellor", vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertNull(vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("BIGFORM", vm.getSelectionTarget());
    Assert.assertEquals("{2:{three=twelloree, one=atwe}<BIGFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree, two=twellor}<BIGFORM>(test01)", vm.toString());
    vm.setSelectionTarget("MEDIUMFORM");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertFalse(vm.containsVariable("four"));
    Assert.assertEquals("atwe", vm.getVariable("one"));
    Assert.assertEquals("twellor", vm.getVariable("two"));
    Assert.assertEquals("twelloree", vm.getVariable("three"));
    Assert.assertNull(vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("MEDIUMFORM", vm.getSelectionTarget());
    Assert.assertEquals("{2:{three=twelloree, one=atwe}<MEDIUMFORM>,1:{one=hello, two=twellor},0:{one=a value}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=atwe, three=twelloree, two=twellor}<MEDIUMFORM>(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertFalse(vm.hasSelectionTarget());
    Assert.assertNull(vm.getSelectionTarget());
    Assert.assertEquals("{1:{one=hello, two=twellor},0:{one=a value}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello, two=twellor}(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertFalse(vm.hasSelectionTarget());
    Assert.assertNull(vm.getSelectionTarget());
    Assert.assertEquals("{0:{one=a value}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}(test01)", vm.toString());
    vm.setSelectionTarget("TOTALFORM");
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("TOTALFORM", vm.getSelectionTarget());
    Assert.assertEquals("{0:{one=a value}<TOTALFORM>(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}<TOTALFORM>(test01)", vm.toString());
    vm.increaseLevel();
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("TOTALFORM", vm.getSelectionTarget());
    Assert.assertEquals("{0:{one=a value}<TOTALFORM>(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}<TOTALFORM>(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertFalse(vm.containsVariable("three"));
    Assert.assertFalse(vm.containsVariable("four"));
    Assert.assertEquals("a value", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertNull(vm.getVariable("three"));
    Assert.assertNull(vm.getVariable("four"));
    Assert.assertTrue(vm.hasSelectionTarget());
    Assert.assertEquals("TOTALFORM", vm.getSelectionTarget());
    Assert.assertEquals("{0:{one=a value}<TOTALFORM>(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value}<TOTALFORM>(test01)", vm.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) WebEngineContext(org.thymeleaf.context.WebEngineContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 33 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class WebEngineContextTest method test07.

@Test
public void test07() {
    final IEngineConfiguration configuration = TestTemplateEngineConfigurationBuilder.build();
    final TemplateData templateData1 = TestTemplateDataConfigurationBuilder.build("test01", TemplateMode.HTML);
    final Map<String, Object> starting = new LinkedHashMap<String, Object>();
    starting.put("one", "ha");
    starting.put("ten", "tieen");
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, LOCALE);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final WebEngineContext vm = new WebEngineContext(configuration, templateData1, null, mockRequest, mockResponse, mockServletContext, LOCALE, starting);
    Assert.assertEquals("{0:{one=ha, ten=tieen}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=ha, ten=tieen}(test01)", vm.toString());
    vm.setVariable("one", "a value");
    Assert.assertEquals("{0:{one=a value, ten=tieen}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=a value, ten=tieen}(test01)", vm.toString());
    vm.increaseLevel();
    vm.setVariable("one", "hello");
    Assert.assertEquals("{1:{one=hello},0:{one=a value, ten=tieen}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello, ten=tieen}(test01)", vm.toString());
    vm.setVariable("two", "twello");
    Assert.assertEquals("{1:{one=hello, two=twello},0:{one=a value, ten=tieen}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{one=hello, ten=tieen, two=twello}(test01)", vm.toString());
    mockRequest.removeAttribute("one");
    Assert.assertEquals("{1:{two=twello},0:{ten=tieen}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{ten=tieen, two=twello}(test01)", vm.toString());
    vm.increaseLevel();
    vm.setVariable("one", "helloz");
    Assert.assertEquals("{2:{one=helloz},1:{two=twello},0:{ten=tieen}(test01)}[2]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{ten=tieen, two=twello, one=helloz}(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertEquals("{1:{two=twello},0:{ten=tieen}(test01)}[1]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{ten=tieen, two=twello}(test01)", vm.toString());
    vm.decreaseLevel();
    Assert.assertEquals("{0:{ten=tieen}(test01)}[0]", vm.getStringRepresentationByLevel());
    Assert.assertEquals("{ten=tieen}(test01)", vm.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) WebEngineContext(org.thymeleaf.context.WebEngineContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 34 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class WebEngineContextTest method test04.

@Test
public void test04() {
    final IEngineConfiguration configuration = TestTemplateEngineConfigurationBuilder.build();
    final TemplateData templateData1 = TestTemplateDataConfigurationBuilder.build("test01", TemplateMode.HTML);
    final Map<String, Object> starting = new LinkedHashMap<String, Object>();
    starting.put("one", "ha");
    starting.put("ten", "tieen");
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, LOCALE);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final WebEngineContext vm = new WebEngineContext(configuration, templateData1, null, mockRequest, mockResponse, mockServletContext, LOCALE, starting);
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertEquals("ha", vm.getVariable("one"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    vm.setVariable("one", "a value");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertEquals("a value", vm.getVariable("one"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    vm.increaseLevel();
    vm.setVariable("one", "hello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    vm.setVariable("two", "twello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    // This are directly set into the request, so they should only be affected by higher levels, never by decreasing levels
    mockRequest.setAttribute("one", "outer1");
    mockRequest.setAttribute("six", "outer6");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertEquals("outer1", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    Assert.assertEquals("outer6", vm.getVariable("six"));
    Assert.assertEquals("outer1", mockRequest.getAttribute("one"));
    Assert.assertEquals("twello", mockRequest.getAttribute("two"));
    Assert.assertEquals("tieen", mockRequest.getAttribute("ten"));
    Assert.assertEquals("outer6", mockRequest.getAttribute("six"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "one"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "two"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "ten"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "six"));
    vm.increaseLevel();
    vm.setVariable("one", "helloz");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertEquals("helloz", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    Assert.assertEquals("outer6", vm.getVariable("six"));
    Assert.assertEquals("helloz", mockRequest.getAttribute("one"));
    Assert.assertEquals("twello", mockRequest.getAttribute("two"));
    Assert.assertEquals("tieen", mockRequest.getAttribute("ten"));
    Assert.assertEquals("outer6", mockRequest.getAttribute("six"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "one"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "two"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "ten"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "six"));
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertEquals("outer1", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    Assert.assertEquals("outer6", vm.getVariable("six"));
    Assert.assertEquals("outer1", mockRequest.getAttribute("one"));
    Assert.assertEquals("twello", mockRequest.getAttribute("two"));
    Assert.assertEquals("tieen", mockRequest.getAttribute("ten"));
    Assert.assertEquals("outer6", mockRequest.getAttribute("six"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "one"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "two"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "ten"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "six"));
    vm.decreaseLevel();
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertFalse(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("ten"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertEquals("outer1", vm.getVariable("one"));
    Assert.assertNull(vm.getVariable("two"));
    Assert.assertEquals("tieen", vm.getVariable("ten"));
    Assert.assertEquals("outer6", vm.getVariable("six"));
    Assert.assertEquals("outer1", mockRequest.getAttribute("one"));
    Assert.assertNull(mockRequest.getAttribute("two"));
    Assert.assertEquals("tieen", mockRequest.getAttribute("ten"));
    Assert.assertEquals("outer6", mockRequest.getAttribute("six"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "one"));
    Assert.assertFalse(enumerationContains(mockRequest.getAttributeNames(), "two"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "ten"));
    Assert.assertTrue(enumerationContains(mockRequest.getAttributeNames(), "six"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) WebEngineContext(org.thymeleaf.context.WebEngineContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 35 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class WebEngineContextTest method test09.

@Test
public void test09() {
    final IEngineConfiguration configuration = TestTemplateEngineConfigurationBuilder.build();
    final TemplateData templateData1 = TestTemplateDataConfigurationBuilder.build("test01", TemplateMode.HTML);
    final Map<String, Object> requestAttributes = new LinkedHashMap<String, Object>();
    final Map<String, Object[]> requestParameters = new LinkedHashMap<String, Object[]>();
    final HttpServletRequest mockRequest = TestMockServletUtil.createHttpServletRequest("WebVariablesMap", null, requestAttributes, requestParameters, LOCALE);
    final HttpServletResponse mockResponse = TestMockServletUtil.createHttpServletResponse();
    final Map<String, Object> servletContextAttributes = new LinkedHashMap<String, Object>();
    final ServletContext mockServletContext = TestMockServletUtil.createServletContext(servletContextAttributes);
    final WebEngineContext vm = new WebEngineContext(configuration, templateData1, null, mockRequest, mockResponse, mockServletContext, LOCALE, null);
    vm.setVariable("one", "a value");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertEquals("a value", vm.getVariable("one"));
    vm.increaseLevel();
    vm.setVariable("one", "hello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    vm.setVariable("two", "twello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    vm.setVariable("three", "trwello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("trwello", vm.getVariable("three"));
    vm.setVariable("four", "fwello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertTrue(vm.containsVariable("four"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("trwello", vm.getVariable("three"));
    Assert.assertEquals("fwello", vm.getVariable("four"));
    vm.setVariable("five", "vwello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertTrue(vm.containsVariable("four"));
    Assert.assertTrue(vm.containsVariable("five"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("trwello", vm.getVariable("three"));
    Assert.assertEquals("fwello", vm.getVariable("four"));
    Assert.assertEquals("vwello", vm.getVariable("five"));
    vm.setVariable("six", "swello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertTrue(vm.containsVariable("four"));
    Assert.assertTrue(vm.containsVariable("five"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("trwello", vm.getVariable("three"));
    Assert.assertEquals("fwello", vm.getVariable("four"));
    Assert.assertEquals("vwello", vm.getVariable("five"));
    Assert.assertEquals("swello", vm.getVariable("six"));
    vm.setVariable("seven", "svwello");
    Assert.assertTrue(vm.containsVariable("one"));
    Assert.assertTrue(vm.containsVariable("two"));
    Assert.assertTrue(vm.containsVariable("three"));
    Assert.assertTrue(vm.containsVariable("four"));
    Assert.assertTrue(vm.containsVariable("five"));
    Assert.assertTrue(vm.containsVariable("six"));
    Assert.assertTrue(vm.containsVariable("seven"));
    Assert.assertEquals("hello", vm.getVariable("one"));
    Assert.assertEquals("twello", vm.getVariable("two"));
    Assert.assertEquals("trwello", vm.getVariable("three"));
    Assert.assertEquals("fwello", vm.getVariable("four"));
    Assert.assertEquals("vwello", vm.getVariable("five"));
    Assert.assertEquals("swello", vm.getVariable("six"));
    Assert.assertEquals("svwello", vm.getVariable("seven"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) WebEngineContext(org.thymeleaf.context.WebEngineContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

IEngineConfiguration (org.thymeleaf.IEngineConfiguration)45 Test (org.junit.Test)26 LinkedHashMap (java.util.LinkedHashMap)15 ServletContext (javax.servlet.ServletContext)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 WebEngineContext (org.thymeleaf.context.WebEngineContext)14 EngineContext (org.thymeleaf.context.EngineContext)10 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)8 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)6 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 EngineConfiguration (org.thymeleaf.EngineConfiguration)3 TemplateModel (org.thymeleaf.engine.TemplateModel)3 IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2