Skip to content

Instantly share code, notes, and snippets.

@jfut
Created September 1, 2014 16:25
Show Gist options
  • Save jfut/ce93824d74d0d6cf7519 to your computer and use it in GitHub Desktop.
Save jfut/ce93824d74d0d6cf7519 to your computer and use it in GitHub Desktop.
mixer2-fruitshop-springboot: ItemControllerTest.java
package org.mixer2.sample.web.controller;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.File;
import java.sql.Connection;
import java.util.Locale;
import javax.sql.DataSource;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.ext.h2.H2Connection;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mixer2.Mixer2Engine;
import org.mixer2.jaxb.exception.Mixer2JAXBException;
import org.mixer2.jaxb.xhtml.H1;
import org.mixer2.jaxb.xhtml.Html;
import org.mixer2.sample.config.MvcConfiguration;
import org.mixer2.sample.config.RootConfiguration;
import org.mixer2.spring.webmvc.AbstractMixer2XhtmlView;
import org.mixer2.spring.webmvc.Mixer2XhtmlViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
RootConfiguration.class,
MvcConfiguration.class })
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class ItemControllerTest {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@Autowired
private Mixer2Engine mixer2Engine;
@Autowired
private Mixer2XhtmlViewResolver mixer2XhtmlViewResolver;
@Autowired
private DataSource dataSource;
private IDatabaseConnection getConnection() throws Exception {
Connection con = dataSource.getConnection();
IDatabaseConnection connection = new H2Connection(con, null);
return connection;
}
@Before
public void before() throws Exception {
// insert test data into database.
// see testdata/ItemControllerTestData.xls
File file =
ResourceUtils.getFile("classpath:testdata/ItemControllerTest.xls");
IDataSet dataset = new XlsDataSet(file);
DatabaseOperation.INSERT.execute(getConnection(), dataset);
}
@Test
public void showItem_exists() throws Exception {
int itemId = 10001;
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
request));
// request init here
request.setMethod("GET");
request.setRequestURI("/item/" + itemId);
// execute Controller method and get result as String
Object handler = handlerMapping.getHandler(request).getHandler();
ModelAndView modelAndView =
handlerAdapter.handle(request, response, handler);
// check viewName
String viewName = modelAndView.getViewName();
assertThat(viewName, is("item"));
// execute render method
AbstractMixer2XhtmlView view =
(AbstractMixer2XhtmlView)mixer2XhtmlViewResolver.resolveViewName(
viewName,
Locale.getDefault());
view.render(modelAndView.getModelMap(), request, response);
// check Html instance
Html renderedHtml = view.getRenderedHtml(); // or view.getHtml();
H1 itemNameH1 = renderedHtml.getById("itemName");
assertThat(itemNameH1.getContent().get(0).toString(), is("Apple"));
// reverse html string to Html object.
try {
mixer2Engine.checkAndLoadHtmlTemplate(response.getContentAsString());
} catch (Mixer2JAXBException e) {
// html5: [org.mixer2.Mixer2Engine] unmarshal failed.
// - super.setDocType("<!DOCTYPE html>");
// - output.replaceFirst(" xmlns=\"http://www.w3.org/1999/xhtml\"",
// "");
fail("unmarshal failed: HTML5");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment