Search in sources :

Example 11 with View

use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.

the class FreeMarkerViewTests method freeMarkerViewResolver.

@Test
public void freeMarkerViewResolver() throws Exception {
    MockServletContext sc = new MockServletContext();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new TestConfiguration());
    StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
    wac.setServletContext(sc);
    wac.getBeanFactory().registerSingleton("configurer", configurer);
    wac.refresh();
    FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
    vr.setApplicationContext(wac);
    View view = vr.resolveViewName("test", Locale.CANADA);
    assertThat(view.getClass()).as("Correct view class").isEqualTo(FreeMarkerView.class);
    assertThat(((FreeMarkerView) view).getUrl()).as("Correct URL").isEqualTo("prefix_test_suffix");
    view = vr.resolveViewName("non-existing", Locale.CANADA);
    assertThat(view).isNull();
    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
    assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Also used : StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) AbstractView(cn.taketoday.web.view.AbstractView) View(cn.taketoday.web.view.View) RedirectView(cn.taketoday.web.view.RedirectView) MockServletContext(cn.taketoday.web.mock.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 12 with View

use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.

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, RequestContext request) 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<>(), requestContext);
    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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Row(org.apache.poi.ss.usermodel.Row) View(cn.taketoday.web.view.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Aggregations

View (cn.taketoday.web.view.View)12 Test (org.junit.jupiter.api.Test)8 RequestContext (cn.taketoday.web.RequestContext)6 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)6 Cell (org.apache.poi.ss.usermodel.Cell)6 Row (org.apache.poi.ss.usermodel.Row)6 Sheet (org.apache.poi.ss.usermodel.Sheet)6 Workbook (org.apache.poi.ss.usermodel.Workbook)6 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)6 BindingContext (cn.taketoday.web.BindingContext)2 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)2 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)2 AbstractView (cn.taketoday.web.view.AbstractView)2 ContentNegotiatingViewResolver (cn.taketoday.web.view.ContentNegotiatingViewResolver)2 ModelAndView (cn.taketoday.web.view.ModelAndView)2 RedirectView (cn.taketoday.web.view.RedirectView)2