use of org.springframework.web.servlet.View in project spring-framework by spring-projects.
the class XlsViewTests method testXls.
@Test
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@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 HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheetName(0));
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertEquals("Test Value", cell.getStringCellValue());
}
use of org.springframework.web.servlet.View in project spring-framework by spring-projects.
the class ResourceBundleViewResolverTests method eagerInitialization.
@Test
public void eagerInitialization() throws Exception {
ResourceBundleViewResolver rb = new ResourceBundleViewResolver();
rb.setBasename(PROPS_FILE);
rb.setCache(getCache());
rb.setDefaultParentView("testParent");
rb.setLocalesToInitialize(new Locale[] { Locale.ENGLISH, Locale.FRENCH });
rb.setApplicationContext(wac);
View v = rb.resolveViewName("debugView", Locale.FRENCH);
assertThat(v, instanceOf(InternalResourceView.class));
InternalResourceView jv = (InternalResourceView) v;
assertEquals("French debugView must have correct URL", "jsp/debug/deboug.jsp", jv.getUrl());
assertEquals("Correct overridden (XML) content type", "text/xml;charset=ISO-8859-1", jv.getContentType());
}
use of org.springframework.web.servlet.View in project spring-framework by spring-projects.
the class ResourceBundleViewResolverTests method debugViewEnglish.
@Test
public void debugViewEnglish() throws Exception {
View v = rb.resolveViewName("debugView", Locale.ENGLISH);
assertThat(v, instanceOf(InternalResourceView.class));
InternalResourceView jv = (InternalResourceView) v;
assertEquals("debugView must have correct URL", "jsp/debug/debug.jsp", jv.getUrl());
Map<String, Object> m = jv.getStaticAttributes();
assertEquals("Must have 2 static attributes", 2, m.size());
assertEquals("attribute foo", "bar", m.get("foo"));
assertEquals("attribute postcode", "SE10 9JY", m.get("postcode"));
assertEquals("Correct default content type", AbstractView.DEFAULT_CONTENT_TYPE, jv.getContentType());
}
use of org.springframework.web.servlet.View in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameAcceptHeaderWithSuffix.
// SPR-9807
@Test
public void resolveViewNameAcceptHeaderWithSuffix() throws Exception {
request.addHeader("Accept", "application/vnd.example-v2+xml");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Arrays.asList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xml");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn("application/*+xml");
View result = viewResolver.resolveViewName(viewName, locale);
assertSame("Invalid view", viewMock, result);
assertEquals(new MediaType("application", "vnd.example-v2+xml"), request.getAttribute(View.SELECTED_CONTENT_TYPE));
}
use of org.springframework.web.servlet.View in project spring-framework by spring-projects.
the class ContentNegotiatingViewResolverTests method resolveViewNameWithPathExtension.
@Test
public void resolveViewNameWithPathExtension() throws Exception {
request.setRequestURI("/test.xls");
ViewResolver viewResolverMock = mock(ViewResolver.class);
viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
viewResolver.afterPropertiesSet();
View viewMock = mock(View.class, "application_xls");
String viewName = "view";
Locale locale = Locale.ENGLISH;
given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
View result = viewResolver.resolveViewName(viewName, locale);
assertSame("Invalid view", viewMock, result);
}
Aggregations