use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class XlsViewTests method testXlsxView.
@Test
@SuppressWarnings("resource")
public void testXlsxView() throws Exception {
View excelView = new AbstractXlsxView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method testExposeSpringMacroHelpers.
@Test
public void testExposeSpringMacroHelpers() throws Exception {
FreeMarkerView fv = new FreeMarkerView() {
@Override
@SuppressWarnings("rawtypes")
protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException {
Map model = fmModel.toMap();
assertThat(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)).isInstanceOf(RequestContext.class);
RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
BindStatus status = rc.getBindStatus("tb.name");
assertThat(status.getExpression()).isEqualTo("name");
assertThat(status.getValue()).isEqualTo("juergen");
}
};
fv.setUrl(TEMPLATE_FILE);
fv.setApplicationContext(wac);
Map<String, Object> model = new HashMap<>();
model.put("tb", new TestBean("juergen", 99));
fv.render(model, request, response);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class FreeMarkerViewTests method validTemplateName.
@Test
public void validTemplateName() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class);
MockServletContext sc = new MockServletContext();
Map<String, FreeMarkerConfig> configs = new HashMap<>();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("configurer", configurer);
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
given(wac.getServletContext()).willReturn(sc);
fv.setUrl("templateName");
fv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.US);
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
HttpServletResponse response = new MockHttpServletResponse();
Map<String, Object> model = new HashMap<>();
model.put("myattr", "myvalue");
fv.render(model, request, response);
assertThat(response.getContentType()).isEqualTo(AbstractView.DEFAULT_CONTENT_TYPE);
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class FreeMarkerViewTests method keepExistingContentType.
@Test
public void keepExistingContentType() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
WebApplicationContext wac = mock(WebApplicationContext.class);
MockServletContext sc = new MockServletContext();
Map<String, FreeMarkerConfig> configs = new HashMap<>();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("configurer", configurer);
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
given(wac.getServletContext()).willReturn(sc);
fv.setUrl("templateName");
fv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.US);
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
HttpServletResponse response = new MockHttpServletResponse();
response.setContentType("myContentType");
Map<String, Object> model = new HashMap<>();
model.put("myattr", "myvalue");
fv.render(model, request, response);
assertThat(response.getContentType()).isEqualTo("myContentType");
}
use of jakarta.servlet.http.HttpServletResponse in project spring-framework by spring-projects.
the class BaseViewTests method renderWithStaticAttributesNoCollision.
/**
* Test attribute passing, NOT CSV parsing.
*/
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
TestView tv = new TestView(wac);
tv.setApplicationContext(wac);
Properties p = new Properties();
p.setProperty("foo", "bar");
p.setProperty("something", "else");
tv.setAttributes(p);
Map<String, Object> model = new HashMap<>();
model.put("one", new HashMap<>());
model.put("two", new Object());
tv.render(model, request, response);
checkContainsAll(model, tv.model);
checkContainsAll(p, tv.model);
assertThat(tv.initialized).isTrue();
}
Aggregations